def getFilter(self, domain, variable, value1, value2, negate,
                  caseSensitive):
        """Returns orange filter.
        """
        if self.operator == Operator.operatorDef:
            try:
                id = domain.index(variable)
            except:
                error("Error: unknown attribute (%s)." % variable)

            if id >= 0:
                f = orange.Filter_isDefined(domain=domain)
                for v in domain.variables:
                    f.check[v] = 0
                f.check[variable] = 1
            else:  # variable is a meta
                f = orange.Filter_hasMeta(id=domain.index(variable))
        elif self.operator in Operator.operatorsD:
            f = orange.Filter_values(domain=domain)
            f[variable] = value1
        else:
            f = orange.Filter_values(domain=domain)
            if value2:
                f[variable] = (Operator._operFilter[str(self.operator)],
                               value1, value2)
            else:
                f[variable] = (Operator._operFilter[str(self.operator)],
                               value1)
            if self.varType == orange.VarTypes.String:
                f[variable].caseSensitive = caseSensitive
        f.negate = negate
        return f
Exemple #2
0
    def applySettings(self):
        """use the setting from the widget, identify the outliers"""
        if self.haveInput == 1:
            outlier = self.outlier
            outlier.setKNN(self.ks[self.k][1])

            newdomain = orange.Domain(self.data.domain)
            newdomain.addmeta(orange.newmetaid(),
                              orange.FloatVariable("Z score"))

            self.newdata = orange.ExampleTable(newdomain, self.data)

            zv = outlier.zValues()
            for i, el in enumerate(zv):
                self.newdata[i]["Z score"] = el

            self.send("Examples with Z-scores", self.newdata)

            filterout = orange.Filter_values(domain=self.newdata.domain)
            filterout["Z score"] = (orange.Filter_values.Greater,
                                    eval(self.zscore))
            outliers = filterout(self.newdata)

            filterin = orange.Filter_values(domain=self.newdata.domain)
            filterin["Z score"] = (orange.Filter_values.LessEqual,
                                   eval(self.zscore))
            inliers = filterin(self.newdata)

            self.send("Outliers", outliers)
            self.send("Inliers", inliers)
        else:
            self.send("Examples with Z-scores", None)
            self.send("Outliers", None)
            self.send("Inliers", None)
Exemple #3
0
    def applySettings(self):

        if self.haveInput == 1:

            outlier = self.outlier

            outlier.setKNN(self.ks[self.k][1])

            newdomain = orange.Domain(self.data.domain)
            newdomain.addmeta(orange.newmetaid(),
                              orange.FloatVariable("Z score"))

            self.newdata = orange.ExampleTable(newdomain, self.data)

            zv = outlier.zValues()
            for i, el in enumerate(zv):
                self.newdata[i]["Z score"] = el

            self.send("Examples with Z-scores", self.newdata)

            filter = orange.Filter_values(domain=self.newdata.domain)
            filter["Z score"] = (orange.Filter_values.Greater,
                                 eval(self.zscore))
            self.outliers = filter(self.newdata)

            self.send("Outliers", self.outliers)
        else:
            self.send("Examples with Z-scores", None)
            self.send("Outliers", None)
Exemple #4
0
def add_sub_rules(rules, examples, weight, learner, dists):
    apriori = orange.Distribution(examples.domain.classVar, examples, weight)
    newRules = orange.RuleList()
    for r in rules:
        newRules.append(r)

    # loop through rules
    for r in rules:
        tmpList = orange.RuleList()
        tmpRle = r.clone()
        tmpRle.filter.conditions = []
        tmpRle.parentRule = None
        tmpRle.filterAndStore(examples, weight, r.classifier.defaultVal)
        tmpList.append(tmpRle)
        while tmpList and len(tmpList[0].filter.conditions) <= len(
                r.filter.conditions):
            tmpList2 = orange.RuleList()
            for tmpRule in tmpList:
                # evaluate tmpRule
                oldREP = learner.ruleFinder.evaluator.returnExpectedProb
                learner.ruleFinder.evaluator.returnExpectedProb = False
                learner.ruleFinder.evaluator.evDistGetter.dists = createEVDistList(
                    dists[int(r.classifier.defaultVal)])
                tmpRule.quality = learner.ruleFinder.evaluator(
                    tmpRule, examples, weight, r.classifier.defaultVal,
                    apriori)
                learner.ruleFinder.evaluator.returnExpectedProb = oldREP
                # if rule not in rules already, add it to the list
                if not True in [rules_equal(ri, tmpRule)
                                for ri in newRules] and len(
                                    tmpRule.filter.conditions
                                ) > 0 and tmpRule.quality > apriori[
                                    r.classifier.defaultVal] / apriori.abs:
                    newRules.append(tmpRule)
                # create new tmpRules, set parent Rule, append them to tmpList2
                if not True in [rules_equal(ri, tmpRule) for ri in newRules]:
                    for c in r.filter.conditions:
                        tmpRule2 = tmpRule.clone()
                        tmpRule2.parentRule = tmpRule
                        tmpRule2.filter.conditions.append(c)
                        tmpRule2.filterAndStore(examples, weight,
                                                r.classifier.defaultVal)
                        if tmpRule2.classDistribution.abs < tmpRule.classDistribution.abs:
                            tmpList2.append(tmpRule2)
            tmpList = tmpList2
    for cl in examples.domain.classVar:
        tmpRle = orange.Rule()
        tmpRle.filter = orange.Filter_values(domain=examples.domain)
        tmpRle.parentRule = None
        tmpRle.filterAndStore(examples, weight, int(cl))
        tmpRle.quality = tmpRle.classDistribution[int(
            cl)] / tmpRle.classDistribution.abs
        newRules.append(tmpRle)
    return newRules
Exemple #5
0
 def __init__(self, data, targetClass, conditions=[], g=1):
     self.g = g
     self.data = data
     self.targetClass = targetClass
     self.filter = orange.Filter_values(domain=data.domain,
                                        conditions=conditions,
                                        conjunction=1)
     self.learner = targetClassLearner.Learner(
         targetClass=targetClass, name="TargetClassClassifierRules")
     self.filterAndStore(
     )  # set examples, classifier, distribution; TP, calculate quality, complexity, support
Exemple #6
0
    def __init__(self,
                 data,
                 targetClass,
                 conditions=[],
                 g=1,
                 negate=False,
                 c_range=None):
        self.g = g
        self.data = data
        self.targetClass = targetClass
        self.filter = orange.Filter_values(domain=data.domain,
                                           conditions=conditions,
                                           conjunction=1,
                                           negate=negate)
        self.__examples__ = None
        self.filterAndStore(
        )  # set examples, classifier, distribution; TP, calculate quality, complexity, support
        self.id = SDRule.__id__
        self.fixed = False  # fixed rules cannot be extended

        self.weight = 1.
        self.quality = -inf
        self.score = 0.
        self.score_norm = None
        self.inf_state = None
        self.isbest = False

        self.c_range = c_range
        if not self.c_range: self.c_range = [infinity, -infinity]

        self.stats_mean = None
        self.stats_std = None
        self.stats_meannorm = None
        self.stats_nmean = None
        self.stats_nstd = None
        self.stats_nmeannorm = None
        self.stats_max = None
        self.parent_rule = None

        # rules within the cluster (for Basic.group_rules)
        self.cluster_rules = set()
        SDRule.__id__ += 1
def testnonref(mid):
    pd0 = data[0][1]
    mid[0][1] += 1
    if pd0 != data[0][1]:
        raise Exception("reference when there shouldn't be")


def testref(mid):
    pd0 = data[0][1]
    mid[0][1] += 1
    if pd0 == data[0][1]:
        raise Exception("not reference when there should be")


filterany = orange.Filter_values()
filterany.domain = data.domain
filterany.conditions.append(
    orange.ValueFilter_continuous(position=data.domain.index("LENGTH"),
                                  min=-9999,
                                  max=9999,
                                  acceptSpecial=True))

# we sometime use LENGT=... and sometimes filterany
# the former cannot be given the 'acceptSpecial' flag, but we would
# still like to test the form of the call when we can
testnonref(data.filter(LENGTH=(-9999, 9999)))
testref(data.filterref(filterany))
testref(data.filterlist(filterany))

ll = [1] * len(data)
Exemple #8
0
print "\nExamples with undefined class"
for ex in orange.Filter_hasClassValue(data2, negate=1):
    print ex


filteryoung = orange.Filter_sameValue()
age = data.domain["age"]
filteryoung.value = orange.Value(age, "young")
filteryoung.position = data.domain.attributes.index(age)
print "\nYoung examples"
for ex in filteryoung(data):
    print ex


print "\nYoung or presbyopic with astigmatism"
fya = orange.Filter_values()
age, astigm = data.domain["age"], data.domain["astigmatic"]
fya.domain = data.domain
fya.conditions.append(orange.ValueFilter_discrete(position = data.domain.attributes.index(age), values=[orange.Value(age, "young"), orange.Value(age, "presbyopic")]))
fya.conditions.append(orange.ValueFilter_discrete(position = data.domain.attributes.index(astigm), values=[orange.Value(astigm, "yes")]))
for ex in fya(data):
    print ex

print "\nYoung or presbyopic with astigmatism"
fya = orange.Filter_values(domain = data.domain,
                           conditions = [orange.ValueFilter_discrete(position = data.domain.attributes.index(age), values=[orange.Value(age, "young"), orange.Value(age, "presbyopic")]),
                                         orange.ValueFilter_discrete(position = data.domain.attributes.index(astigm), values=[orange.Value(astigm, "yes")])
                                        ]
                          )
for ex in fya(data):
    print ex
Exemple #9
0
import orange

############ THIS IS WHAT YOU CAN DO WITH DISCRETE ATTRIBUTES

data = orange.ExampleTable("lenses")

############ THIS IS WHAT YOU CAN DO WITH DISCRETE ATTRIBUTES

print "\nYoung or presbyopic with astigmatism"
fya = orange.Filter_values(domain=data.domain)
fya["age"] = "young"
print "\nYoung examples\n"
for ex in fya(data):
    print ex

fya["age"] = "presbyopic"
print "\n\nPresbyopic examples\n"
for ex in fya(data):
    print ex

fya["age"] = ["presbyopic", "young"]
print "\n\nYoung and presbyopic examples\n"
for ex in fya(data):
    print ex

astigm = data.domain["astigmatic"]
fya["age"] = ["presbyopic", "young"]
fya[astigm] = "yes"
print "\n\nYoung and presbyopic examples that are astigmatic\n"
for ex in fya(data):
    print ex