[英]numpy.AxisError: axis 1 is out of bounds for array of dimension 1 - numpy array
I am trying to get the cumulative sum of an array which has a shape of: (1000, 117)我正在尝试获取具有以下形状的数组的累积和: (1000, 117)
But it seems that the array is taken as vector and the "numpy.cumsum" output comes out as a 117000 size vector instead of a (1000, 117) dimension matrix These are the code lines and their outputs in #:但似乎该数组被视为向量,并且“numpy.cumsum” output 作为 117000 大小的向量而不是 (1000, 117) 维度矩阵出现这些是代码行及其在 # 中的输出:
print(np.shape(rates))
#(1000, 117)
cum_rates = numpy.cumsum(rates[:, -1], axis = 1)
#numpy.AxisError: axis 1 is out of bounds for array of dimension 1
Here is how I created the rates array:这是我创建rates数组的方法:
rates = np.zeros([taille, pas+1])
rates[:, 0] = r0
for i in range(pas):
z = np.random.normal(taille)
x[:, i+1] = x[:, i] + kappa*(theta - x[:, i])*dt + sigma*np.sqrt(x[:, i])*z*dt
rates[:, i+1] = x[:, i+1] + phi[i]
How do you suggest I solve this probelm?你建议我如何解决这个问题?
Precision: the "rates[:,-1]"
was to drop one column of the rates matrix精度: "rates[:,-1]"
是删除一列的rates矩阵
Finally, I put the actual index instead of the -1 and it seems to have worked...最后,我放了实际的索引而不是 -1,它似乎已经奏效了......
This reproduces your error message:这会重现您的错误消息:
In [20]: rates = np.ones((3,5))
In [21]: rates[:,-1]
Out[21]: array([1., 1., 1.])
In [22]: np.cumsum(rates[:,-1], axis=1)
---------------------------------------------------------------------------
AxisError Traceback (most recent call last)
<ipython-input-22-4261312fb007> in <module>
----> 1 np.cumsum(rates[:,-1], axis=1)
<__array_function__ internals> in cumsum(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in cumsum(a, axis, dtype, out)
2468
2469 """
-> 2470 return _wrapfunc(a, 'cumsum', axis=axis, dtype=dtype, out=out)
2471
2472
/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
59
60 try:
---> 61 return bound(*args, **kwds)
62 except TypeError:
63 # A TypeError occurs if the object does have such a method in its
AxisError: axis 1 is out of bounds for array of dimension 1
Out[21]
is 1d. Out[21]
是 1d。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.