xeon123
xeon123

Reputation: 909

Calculate multivariate linear regression with numpy

1 - Using A = np.array([x1,x2,x3]) worked to fix the error in How I plot the linear regression.

So I decided increase the number of elements in x1,x2 and x3 and continue to use example in How I plot the linear regression, and now I get the error "ValueError: too many values to unpack". Numpy can't calculate with so many numbers?

>>> x1 = np.array([3,2,2,3,4,5,6,7,8])
>>> x2 = np.array([2,1,4.2,1,1.5,2.3,3,6,9])
>>> x3 = np.array([6,5,8,9,7,0,1,2,1])
>>> y = np.random.random(3)
>>> A = np.array([x1,x2,x3])
>>> m,c = np.linalg.lstsq(A,y)[0]
Traceback (most recent call last):
File "testNumpy.py", line 18, in <module>
  m,c = np.linalg.lstsq(A,y)[0]
ValueError: too many values to unpack

2 - I also compared my version with the one defined in Multiple linear regression with python. Which one is correct? Why they use the transpose in this example?

Thanks,

Upvotes: 0

Views: 7677

Answers (1)

Matti Lyra
Matti Lyra

Reputation: 13088

The unpack error doesn't come from NumPy it comes from you trying to unpack two values from the function call, when only one is returned, NOTE the [0] at the end of the line

>>> x1 = np.array([3,2,2,3,4,5,6,7,8])
>>> x2 = np.array([2,1,4.2,1,1.5,2.3,3,6,9])
>>> x3 = np.array([6,5,8,9,7,0,1,2,1])
>>> y = np.random.random(3)
>>> A = np.array([x1,x2,x3])
>>> print np.linalg.lstsq(A,y)[0]
array([ 0.01789803,  0.01546994,  0.01128087,  0.02851178,  0.02561285,
        0.00984112,  0.01332656,  0.00870569, -0.00064135])

compared to

>>> print np.linalg.lstsq(A,y)
(array([ 0.01789803,  0.01546994,  0.01128087,  0.02851178,  0.02561285,
         0.00984112,  0.01332656,  0.00870569, -0.00064135]),
 array([], dtype=float64), 
 3,
 array([ 21.78630954,  12.03873305,   3.8217304 ]))

See the numpy docs, the first array are the coefficients of the variables. I think the confusion here is a variable versus an observation. You currently have three observations, and nine variables. The A.T turns the variables into observations and vice versa.

Upvotes: 2

Related Questions