草庐IT

python - 索引错误 : boolean index did not match indexed array along dimension 0

coder 2023-08-24 原文

在我将 Numpy 更新到 1.13.1 之前,我的代码工作正常。现在我得到以下错误

IndexError: boolean index did not match indexed array along dimension 0; dimension is 5 but corresponding boolean dimension is 4

...在这一行抛出:

m = arr[np.diff(np.cumsum(arr) >= sum(arr) * i)]

我似乎无法理解它。有什么建议吗?

这是我的示例代码:

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

最佳答案

np.diff 是比 data_array 小一个元素。

The shape of the output is the same as a except along axis where the dimension is smaller by n.

numpy.diff

我使用的是 Numpy 1.11,而不是 IndexError 我得到了 VisibleDeprecationWarning。所以我想不再容忍使用不正确的尺寸。

您需要定义您想要的行为,例如从第二个元素开始,或删除最后一个:

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

关于python - 索引错误 : boolean index did not match indexed array along dimension 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45207650/

有关python - 索引错误 : boolean index did not match indexed array along dimension 0的更多相关文章

随机推荐