Mercurial > hg > tvii
changeset 70:351fc97bb996
add error computation + test functions
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 17 Dec 2017 13:22:44 -0800 |
parents | 0bb36ae047c3 |
children | 9d55e0299c3f |
files | tests/test_error.py tvii/error.py |
diffstat | 2 files changed, 41 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_error.py Sun Dec 17 13:22:44 2017 -0800 @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +""" +test error computation functionality +""" + +import numpy as np +import os +import unittest +from tvii import error + +class ErrorTest(unittest.TestCase): + + def test_error_computation(self): + """test error computation""" + + def square(x): + return x*x + + x = [[1], [2], [3]] + y = np.array([0, 3, 8]) + + yhat, _err = error.error(square, x, y) + + assert list(yhat) == [1, 4, 9] + assert list(_err) == [1, 1, 1] + +if __name__ == '__main__': + unittest.main()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tvii/error.py Sun Dec 17 13:22:44 2017 -0800 @@ -0,0 +1,12 @@ +import numpy as np + +def error(function, x, y): + """ + computes true value and error and returns + them both as a 2-tuple + """ + + yhat = np.array([function(*val) for val in x]) + error = yhat - np.array(y) + + return (yhat, error)