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

Time:01-02

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.

CodePudding user response:

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]])
  • Related