Check if values in a set are in a numpy array in python

In versions 1.4 and higher, numpy provides the in1d function.

>>> test = np.array([0, 1, 2, 5, 0])
>>> states = [0, 2]
>>> np.in1d(test, states)
array([ True, False,  True, False,  True], dtype=bool)

You can use that as a mask for assignment.

>>> test[np.in1d(test, states)] = 1
>>> test
array([1, 1, 1, 5, 1])

Here are some more sophisticated uses of numpy's indexing and assignment syntax that I think will apply to your problem. Note the use of bitwise operators to replace if-based logic:

>>> numpy_array = numpy.arange(9).reshape((3, 3))
>>> confused_array = numpy.arange(9).reshape((3, 3)) % 2
>>> mask = numpy.in1d(numpy_array, repeat_set).reshape(numpy_array.shape)
>>> mask
array([[False, False, False],
       [ True, False,  True],
       [ True, False,  True]], dtype=bool)
>>> ~mask
array([[ True,  True,  True],
       [False,  True, False],
       [False,  True, False]], dtype=bool)
>>> numpy_array == 0
array([[ True, False, False],
       [False, False, False],
       [False, False, False]], dtype=bool)
>>> numpy_array != 0
array([[False,  True,  True],
       [ True,  True,  True],
       [ True,  True,  True]], dtype=bool)
>>> confused_array[mask] = 1
>>> confused_array[~mask & (numpy_array == 0)] = 0
>>> confused_array[~mask & (numpy_array != 0)] = 2
>>> confused_array
array([[0, 2, 2],
       [1, 2, 1],
       [1, 2, 1]])

Another approach would be to use numpy.where, which creates a brand new array, using values from the second argument where mask is true, and values from the third argument where mask is false. (As with assignment, the argument can be a scalar or an array of the same shape as mask.) This might be a bit more efficient than the above, and it's certainly more terse:

>>> numpy.where(mask, 1, numpy.where(numpy_array == 0, 0, 2))
array([[0, 2, 2],
       [1, 2, 1],
       [1, 2, 1]])

Tags:

Python

Numpy