Linear regression in NumPy
crema at bu.edu
Sat Mar 18 16:24:34 CET 2006
Robert Kern wrote:
> 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:
>>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 : x = array([0., 1, 2, 3])
> In : y = array([1., 3, 5, 7])
> In : A = ones((len(y), 2), dtype=float)
> In : A[:,0] = x
> In : from numpy import linalg
> In : linalg.lstsq(A, y)
> (array([ 2., 1.]),
> array([ 1.64987674e-30]),
> array([ 4.10003045, 1.09075677]))
I'm new to numpy myself.
The above posters are correct to say that the problem must be cast into
matrix form. However, as this is such a common technique, don't most
math/stats packages do it behind the scenes?
For example, in Matlab or Octave I could type:
and I'd get the answer with shorter, more readable code. A one-liner!
Is there a 'canned' routine to do it in numpy?
btw, I am not advocating that one should not understand the concepts
behind a 'canned' routine. If you do not understand this concept you
should take <Robert Kern>'s advice and dive into a linear algebra book.
It's not very difficult, and it is essential that a scientific
programmer understand it.
More information about the Python-list