Linear regression in NumPy

Robert Kern robert.kern at gmail.com
Fri Mar 17 18:58:23 EST 2006


nikie wrote:
> I still don't get it...
> My data looks like this:
>  x = [0,1,2,3]
>  y = [1,3,5,7]
> The expected output would be something like (2, 1), as y[i] = x[i]*2+1
> 
> (An image sometimes says more than 1000 words, so to make myself clear:
> this is what I want to do:
> http://www.statistics4u.info/fundstat_eng/cc_regression.html)
> 
> So, how am I to fill these matrices?

As the docstring says, the problem it solves is min ||A*x - b||_2. In order to
get it to solve your problem, you need to cast it into this matrix form. This is
out of scope for the docstring, but most introductory statistics or linear
algebra texts will cover this.

In [201]: x = array([0., 1, 2, 3])

In [202]: y = array([1., 3, 5, 7])

In [203]: A = ones((len(y), 2), dtype=float)

In [204]: A[:,0] = x

In [205]: from numpy import linalg

In [206]: linalg.lstsq(A, y)
Out[206]:
(array([ 2.,  1.]),
 array([  1.64987674e-30]),
 2,
 array([ 4.10003045,  1.09075677]))

-- 
Robert Kern
robert.kern at gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list