Beispiel #1
0
    def test_equality(self):
        self.assertNotEqual(self.sample, self.sample_cl)

        meta = [NumericAttribute(), NominalAttribute(animals)]
        sample = Sample([1.2, meta[1].get_idx("dog")], meta)
        self.assertEqual(self.sample, sample)
        self.assertNotEqual(self.sample, self.sample_cl)
        meta = [NumericAttribute(), NominalAttribute(animals), NumericAttribute()]
        sample = Sample([1.2, meta[1].get_idx("dog"), 3.14], meta)
        self.assertNotEqual(self.sample, sample)
        self.assertNotEqual(self.sample_cl, sample)

        meta = [NumericAttribute(), NominalAttribute(animals)]
        sample = Sample([1.2, meta[1].get_idx("cat")], meta)
        self.assertNotEqual(self.sample, sample)
        self.assertNotEqual(self.sample_cl, sample)

        sample = Sample([1.3, meta[1].get_idx("dog")], meta)
        self.assertNotEqual(self.sample, sample)
        self.assertNotEqual(self.sample_cl, sample)

        sample = Sample([100, self.meta[1].get_idx("cat")], self.meta,
                                self.meta_cl.get_idx("duck"), self.meta_cl)
        self.assertEqual(self.sample_cl, sample)
        self.assertNotEqual(self.sample, sample)

        sample = Sample([10.20, self.meta[1].get_idx("cat")], self.meta,
                                self.meta_cl.get_idx("duck"), self.meta_cl)
        self.assertNotEqual(self.sample, sample)
        self.assertNotEqual(self.sample_cl, sample)
Beispiel #2
0
 def test_equality(self):
     self.assertEqual(self.na, NominalAttribute(animals))
     self.assertEqual(self.na,
             NominalAttribute(["dog", "cat", "lion", "duck", "python:)"]))
     self.assertNotEqual(self.na, NominalAttribute(animals + ["donkey"]))
     self.assertEqual(NumericAttribute(), NumericAttribute())
     self.assertNotEqual(self.na, NumericAttribute())
Beispiel #3
0
    def test_classification(self):
        cls_meta = NominalAttribute([0, 1])
        meta = [NumericAttribute() for _ in xrange(3)]
        train_set = [
            Sample([0, 0, 0], meta, 0, cls_meta),
            Sample([0, 1, 0], meta, 0, cls_meta),
            Sample([0, 0, 1], meta, 0, cls_meta),
            Sample([3, 0, 0], meta, 1, cls_meta),
            Sample([3, 1, 0], meta, 1, cls_meta),
            Sample([3, 0, 1], meta, 1, cls_meta),
        ]

        classifier = self.knn1
        #        classifier = self.tree
        classifier.train(train_set)
        expected = [str(x) for x in [0, 0, 0, 1, 1, 1, 0, 1]]
        samples = [s for s in train_set]
        samples.extend([Sample([1, 0, 0], meta), Sample([2, 1, 0], meta)])
        for e, s in izip(expected, samples):
            self.assertEqual(e, classifier.classify(s))
            k, p = classifier.classify_pval(s)
            self.assertTrue(0. <= p <= 1.)
            self.assertEqual(k, classifier.classify(s))
            p2 = classifier.class_probabilities(s)
            self.assertAlmostEqual(1., sum(p2.values()), delta=0.00001)
Beispiel #4
0
    def test_loading_arff(self):
        expected_meta = [NumericAttribute() for _ in xrange(4)]
        expected_cls_meta = NominalAttribute(
                    ["Iris-setosa", "Iris-versicolor", "Iris-virginica"])

        sample = self.samples[0]
        self.assertEqual(sample.meta, expected_meta)
        self.assertEqual(sample.cls_meta, expected_cls_meta)
Beispiel #5
0
class TestSample(unittest.TestCase):

    def setUp(self):
        self.meta = [NumericAttribute(), NominalAttribute(animals)]
        self.sample = Sample([1.2, self.meta[1].get_idx("dog")], self.meta)
        self.meta_cl = NominalAttribute(animals)
        self.sample_cl = Sample([100, self.meta[1].get_idx("cat")], self.meta,
                                self.meta_cl.get_idx("duck"), self.meta_cl)

    def test_basic(self):
        self.assertIsNone(self.sample.get_cls())
        self.assertEqual(self.sample_cl.get_cls(), "duck")

        self.assertEqual(self.sample.get_values(), [1.2, "dog"])
        self.assertEqual(self.sample_cl.get_values(), [100, "cat"])

    def test_equality(self):
        self.assertNotEqual(self.sample, self.sample_cl)

        meta = [NumericAttribute(), NominalAttribute(animals)]
        sample = Sample([1.2, meta[1].get_idx("dog")], meta)
        self.assertEqual(self.sample, sample)
        self.assertNotEqual(self.sample, self.sample_cl)
        meta = [NumericAttribute(), NominalAttribute(animals), NumericAttribute()]
        sample = Sample([1.2, meta[1].get_idx("dog"), 3.14], meta)
        self.assertNotEqual(self.sample, sample)
        self.assertNotEqual(self.sample_cl, sample)

        meta = [NumericAttribute(), NominalAttribute(animals)]
        sample = Sample([1.2, meta[1].get_idx("cat")], meta)
        self.assertNotEqual(self.sample, sample)
        self.assertNotEqual(self.sample_cl, sample)

        sample = Sample([1.3, meta[1].get_idx("dog")], meta)
        self.assertNotEqual(self.sample, sample)
        self.assertNotEqual(self.sample_cl, sample)

        sample = Sample([100, self.meta[1].get_idx("cat")], self.meta,
                                self.meta_cl.get_idx("duck"), self.meta_cl)
        self.assertEqual(self.sample_cl, sample)
        self.assertNotEqual(self.sample, sample)

        sample = Sample([10.20, self.meta[1].get_idx("cat")], self.meta,
                                self.meta_cl.get_idx("duck"), self.meta_cl)
        self.assertNotEqual(self.sample, sample)
        self.assertNotEqual(self.sample_cl, sample)
Beispiel #6
0
 def setUp(self):
     self.classifiers = [("BayesLearner", [], {}), ("TreeLearner", [], {}),
                         ("kNNLearner", [], {
                             "k": 1
                         }), ("kNNLearner", [], {
                             "k": 3
                         }), ("TreeLearner", [], {})]
     self.knn1 = OrangeClassifier(self.classifiers[2][0])
     self.knn3 = OrangeClassifier(self.classifiers[3][0])
     self.tree = OrangeClassifier(self.classifiers[4][0])
     self.cls_meta = NominalAttribute([0, 1])
     self.meta = [NumericAttribute() for _ in xrange(3)]
     self.train_set = [
         Sample([0, 0, 0], self.meta, 0, self.cls_meta),
         Sample([0, 1, 0], self.meta, 0, self.cls_meta),
         Sample([0, 0, 1], self.meta, 0, self.cls_meta),
         Sample([3, 0, 0], self.meta, 1, self.cls_meta),
         Sample([3, 1, 0], self.meta, 1, self.cls_meta),
         Sample([3, 0, 1], self.meta, 1, self.cls_meta),
     ]
Beispiel #7
0
 def setUp(self):
     self.meta = [NumericAttribute(), NominalAttribute(animals)]
     self.sample = Sample([1.2, self.meta[1].get_idx("dog")], self.meta)
     self.meta_cl = NominalAttribute(animals)
     self.sample_cl = Sample([100, self.meta[1].get_idx("cat")], self.meta,
                             self.meta_cl.get_idx("duck"), self.meta_cl)
Beispiel #8
0
 def test_nominal_attr_getting_value(self):
     na = NominalAttribute(self.symbols)
     for i, s in enumerate(self.symbols):
         self.assertEqual(s, na.get_value(i))
         self.assertEqual(s, na.get_symbol(i))
         self.assertEqual(i, na.get_idx(s))
Beispiel #9
0
 def setUp(self):
     self.symbols = animals
     self.na = NominalAttribute(self.symbols)
Beispiel #10
0
def simple_meta_attrs(class_attrs=[0, 1]):
    return [NumericAttribute(), NominalAttribute(list(class_attrs))]