我正在尝试使用SciPy的ODR将不同的曲线拟合到相同的数据。为了简化此过程,我将每个候选函数定义为Lambda函数。然后,这个Lambda函数通过我自己的函数odr_fit()
传递给ODR:
if trend_name == 'linear':
eps = odr_fit(f=lambda x, p: p[0]*x+p[1], xdata=x, ydata=y)
elif trend_name == 'quadratic':
eps = odr_fit(f=lambda x, p: p[0]*x**2+p[1]*x+p[2], xdata=x, ydata=y)
# and so on....
odr_fit()
函数定义如下:
def odr_fit(f, xdata, ydata):
"""
Function to calculate orthogonal residuals given data and a function to fit
:param f: function in the format f(x, p), where p is a list of parameters
:param xdata: Pandas Series with independent variable
:param ydata: Pandas Series of same length with dependent variable
:return: eps: estimated orthogonal errors, same length as xdata and ydata
"""
f = np.vectorize(f, excluded=['p'])
model = Model(f)
data = RealData(xdata, ydata)
odr = ODR(data, model, beta0=[10]*3) # beta0 is an initial estimate. Should be the same length as p ideally as well
odr_result = odr.run()
return odr_result.eps
我的问题:在这段代码的某个地方,p
(参数,Lambda函数的第二个参数)应该是标量。我希望它被看作一个列表或元组
IndexError: invalid index to scalar variable.
ODR还需要一个列表/元组(参见文档here)。当然,我可以单独定义每个函数,但这会很麻烦,也很难扩展。有没有办法让ODR使用Lambda函数工作?也许是某种可以强迫p
成为某个长度的向量的东西
希望有人知道解决办法
谢谢
亚历克斯
编辑:以下是完整的错误:
ERROR - <class 'IndexError'>: invalid index to scalar variable.
<traceback object at 0x000001F4669271C8>
ERROR - <class 'IndexError'>: invalid index to scalar variable.
<traceback object at 0x000001F4669271C8>
ERROR - <class 'IndexError'>: invalid index to scalar variable.
<traceback object at 0x000001F4669271C8>
ERROR - <class 'IndexError'>: invalid index to scalar variable.
<traceback object at 0x000001F4669271C8>
ERROR - <class 'IndexError'>: invalid index to scalar variable.
<traceback object at 0x000001F4669271C8>
Traceback (most recent call last):
File "C:\Users\...\features.py", line 296, in add_features
data = _add_trend_features(data, col_trends={'x':'TemperatureOutsideAvgNight', 'y':'TemperatureInsideAvgNight'})
File "C:\Users\...\trend_features.py", line 109, in _add_trend_features
return_orth_dist=True)
File "C:\Users\...\trend_features.py", line 88, in _add_trend_features
eps = odr_fit(f=lambda x, p: p[0]*x+p[1], xdata=x, ydata=y)
File "C:\Users\...\Documents\phil_anomaly_detection\phil_anomaly_detection\trend_features.py", line 23, in odr_fit
odr = ODR(data, model, beta0=[10]*3)
File "C:\Users\...\miniconda3\lib\site-packages\scipy\odr\odrpack.py", line 770, in __init__
self._check()
File "C:\Users\...\miniconda3\lib\site-packages\scipy\odr\odrpack.py", line 831, in _check
res = self.model.fcn(*arglist)
File "C:\Users\...\miniconda3\lib\site-packages\numpy\lib\function_base.py", line 2091, in __call__
return self._vectorize_call(func=func, args=vargs)
File "C:\Users\...\miniconda3\lib\site-packages\numpy\lib\function_base.py", line 2161, in _vectorize_call
ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
File "C:\Users\...\miniconda3\lib\site-packages\numpy\lib\function_base.py", line 2121, in _get_ufunc_and_otypes
outputs = func(*inputs)
File "C:\Users\...\miniconda3\lib\site-packages\numpy\lib\function_base.py", line 2086, in func
return self.pyfunc(*the_args, **kwargs)
File "C:\Users\...\trend_features.py", line 88, in <lambda>
eps = odr_fit(f=lambda x, p: p[0]*x+p[1], xdata=x, ydata=y)
IndexError: invalid index to scalar variable.
Process finished with exit code 1
目前没有回答
相关问题 更多 >
编程相关推荐