Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove positive infinity from numpy array...if it is already converted to a number?

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?

like image 494
songololo Avatar asked Jul 06 '14 19:07

songololo


People also ask

How do I remove a specific value from a NumPy array?

To remove an element from a NumPy array: Specify the index of the element to remove. Call the numpy. delete() function on the array for the given index.

How do you check for infinity in NumPy array?

To test array for positive or negative infinity, use the numpy. isinf() method in Python Numpy. Returns a boolean array of the same shape as x, True where x == +/-inf, otherwise False.

What is NP INF in NumPy?

Python's Numpy module can also be used for representing infinite values. It is used as np. inf for positive and -np. inf for negative infinite value.

How do I remove Nan values from NumPy Ndarray?

How to drop all missing values from a numpy array? Droping the missing values or nan values can be done by using the function "numpy. isnan()" it will give us the indexes which are having nan values and when combined with other function which is "numpy. logical_not()" where the boolean values will be reversed.


2 Answers

First of all, 1.79769313486e+308 is not the same as +inf. The former is the largest number which can be expressed with a 64-bit float, the latter is a special float.

If you just have very large numbers in your array, then:

A[A > 1e308] = 0

is sufficient. Thet'll replace oll elements above 1e308 with 0.

It is also possible to operate with the inf's. For example:

>>> fmax = np.finfo(np.float64).max
>>> pinf = float('+inf')
>>> ninf = float('-inf')
>>> fnan = float('nan')
>>> print fmax, pinf, ninf, fnan
1.79769313486e+308 inf -inf nan

So, these are completely different things. You may compare some of them:

>>> pinf > fmax
True
>>> ninf < 0.0
True
>>> pinf == pinf
True
>>> pinf == ninf
False

This looks good! However, nan acts differently:

>>> fnan > 0
False
>>> fnan < 0
False
>>> fnan == 0
False
>>> fnan < pinf
False
>>> fnan == fnan
False

You may use positive and negativi infinities with Numpy ndarraywithout any problems. This will work:

A[A == pinf] = 0.0

But if you have nans in the array, you'll get some complaints:

>>> np.array([fnan, pinf, ninf]) < 0
RuntimeWarning: invalid value encountered in less
[False, False, True]

So, it works but complains => do not use. The same without the nan:

>>> np.array([0.0, pinf, ninf]) < 0
[False, False, True]

If you want to do something with the nans (should you have them), use numpy.isnan:

A[np.isnan(A)] = 0.0

will change all nans into zeros.


And -- this you did not ask -- here is one to surprise your friends (*):

>>> [float('-0.0'), 0.0] * 3
[-0.0, 0.0, -0.0, 0.0, -0.0, 0.0]

Yep, float64 (and float32) have even a separate -0.0. In calculations it acts as an ordinary zero, though:

>>> float('-0.0') == 0.0
True

(*) Depending on the kind of people you call friends.

like image 82
DrV Avatar answered Oct 02 '22 00:10

DrV


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.])
like image 38
timgeb Avatar answered Oct 02 '22 02:10

timgeb