I'm using scipy ODR to perform a Othogonal Linear Regression. I have a matrix of shape (nlines,3) representing trajectory coordinates, that is the columns are the x,y,z coordinates of a point moving in space.
The goal is to find the straight line that best approximate/fits the trajectory (similarly as asked here). Hence, the output straight line has the same shape as input: (nlines,3).
Problem: what Model should I use for the intended goal? I'm trying odr.multilinear but I get an error.
Following the example given in the documentation with minor modification
# traj_data is my 2D data matrix with x,y,z coordinates
# create the array for the independent variable
nsamples = np.arange(0,traj_data.shape[0])
nsamples_3d = np.column_stack((nsamples, nsamples, nsamples))
# Define the function you want to fit against.:
linear = odr.multilinear # is this the correct Model to use?
# Create a Data instance.:
mydata = odr.Data(nsamples_3d, traj_data, wd=1, we=1)
# Instantiate ODR with your data, model and initial parameter estimate.:
myodr = odr.ODR(mydata, linear, beta0=[1., 2.])
# Run the fit.:
myoutput = myodr.run()
However, execution stops at
myodr = odr.ODR(mydata, linear, beta0=[1., 2.])
with the error
scipy.odr.odrpack.OdrError: fcn does not output [11700, 3]-shaped array
(where 11700 is nlines in the specific example I ran)
I have no problem when using 1D data.
Am I doing something wrong?