对象太深,无法使用所需的数组-scipy.integrate.odein

2024-04-18 13:15:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我昨天刚开始使用Python,使用scipy.integrate.odeint时出现错误。

我定义了一个函数

def SIR(x, t, beta, gamma, mu, M):

它接受numpy.array对象xtM;标量浮动betagammamu

M的大小是(60,60),但我认为这无关紧要。

xt都是不独立的,其中x.shape(180,)t.shape(5000,)。我试着给它们一个单重维度,使它们分别具有形状(180,1)(5000,1),但我仍然得到相同的错误:

In [1]: run measles_age.py
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/IPython/utils/py3compat.py in execfile(fname, *where)
    173             else:
    174                 filename = fname
--> 175             __builtin__.execfile(filename, *where)

/Users/qcaudron/Documents/SIR/measles_age.py in <module>()
    111 
    112 
--> 113         x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu, M));
    114 
    115 #       plot(t, x);


/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/scipy/integrate/odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg)
    141     output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
    142                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
--> 143                              ixpr, mxstep, mxhnil, mxordn, mxords)
    144     if output[-1] < 0:
    145         print _msgs[output[-1]]

即使当SIR只返回x时,我也会得到这个错误,如果我从中除去xt之外的所有参数:

def SIR(x, t):
    return x;

如您所见,导致错误的行是

x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu, M));

编辑:

我被要求添加SIR方法的完整代码。因为它比较长,所以我将完整的.py脚本放到了一个pastebin中: http://pastebin.com/RphJbCHN

再次感谢。


Tags: inpyoutputdef错误argsscipybeta
2条回答

tutorial看来,integrate.odeint()的第一个参数需要是一个函数来操作(在您的例子中)x0t。由于SIR()函数只接受一个参数,因此操作失败。从SIR()返回的结果的大小和/或形状可能与其余参数有关。

我可以用几种方法再现你的错误。

如果y0参数或odeintt参数不是一维数组,则会立即发生错误。在粘贴到pastebin(在注释中引用)上的代码示例中,t的形状如下:

t = np.arange(0, 520, 1);
t = t.reshape(len(t),1);

删除重塑t形状的行。t必须是一维数组,而不是具有形状(len(t),1)的二维数组。

例如。。。

In [177]: def SIR(x, t):
   .....:     return x
   .....: 

这很管用。。。

In [178]: x0 = [0.1, 0.2]

In [179]: odeint(SIR, x0, t=[0, 0.5, 1])
Out[179]: 
array([[ 0.1       ,  0.2       ],
       [ 0.16487213,  0.32974426],
       [ 0.27182822,  0.54365643]])

这将导致错误:

In [180]: x0 = [[0.1, 0.2]]  # wrong shape

In [181]: odeint(SIR, x0, t=[0, 0.5, 1])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-181-a37878f92395> in <module>()
----> 1 odeint(SIR, x0, t=[0, 0.5, 1])

/home/warren/anaconda/lib/python2.7/site-packages/scipy/integrate/odepack.pyc in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg)
    142     output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
    143                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
--> 144                              ixpr, mxstep, mxhnil, mxordn, mxords)
    145     if output[-1] < 0:
    146         print _msgs[output[-1]]

ValueError: object too deep for desired array

检查给odeint(第二个参数)的初始条件是1-Dnumpy数组(不是具有形状(1,180)或(180,1)的二维数组)。

如果SIR返回一个形状错误的数组,则还会出现“object too deep…”错误。它必须返回一个1-D数组,其形状与其第一个参数相同。确保它是真正的一维,而不是具有形状(1,180)或(180,1)的二维。

相关问题 更多 >