[Numpy-discussion] Computing least squares fit with masked array

Thomas Goebel Thomas.Goebel@th-nuernberg...
Mon Jun 24 06:40:08 CDT 2013


Hi list,

i want to calculate the least squares fit of some data with
missing values. So i masked all values using numpys masked array
module.

Unfortunately using linalg.lstsq i only get nan data back. Can
somebody help me solve this problem?

The output of np.linalg.lstsq ist:
(masked_array(data = [ nan  nan  nan  nan],
             mask = False,
       fill_value = 1e+20)
, masked_array(data = [ nan],
             mask = False,
       fill_value = 1e+20)
, 4, array([  2.08406074e+03,   1.73066556e+01,   1.32393450e+01,
         1.09943429e-01]))

Thanks for your help,
Tom

The python code i use is:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import numpy as np

x = np.array([-30, -25, -20])
y = np.array([25, 20, 35])

z = np.array([
    [1285, 1613, 2190],
    [np.nan, 1283, 1897],
    [np.nan, np.nan, 1619]
    ])

N, M = len(x), len(y)

x_new = np.hstack([x] * N)
y_new = np.repeat(y, M)

z_new = np.repeat(z, 1)
z_new = np.ma.masked_array(z_new, np.isnan(z_new))

A = np.array([x_new, y_new, x_new*y_new, np.ones(N*M)])

out = np.linalg.lstsq(A.T, z_new)

print out


More information about the NumPy-Discussion mailing list