Sunday, April 27, 2014

Linrear Regression in Python using Scipy with plot

Here is a linear regression example using the scipy.stats library.

from scipy import stats
import numpy
x = [5.05, 6.75, 3.21, 2.66]
y = [1.65, 26.5, -5.93, 7.96]
plot(x,y,'b+',markersize=19,label='Real Data')
gradient, intercept, r_value, p_value, std_err = stats.linregress(x,y)
print "Gradient and intercept", gradient, intercept
xx= numpy.linspace(-5,10,100)
z=gradient*xx+intercept
plt.plot(xx,z,'r',label='Linear Regression Function')
plt.legend(loc=2)


Here is the result of running the above code:





No comments:

Post a Comment