Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)]
like image 877
Ibe Avatar asked Jul 20 '17 07:07

Ibe


1 Answers

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, 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))]
like image 74
Joe Avatar answered Nov 11 '22 11:11

Joe