Back to index

python-biopython  1.60
test_LogisticRegression.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 try:
00010     import numpy
00011     from numpy import linalg #missing in PyPy's micronumpy
00012 except ImportError:
00013     from Bio import MissingExternalDependencyError
00014     raise MissingExternalDependencyError(
00015         "Install NumPy if you want to use Bio.LogisticRegression.")
00016 
00017 import unittest
00018 import sys
00019 from Bio import LogisticRegression
00020 
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 TestLogisticRegression(unittest.TestCase):
00059 
00060     def test_calculate_model(self):
00061         model = LogisticRegression.train(xs, ys)
00062         beta = model.beta
00063         self.assertAlmostEqual(beta[0],  8.9830, places=4)
00064         self.assertAlmostEqual(beta[1], -0.0360, places=4)
00065         self.assertAlmostEqual(beta[2],  0.0218, places=4)
00066 
00067     def test_classify(self):
00068         model = LogisticRegression.train(xs, ys)
00069         result = LogisticRegression.classify(model, [6,-173.143442352])
00070         self.assertEqual(result, 1)
00071         result = LogisticRegression.classify(model, [309, -271.005880394])
00072         self.assertEqual(result, 0)
00073 
00074     def test_calculate_probability(self):
00075         model = LogisticRegression.train(xs, ys)
00076         q, p = LogisticRegression.calculate(model, [6,-173.143442352])
00077         self.assertAlmostEqual(p, 0.993242, places=6)
00078         self.assertAlmostEqual(q, 0.006758, places=6)
00079         q, p = LogisticRegression.calculate(model, [309, -271.005880394])
00080         self.assertAlmostEqual(p, 0.000321, places=6)
00081         self.assertAlmostEqual(q, 0.999679, places=6)
00082 
00083     def test_model_accuracy(self):
00084         correct = 0
00085         model = LogisticRegression.train(xs, ys)
00086         predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
00087         for i in range(len(predictions)):
00088             prediction = LogisticRegression.classify(model, xs[i])
00089             self.assertEqual(prediction, predictions[i])
00090             if prediction==ys[i]:
00091                 correct+=1
00092         self.assertEqual(correct, 16)
00093 
00094     def test_leave_one_out(self):
00095         correct = 0
00096         predictions = [1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0]
00097         for i in range(len(predictions)):
00098             model = LogisticRegression.train(xs[:i]+xs[i+1:], ys[:i]+ys[i+1:])
00099             prediction = LogisticRegression.classify(model, xs[i])
00100             self.assertEqual(prediction, predictions[i])
00101             if prediction==ys[i]:
00102                 correct+=1
00103         self.assertEqual(correct, 15)
00104 
00105 if __name__ == "__main__":
00106     runner = unittest.TextTestRunner(verbosity = 2)
00107     unittest.main(testRunner=runner)