Numpy array filtering
Filtering is essentially done using a Boolean mask array versus a Target array.
import numpy as np
target = np.array([1, 5, 9, 4, 0])
filter_array = [True, False, False, True, True] # mask array
print(target[filter_array])
# result is:
[1 4 0]
The Mask array can either be longer or shorter than the Target array; you won't get any warnings or errors. Everything will be executed left to right. If a part of the mask is not needed, it will be ignored. If any section of the target array is not needed on the right hand side, it'll be ignored.
A possibly non-obvious and/or hidden feature: Use integers to point to the indices to keep, including repeats!!
import numpy as np
target = np.array([1, 5, 9, 4, 0])
filter_array = [0, 1, 1, 1, 0]
print(target[filter_array])
# result is, keep the 0th and 1st index positions' values:
[1 5 5 5 1]
The trick is to set your filter to a default list (which can be an empty list) if no conditions are met vs. the mask array. At an intermediate stage, even other code command statements could populate the filter before the mask gets a chance to hit it.
numpy considers zero to be an even number.
import numpy as np
target_array = np.array([123, 5, -6, 7, 98])
filter_array = [] # could be populated with default values too
for num in target_array:
if num > 52:
filter_array.append(True)
else:
filter_array.append(False)
modified_array = target_array[filter_array]
print(filter_array)
print(modified_array)
A short hand trick to save coding lines is to directly insert the condition in the indices bracketed on the Target array.
import numpy as np
array0 = np.array([-1, -2, -3, -4])
filtarray = array0 > -4
newarray = arr[filtarray]
print(filtarray)
print(newarray)
# result is
[ True True True False]
[-1 -2 -3]
However, many efforts on my part to do compound conditional statements gave errors. The only way I see to do it is to create extra arrays and extra filters, with an example as shown below.
import numpy as np
array0 = np.array([-1, -2, -3, -4])
filtarray1 = []
filtarray2 = []
filtarray1 = array0 > -3
array1 = array0[filtarray1]
filtarray2 = array1 < -1
array2 = array1[filtarray2]
print(array2)
# result is
[-2]
No comments:
Post a Comment