0

i am trying to implement an python code right now, which computes the least squared error for a matrix equation

First, I have some 2 dimensional Data XDATA (Array of Array of Floats), Shape 100,9 Second, I have some 2 dimesional YDATA, Shape 100,3 My function creates an array of 3 entries out of an array of 9 entries and 3 unknow parameters. So im trying to estimate these parameters via linear least squares:

#linear regression
#Messured Data
xdata = np.array(data[0:9])
xdata = xdata.astype(np.float)
xdata = np.transpose(xdata)
#True y
ydata = np.array(data[9:12])
ydata = ydata.astype(np.float)
ydata = np.transpose(ydata)

#Timestamps
size = np.size(xdata)/np.size(xdata[0])

def func(xdata,m1,m2,g):
    y_est = []
    for x in xdata:
        u_est = []
        u_est.append((m1+m2)*(x[6]+g))
        u_est.append(m2*(2*x[5]*x[4]*x[2]+(x[2]**2)*x[7]))
        u_est.append(m2*(x[8]-x[2]*(x[4]**2)))
        y_est.append(u_est)
    y_est = np.array(y_est)
    return y_est

print (curve_fit(func,xdata,ydata))

But it throws an error i am not (yet) able to fix: Error

jmd_dk
  • 12,125
  • 9
  • 63
  • 94
nik.yan
  • 71
  • 1
  • 11
  • It looks your Y data is multidimensional. `curve_fit` requires the output array to be one-dimensional. You might take a look at [this question](http://stackoverflow.com/questions/40829791/fitting-a-vector-function-with-curve-fit-in-scipy) or [this one](http://stackoverflow.com/questions/21566379/fitting-a-2d-gaussian-function-using-scipy-optimize-curve-fit-valueerror-and-m). – BrenBarn Jan 11 '17 at 20:50
  • Show us what `func` produces for typical inputs. – hpaulj Jan 11 '17 at 20:56
  • func produces an array of arrays with 3 entries [x][3] out of the data array of arrays with 9 entries [x][9] Running func over complete xdata returns calculated ydata for every array entry of xdata , but i need to estimate the parameters (m1,m2,g), which are not given. curve_fit and lstsq throw the same error (see above) – nik.yan Jan 15 '17 at 12:44

1 Answers1

0

curve_fit solves a nonlinear least squares problem. For linear least squares, better use lsq_linear or numpy.lstsq

ev-br
  • 24,968
  • 9
  • 65
  • 78