Back to index

python-biopython  1.60
test_kNN.py
Go to the documentation of this file.
00001 # Copyright 2004-2008 by Michiel de Hoon.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 # See the Biopython Tutorial for an explanation of the biological
00007 # background of these tests.
00008 
00009 import unittest
00010 
00011 try:
00012     import numpy
00013     from numpy import asarray
00014     del asarray
00015 except ImportError:
00016     from Bio import MissingPythonDependencyError
00017     raise MissingPythonDependencyError(
00018         "Install NumPy if you want to use Bio.kNN.")
00019 
00020 from Bio import kNN
00021 
00022 xs = [[-53, -200.78],
00023       [117, -267.14],
00024       [57, -163.47],
00025       [16, -190.30],
00026       [11, -220.94],
00027       [85, -193.94],
00028       [16, -182.71],
00029       [15, -180.41],
00030       [-26, -181.73],
00031       [58, -259.87],
00032       [126, -414.53],
00033       [191, -249.57],
00034       [113, -265.28],
00035       [145, -312.99],
00036       [154, -213.83],
00037       [147, -380.85],
00038       [93, -291.13]]
00039 
00040 ys = [1,
00041       1,
00042       1,
00043       1,
00044       1,
00045       1,
00046       1,
00047       1,
00048       1,
00049       1,
00050       0,
00051       0,
00052       0,
00053       0,
00054       0,
00055       0,
00056       0]
00057 
00058 class TestKNN(unittest.TestCase):
00059 
00060     def test_calculate_model(self):
00061         k = 3
00062         model = kNN.train(xs, ys, k)
00063         self.assertEqual(model.classes, set([0,1]))
00064         n = len(xs)
00065         for i in range(n):
00066             self.assertAlmostEqual(model.xs[i,0], xs[i][0], places=4)
00067             self.assertAlmostEqual(model.xs[i,1], xs[i][1], places=4)
00068             self.assertEqual(model.ys[i], ys[i])
00069         self.assertEqual(model.k, k)
00070 
00071     def test_classify(self):
00072         k = 3
00073         model = kNN.train(xs, ys, k)
00074         result = kNN.classify(model, [6,-173.143442352])
00075         self.assertEqual(result, 1)
00076         result = kNN.classify(model, [309, -271.005880394])
00077         self.assertEqual(result, 0)
00078 
00079     def test_calculate_probability(self):
00080         k = 3
00081         model = kNN.train(xs, ys, k)
00082         weights = kNN.calculate(model, [6,-173.143442352])
00083         self.assertAlmostEqual(weights[0], 0.0, places=6)
00084         self.assertAlmostEqual(weights[1], 3.0, places=6)
00085         weights = kNN.calculate(model, [309, -271.005880394])
00086         self.assertAlmostEqual(weights[0], 3.0, places=6)
00087         self.assertAlmostEqual(weights[1], 0.0, places=6)
00088         weights = kNN.calculate(model, [117, -267.13999999999999])
00089         self.assertAlmostEqual(weights[0], 2.0, places=6)
00090         self.assertAlmostEqual(weights[1], 1.0, places=6)
00091 
00092     def test_model_accuracy(self):
00093         correct = 0
00094         k = 3
00095         model = kNN.train(xs, ys, k)
00096         predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
00097         for i in range(len(predictions)):
00098             prediction = kNN.classify(model, xs[i])
00099             self.assertEqual(prediction, predictions[i])
00100             if prediction==ys[i]:
00101                 correct+=1
00102         self.assertEqual(correct, 15)
00103 
00104     def test_leave_one_out(self):
00105         correct = 0
00106         k = 3
00107         model = kNN.train(xs, ys, k)
00108         predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1]
00109         for i in range(len(predictions)):
00110             model = kNN.train(xs[:i]+xs[i+1:], ys[:i]+ys[i+1:], k)
00111             prediction = kNN.classify(model, xs[i])
00112             self.assertEqual(prediction, predictions[i])
00113             if prediction==ys[i]:
00114                 correct+=1
00115         self.assertEqual(correct, 13)
00116 
00117 if __name__ == "__main__":
00118     runner = unittest.TextTestRunner(verbosity = 2)
00119     unittest.main(testRunner=runner)