[英]ValueError loading data for scipy.odr regression
我最近嘗試使用scipy.odr包進行回歸分析。 每當我嘗試加載元素依賴於函數的數據列表時,都會引發值錯誤:
ValueError: x could not be made into a suitable array
我一直在使用相同類型的編程來使用scipy的minimumsq和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
簡而言之,您的代碼不起作用,因為damx
現在是functools.partial
的list
。
scipy.odr
是Fortran
正交距離回歸( ODRPACK
)的簡單包裝,xdata和ydata都必須是數字的,因為它們將在幕后轉換為某些Fortran
類型。 它不知道如何處理functools.partial
list
,因此會出錯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.