示例#1
0
def printLookupFunction(func):
    if isinstance(func, orange.Variable):
        if not func.getValueFrom:
            raise TypeError("attribute '%s' does not have an associated function" % func.name)
        else:
            func = func.getValueFrom

    outp = ""
    if isinstance(func, orange.ClassifierByExampleTable):
    # XXX This needs some polishing :-)
        for i in func.sortedExamples:
            outp += "%s\n" % i
    else:
        boundset = func.boundset()
        for a in boundset:
            outp += "%s\t" % a.name
        outp += "%s\n" % func.classVar.name
        outp += "------\t" * (len(boundset)+1) + "\n"
        
        lc = 0
        if len(boundset)==1:
            cnt = orngMisc.LimitedCounter([len(x.values)+1 for x in boundset])
        else:
            cnt = orngMisc.LimitedCounter([len(x.values) for x in boundset])
        for ex in cnt:
            for i in range(len(ex)):
                if ex[i]<len(boundset[i].values):
                    outp += "%s\t" % boundset[i].values[ex[i]]
                else:
                    outp += "?\t",
            outp += "%s\n" % func.classVar.values[int(func.lookupTable[lc])]
            lc += 1
    return outp
示例#2
0
def createClassVar(attributes, MQCNotation=False):
    import orngMisc
    if MQCNotation:
        return orange.EnumVariable(
            "Q",
            values=[
                "%s(%s)" % ("".join(["+-"[x] for x in v if x < 2]), ", ".join(
                    [attr for attr, x in zip(attributes, v) if x < 2]))
                for v in orngMisc.LimitedCounter([3] * len(attributes))
            ])
    else:
        return orange.EnumVariable(
            "Q",
            values=[
                "Q(%s)" % ", ".join([
                    "+-"[x] + attr for attr, x in zip(attributes, v) if x < 2
                ]) for v in orngMisc.LimitedCounter([3] * len(attributes))
            ])
示例#3
0
def lookupFromFunction(attribute, bound, function):
    """
    Constructs ClassifierByExampleTable or ClassifierByLookupTable mirroring the given function
    """
    lookup = lookupFromBound(attribute, bound)
    if lookup:
        lookup.lookupTable = [
            orange.Value(attribute, function(attributes)) for attributes in
            orngMisc.LimitedCounter([len(attr.values) for attr in bound])
        ]
        return lookup
    else:
        examples = orange.ExampleTable(orange.Domain(bound, attribute))
        for attributes in orngMisc.LimitedCounter(
            [len(attr.values) for attr in dom.attributes]):
            examples.append(
                orange.Example(dom, attributes + [function(attributes)]))
        return orange.LookupLearner(examples)
示例#4
0
    def __call__(self, table, bound, weightID=0):
        if not len(bound):
            raise AttributeError, "no bound attributes"

        bound = [table.domain[a] for a in bound]
        newVar = orange.EnumVariable("-".join([a.name for a in bound]))

        if (len(bound) == 1):
            newVar.values = list(bound[0].values)
            clsfr = orange.ClassifierByLookupTable(newVar, bound[0])
        else:
            import orngMisc
            for vs in orngMisc.LimitedCounter([len(a.values) for a in bound]):
                newVar.values.append("-".join(
                    [bound[i].values[v] for i, v in enumerate(vs)]))
            clsfr = orange.ClassifierByLookupTable(newVar, bound)

##    elif (len(bound)==2):
##      for v1 in bound[0].values:
##        for v2 in bound[1].values:
##          newVar.values.append(v1+"-"+v2)
##      clsfr = orange.ClassifierByLookupTable2(newVar, bound[0], bound[1])
##    elif (len(bound)==3):
##      for v1 in bound[0].values:
##        for v2 in bound[1].values:
##          for v3 in bound[2].values:
##            newVar.values.append(v1+"-"+v2+"-"+v3)
##      clsfr = orange.ClassifierByLookupTable3(newVar, bound[0], bound[1], bound[2])
##    else:
##      raise AttributeError, "cannot deal with more than 3 bound attributes"

        for i in range(len(newVar.values)):
            clsfr.lookupTable[i] = orange.Value(newVar, i)

        newVar.getValueFrom = clsfr

        if self.measure:
            meas = self.measure(newVar, table)
        else:
            meas = 0
        return newVar, meas
示例#5
0
    def __call__(self, table, weight=None, verbose=0):
        import orngTest, orngStat, orngMisc

        evaluate = getattr(self, "evaluate", orngStat.CA)
        folds = getattr(self, "folds", 5)
        compare = getattr(self, "compare", cmp)
        verbose = verbose or getattr(self, "verbose", 0)
        returnWhat = getattr(self, "returnWhat",
                             Tune1Parameter.returnClassifier)
        progressCallback = getattr(self, "progressCallback", lambda i: None)

        to_set = []
        parnames = []
        for par in self.parameters:
            if (type(par[0]) == list) or (type(par[0]) == tuple):
                to_set.append([self.findobj(ld) for ld in par[0]])
                parnames.append(par[0])
            else:
                to_set.append([self.findobj(par[0])])
                parnames.append([par[0]])

        cvind = orange.MakeRandomIndicesCV(table, folds)
        findBest = orngMisc.BestOnTheFly(seed=table.checksum(),
                                         callCompareOn1st=True)
        tableAndWeight = weight and (table, weight) or table
        numOfTests = sum([len(x[1]) for x in self.parameters])
        milestones = set(range(0, numOfTests, max(numOfTests / 100, 1)))
        for itercount, valueindices in enumerate(
                orngMisc.LimitedCounter([len(x[1]) for x in self.parameters])):
            values = [
                self.parameters[i][1][x] for i, x in enumerate(valueindices)
            ]
            for pi, value in enumerate(values):
                for i, par in enumerate(to_set[pi]):
                    setattr(par[0], par[1], value)
                    if verbose == 2:
                        print("%s: %s" % (parnames[pi][i], value))

            res = evaluate(
                orngTest.testWithIndices([self.object], tableAndWeight, cvind))
            if itercount in milestones:
                progressCallback(100.0 * itercount / numOfTests)

            findBest.candidate((res, values))
            if verbose == 2:
                print("===> Result: %s\n" % res)

        bestpar = findBest.winner()[1]
        if verbose:
            print("*** Optimal set of parameters: ", end=' ')
        for pi, value in enumerate(bestpar):
            for i, par in enumerate(to_set[pi]):
                setattr(par[0], par[1], value)
                if verbose:
                    print("%s: %s" % (parnames[pi][i], value), end=' ')
        if verbose:
            print()

        if returnWhat == Tune1Parameter.returnNone:
            return None
        elif returnWhat == Tune1Parameter.returnParameters:
            return bestpar
        elif returnWhat == Tune1Parameter.returnLearner:
            return self.object
        else:
            classifier = self.object(table)
            classifier.fittedParameters = bestpar
            return classifier