コード例 #1
0
 def test_prediction(self):
     print """Test basic classification"""
     classifier = NaiveBayes()
     classifier.train(self.training_set)
     predictions = [classifier.classify_instance(x) for x in self.training_set]
     self.assertEqual(predictions[0], 'cat')
     self.assertEqual(predictions[1], 'dog')
     self.assertEqual(predictions[2], 'cat')
     self.assertEqual(predictions[3], 'dog')
     self.assertEqual(predictions[4], 'dog')
コード例 #2
0
count_x_y[woof_index, dog_index] == (2+1)

count_y = classifier.count_y_table
count_y[cat_index] == 3
count_y[dog_index] == 2

"""Test probability tables
Regardless of what kind of smoothing you do, the signs have to be right
"""

classifier.train(training_set)

p_x_given_y = classifier.p_x_given_y_table
p_x_given_y[meow_index, cat_index] > p_x_given_y[meow_index, dog_index]
p_x_given_y[bark_index, cat_index] < p_x_given_y[bark_index, dog_index]

p_y = classifier.p_y_table
p_y[cat_index] > p_y[dog_index]

"""Test smoothing
Zero counts must not result in zero proability
"""
p_x_given_y[bark_index, cat_index] != 0
p_x_given_y[purr_index, dog_index] != 0

"""Test basic classification"""
predictions = [classifier.classify_instance(x) for x in training_set]
predictions == ['cat', 'cat', 'cat', 'dog', 'dog']