# Linear regression in NumPy

Discussion in 'Python' started by Jianzhong Liu, Dec 5, 2006.

1. ### Jianzhong LiuGuest

Hello, Guys,

I have a question about the linear_least_squares in Numpy.

My linear_least_squares cannot give me the results.

I use Numpy1.0. The newest version. So I checked online and get your
guys some examples.

I did like this.

[john@crux 77] ~ >> py
Python 2.4.3 (#1, May 18 2006, 07:40:45)
[GCC 3.3.3 (cygwin special)] on cygwin
>>> from Numeric import *
>>> from LinearAlgebra import linear_least_squares
>>> from Matrix import *
>>> y = Matrix([[1], [2], [4]])
>>> x = Matrix([[1, 1], [1, 2], [1, 3]])
>>> print y

Matrix([[1],
[2],
[4]])
>>> x

Matrix([[1, 1],
[1, 2],
[1, 3]])
>>> print linear_least_squares(x, y)

Here my Numpy stops. and never give me a result. THis is the problem of
Numpy or sth wrong.

Can you guys give me a LinearAlgebra.py so that I can have a try?

Thanks,

John

Jianzhong Liu, Dec 5, 2006

2. ### JonGuest

> I have a question about the linear_least_squares in Numpy.

Not quite sure what is going on, it looks like there could be some
confusion as to linear_least_squares is expecting as an argument of
some Numeric arrays and what you are supplying (a Matrix) is perhaps
not close enough to being the same thing.

Up-to-date versions for all this are all in "numpy" nowadays and the
numpy mailing list is perhaps a better place to ask:
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Anyway, I've pasted in an example using Numeric and LinearAlgebra,
which you seem to have on your system. They still work fine. Not sure
what the "Matrix" package is that you are using?

HTH,

Jon

example:

C:\>python
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32
>>> from Numeric import *
>>> from LinearAlgebra import linear_least_squares
>>> a=array( [[1, 1], [1, 2], [1, 3]] )
>>> y=array( [ 1,2,4] ) # note 1 D
>>> a

array([[1, 1],
[1, 2],
[1, 3]])
>>> y

array([1, 2, 4])
>>> linear_least_squares(a,y)

(array([-0.66666667, 1.5 ]), array([ 0.16666667]), 2, array([
4.07914333, 0.60049122]))
>>>

# Is this what you expect as output???

Jianzhong Liu wrote:
> Hello, Guys,
>
> I have a question about the linear_least_squares in Numpy.
>
> My linear_least_squares cannot give me the results.
>
> I use Numpy1.0. The newest version. So I checked online and get your
> guys some examples.
>
> I did like this.
>
> [john@crux 77] ~ >> py
> Python 2.4.3 (#1, May 18 2006, 07:40:45)
> [GCC 3.3.3 (cygwin special)] on cygwin
> >>> from Numeric import *
> >>> from LinearAlgebra import linear_least_squares
> >>> from Matrix import *
> >>> y = Matrix([[1], [2], [4]])
> >>> x = Matrix([[1, 1], [1, 2], [1, 3]])
> >>> print y

> Matrix([[1],
> [2],
> [4]])
> >>> x

> Matrix([[1, 1],
> [1, 2],
> [1, 3]])
> >>> print linear_least_squares(x, y)

>
> Here my Numpy stops. and never give me a result. THis is the problem of
> Numpy or sth wrong.
>
> Can you guys give me a LinearAlgebra.py so that I can have a try?
>
> Thanks,
>
> John

Jon, Dec 5, 2006