可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Here is my 2D numpy array:
returns = np.array([ [ -4.78878057e-03 9.79090927e-03 -2.06883581e-03 -1.25786164e-02] [ 5.79128440e-03 -2.85791008e-03 1.69555388e-03 -5.63798220e-02] [ 5.73427375e-05 2.45043133e-02 8.55025651e-03 -4.53257790e-02] [ 6.75441635e-03 8.70168484e-03 1.07547532e-02 -1.36919315e-01] [ 6.68332655e-03 6.76498174e-03 3.08225775e-03 0.00000000e+00]])
And when I'm trying to calculate the STD for each column:
print np.std(returns, axis=1)
I'm getting the following error:
ValueError: 'axis' entry is out of bounds
How can I fix that?
回答1:
Check if your array is a 2-D array querying a.ndim
. It may happen that you have a 1-D array of objects that, when printed, looks like a 2-D array. In this case you can convert it to a 2-D array doing:
a = np.array(list(a))
or
a = np.array(tuple(a))
回答2:
Michael,
I am not sure why you have the issue. It might be because of the way you are defining the 2D array (that's my only guess). I used the following code and it works properly:
import numpy as np arr = np.array([ \ [ -4.78878057e-03, 9.79090927e-03, -2.06883581e-03, -1.25786164e-02], \ [ 5.79128440e-03, -2.85791008e-03, 1.69555388e-03, -5.63798220e-02], \ [ 5.73427375e-05, 2.45043133e-02, 8.55025651e-03, -4.53257790e-02], \ [ 6.75441635e-03, 8.70168484e-03, 1.07547532e-02, -1.36919315e-01], \ [ 6.68332655e-03, 6.76498174e-03, 3.08225775e-03, 0.00000000e+00] \ ]) std = np.std(arr,axis=1) print std
and the answer will be:
[ 0.00803178 0.02526721 0.02593599 0.06308687 0.00281146]
回答3:
Recently had this same problem:
np.sum(myndarray, axis=0)
was producing the "ValueError: 'axis' entry is out of bounds" error when executed inside a function called from a script. Strangely, when executing the line from the IPython shell it ran without a hitch (I checked that the myndarray variable was the same in both contexts).
I was working with Spyder, so perhaps it somehow got stuck in a weird state as a result of using the Python console + pdb, and/or IPython + ipdb, with the same set of variables. Restarting the IDE solved the issue, which also explains the others' inability to reproduce the bug.