简体   繁体   中英

IndexError: boolean index did not match indexed array along dimension 0

My code was working fine until I updated Numpy to 1.13.1. Now I get the following error

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

np.diff is one element smaller than data_array.

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

numpy.diff

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.

You need to define which behaviour you want, eg 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))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM