shongololo - 2 months ago 18

Python Question

How does one remove positive infinity numbers from a numpy array once these are already converted into a number format? I am using a package which uses numpy internally, however when returning certain arrays, certain values are returned as the 1.79769313486e+308 positive infinity number.

Is there an elegant and fast way to remove these (I would like '0' in my case), or is iterating through the array the best solution?

Answer

To remove the very high values:

```
>>> a = numpy.array([1, 2, 1.8E308, 1.8E308, 42])
>>> a[a < 1E308] # use whatever threshold you like
array([ 1., 2., 42.])
```

To set them 0:

```
>>> a = numpy.array([1, 2, 1.8E308, 1.8E308, 42])
>>> a[a >= 1E308] = 0
>>> a
array([ 1., 2., 0., 0., 42.])
```