Quick linear regression

By Hal Canary, 2011-11-16 12:00:00 (link)

Quick linear regression script

#!/usr/bin/env python
#sudo yum -y install scipy
# input is space-deleimited (x y) pairs.
import scipy
import numpy
import sys

inp = [map(float, l.split()[0:2]) for l in sys.stdin]
x = numpy.array([t[0] for t in inp])
y = numpy.array([t[1] for t in inp])
fit = scipy.polyfit(x,y,1)
print 'y = (%f) x + (%f)' % tuple(fit)

power fit:

#!/usr/bin/env python
#sudo yum -y install scipy
# input is space-deleimited (x y) pairs.
import scipy
import numpy
import sys
import math
inp = [map(math.log, map(float, l.split()[0:2]))
	for l in sys.stdin]
x = numpy.array([t[0] for t in inp])
y = numpy.array([t[1] for t in inp])
fit = scipy.polyfit(x,y,1)
print 'y = ({0[1]})e^({0[0]})'.format(tuple(fit))

(back)