array_one and array_two can be assumed to be the same shape.
I want to test array_two against two conditions (that it's not "invalid_data" and the value is one of the numbers in "list_of_desired_values").
For the index where those conditions were deemed true for array_two, I want to then change array_one at that same index to -9999 in this case.
I can't get this to work, and ideally I'd like to be able to do this within Numpy rather than a for loop for speed. Does anyone know an effective way to do this?
invalid_data = 5 #example
list_of_desired_values = [11, 2]
array_one = numpy.array([[2, 4, 6], [6, 8, 10]], numpy.int32)
array_two = numpy.array([[5, 2, 1], [7, 11, 55]], numpy.int32)
for x in array_two.flatten():
if array_two.flatten()[x] != invalid_data:
if array_two.flatten()[x] in list_of_desired_values:
array_one.flatten()[x] = -9999
Your code has 2 flaws:
In for x in array_two.flatten():
x is set to the value of each element, so there is no sense to use it as an index (index out of bounds exception is likely to occur).
array_one.flatten()[x] = -9999
has also no sense, since the result of array_one.flatten() is another (temporary) array, which then immediately disappears ( flatten()
returns a copy of the source array, collapsed into one dimension).
Your code could be reworked to:
for idx, x in np.ndenumerate(array_two):
if x != invalid_data:
if x in list_of_desired_values:
array_one[idx] = -9999
But the proper, numpythonic solution is to refer just to array_one with boolean indexing (indices meeting your criteria) and set there your target value:
array_one[np.logical_and(np.not_equal(array_two, invalid_data),
np.isin(array_two, list_of_desired_values))] = -9999
The result ( array_one after this operation) is:
array([[ 2, -9999, 6],
[ 6, -9999, 10]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.