首页 文章

对象太深了所需的数组 - scipy.integrate.odeint

提问于
浏览
2

我've just started with Python yesterday, and I' m使用 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 方法的完整代码 . 因为's relatively long, I'已经在pastebin中删除了完整的.py脚本:http://pastebin.com/RphJbCHN

再次感谢 .

2 回答

  • 3

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

  • -1

    我可以通过多种方式重现您的错误 .

    如果 y0 参数或 odeintt 参数不是1-D数组,则会立即发生错误 . 在pastebin上发布的代码示例中(在注释中引用), t 被重新整形为:

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

    删除重塑 t 的行 . t 必须是1-D数组,而不是具有形状(len(t),1)的2-D数组 .

    例如...

    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-D numpy数组(不是具有形状(1,180)或(180,1)的2-D数组) .

    如果 SIR 返回一个形状错误的数组,我也会得到'object too deep...'错误 . 它必须返回一维数组,其形状与第一个参数相同 . 确保它是真正的1-D,而不是具有形状(1,180)或(180,1)的2-D .

相关问题