簡體   English   中英

numpy.AxisError:軸 1 超出維度 1 數組的范圍 - numpy 數組

[英]numpy.AxisError: axis 1 is out of bounds for array of dimension 1 - numpy array

我正在嘗試獲取具有以下形狀的數組的累積和: (1000, 117)

但似乎該數組被視為向量,並且“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

這是我創建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]

你建議我如何解決這個問題?

精度: "rates[:,-1]"是刪除一列的rates矩陣

最后,我放了實際的索引而不是 -1,它似乎已經奏效了......

這會重現您的錯誤消息:

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]是 1d。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM