Skip to content Skip to sidebar Skip to footer

Check If Values In A Set Are In A Numpy Array In Python

I want to check if a NumPyArray has values in it that are in a set, and if so set that area in an array = 1. If not set a keepRaster = 2. numpyArray = #some imported array repeatSe

Solution 1:

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

Solution 2:

Here is one possible way of doing what you whant:

numpyArray = np.array([1, 8, 35, 343, 23, 3, 8]) # could be n-Dimensional array
repeatSet = np.array([3, 5, 6, 8])
mask = (numpyArray[...,None] == repeatSet[None,...]).any(axis=-1) 
print mask
>>> [FalseTrueFalseFalseFalseTrueTrue]

Solution 3:

In recent numpy you could use a combination of np.isin and np.where to achieve this result. The first method outputs a boolean numpy array that evaluates to True where its vlaues are equal to an array-like specified test element (see doc), while with the second you could create a new array that set some a value where the specified confition evaluates to True and another value where False.

Example

I'll make an example with a random array but using the specific values you provided.

import numpy as np

repeatSet = ([2, 5, 6, 8])

arr = np.array([[1,5,1],
                [0,1,0],
                [0,0,0],
                [2,2,2]])

out = np.where(np.isin(arr, repeatSet), 1, 77)

> out
array([[77,  1, 77],
       [77, 77, 77],
       [77, 77, 77],
       [ 1,  1,  1]])

Post a Comment for "Check If Values In A Set Are In A Numpy Array In Python"