def setUp(self): self.N = 100 self.samples = [ Sample([1, 2, 3, 4]), Sample([1, 1, 1, 1]), Sample([2, 2, 2, 2]), Sample([3, 3, 3, 3]) ] self.ru = [ReactiveUnit(s.get_values()) for s in self.samples]
def setUp(self): self.meta = simple_meta_attrs() self.samples = [Sample([i, 0], self.meta, last_is_class=True) for i in xrange(10)] + \ [Sample([i, 1], self.meta, last_is_class=True) for i in xrange(10, 15)] self.classifiers = tuple(StupidClassifer(i) for i in xrange(2)) self.basics = tuple([(10., 0., 5., 0.), (0., 5., 0., 10.)])
def test_classify_similar(self): self._init() samples = [ Sample([0.9, 0.9, 0.9, 0.9]), Sample([1.1, 1.1, 1.1, 1.1]), Sample([0.9, 1.1, 0.9, 1.1]), Sample([1, 1, 1, 1]) ] wc = [self.sc.classify(s) for s in samples] wc.sort() self.assertEqual(wc[0], wc[len(wc) - 1])
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)
def test_classification(self): classifier = self.knn1 # classifier = self.tree classifier.train(self.train_set) expected = map(str, [0, 0, 0, 1, 1, 1, 0, 1]) samples = [s for s in self.train_set] samples.extend( [Sample([1, 0, 0], self.meta), Sample([2, 1, 0], self.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)
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)
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)
def setUp(self): self.meta = simple_meta_attrs(['-', '+']) self.cs = lambda i, v: Sample( [i, self.meta[1].set_value(v)], self.meta, last_is_class=True) self.classifier = OrangeClassifier('kNNLearner', k=1) test_samples = '+++-++-+-+--+---' N = len(test_samples) train_samples = ('+' * (N / 2)) + ('-' * (N / 2)) self.test_samples, self.train_samples = ([ self.cs(i, v) for i, v in enumerate(samples) ] for samples in [test_samples, train_samples]) random.shuffle(self.test_samples) self.classifier.train(self.train_samples)
def test_random_stimuli_with_distance(self): samples = [Sample([x]) for x in xrange(10)] * 10 chooser = RandomStimuliChooser(None, True, 3) env = Environment(samples, chooser) for _ in xrange(10): sort = sorted([x.get_values()[0] for x in env.get_stimuli(4)]) self.assertEqual([0, 3, 6, 9], sort) chooser = RandomStimuliChooser(None, True, 5) env = Environment(samples, chooser) for _ in xrange(10): sort = sorted([x.get_values()[0] for x in env.get_stimuli(2)]) self.assertEqual(len(sort), 2) self.assertTrue(sort[0] < 5) self.assertTrue(sort[1] >= 5) self.assertRaises(Exception, chooser.get_stimuli, samples, 100)
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), ]
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)