Back to index

python-biopython  1.60
Public Member Functions
test_kNN.TestKNN Class Reference

List of all members.

Public Member Functions

def test_calculate_model
def test_classify
def test_calculate_probability
def test_model_accuracy
def test_leave_one_out

Detailed Description

Definition at line 58 of file test_kNN.py.


Member Function Documentation

Definition at line 60 of file test_kNN.py.

00060 
00061     def test_calculate_model(self):
00062         k = 3
00063         model = kNN.train(xs, ys, k)
00064         self.assertEqual(model.classes, set([0,1]))
00065         n = len(xs)
00066         for i in range(n):
00067             self.assertAlmostEqual(model.xs[i,0], xs[i][0], places=4)
00068             self.assertAlmostEqual(model.xs[i,1], xs[i][1], places=4)
00069             self.assertEqual(model.ys[i], ys[i])
00070         self.assertEqual(model.k, k)

Definition at line 79 of file test_kNN.py.

00079 
00080     def test_calculate_probability(self):
00081         k = 3
00082         model = kNN.train(xs, ys, k)
00083         weights = kNN.calculate(model, [6,-173.143442352])
00084         self.assertAlmostEqual(weights[0], 0.0, places=6)
00085         self.assertAlmostEqual(weights[1], 3.0, places=6)
00086         weights = kNN.calculate(model, [309, -271.005880394])
00087         self.assertAlmostEqual(weights[0], 3.0, places=6)
00088         self.assertAlmostEqual(weights[1], 0.0, places=6)
00089         weights = kNN.calculate(model, [117, -267.13999999999999])
00090         self.assertAlmostEqual(weights[0], 2.0, places=6)
00091         self.assertAlmostEqual(weights[1], 1.0, places=6)

Definition at line 71 of file test_kNN.py.

00071 
00072     def test_classify(self):
00073         k = 3
00074         model = kNN.train(xs, ys, k)
00075         result = kNN.classify(model, [6,-173.143442352])
00076         self.assertEqual(result, 1)
00077         result = kNN.classify(model, [309, -271.005880394])
00078         self.assertEqual(result, 0)

Definition at line 104 of file test_kNN.py.

00104 
00105     def test_leave_one_out(self):
00106         correct = 0
00107         k = 3
00108         model = kNN.train(xs, ys, k)
00109         predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1]
00110         for i in range(len(predictions)):
00111             model = kNN.train(xs[:i]+xs[i+1:], ys[:i]+ys[i+1:], k)
00112             prediction = kNN.classify(model, xs[i])
00113             self.assertEqual(prediction, predictions[i])
00114             if prediction==ys[i]:
00115                 correct+=1
00116         self.assertEqual(correct, 13)

Definition at line 92 of file test_kNN.py.

00092 
00093     def test_model_accuracy(self):
00094         correct = 0
00095         k = 3
00096         model = kNN.train(xs, ys, k)
00097         predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
00098         for i in range(len(predictions)):
00099             prediction = kNN.classify(model, xs[i])
00100             self.assertEqual(prediction, predictions[i])
00101             if prediction==ys[i]:
00102                 correct+=1
00103         self.assertEqual(correct, 15)


The documentation for this class was generated from the following file: