掩盖一个numpy阵列并在不使用for循环的情况下应用每个掩码的计算



假设我们有以下数据数组:

data_array = np.array([[1, 1, 1], [1, 1, 2], [2, 2, 2], [3, 3, 3], [4, 4, 4]], np.int16)
data_array
array([[1, 1, 1],
       [1, 1, 2],
       [2, 2, 2],
       [3, 3, 3],
       [4, 4, 4]])

,我们希望根据以下范围掩盖数组,以便能够在蒙版零件上应用计算:

intervals = [[1, 2], [2, 3], [3, 4]]

我们首先基于数据数组创建一个空数组和掩码,因此我们可以将每个蒙版数组的结果组合在一起:

init = np.zeros((data_array.shape[0], data_array.shape[1]))
result_array = np.ma.masked_where((init == 0), init)
result_array
masked_array(
data=[[--, --, --],
      [--, --, --],
      [--, --, --],
      [--, --, --],
      [--, --, --]],
mask=[[ True,  True,  True],
      [ True,  True,  True],
      [ True,  True,  True],
      [ True,  True,  True],
      [ True,  True,  True]]

这样,我们可以启动一个用于循环的循环,该循环根据间隔范围掩盖了数组,在蒙版数组上执行计算,并将结果结合到单个结果数组:

for inter in intervals:
    # Extact the start and en values for interval range
    start_inter = inter[0]
    end_inter = inter[1]
    # Mask the array based on interval range
    mask_init = np.ma.masked_where((data_array > end_inter), data_array)
    masked_array = np.ma.masked_where((mask_init < start_inter), mask_init)
    # Perform a dummy calculation on masked array
    outcome = (masked_array + end_inter) * 100
    # Combine the outcome arrays
    result_array[result_array.mask] = outcome[result_array.mask]

以下结果:

array([[300.0, 300.0, 300.0],
      [300.0, 300.0, 400.0],
      [400.0, 400.0, 400.0],
      [600.0, 600.0, 600.0],
      [800.0, 800.0, 800.0]])

我的问题是,如果不将其用于循环,如何实现相同的结果?因此,在单个操作中对整个data_array应用掩蔽和计算。请注意,计算的变量随着每个掩码而变化。是否可以将矢量化方法应用于此问题?我想numpy_indexed可能会有所帮助。谢谢。

如果可以使间隔不重叠,则可以使用这样的函数:

import numpy as np
def func(data_array, intervals):
    data_array = np.asarray(data_array)
    start, end = np.asarray(intervals).T
    data_array_exp = data_array[..., np.newaxis]
    mask = (data_array_exp >= start) & (data_array_exp <= end)
    return np.sum((data_array_exp + end) * mask * 100, axis=-1)

结果应与原始代码相同:

import numpy as np
def func_orig(data_array, intervals):
    init = np.zeros((data_array.shape[0], data_array.shape[1]))
    result_array = np.ma.masked_where((init == 0), init)
    for inter in intervals:
        start_inter = inter[0]
        end_inter = inter[1]
        mask_init = np.ma.masked_where((data_array > end_inter), data_array)
        masked_array = np.ma.masked_where((mask_init < start_inter), mask_init)
        outcome = (masked_array + end_inter) * 100
        result_array[result_array.mask] = outcome[result_array.mask]
    return result_array.data
data_array = np.array([[1, 1, 1], [1, 1, 2], [2, 2, 2], [3, 3, 3], [4, 4, 4]], np.int16)
intervals = [[1, 1.9], [2, 2.9], [3, 4]]
print(np.allclose(func(data_array, intervals), func_orig(data_array, intervals)))
# True

最新更新