diff options
| author | wukong <wukong@longaeva> | 2018-06-05 22:34:51 -0700 |
|---|---|---|
| committer | wukong <wukong@longaeva> | 2018-06-05 22:34:51 -0700 |
| commit | 8f263e859e0970ce87b77addc80dec28e8fc7e82 (patch) | |
| tree | b0bdc392230c9960f5e5f5b3dea979405334628f /lin_reg_example.py | |
re-init
Diffstat (limited to 'lin_reg_example.py')
| -rw-r--r-- | lin_reg_example.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lin_reg_example.py b/lin_reg_example.py new file mode 100644 index 0000000..3a2043e --- /dev/null +++ b/lin_reg_example.py @@ -0,0 +1,44 @@ +#!/usr/bin/python +#/usr/local/bin/python2.7 + +import numpy as np + + +def main(): + + dataz = np.genfromtxt('example.csv', delimiter=',', names=True) + + y = dataz['attitude'] + x = dataz['correct'] + print 'x = {} {}'.format(x, np.mean(x)) + print 'y = {} {}'.format(y, np.mean(y)) + + delta_x = x - np.mean(x) + delta_y = y - np.mean(y) + delta_xy = delta_x*delta_y + delta2_x = delta_x**2 + delta2_y = delta_y**2 + print 'delta_x = {} {}'.format(delta_x, np.sum(delta_x)) + print 'delta_y = {} {}'.format(delta_y, np.sum(delta_y)) + print 'delta_xy = {} {}'.format(delta_xy, np.sum(delta_xy)) + print 'delta2_x = {} {}'.format(delta2_x, np.sum(delta2_x)) + print 'delta2_y = {} {}'.format(delta2_y, np.sum(delta2_y)) + + Sx = np.sqrt(np.sum(delta2_x)/(delta2_x.size - 1)) + Sy = np.sqrt(np.sum(delta2_y)/(delta2_y.size - 1)) + r = np.sum(delta_xy)/np.sqrt(np.sum(delta2_x)*np.sum(delta2_y)) + print 'Sx = {}'.format(Sx) + print 'Sy = {}'.format(Sy) + print 'r = {}'.format(r) + print 'r^2 = {}'.format(r**2) + + b = r*(Sy/Sx) + a = np.mean(y) - b*np.mean(x) + print 'b = {}'.format(b) + print 'a = {}'.format(a) + + print ' (attitude) \t= {}(correct) + {}'.format(b, a) + + +if __name__ == '__main__': + main() |
