annotate tests/test_linear_regression.py @ 92:f1d1f2388fd6

test linear regression
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 14:26:15 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
92
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 """
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 test linear regression
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 import csv
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 import math
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import os
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import numpy as np
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import random
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 from tvii import linear_regression
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 from tvii.noise import add_noise
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 def test_linear_regression():
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 """Make sure we can do `W*x + b = y` properly"""
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 # training data: exact fit, W=-1, b=1
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 x_train = [1,2,3,4]
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 y_train = [0,-1,-2,-3]
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 # our guesses
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 W_guess = 0. # Why not? Be bold
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 b_guess = 0.
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 # perform the regression
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 W, b, loss = linear_regression.linear_regression(x_train,
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 y_train,
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 W_guess=W_guess,
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 b_guess=b_guess)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 # make sure we're close
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 W_exact = -1.
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 b_exact = 1.
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 assert abs(W - W_exact) < 1e-5
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 assert abs(b - b_exact) < 1e-5
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 def test_linear_regression_noisy():
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 """
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 Make sure we can do `W*x + b = y` with some noise
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 """
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 # start simple
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 slope = 1.5 # rises 3 every 2
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 intercept = random.random() * 5.
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 line = lambda x: slope*x + intercept
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 # make range
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 # TODO: np.linspace(-10., 10, 100)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 xspan = (-10., 10.)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 npoints = 100
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 dx = (xspan[-1] - xspan[0])/(npoints-1.)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 xi = [xspan[0]+dx*i
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 for i in range(npoints)]
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 # add some noise to it
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 x = add_noise(xi, fraction=0.01)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58 assert len(x) == len(xi)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59 assert x != xi
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 assert x == sorted(x)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62 # calculate true y
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 truey = [line(xx) for xx in x]
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65 # add some noise to that
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
66 y = add_noise(truey, fraction=0.01)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
67 assert len(y) == len(truey)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
68
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
69 # you're now all set up for your regression
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
70 W, b, loss = linear_regression.linear_regression(x, y)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
71
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
72 # Show us what you got!
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
73 # TODO: this gives nan for both `W` and `b`
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
74 # The lines loop okay so I'm guessing some sort of
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
75 # numerical instability
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
76 try:
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
77 assert W == slope # XXX shouldn't be exactly equal anyway
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
78 except AssertionError:
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
79 dumpfile = os.environ.get('NETTWERK_FAILURE')
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
80 if dumpfile:
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
81 # dump the points
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
82 with open(dumpfile, 'w') as f:
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
83 writer = csv.writer(f)
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
84 writer.writerows(zip(x, y))
f1d1f2388fd6 test linear regression
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
85 pass # XXX ignoring true negative :(