Skip to content

Instantly share code, notes, and snippets.

@oldcai
Created March 17, 2014 09:57
Show Gist options
  • Save oldcai/9596711 to your computer and use it in GitHub Desktop.
Save oldcai/9596711 to your computer and use it in GitHub Desktop.
linear regression using python scipy
import numpy
import pylab
from scipy import stats
xi = numpy.arange(0, 9)
A = numpy.array([xi, numpy.ones(9)])
# linearly generated sequence
y = [19, 20, 20.5, 21.5, 22, 23, 23, 25.5, 24]
slope, intercept, r_value, p_value, std_err = stats.linregress(xi, y)
print 'slope', slope
print 'intercept', intercept
print 'r value', r_value
print 'p_value', p_value
print 'standard deviation', std_err
line = slope * xi + intercept
pylab.plot(xi, line, 'r-', xi, y, 'o')
pylab.savefig('fig.png')
pylab.show()
pylab.close()
numpy
matplotlib
scipy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment