Issue
I am using a digital elevation model as an array, and I want to create a new array with only the pixels below a certain value. I tried using a for loop.
Here is the operation with simpler data :
import numpy as np
array1 = array2 = np.arange(9).reshape(3,3)
array2 = array2.astype("float") #np.nan doesn't work with integers
for row in array2:
for item in row:
if item > 3.0:
item=np.nan
print(np.array_equal(array1,array2))
The arrays remain equal after the loop is over. Do you know why the values won't be replaced?
Solution
You only change item
you need insert item
in array2
, you can use np.where
like below:
>>> array1 = np.arange(9).reshape(3,3)
>>> array2 = np.where(array1 > 3, np.nan, array1)
>>> array2
array([[ 0., 1., 2.],
[ 3., nan, nan],
[nan, nan, nan]])
>>> np.array_equal(array1, array2)
False
Answered By - user1740577
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.