Check if values โ€‹โ€‹are in set in numpy array in python

I want to check if the NumPyArray in it has values โ€‹โ€‹that are in the set, and if it is so specified in the array = 1. If you do not set keepRaster = 2.

numpyArray = #some imported array repeatSet= ([3, 5, 6, 8]) confusedRaster = numpyArray[numpy.where(numpyArray in repeatSet)]= 1 

Productivity:

 <type 'exceptions.TypeError'>: unhashable type: 'numpy.ndarray' 

Is there any way to skip it?

  for numpyArray if numpyArray in repeatSet confusedRaster = 1 else keepRaster = 2 

To clarify and request additional help:

What I'm trying to get, and am currently doing, is setting the bitmap input to an array. I need to read the values โ€‹โ€‹in a 2-d array and create another array based on these values. If the value of the array is in the set, then the value will be 1. If it is not in the set, the value will be obtained from another input, but now I will say 77. This is what I am using now. My test input contains about 1,500 rows and 3,500 columns. It always freezes around 350.

 for rowd in range(0, width): for cold in range (0, height): if numpyarray.item(rowd,cold) in repeatSet: confusedArray[rowd][cold] = 1 else: if numpyarray.item(rowd,cold) == 0: confusedArray[rowd][cold] = 0 else: confusedArray[rowd][cold] = 2 
+6
source share
2 answers

In versions 1.4 and above, the numpy function 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 this as a mask for assignment.

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

Here are some more complex uses of the numpy indexing and assignment syntax that I think 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 new array using the values โ€‹โ€‹from the second argument, where mask is true and the values โ€‹โ€‹from the third argument, where mask is false. (As with the assignment, the argument can be a scalar or an array of the same shape as mask .) It can be a little more efficient than the above, and it is certainly more concise:

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

Here is one possible way to do what you want:

 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 >>> [False True False False False True True] 
+1
source

Source: https://habr.com/ru/post/916288/


All Articles