加载scipy.odr回归的ValueError加载数据

时间:2014-09-25 10:36:35

标签: python scipy regression

我最近尝试使用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()

编辑:

@ Robert: 感谢您的回复。我正在使用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

1 个答案:

答案 0 :(得分:0)

简而言之,您的代码无效,因为damx现在是list functools.partial

scipy.odr是围绕Fortran正交距离回归(ODRPACK)的简单包装器,xdata和ydata都必须是数字的,因为它们将被转换为某种Fortran类型引擎盖下。它不知道如何处理list functools.partial,因此错误。