所需阵列的对象太深-scipy.integrate.odeint [英] Object Too Deep for Desired Array - scipy.integrate.odeint
问题描述
我昨天刚开始使用Python,使用scipy.integrate.odeint
时出现错误.
I've just started with Python yesterday, and I'm getting an error using scipy.integrate.odeint
.
我已经定义了一个函数
def SIR(x, t, beta, gamma, mu, M):
接受numpy.array
个对象x
,t
和M
;标量浮点数beta
,gamma
和mu
.
which takes the numpy.array
objects x
, t
, and M
; and the scalar floats beta
, gamma
, and mu
.
M
的大小为(60,60)
,但我认为这无关紧要.
M
is (60,60)
in size, but I don't think this matters.
x
和t
都是非单例的,其中x.shape
是(180,)
和t.shape
是(5000,)
.我尝试给他们一个单例尺寸,以使它们分别具有形状(180,1)
和(5000,1)
,但是我仍然遇到相同的错误:
x
and t
are both nonsingleton, with x.shape
being (180,)
and t.shape
being (5000,)
. I've tried giving them a singleton dimension, such that they have shapes (180,1)
and (5000,1)
respectively, but I still get the same error :
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
,并且如果我从中剥离x
和t
的所有参数,我也会收到此错误:
I get this error even when SIR
just returns x
, and if I strip all arguments apart from x
and t
from it :
def SIR(x, t):
return x;
如您所见,导致错误的行是
As you can see, the line causing the error is
x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu, M));
已要求我为SIR
方法添加完整的代码.由于时间相对较长,因此将完整的.py脚本放入了pastebin中:
http://pastebin.com/RphJbCHN
I've been asked to add the full code for the SIR
method. Because it's relatively long, I've dropped the full .py script in a pastebin :
http://pastebin.com/RphJbCHN
再次感谢.
推荐答案
我可以通过多种方式重现您的错误.
I can reproduce your error in several ways.
如果odeint
的y0
自变量或t
自变量不是一维数组,则会立即发生错误.在发布在pastebin上的代码示例(在注释中引用)中,t
的格式如下:
The error occurs immediately if either the y0
argument or the t
argument to odeint
is not a 1-D array. In the code example posted on pastebin (referred to in a comment), t
is reshaped like this:
t = np.arange(0, 520, 1);
t = t.reshape(len(t),1);
删除重塑t
的线. t
必须是一维数组,而不是形状为(len(t),1)的二维数组.
Delete the line that reshapes t
. t
must be a 1-D array, not a 2-D array with shape (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-D numpy数组(而不是形状为(1,180)或(的2-D数组180,1)).
Check that the initial condition that you give to odeint
(the second argument) is a 1-D numpy array (not a 2-D array with shape (1, 180) or (180, 1)).
如果SIR
返回具有错误形状的数组,我也会收到对象太深..."错误.它必须返回一个 1-D 数组,其形状与第一个参数相同.确保它是真正的一维,而不是形状为(1,180)或(180,1)的2D.
I also get the 'object too deep...' error if SIR
returns an array with the wrong shape. It must return a 1-D array, with the same shape as its first argument. Make sure it is truly 1-D, and not 2-D with shape (1, 180) or (180, 1).
这篇关于所需阵列的对象太深-scipy.integrate.odeint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!