python - Scipy.Odr multiple variable regression -
i perform multidimensional odr scipy.odr
. read api documentation, says multi-dimensionality possible, cannot make work. cannot find working example on internet , api crude , give no hints how proceed.
here mwe:
import numpy np import scipy.odr def linfit(beta, x): return beta[0]*x[:,0] + beta[1]*x[:,1] + beta[2] n = 1000 t = np.linspace(0, 1, n) x = np.full((n, 2), float('nan')) x[:,0] = 2.5*np.sin(2*np.pi*6*t)+4 x[:,1] = 0.5*np.sin(2*np.pi*7*t + np.pi/3)+2 e = 0.25*np.random.randn(n) y = 3*x[:,0] + 4*x[:,1] + 5 + e print(x.shape) print(y.shape) linmod = scipy.odr.model(linfit) data = scipy.odr.data(x, y) odrfit = scipy.odr.odr(data, linmod, beta0=[1., 1., 1.]) odrres = odrfit.run() odrres.pprint()
it raises following exception:
scipy.odr.odrpack.odr_error: number of observations not match
which seems related matrix shapes, not know how must shape properly. know?
firstly, in experience scipy.odr uses arrays, not matrices. library seems make large amount of size checks along way , getting work multiple variables seems quite troublesome.
this workflow how work (and worked @ least on python 2.7):
import numpy np import scipy.odr n = 1000 t = np.linspace(0, 1, n) def linfit(beta, x): return beta[0]*x[0] + beta[1]*x[1] + beta[2] #notice changed indices x x1 = 2.5*np.sin(2*np.pi*6*t)+4 x2 = 0.5*np.sin(2*np.pi*7*t + np.pi/3)+2 x = np.row_stack( (x1, x2) ) #odr doesn't seem work column_stack e = 0.25*np.random.randn(n) y = 3*x[0] + 4*x[1] + 5 + e #indices changed linmod = scipy.odr.model(linfit) data = scipy.odr.data(x, y) odrfit = scipy.odr.odr(data, linmod, beta0=[1., 1., 1.]) odrres = odrfit.run() odrres.pprint()
so using identical (1d?) arrays, using row_stack , adressing single index number seems work.
Comments
Post a Comment