追溯 argmin/argmax 在布尔掩蔽 NumPy 数组上的原始位置 - Python



上下文

由于使用 numpy.ma -module 进行掩码比直接布尔掩码慢得多,因此我必须使用后者进行argmin/argmax -计算。

稍微比较一下:

import numpy as np
# Masked Array
arr1 = np.ma.masked_array([12,4124,124,15,15], mask=[0,1,1,0,1])
# Boolean masking
arr2 = np.array([12,4124,124,15,15])
mask = np.array([0,1,1,0,1], dtype=np.bool)
%timeit arr1.argmin()
# 16.1 µs ± 4.88 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit arr2[mask].argmin()
# 946 ns ± 55.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

无论如何,使用 argmin/argmax 返回数组中第一次出现的索引。在布尔掩蔽的情况下,这意味着索引在 arr2[mask] 内而不是arr2 。还有我的问题:在屏蔽数组上计算索引时,我需要未屏蔽数组中的索引


问题

如何获取未屏蔽arr2argmin/argmax -index,即使我将其应用于布尔掩码版本 arr2[mask]

这是一个主要基于masking,特别是 - mask-the-mask并且应该具有内存效率,并且希望性能也很好,尤其是在处理大型数组时 -

def reset_first_n_True(mask, n):
    # Resets (fills with False) first n True places in mask
    # Count of True in original mask array
    c = np.count_nonzero(mask)
    # Setup second mask that is to be assigned into original mask on its
    # own True positions with the idea of setting first argmin_in_masked_ar
    # True values to False
    second_mask = np.ones(c, dtype=bool)
    second_mask[:n] = False
    mask[mask] = second_mask
    return
# Use reduction function on masked data array 
idx = np.argmin(random_array[random_mask])
reset_first_n_True(random_mask, idx)
out = random_mask.argmax()

要在屏蔽数据数组上获取 argmax 并将其追溯到原始位置,只有第一步会更改为包含以下内容:

idx = np.argmax(random_array[random_mask])

因此,可以使用任何还原操作并以这种方式追溯到其原始位置。


如果您正在寻找紧凑的解决方案,请使用nonzero() -

idx = np.flatnonzero(random_mask)
out = idx[random_array[random_mask].argmin()]
# Or idx[random_array[idx].argmin()]

我的解决方案是使用查找逻辑,其中我有第二个数组存储正确的索引。

假设我们有一个随机值数组,我们屏蔽了布尔值,并希望应用 argmin/argmax,这将看起来像:

random_array = np.random.randint(10, size=100)
random_mask  = np.random.randint(2, size=100, dtype=np.bool)
# Returns index of fist occurrence of minimum value within the masked array
random_array[random_mask].argmin()

现在我们必须创建一个查找表,其中包含未屏蔽random_array的 indeces:

lookup = np.arange(len(random_array), dtype=np.int))

如果我们现在以与屏蔽random_array相同的方式屏蔽lookup,我们检索原始索引:

# Returns the index within the unmasked array
result = lookup[random_mask][random_array[random_mask].argmin()]

最新更新