IndexError:索引3超出了尺寸为3的轴1的范围 [英] IndexError: index 3 is out of bounds for axis 1 with size 3
本文介绍了IndexError:索引3超出了尺寸为3的轴1的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想解决该错误:
IndexError:索引3超出了尺寸1为3的轴1的边界
IndexError: index 3 is out of bounds for axis 1 with size 3
from numpy import *
from pylab import *
def SIR2(I0, beta, gama, w, sigma, p, dt, tmax):
N = int(tmax / dt) + 1 # nombre de pas de temps
T = zeros([N + 1, 3])
T[0, 0] = 1000000 - I0
T[0, 1] = I0
for i in range(N + 1):
T[i + 1, 0] = T[i, 0] - dt * (beta * T[i, 0] * T[i, 1] + p * T[i, 0] - w * T[i, 2])
T[i + 1, 1] = T[i, 1] + dt * T[i, 1] * (beta * T[i, 0] - gama - sigma)
T[i + 1, 2] = T[i, 2] + dt * (gama * T[i, 1] + p * T[i, 0] + w * T[i, 2])
T[i + 1, 3] = sigma * T[i, 1] * dt + T[i, 3]
return T
beta = 1 / 3000000
gama = 1 / 20
I0 = 10
p = 0
sigma = 5 / 1000
w = 0
dt = 0.05
tmax = 150
T = SIR2(I0, beta, gama, w, sigma, p, dt, tmax)
N = int(tmax / dt) + 1
X = zeros(N + 1)
for i in range(N + 1):
X[i] = i * dt
plot(X, T[:, 0])
plot(X, T[:, 1])
plot(X, T[:, 2])
plot(X, T[:, 3])
show()
推荐答案
numpy
中的数组的索引从0
开始.因此,第二个轴为3的数组将可订阅,最大索引为2.
Indices of arrays in numpy
start from 0
. So an array with a second axis of 3, will be subscriptable up to a maximum index of 2.
因此,以下内容不适用于T[:,3]
:
So the following will not work for T[:,3]
:
T=zeros([N+1,3])
如果打算在0、1、2和3上建立索引,则应考虑增加数组的大小,例如:
If you intend to index on 0,1,2 and 3, then you should consider increasing the size of your array, say:
T=zeros([N+1,4])
# ^ subscriptable up to 3
这篇关于IndexError:索引3超出了尺寸为3的轴1的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文