以numpy的矢量化方式:有条件地根据块中的内容切出不同大小的数据块

问题描述 投票:0回答:1

我有一个整数向量(一维numpy数组),看起来像:

8, 1, 1, 2, 8, 99, 1, 2, 1, 2, 8, 2, 2, 2, 8, 99, 99, 8, 1, 1

(以向量化的方式,)我想过滤掉包含至少一个99值的8之间的所有数据。

因此在此示例中,我要剪切的数据以粗体显示:

8,1,1,2,8,99,1,2,1,2,8,2,2,2,8,99,99,8,1,1] >

(即,数据位于包含至少一个99的8位数之间)

因此,如果我制作了一个布尔掩码来裁剪该数据,它将看起来像:

数据:8,1,1,2,8,99,1,2,1,2,8,2,2,2,8,99,99,8,1, 1个遮罩:T,T,T,T,F,F,F,F,F,F,T,T,T,T,F,F,F,T,T,T] >

裁剪后的数据如下:

Data(Mask) = 8, 1, 1, 2, 8, 2, 2, 2, 8, 1, 1

我可以提出矢量化的代码,如果8之间有相等的间距,则可以执行此操作。这是该代码:

inputRaw = np.array([8, 2, 3, 2, 99, 2, 8, 2, 3, 2, 2, 2, 8, 2, 3, 3, 3, 3])
inputPartitioned = np.reshape(inputRaw, (3, 6))
# reshaping it into an array of the form: np.array([[8, 2, 3, 2, 99, 2], [8, 2, 3, 2, 2, 2], [8, 2, 3, 3, 3, 3]])
selectedSections = np.logical_not(np.any(inputPartitioned>8, axis=1))
outputPartitioned = inputPartitioned[selectedSections]
outputFlattened = outputPartitioned.flatten()

我需要做的另一件事是一个掩码或索引,它告诉我(在原始索引中)被裁剪的数据。 (我需要这样做,因为我要跟踪第二个与第一个数组共享索引的数组)。我可以像这样编写此掩码(假设8的间距相等):

inputIndex = np.arange(inputRaw.size)
inputIndexPartitioned =  np.reshape(inputIndex, (3, 6))
outputPartitionedIndex = inputIndexPartitioned[selectedSections]
outputFlattenedIndex = outputPartitionedIndex.flatten()

但是我不确定在8字间距不相等的情况下如何以矢量化方式执行此操作。

有什么想法吗?这些阵列很长,因此对于大型阵列快速工作的解决方案很有帮助。另外,我非常有信心,这些“ 99”将始终在8之后出现,因此也许对制作算法很有帮助。

我有一个整数向量(一维numpy数组),看起来像:8、1、1、2、8、99、1、2、1、2、2、8、2、2、2、8、99, 99、8、1、1(以矢量化方式),我想过滤掉8的之间的所有数据...

numpy vectorization
1个回答
0
投票

这应该可以解决问题。输入:

import numpy as np

in_arr = np.array([8, 1, 1, 2, 8, 99, 1, 2, 1, 2, 8, 2, 2, 2, 8, 99, 99, 8, 1, 1])

mask_8 = in_arr == 8
mask_8_cumsum = np.cumsum(mask_8)
print(mask_8_cumsum)
>>> [1 1 1 1 2 2 2 2 2 2 3 3 3 3 4 4 4 5 5 5]

unique_inds = np.unique(mask_8_cumsum[in_arr == 99])
print(unique_inds)
>>> [2 4]

final_mask = ~np.isin(mask_8_cumsum, unique_inds)
final_data = in_arr[final_mask]
print(final_mask)
>>> [ True  True  True  True False False False False False False  True  True
  True  True False False False  True  True  True]

print(final_data)
>>> [8 1 1 2 8 2 2 2 8 1 1]
© www.soinside.com 2019 - 2024. All rights reserved.