Linear regression in NumPy
marek.rocki at wp.pl
marek.rocki at wp.pl
Fri Mar 17 16:45:26 EST 2006
nikie napisal(a):
> I'm a little bit stuck with NumPy here, and neither the docs nor
> trial&error seems to lead me anywhere:
> I've got a set of data points (x/y-coordinates) and want to fit a
> straight line through them, using LMSE linear regression. Simple
> enough. I thought instead of looking up the formulas I'd just see if
> there isn't a NumPy function that does exactly this. What I found was
> "linear_least_squares", but I can't figure out what kind of parameters
> it expects: I tried passing it my array of X-coordinates and the array
> of Y-coordinates, but it complains that the first parameter should be
> two-dimensional. But well, my data is 1d. I guess I could pack the X/Y
> coordinates into one 2d-array, but then, what do I do with the second
> parameter?
Well, it works for me:
x = Matrix([[1, 1], [1, 2], [1, 3]])
y = Matrix([[1], [2], [4]])
print linear_least_squares(x, y)
Make sure the dimensions are right. X should be n*k, Y should (unless
you know what you are doing) be n*1. So the first dimension must be
equal.
If you wanted to:
y = Matrix([1, 2, 4])
it won't work because it'll have dimensions 1*3. You would have to
transpose it:
y = transpose(Matrix([1, 2, 4]))
Hope this helps.
More information about the Python-list
mailing list