简体   繁体   中英

How to efficiently filter/create mask of numpy.array based on list of tuples

I try to create mask of numpy.array based on list of tuples. Here is my solution that produces expected result:

import numpy as np

filter_vals = [(1, 1, 0), (0, 0, 1), (0, 1, 0)] 
data = np.array([
    [[0, 0, 0], [1, 1, 0], [1, 1, 1]],
    [[1, 0, 0], [0, 1, 0], [0, 0, 1]],
    [[1, 1, 0], [0, 1, 1], [1, 0, 1]],
])

mask = np.array([], dtype=bool)
for f_val in filter_vals:
    if mask.size == 0:
        mask = (data == f_val).all(-1)
    else:
        mask = mask | (data == f_val).all(-1)

Output/mask:

array([[False,  True, False],
       [False,  True,  True],
       [ True, False, False]]

Problem is that with bigger data array and increasing number of tuples in filter_vals , it is getting slower. It there any better solution? I tried to use np.isin(data, filter_vals) , but it does not provide result I need.

A classical approach using broadcasting would be:

*A, B = data.shape
(data.reshape((-1,B)) == np.array(filter_vals)[:,None]).all(-1).any(0).reshape(A)

This will however be memory expensive. So applicability really depends on your use case.

output:

array([[False,  True, False],
       [False,  True,  True],
       [ True, False, False]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM