Numpy-从形状和索引中获取蒙版

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

使用np.instersect1d之后,我已经检索了要检索的索引。我想将这些索引变成一个蒙版,然后再将其与另一个蒙版组合。

我想要的是:

>>> times_1.shape
... (160,)

>>> _, time_indices, _ = np.intersect1d(times_1, times_2, return_indices=True)

>>> time_indices.shape
... (145,)

    # Some way to turn return mask from original shape and indices
>>> time_mask = get_mask_from_shape(time_1.shape, time_indices)

>>> time_mask.shape
... (160,)

实现功能get_mask_from_shape的最简单方法是什么?

python python-3.x numpy mask
2个回答
0
投票

如果我有一个正确的想法,我将采用我一直在寻找few days before的这种模式:

intersection = np.intersect1d(times_1, times_2)
intersection = np.sort(intersection)
locations = np.searchsorted(intersection, times_1)
locations[locations==len(intersection)] = 0
time_mask = intersection[locations]==times_1

[searchsorted方法根据times_1]查找应将intersection的元素插入到numpy documentation.中以保持顺序的索引>


0
投票

我找到的解决方案是:

© www.soinside.com 2019 - 2024. All rights reserved.