[SciPy-user] 'linear regression'

Robert Kern rkern at ucsd.edu
Fri Jun 3 04:40:09 CDT 2005

Christian Meesters wrote:
> Hi
> I'm wondering whether there is a tool in scipy which can do a line fit 
> ('linear regression') fit for me. (Preferably something which can work 
> with and without given error arrays in x and/or y.) Else I'd have to 
> code it myself ...
> Well, I couldn't find such a function described in the manual: Could 
> anyone pleas point it out to me where I can find this in scipy - in case 
> it really exists?

x = array([1., 2, ...])
y = array([3., 4, ...])

# standard error of the y-variable:
sy = array([0.1, 0.2, ...])

N = len(x)
A = ones((2, N), x.typecode())
A[1] = x
A /= sy
A = transpose(A)
y /= sy

sol, res, rank, s = scipy.linalg.lstsq(A, y)
# y = sol[0] + sol[1]*x

I forget the linear implementation of total least squares (errors in 
both variables); I always use the full-blown nonlinear implementation, 
which is not yet integrated into scipy.


Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter

More information about the SciPy-user mailing list