Python 对象对于所需数组太深-scipy.integrate.odeint
昨天我刚开始使用Python,使用Python 对象对于所需数组太深-scipy.integrate.odeint,python,numpy,scipy,odeint,Python,Numpy,Scipy,Odeint,昨天我刚开始使用Python,使用scipy.integrate.odeint时出现了一个错误 我定义了一个函数 def SIR(x, t, beta, gamma, mu, M): 它接受numpy.array对象x、t和M;标量浮动为beta,gamma和mu M的大小是(60,60),但我认为这无关紧要 x和t都是非单数,其中x.shape为(180,)和t.shape为(5000,)。我试着给它们一个单体维度,这样它们分别有(180,1)和(5000,1),但我仍然得到相同的错误: I
scipy.integrate.odeint
时出现了一个错误
我定义了一个函数
def SIR(x, t, beta, gamma, mu, M):
它接受numpy.array
对象x
、t
和M
;标量浮动为beta
,gamma
和mu
M
的大小是(60,60)
,但我认为这无关紧要
x
和t
都是非单数,其中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]]
如您所见,导致错误的行是
x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu, M));
编辑:
我被要求为SIR
方法添加完整的代码。因为它相对较长,所以我将完整的.py脚本放在了一个粘贴箱中:
再次感谢。从中可以看出,
integrate.odeint()
的第一个参数需要是一个函数来操作(在您的情况下)x0
和t
。由于SIR()
函数只接受一个参数,因此操作失败。从SIR()
返回的结果的大小和/或形状可能与其余参数有关。我可以通过几种方式重现您的错误
如果y0
参数或t
参数到odeint
不是一维数组,则会立即发生错误。在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
In[180]:x0=[[0.1,0.2]]#形状错误
在[181]中:odeint(SIR,x0,t=[0,0.5,1])
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
---->1阶积分(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、mxwords、printmessg)
142输出=_odepack.odeint(func、y0、t、args、Dfun、col_deriv、ml、mu、,
143全输出,rtol,atol,tcrit,h0,hmax,hmin,
-->144 ixpr、mxstep、mxhnil、mxordn、mxords)
145如果输出[-1]<0:
146打印_msgs[输出[-1]]
ValueError:对象对于所需数组太深
检查您为odeint
(第二个参数)提供的初始条件是否为一维numpy数组(不是形状为(1180)或(180,1)的二维数组)
如果
SIR
返回一个形状错误的数组,我还会得到“object too deep…”错误。它必须返回与第一个参数形状相同的一维数组。确保它是真正的一维,而不是形状为(1180)或(180,1)的二维。欢迎使用python!大多数分号在代码中都是不必要的。谢谢。我知道它们没有必要-我只是觉得它们看起来更干净:)谢谢上面的编辑。从中,SIR
应该返回dx/dt
att
。这就是SIR
所做的吗?是的,在完整的代码中是这样的。当它立即返回x
时,它实际上并不相关-我们正在求解的方程很可能是dx/dt=x
,在这种情况下,是的,它返回了正确的计算。我不确定为什么返回“对象对于所需数组来说太深”错误。你试过用吗?对不起,也许我不清楚。我将其剥离为仅此而已,以查看问题是否出在SIR
功能上。事实并非如此。我得到的错误与SIR
接受五个参数时的错误相同:defsir(x,t,beta,gamma,mu):
返回x使用x=integrate.odeint(SIR,x0,t,args=(beta,gamma,mu))调用代码>谢谢-不过,我已经检查过了SIR
返回一个大小为(180,)
的numpy数组,odeint
的y0
参数也是(180,)
的形状。@Quentin:如果可以,请将SIR的代码添加到问题中,并提供一种独立的运行方式,所以我们可以重现你得到的错误。我更新了我的答案,并评论了t
是一维的。不要将其重塑为二维数组。非常感谢你,沃伦。我不知道我怎么会错过。我把它修好了,它好像跑了。
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