2017-07-20 57 views
3

我的代碼工作正常,直到我將Numpy更新爲1.13.1。現在,我得到以下錯誤IndexError:布爾指數與尺寸爲0的索引數組不匹配0

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

加樣品再現這個問題呢? – Divakar

+0

類型與輸入相同:https://docs.scipy.org/doc/numpy/reference/generated/numpy.diff.html – Ibe

回答

3

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

謝謝。相反,我將「0」添加到原始數組中,保留所有數據點。 – Ibe

相關問題