Python: numpy combo masking

In the numpy array, I want to replace all nan and inf with a fixed number. Can I do this in one step to save the computation time (arrays are really big)?

 a = np.arange(10.0) a[3] = np.nan a[5] = np.inf a[7] = -np.inf # a: [ 0. 1. 2. nan 4. inf 6. -inf 8. 9.] a[np.isnan(a)] = -999 a[np.isinf(a)] = -999 # a: [ 0. 1. 2. -999. 4. -999. 6. -999. 8. 9.] 

This code works just fine. But I'm looking for something like:

 a[np.isnan(a) or np.isinf(a)] = -999 

What does not work, and I understand why. Just think what could be better if each element of a is checked only once.

+2
source share
3 answers

it works:

 a[np.isnan(a) | np.isinf(a)] = 2 

np.isnan() and np.isinf() actually return two numpy boolean arrays.

boolean numpy arrays can be combined with bitwise operations such as and and |

+2
source

Numpy comes with its own vectorized version or:

 a[np.logical_or(np.isnan(a), np.isinf(a))] = -999 

Despite the fact that the above version is understandable, it is faster, which is a bit strange:

 a[np.isnan(aa)] = -9999 

The idea behind this is that 'np.inf-np.inf = np.nan`

 %timeit a[np.isnan(aa)] = -999 # 100000 loops, best of 3: 11.7 Β΅s per loop %timeit a[np.isnan(a) | np.isinf(a)] = -999 # 10000 loops, best of 3: 51.4 Β΅s per loop %timeit a[np.logical_or(np.isnan(a), np.isinf(a))] = -999 # 10000 loops, best of 3: 51.4 Β΅s per loop 

Therefore, the version | and np.logical_or seems internally equivalent

+3
source

You can use np.isfinite , which checks that the number is not infinite, and NaN:

 a[~np.isfinite(a)] = -999 
+3
source

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


All Articles