How can I conditionally change the values in a numpy array taking into account nans?

Question:

My array is a 2D matrix and it has numpy.nan values besides negative and positive values:

>>> array
array([[        nan,         nan,         nan, ..., -0.04891211,
                nan,         nan],
       [        nan,         nan,         nan, ...,         nan,
                nan,         nan],
       [        nan,         nan,         nan, ...,         nan,
                nan,         nan],
       ..., 
       [-0.02510989, -0.02520096, -0.02669156, ...,         nan,
                nan,         nan],
       [-0.02725595, -0.02715945, -0.0286231 , ...,         nan,
                nan,         nan],
       [        nan,         nan,         nan, ...,         nan,
                nan,         nan]], dtype=float32)

(There are positive numbers in the array, they just don’t show in the preview.)

And I want to replace all the positive numbers with a number and all the negative numbers with another number.

How can I perform that using python/numpy?

(For the record, the matrix is a result of geoimage, which I want to perform a classification)

Asked By: user528025

||

Answers:

Try:

a[a>0] = 1
a[a<0] = -1
Answered By: silvado

The fact that you have np.nan in your array should not matter. Just use fancy indexing:

x[x>0] = new_value_for_pos
x[x<0] = new_value_for_neg

If you want to replace your np.nans:

x[np.isnan(x)] = something_not_nan

More info on fancy indexing a tutorial and the NumPy documentation.

Answered By: Pierre GM

to add or subtract to current value then (np.nan not affected)

import numpy as np

a = np.arange(-10, 10).reshape((4, 5))

print("after -")
print(a)

a[a<0] = a[a<0] - 2
a[a>0] = a[a>0] + 2


print(a)

output

[[-10  -9  -8  -7  -6]
 [ -5  -4  -3  -2  -1]
 [  0   1   2   3   4]
 [  5   6   7   8   9]]

after -

[[-12 -11 -10  -9  -8]
 [ -7  -6  -5  -4  -3]
 [  0   3   4   5   6]
 [  7   8   9  10  11]]
Answered By: ziad.rida

Pierre’s answer doesn’t work if new_value_for_pos is negative. In that case, you could use np.where() in a chain:

# Example values
x = np.array([np.nan, -0.2, 0.3])
new_value_for_pos = -1
new_value_for_neg = 2

x[:] = np.where(x>0, new_value_for_pos, np.where(x<0, new_value_for_neg, x))

Result:

array([nan,  2., -1.])
Answered By: wjandrea
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.