Reputation: 45
I am trying to get the cumulative sum of an array which has a shape of: (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 # :
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 = 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
Finally, I put the actual index instead of the -1 and it seems to have worked...
Upvotes: 1
Views: 5193
Reputation: 231385
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.
Upvotes: 1