Python ValueError: 'axis' 条目超出范围 // numpy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19572178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:04:46  来源:igfitidea点击:

ValueError: 'axis' entry is out of bounds // numpy

pythonarraysnumpy

提问by Michael

Here is my 2D numpy array:

这是我的二维 numpy 数组:

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:

当我尝试计算每列的 STD 时:

print np.std(returns, axis=1)

I'm getting the following error:

我收到以下错误:

ValueError: 'axis' entry is out of bounds

How can I fix that?

我该如何解决?

回答by Moe

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]

回答by Saullo G. P. Castro

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.ndim。您可能有一个一维对象数组,打印时看起来像一个二维数组。在这种情况下,您可以将其转换为二维数组:

a = np.array(list(a))

or

或者

a = np.array(tuple(a))

回答by dkv

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).

在从脚本调用的函数内执行时,会产生“ValueError: 'axis' entry is out of bounds”错误。奇怪的是,当从 IPython shell 执行该行时,它顺利运行(我检查了 myndarray 变量在两种上下文中是否相同)。

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.

我正在使用 Spyder,所以也许由于使用 Python 控制台 + pdb 和/或 IPython + ipdb 以及相同的变量集,它以某种方式陷入了一种奇怪的状态。重新启动 IDE 解决了问题,这也解释了其他人无法重现该错误。

回答by Evan McFarland

This is for someone who stumbles across this thread having the same error for a different reason.

这是为了那些由于不同原因偶然发现这个线程有相同错误的人。

I had the same error for any axis other than 0. I used a.ndimand saw there were 3 dimensions in the array. The fix was array.reshape(x,y)which made it 2d and then calling from the array worked.

对于 0 以外的任何轴,我都有相同的错误。我使用a.ndim并看到数组中有 3 个维度。修复是array.reshape(x,y)使它成为 2d 然后从数组调用工作。