如何将布尔值从 np.diff() 转换为实际值

How can I convert boolean values from np.diff() to the actual values

我有一个 numpy 数组 np.shape=(n,)

我正在尝试遍历数组的每个值并从第一个值中减去第二个值,然后查看差值是否大于 1。

所以,array[1]-array[0] > 1 ?,然后array[2]-array[1] > 1 ?

我读到 np.diff() 很简单,returns 对我来说是另一个布尔 True/False 值数组:

例如,

np.diff(array) > 1 给出 [False False True False True]

  1. 如何获取单独数组中每个 FalseTrue 项的准确值?
  2. 如何获取数组中每个 FalseTrue 的位置?

我尝试了 array[array>1] 及其变体,但到目前为止没有任何效果。

编辑:我在下面使用了 AJH 的回答,这对我有用。

希望这能回答您的问题:

# Gets the difference between consecutive elements.
exact_diffs = np.diff(array, n=1)

# true_diffs = exact values in exact_diffs that are > 1.
# false_diffs = exact values in exact_diffs that are <= 1.
true_diffs = exact_diffs[exact_diffs > 1]
false_diffs = exact_diffs[exact_diffs <= 1]

# true_indices = indices of values in exact_diffs that are > 1.
# false_indices = indices of values in exact_diffs that are <= 1.
true_indices = np.where(exact_diffs > 1)[0]
false_indices = np.where(exact_diffs <= 1)[0]

# Note that true_indices gets the corresponding values in exact_diffs.
# To get the indices of values in array that are at least 1 more than preceding element, do:
true_indices += 1

# and then you can index those values in array with array[true_indices].