Python 索引错误:布尔索引与维度 0 的索引数组不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45207650/
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
IndexError: boolean index did not match indexed array along dimension 0
提问by Ibe
My code was working fine until I updated Numpy to 1.13.1. Now I get the following error
我的代码运行良好,直到我将 Numpy 更新到 1.13.1。现在我收到以下错误
IndexError: boolean index did not match indexed array along dimension 0; dimension is 5 but corresponding boolean dimension is 4
... which is thrown at this line:
...这是在这一行抛出的:
m = arr[np.diff(np.cumsum(arr) >= sum(arr) * i)]
I can't seem to wrap my head around it. Any suggestions?
我似乎无法绕过它。有什么建议?
Here is my sample code:
这是我的示例代码:
a = [1,2,3,4,5]
l = [0.85,0.90]
s = sorted(a, reverse = False)
arr = np.array(s)
for i in l:
m = arr[np.diff(np.cumsum(arr) >= sum(arr) * i)]
采纳答案by Joe
np.diff
is one element smaller than data_array.
np.diff
比 data_array 小一个元素。
The shape of the output is the same as a except along axis where the dimension is smaller by n.
输出的形状与 a 相同,除了沿轴的维度小 n。
I am using Numpy 1.11, instead of an IndexError
I get a VisibleDeprecationWarning
. So I guess using an incorrect size is no longer tolerated.
我正在使用 Numpy 1.11,而不是IndexError
我得到一个VisibleDeprecationWarning
. 所以我想不再容忍使用不正确的大小。
You need to define which behaviour you want, e.g. start at the second element, or remove the last:
您需要定义您想要的行为,例如从第二个元素开始,或删除最后一个:
arr = np.array([1,2,3,4,5])
arr2 = arr[:-1]
m = arr2[np.diff(np.cumsum(arr) >= sum(arr))]
arr3 = arr[1:]
m = arr3[np.diff(np.cumsum(arr) >= sum(arr))]