Least Linear Squares: scipy.optimize.curve_fit() t

2019-09-05 17:25发布

问题:

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

回答1:

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