Search code examples
pythonnumpyleast-squares

Multiple linear regression for a surface using NumPy - example


This question is close to: fitting a linear surface with numpy least squares, but there's no sample data. I must be terribly slow but it seems I can't get it to work.

I have the following code:

    import numpy as np
    XYZ = np.array([[0, 1, 0, 1],
        [0, 0, 1, 1],
        [1, 1, 1, 1]])
    A = np.row_stack((np.ones(len(XYZ[0])), XYZ[0, :], XYZ[1:]))
    coeffs = np.linalg.lstsq(A.T, XYZ[2, :])[0]
    print coeffs

The output is:

    [  5.00000000e-01   5.55111512e-17   9.71445147e-17   5.00000000e-01]

I want z = a + bx + cy, i.e. three coefficients, but the output gives me four. Where do I go wrong here? I expected coeffs to be something like:

    [ 1.0 0.0 0.0]

Any help appreciated.


Solution

  • Peter Schneider (comment) is right: you'll want to feed XYZ[1, :] to row_stack:

    >>> A = np.row_stack((np.ones(len(XYZ[0])), XYZ[0, :], XYZ[1, :]))
    >>> np.linalg.lstsq(A.T, XYZ[2, :])[0]
    array([  1.00000000e+00,  -7.85046229e-17,  -7.85046229e-17])