Menu

[r3030]: / trunk / users_guide / code / linear_regression.py  Maximize  Restore  History

Download this file

24 lines (18 with data), 614 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from pylab import *
# Generate some test data; y is a linear function of x + nse
x = arange(0.0, 2.0, 0.05)
nse = 0.3*randn(len(x))
y = 2+ 3*x + nse
# the bestfit line from polyfit; you can do arbitrary order
# polynomials but here we take advantage of a line being a first order
# polynomial
m,b = polyfit(x,y,1)
# plot the data with blue circles and the best fit with a thick
# solid black line
plot(x, y, 'bo', x, m*x+b, '-k', linewidth=2)
ylabel('regression')
grid(True)
# save the image to hardcopy
savefig('../figures/linear_regression.eps')
savefig('../figures/linear_regression.png')
show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.