Search code examples
pythonnumpyleast-squares

"Incompatible Dimensions" using lstsq with Python's numpy


First my code:

import numpy as np

def square(list):
    return [i ** 2 for i in list]

def multLists(x, y):
    return x * y

def main():
    x = np.array([1.02, 0.95, 0.87, 0.77, 0.67, 0.55, 0.44, 0.30, 0.16, 0.01])
    y = np.array([0.39, 0.32, 0.27, 0.22, 0.18, 0.15, 0.13, 0.12, 0.13, 0.15])

    a = square(y)
    b = multLists(x,y)
    c = x
    d = y
    e = np.ones(len(x))
    x2 = square(x)

    Matrix = np.matrix([a,b,c,d,e])

    Output = np.linalg.lstsq(Matrix,x2)[0]
    print Output


main()

And my error code:

Traceback (most recent call last):
File "problem6.py", line 26, in <module>
main()
File "problem6.py", line 22, in main
Output = np.linalg.lstsq(Matrix,x2)[0]
File "/usr/lib/python2.7/dist-packages/numpy/linalg/linalg.py", line 1828, in lstsq
raise LinAlgError('Incompatible dimensions')
numpy.linalg.linalg.LinAlgError: Incompatible dimensions

Essentially, I'm trying to solve Ax=b, with A equaling "Matrix", and b="x2". I'm then trying to use least-squares to solve for X, but it isn't working. If, however, I change Matrix to equal:

Matrix = [[0 for x in range(5)] for x in range(10)] 

The code then compiles and runs properly (although obviously with wrong values, since Matrix isn't supposed to equal ^ that. Any thoughts? Thanks.


Solution

  • You need to transpose your matrix:

    Output = np.linalg.lstsq(Matrix.T, x2)[0]
    

    Your broken code produces a matrix with 5 rows of 10 elements. Your working code produces a matrix with 10 rows of 5 elements.


    You should probably also use np.array instead of np.matrix, and return an np.array rather than a list from square