简体   繁体   English

numpy.AxisError:轴 1 超出维度 1 数组的范围 - numpy 数组

[英]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.

相关问题 numpy.AxisError:来源:轴 2 超出了维度 2 数组的范围 - numpy.AxisError: source: axis 2 is out of bounds for array of dimension 2 遇到 AxisError:轴 1 超出维度 0 数组的范围 - Encountering AxisError: axis 1 is out of bounds for array of dimension 0 AxisError:轴 1 超出维度 1 数组的范围 - AxisError: axis 1 is out of bounds for array of dimension 1 如何解决 AxisError:轴 1 超出维度 0 数组的范围 - How to solve AxisError: axis 1 is out of bounds for array of dimension 0 np.linalg.norm AxisError:轴 1 超出维度 1 数组的范围 - np.linalg.norm AxisError: axis 1 is out of bounds for array of dimension 1 AxisError:计算类的准确性时,轴 1 超出维度 1 数组的范围 - AxisError: axis 1 is out of bounds for array of dimension 1 when calculating accuracy of classes Numpy 连接给出错误:轴 1 超出维度 1 数组的范围 - Numpy concatenate giving error: axis 1 is out of bounds for array of dimension 1 2D 数组的 median_absolute_deviation 抛出 AxisError: axis 1 is out of bounds for array of dimension 1 - median_absolute_deviation of 2D array throws AxisError: axis 1 is out of bounds for array of dimension 1 AxisError:计算 roc_auc_score 时,轴 1 超出维度 1 数组的范围 - AxisError: axis 1 is out of bounds for array of dimension 1 when calculating roc_auc_score NumPy数组IndexError:索引99超出了大小为1的轴0的范围 - NumPy array IndexError: index 99 is out of bounds for axis 0 with size 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM