【发布时间】:2014-09-30 13:41:57
【问题描述】:
我最近尝试使用 scipy.odr 包进行回归分析。每当我尝试加载元素依赖于函数的数据列表时,都会引发值错误:
ValueError: x could not be made into a suitable array
我一直在使用相同类型的编程来进行拟合,使用 scipy 的 leastsq 和 curve_fit 例程没有问题。
知道要更改什么以及如何进行吗?非常感谢...
这里我包括一个最小的工作示例:
from scipy import odr
from functools import partial
import numpy as np
import matplotlib.pyplot as plt
### choose select=0 and for myModel a list of elements is called which are a function of some parameters
### this results in error message: ValueError: x could not be made into a suitable array
### choose select=1, the function temp is exlcuded, and a fit is generated
### what do i have to do in order to run the programm successfully using select=0?
## choose here!
select=1
pfit=[1.0,1.0]
q0=[1,2,3,4,5]
q1=[3,8,10,19,27]
def temp(par, val):
p1,p2=par
temp_out = p1*val**p2
return temp_out
def fodr(a,x):
if select==0:
fitf = np.array([xi(a) for xi in x])
else:
fitf= a[0]*x**a[1]
return fitf
# define model
myModel = odr.Model(fodr)
# load data
damy=q1
if select==0:
damx=[]
for el in q0:
elm=partial(temp,val=el)
damx.append(elm)
#damx=[el(pfit) for el in damx] # check that function temp works fine
#print damx
else:
damx=q0
myData = odr.Data(damx, damy)
myOdr = odr.ODR(myData, myModel , beta0=pfit, maxit=100, ifixb=[1,1])
out = myOdr.run()
out.pprint()
编辑:
@罗伯特: 感谢您的回复。我正在使用 scipy 版本“0.14.0”。在我的最小示例中使用 select==0 我得到以下回溯:
Traceback (most recent call last):
File "scipy-odr.py", line 48, in <module>
out = myOdr.run()
File "/home/tg/anaconda/lib/python2.7/site-packages/scipy/odr/odrpack.py", line 1061, in run
self.output = Output(odr(*args, **kwds))
ValueError: x could not be made into a suitable array
【问题讨论】:
-
请发布完整的回溯,并说明您正在使用的 scipy 版本。你的例子对我来说很好。
-
嗨罗伯特,我编辑了我的问题。希望这将有助于解决问题。非常感谢 - 托比
标签: python scipy regression