source: orange/Orange/doc/reference/filter.py @ 10081:9362249ada56

Revision 10081:9362249ada56, 4.4 KB checked in by ales_erjavec, 2 years ago (diff)

Merge

Line 
1# Description: Shows how to filter examples using various classes derived from orange.Filter
2# Category:    filtering, preprocessing
3# Classes:     Filter, Filter_random, Filter_hasSpecial, Filter_hasClassValue, Filter_sameValue, Filter_values
4# Uses:        lenses
5# Referenced:  filters.htm
6
7import orange
8
9data = orange.ExampleTable("lenses")
10
11example = data[0]
12
13randomfilter = orange.Filter_random(prob = 0.7, randomGenerator = 24)
14for i in range(10):
15    print randomfilter(example),
16print
17
18data70 = randomfilter(data)
19print len(data), len(data70)
20
21data2 = data[:5]
22data2[0]["age"] = "?"
23data2[1].setclass("?")
24print "First five examples"
25for ex in data2:
26    print ex
27
28print "\nExamples without unknown values"
29f = orange.Filter_isDefined(domain = data.domain)
30for ex in f(data2):
31    print ex
32
33print "\nExamples without unknown values, ignoring 'age'"
34f.check["age"] = 0
35for ex in f(data2):
36    print ex
37
38print "\nExamples with unknown values (ignoring age)"
39for ex in f(data2, negate=1):
40    print ex
41
42
43print "\nExamples with unknown values (Filter_hasSpecial)"
44for ex in orange.Filter_hasSpecial(data2):
45    print ex
46
47print "\nExamples with no unknown values (Filter_hasSpecial)"
48for ex in orange.Filter_hasSpecial(data2, negate=1):
49    print ex
50
51print "\nExamples with defined class"
52for ex in orange.Filter_hasClassValue(data2):
53    print ex
54
55print "\nExamples with undefined class"
56for ex in orange.Filter_hasClassValue(data2, negate=1):
57    print ex
58
59
60filteryoung = orange.Filter_sameValue()
61age = data.domain["age"]
62filteryoung.value = orange.Value(age, "young")
63filteryoung.position = data.domain.attributes.index(age)
64print "\nYoung examples"
65for ex in filteryoung(data):
66    print ex
67
68
69print "\nYoung or presbyopic with astigmatism"
70fya = orange.Filter_values()
71age, astigm = data.domain["age"], data.domain["astigmatic"]
72fya.domain = data.domain
73fya.conditions.append(orange.ValueFilter_discrete(position = data.domain.attributes.index(age), values=[orange.Value(age, "young"), orange.Value(age, "presbyopic")]))
74fya.conditions.append(orange.ValueFilter_discrete(position = data.domain.attributes.index(astigm), values=[orange.Value(astigm, "yes")]))
75for ex in fya(data):
76    print ex
77
78print "\nYoung or presbyopic with astigmatism"
79fya = orange.Filter_values(domain = data.domain,
80                           conditions = [orange.ValueFilter_discrete(position = data.domain.attributes.index(age), values=[orange.Value(age, "young"), orange.Value(age, "presbyopic")]),
81                                         orange.ValueFilter_discrete(position = data.domain.attributes.index(astigm), values=[orange.Value(astigm, "yes")])
82                                        ]
83                          )
84for ex in fya(data):
85    print ex
86
87
88print "\nYoung or presbyopic with astigmatism"
89fya = orange.Filter_values(domain = data.domain,
90                           conditions = [orange.ValueFilter_discrete(position = data.domain.attributes.index(age), values=[orange.Value(age, "young"), orange.Value(age, "presbyopic")], acceptSpecial = 0),
91                                         orange.ValueFilter_discrete(position = data.domain.attributes.index(astigm), values=[orange.Value(astigm, "yes")])
92                                        ],
93                          )
94for ex in fya(data):
95    print ex
96
97print "\nYoung or presbyopic with astigmatism"
98fya = orange.Filter_values(domain = data.domain,
99                           conditions = [orange.ValueFilter_discrete(position = data.domain.attributes.index(age), values=[orange.Value(age, "young"), orange.Value(age, "presbyopic")], acceptSpecial = 1),
100                                         orange.ValueFilter_discrete(position = data.domain.attributes.index(astigm), values=[orange.Value(astigm, "yes")])
101                                        ],
102                          )
103for ex in fya(data):
104    print ex
105
106print "\nYoung or presbyopic or astigmatic"
107fya = orange.Filter_values(domain = data.domain,
108                           conditions = [orange.ValueFilter_discrete(position = data.domain.attributes.index(age), values=[orange.Value(age, "young"), orange.Value(age, "presbyopic")], acceptSpecial = 1),
109                                         orange.ValueFilter_discrete(position = data.domain.attributes.index(astigm), values=[orange.Value(astigm, "yes")])
110                                        ],
111                           conjunction = 0
112                          )
113for ex in fya(data):
114    print ex
Note: See TracBrowser for help on using the repository browser.