def test_indexing(self): d = orange.ExampleTable("zoo") indamphibian = int(orange.PyValue(d.domain.classVar, "amphibian")) disc = orange.get_class_distribution(d) self.assertEqual(len(disc), len(d.domain.classVar.values)) self.assertEqual(disc["mammal"], 41) self.assertEqual(disc[indamphibian], 4) self.assertEqual(disc[orange.PyValue(d.domain.classVar, "fish")], 13) disc["mammal"] = 100 self.assertEqual(disc[orange.PyValue(d.domain.classVar, "mammal")], 100) disc[indamphibian] = 33 self.assertEqual(disc["amphibian"], 33) disc[orange.PyValue(d.domain.classVar, "fish")] = 12 self.assertEqual(disc["fish"], 12) disc = orange.get_class_distribution(d) self.assertEqual(list(disc), self.freqs) self.assertEqual(disc.values(), self.freqs) self.assertEqual(disc.native(), self.freqs) self.assertEqual(disc.keys(), d.domain["type"].values) self.assertEqual(disc.items(), list(zip(d.domain["type"].values, self.freqs)))
def test_addClassWeight(self): self.data += [[0, 0, 0]] * 12 + [[1, 1, 1]] * 6 + [[2, 2, 2]] * 6 self.assertRaises(ValueError, orange.Preprocessor_addClassWeight, self.data) self.assertRaises(ValueError, orange.Preprocessor_addClassWeight, self.data, classWeights=[1, 2]) cw = [1, 2, 3] d2 = orange.Preprocessor_addClassWeight(self.data, classWeights=cw) for e in d2: self.assertEqual(e.get_weight(), cw[int(e.getclass())]) d2 = orange.Preprocessor_addClassWeight(self.data, equalize=True) cw = [2 / 3, 4 / 3, 4 / 3] for e in d2: self.assertAlmostEqual(e.get_weight(), cw[int(e.getclass())]) for e in orange.get_class_distribution(d2): self.assertAlmostEqual(e, 8) cw = [1, 2, 3] d2 = orange.Preprocessor_addClassWeight(self.data, classWeights=cw, equalize=True) dd = orange.get_class_distribution(d2) self.assertAlmostEqual(dd[0], 8) self.assertAlmostEqual(dd[1], 16) self.assertAlmostEqual(dd[2], 24)
def test_sample(self): d = orange.ExampleTable("iris") li = [1] * 10 + [0] * 140 d1 = d.sample(li) self.assertEqual(len(d1), 10) for i in range(10): self.assertEqual(d1[i], d[i]) self.assertEqual(d1[i].id, d[i].id) d[0, 0] = 42 self.assertEqual(d1[0, 0], 42) d1 = d.sample(li, copy=True) self.assertEqual(len(d1), 10) self.assertEqual(d1[0], d[0]) self.assertNotEqual(d1[0].id, d[0].id) d[0, 0] = 41 self.assertEqual(d1[0, 0], 42) li = [1, 2, 3, 4, 5] * 30 d1 = d.sample(li, 2) self.assertEqual(len(d1), 30) for i in range(30): self.assertEqual(d1[i].id, d[1 + 5 * i].id) ri = orange.MakeRandomIndicesCV(d) for fold in range(10): d1 = d.sample(ri, fold) self.assertEqual(orange.get_class_distribution(d1), [5, 5, 5])
def test_classAttr_cont(self): d = orange.ExampleTable("iris") cd = orange.get_class_distribution(d) ad = orange.Distribution(0, d) cont = orange.ContingencyClassAttr(0, d) fv = cont[0].keys()[0] self.assertEqual(cont.inner_distribution, ad) self.assertEqual(cont.outer_distribution, cd) self.assertEqual(len(cont), len(cd)) s = pickle.dumps(cont) cont2 = pickle.loads(s) self.assertEqual(cont.innerDistribution, cont2.innerDistribution) self.assertEqual(cont.innerVariable, cont2.innerVariable) self.assertEqual(cont.outerDistribution, cont2.outerDistribution) self.assertEqual(cont.outerVariable, cont2.outerVariable) self.assertEqual(cont[0], cont2[0]) cont.normalize() self.assertAlmostEqual(sum(cont.p_attr(0).values()), 1.0) self.assertEqual(cont.p_attr(0)[fv], cont.p_attr(fv, 0)) self.assertEqual(cont.p_attr(0)[fv], cont2.p_attr(fv, 0)/cont2.p_attr(0).abs) x = cont[0][0] cont.add_var_class(0, 0, 0.5) self.assertEqual(x+0.5, cont[0][0]) self.assertEqual(cont[fv][0], cont[fv,0]) with self.assertRaises(IndexError): cont["?"]
def test_mammal(self): d = orange.ExampleTable("zoo") cc = orange.MajorityLearner(d) cd = orange.get_class_distribution(d) cd.normalize() for e in d: self.assertEqual(cc(e), "mammal") self.assertEqual(cc(e, orange.Classifier.GetProbabilities), cd)
def test_fromExamples(self): d = orange.ExampleTable("zoo") disc = orange.Distribution("type", d) disc2 = orange.Distribution(d.domain.classVar, d) self.assertEqual(disc, disc2) disc3 = orange.Distribution(len(d.domain.attributes), d) self.assertEqual(disc, disc3) disc4 = orange.Distribution(-1, d) self.assertEqual(disc, disc4) disc5 = orange.get_class_distribution(d) self.assertEqual(disc, disc5)
def test_relative(self): d = orange.ExampleTable("iris") distr = orange.get_class_distribution(d) pe = orange.ProbabilityEstimatorConstructor_relative(distr) self.assertEqual(list(pe()), [1 / 3, 1 / 3, 1 / 3]) self.assertEqual(pe(0), 1 / 3) self.assertEqual(pe(4), 0) self.assertEqual(pe(d[0].getclass()), 1 / 3) self.assertEqual(pe("Iris-setosa"), 1 / 3) self.assertRaises(ValueError, pe, "x") self.assertRaises(ValueError, pe, "?") self.assertRaises(TypeError, pe, orange) pec = orange.ProbabilityEstimatorConstructor_relative() pe = pec(distr) self.assertEqual(list(pe()), [1 / 3, 1 / 3, 1 / 3]) self.assertEqual(list(pe.probabilities), [1 / 3, 1 / 3, 1 / 3])
def test_domainContingency(self): d = orange.ExampleTable("zoo") dc = orange.DomainContingency(d) cd = orange.get_class_distribution(d) for i, c in enumerate(dc): self.assertEqual(id(d.domain.attributes[i]), id(c.outer_variable)) self.assertEqual(id(dc[d.domain.attributes[i]]), id(c)) self.assertFalse(dc.class_is_outer) self.assertEqual(dc.classes, cd) dc.normalize() self.assertEqual(sum(dc[0][0]), 1) dc.foo = 42 s = pickle.dumps(dc) dc2 = pickle.loads(s) self.assertEqual(dc2.foo, dc.foo) self.assertEqual(dc2.classes, dc.classes) self.assertEqual(dc2[0].outer_variable, dc[0].outer_variable)
def test_attrClass_disc(self): d = orange.ExampleTable("zoo") cd = orange.get_class_distribution(d) ad = orange.Distribution(0, d) cont = orange.ContingencyAttrClass(0, d) self.assertEqual(cont.inner_distribution, cd) self.assertEqual(cont.outer_distribution, ad) self.assertNotEqual(cont[0], cont[1]) self.assertEqual(id(cont[0]), id(cont[d.domain[0].values[0]])) self.assertEqual(len(cont), len(ad)) for cc in cont: self.assertEqual(cc, cont[0]) break self.assertEqual(cont.keys(), d.domain[0].values) self.assertEqual(cont.values()[0], cont[0]) self.assertEqual(cont.values()[1], cont[1]) k, v = cont.items()[0] self.assertEqual(k, d.domain[0].values[0]) self.assertEqual(v, cont[0]) s = pickle.dumps(cont) cont2 = pickle.loads(s) self.assertEqual(cont.innerDistribution, cont2.innerDistribution) self.assertEqual(cont.innerVariable, cont2.innerVariable) self.assertEqual(cont.outerDistribution, cont2.outerDistribution) self.assertEqual(cont.outerVariable, cont2.outerVariable) self.assertEqual(cont[0], cont2[0]) self.assertEqual(cont[1], cont2[1]) cont.normalize() self.assertAlmostEqual(sum(cont.p_class(0)), 1.0) self.assertEqual(cont.p_class(0)[0], cont.p_class(0, 0)) self.assertEqual(cont.p_class(0)[0], cont2.p_class(0)[0]/cont2.p_class(0).abs) x = cont[0][0] cont.add_var_class(0, 0, 0.5) self.assertEqual(x+0.5, cont[0][0]) self.assertEqual(cont[0][0], cont[0,0]) self.assertEqual(cont[d.domain[0].values[0], d.domain.classVar.values[0]], cont[0,0]) with self.assertRaises(IndexError): cont["?"]