Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scipy.Odr多元回归_Python_Python 3.x_Numpy_Scipy_One Definition Rule - Fatal编程技术网

Python Scipy.Odr多元回归

Python Scipy.Odr多元回归,python,python-3.x,numpy,scipy,one-definition-rule,Python,Python 3.x,Numpy,Scipy,One Definition Rule,我想使用scipy.ODR执行多维ODR。我读了API文档,它说多维是可能的,但我不能让它工作。我在互联网上找不到工作示例,API非常粗糙,没有给出如何继续的提示 这是我的MWE: import numpy as 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((

我想使用
scipy.ODR
执行多维ODR。我读了API文档,它说多维是可能的,但我不能让它工作。我在互联网上找不到工作示例,API非常粗糙,没有给出如何继续的提示

这是我的MWE:

import numpy as 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()
它引发了以下异常:

scipy.odr.odrpack.odr_error: number of observations do not match

这似乎与我的矩阵形状有关,但我不知道如何正确地塑造它。有人知道吗?

首先,根据我的经验,scipy.odr主要使用数组,而不是矩阵。该库似乎在这一过程中进行了大量的大小检查,让它处理多个变量似乎相当麻烦

这是我通常使用的工作流程(至少在python 2.7上使用过):


因此,使用相同的(1D?)数组、使用行堆栈和按单个索引编号寻址似乎是可行的。

非常感谢,很有效,但实际上并不明显或有很好的文档记录。你是自己找到的吗?或者你有关于这个的任何文件吗?我真的不记得了。我想我是通过模仿某个地方的一个例子来实现它的,现在我有了一些使用它的脚本,所以我需要检查一个脚本来编写它。
import numpy as 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 for 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 to work with 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()