File: test_kNN.py

package info (click to toggle)
python-biopython 1.68%2Bdfsg-3~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 46,856 kB
  • sloc: python: 160,306; xml: 93,216; ansic: 9,118; sql: 1,208; makefile: 155; sh: 63
file content (120 lines) | stat: -rw-r--r-- 3,531 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright 2004-2008 by Michiel de Hoon.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

# See the Biopython Tutorial for an explanation of the biological
# background of these tests.

import unittest

try:
    import numpy
    from numpy import asarray
    del asarray
except ImportError:
    from Bio import MissingPythonDependencyError
    raise MissingPythonDependencyError(
        "Install NumPy if you want to use Bio.kNN.")

from Bio import kNN

xs = [[-53, -200.78],
      [117, -267.14],
      [57, -163.47],
      [16, -190.30],
      [11, -220.94],
      [85, -193.94],
      [16, -182.71],
      [15, -180.41],
      [-26, -181.73],
      [58, -259.87],
      [126, -414.53],
      [191, -249.57],
      [113, -265.28],
      [145, -312.99],
      [154, -213.83],
      [147, -380.85],
      [93, -291.13]]

ys = [1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0]


class TestKNN(unittest.TestCase):

    def test_calculate_model(self):
        k = 3
        model = kNN.train(xs, ys, k)
        self.assertEqual(model.classes, set([0, 1]))
        n = len(xs)
        for i in range(n):
            self.assertAlmostEqual(model.xs[i, 0], xs[i][0], places=4)
            self.assertAlmostEqual(model.xs[i, 1], xs[i][1], places=4)
            self.assertEqual(model.ys[i], ys[i])
        self.assertEqual(model.k, k)

    def test_classify(self):
        k = 3
        model = kNN.train(xs, ys, k)
        result = kNN.classify(model, [6, -173.143442352])
        self.assertEqual(result, 1)
        result = kNN.classify(model, [309, -271.005880394])
        self.assertEqual(result, 0)

    def test_calculate_probability(self):
        k = 3
        model = kNN.train(xs, ys, k)
        weights = kNN.calculate(model, [6, -173.143442352])
        self.assertAlmostEqual(weights[0], 0.0, places=6)
        self.assertAlmostEqual(weights[1], 3.0, places=6)
        weights = kNN.calculate(model, [309, -271.005880394])
        self.assertAlmostEqual(weights[0], 3.0, places=6)
        self.assertAlmostEqual(weights[1], 0.0, places=6)
        weights = kNN.calculate(model, [117, -267.13999999999999])
        self.assertAlmostEqual(weights[0], 2.0, places=6)
        self.assertAlmostEqual(weights[1], 1.0, places=6)

    def test_model_accuracy(self):
        correct = 0
        k = 3
        model = kNN.train(xs, ys, k)
        predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
        for i in range(len(predictions)):
            prediction = kNN.classify(model, xs[i])
            self.assertEqual(prediction, predictions[i])
            if prediction == ys[i]:
                correct += 1
        self.assertEqual(correct, 15)

    def test_leave_one_out(self):
        correct = 0
        k = 3
        model = kNN.train(xs, ys, k)
        predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1]
        for i in range(len(predictions)):
            model = kNN.train(xs[:i] + xs[i + 1:], ys[:i] + ys[i + 1:], k)
            prediction = kNN.classify(model, xs[i])
            self.assertEqual(prediction, predictions[i])
            if prediction == ys[i]:
                correct += 1
        self.assertEqual(correct, 13)

if __name__ == "__main__":
    runner = unittest.TextTestRunner(verbosity=2)
    unittest.main(testRunner=runner)