我昨天刚开始使用Python,使用时遇到错误scipy.integrate.odeint
.
我定义了一个函数
def SIR(x, t, beta, gamma, mu, M):
这需要numpy.array
物体x
, t
, and M
;标量浮动beta
, gamma
, and mu
.
M
is (60,60)
大小,但我认为这并不重要。
x
and t
都是非单例的,有x.shape
being (180,)
and t.shape
being (5000,)
。我尝试给它们一个单一的维度,这样它们就有形状(180,1)
and (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
,如果我剥离除以下之外的所有参数x
and t
从中 :
def SIR(x, t):
return x;
如您所见,导致错误的行是
x = integrate.odeint(SIR, x0, t, args=(beta, gamma, mu, M));
EDIT :
我被要求添加完整的代码SIR
方法。因为它相对较长,所以我将完整的 .py 脚本放入 Pastebin 中:http://pastebin.com/RphJbCHN http://pastebin.com/RphJbCHN
再次感谢。