Linear regression with Scipy

Import directives

In [1]:
%matplotlib inline
from scipy import stats
import numpy as np

Linear regression

In [2]:
x = np.arange(1, 10, 1)
y = 2. * x + 1.

plt.plot(x, y, ".")

slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)

print(slope, intercept, r_value, p_value, std_err)
print("r-squared:", r_value**2)
2.0 1.0 1.0 3.2925853848e-70 0.0
r-squared: 1.0
In [3]:
x = np.arange(1., 10., 1.)
x += np.random.normal(0, 1, x.shape[0])

y = 2. * x + 1.
y += np.random.normal(0, 1, x.shape[0])

slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)

print(slope, intercept, r_value, p_value, std_err)
print("r-squared:", r_value**2)

plt.plot(x, y, ".")
plt.plot([x.min(), x.max()], [slope * x.min() + intercept, slope * x.max() + intercept], "-r")
1.83789442498 1.60437893622 0.974295673511 8.74289767194e-06 0.160616433338
r-squared: 0.949252059423
Out[3]:
[<matplotlib.lines.Line2D at 0x1150aaf28>]
In [4]:
x = np.random.random(10)
y = np.random.random(10)

slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)

plt.plot(x, y, ".")
plt.plot(x, y, ".")

print(slope, intercept, r_value, p_value, std_err)
print("r-squared:", r_value**2)
0.265097984145 0.480589678242 0.38987949715 0.265382946456 0.221374410916
r-squared: 0.152006022298
In [5]:
from scipy import linspace, polyval, polyfit, sqrt, stats, randn
from pylab import plot, title, show , legend

#Linear regression example
# This is a very simple example of using two scipy tools
# for linear regression, polyfit and stats.linregress

#Sample data creation
#number of points
n=50
t=linspace(-5,5,n)
#parameters
a=0.8; b=-4
x=polyval([a,b],t)
#add some noise
xn=x+randn(n)

#Linear regressison -polyfit - polyfit can be used other orders polys
(ar,br)=polyfit(t,xn,1)
xr=polyval([ar,br],t)
#compute the mean square error
err=sqrt(sum((xr-xn)**2)/n)

print('Linear regression using polyfit')
print('parameters: a=%.2f b=%.2f \nregression: a=%.2f b=%.2f, ms error= %.3f' % (a,b,ar,br,err))

#matplotlib ploting
title('Linear Regression Example')
plot(t,x,'g.--')
plot(t,xn,'k.')
plot(t,xr,'r.-')
legend(['original','plus noise', 'regression'])

show()

#Linear regression using stats.linregress
(a_s,b_s,r,tt,stderr)=stats.linregress(t,xn)
print('Linear regression using stats.linregress')
print('parameters: a=%.2f b=%.2f \nregression: a=%.2f b=%.2f, std error= %.3f' % (a,b,a_s,b_s,stderr))
Linear regression using polyfit
parameters: a=0.80 b=-4.00 
regression: a=0.86 b=-4.12, ms error= 0.988
Linear regression using stats.linregress
parameters: a=0.80 b=-4.00 
regression: a=0.86 b=-4.12, std error= 0.048