Example #1
0
 def test_classify_all(self):
     training_set = DataSet([[1, 1], [2, 2], [11, 11], [12, 12]], 
                            labels=["a", "a", "b", "b"])
     classifier = Knn(training_set, k=3)
     dataset = [[1.5, 1.3], [12.2, 12.9]]
     classes = classifier.classify_all(dataset).get_classifications()
     assert_that(classes, contains("a", "b"))
Example #2
0
 def test_three_classes_no_tie(self):
     training_set = DataSet([[1, 0.5], [1, 2], [1, 3], [2, 0.5], [2, 2.5], 
                             [2, 3], [2.5, 1.5], [3, 1]], 
                            labels=["a", "a", "a", "a", "b", "a", "c", "a"])
     classifier = Knn(training_set, k=5)
     result = classifier.classify([2, 1.5])
     self.assertEqual(result, "a")
Example #3
0
 def test_classify_incomplete_sample(self):
     training_set = DataSet([[1, 2, 3], [4, 5, 6]], labels=["a", "a"])
     classifier = Knn(training_set)
     self.assertRaises(InconsistentFeaturesError, 
                       classifier.classify, [1, 2])
Example #4
0
 def test_k_greater_than_num_samples(self):
     training_set = DataSet([[1, 1], [1, 3], [3, 2]], 
                            labels=["a", "a", "b"])
     classifier = Knn(training_set, k=7)
     result = classifier.classify([2, 2])
     self.assertEqual(result, "a")
Example #5
0
 def test_tie(self):
     training_set = DataSet([[1, 7], [1.5, 6.5], [1.5, 8], [2.5, 6.5], 
                             [2.5, 8]], labels=["c", "b", "a", "b", "a"])
     classifier = Knn(training_set, k=5)
     result = classifier.classify([2, 7])
     self.assertEqual(result, "b")
Example #6
0
 def test_two_classes_no_tie(self):
     training_set = DataSet([[8, 4], [8, 5], [8, 6], [9, 4], [9, 6]], 
                            labels=["b", "c", "c", "b", "c"])
     classifier = Knn(training_set, k=5)
     result = classifier.classify([9, 5])
     self.assertEqual(result, "c")