In this post, we are going to understand how to replace inf with zero in the NumPy array. The inf values are infinite values that can be positive or negative, NumPy library np.isinf() function checks element-wise positive or negative infinite values and returns a boolean array.The np. nan_to_num() function also used for replace infinite values.To run all the programs first NumPy must be installed on our system and then we can import in our programs.
NumPy replace inf with zero
To replace inf values with zero in a numpy array, First, we have used the np.isinf() function to find inf values that return an array of infinite values and finally replace infinite values with zero using ndarray[np.isinf(ndarray)] = 0 will replace all positive or negative infinite values with zero in the NumPy array.
import numpy as np
ndarray = np.array([[ 5,np.inf, 15, 45], [ 9, np.inf, 11, 60],
[16,10, 19, 70],
[18, 26, 20, np.inf],
[20, 7, 21, np.inf]])
print('Infinite value in array:',ndarray[np.isinf(ndarray)])
ndarray[np.isinf(ndarray)] = 0
print(ndarray)
Output
Infinite value in array : [inf inf inf inf]
[[ 5. 0. 15. 45.]
[ 9. 0. 11. 60.]
[16. 10. 19. 70.]
[18. 26. 20. 0.]
[20. 7. 21. 0.]]
2. np.nan_to_num() to numpy replace posinf with zero
The numpy.isinf() function replaces all positive or negative infinite values with zero. But in the case of np. nan_to_num() we have to pass argument posinf=0 and copy=True to replace positive infinite values with zero.
import numpy as np
ndarray = np.array([[ 5,np.inf, 15, 45], [ 9, np.inf, 11, 60],
[16,10, 19, 70],
[18, 26, 20, np.inf],
[20, 7, 21, np.inf]])
resarr = np.nan_to_num(ndarray,copy=True,posinf=0)
print(resarr)
Output
[[ 5. 0. 15. 45.]
[ 9. 0. 11. 60.]
[16. 10. 19. 70.]
[18. 26. 20. 0.]
[20. 7. 21. 0.]]
3. NumPy replace neginf with zero
In this Python program, we will understand how to replace negative infinite values with zero in a numpy array.We have to to used pass arguments copy=True,neginf=0 in nan_to_num() function .To replace infinite negative values with any value of our choice just need to assign neginf to that values example resarr = np.nan_to_num(ndarray,copy=True,neginf=5).
import numpy as np
ndarray = np.array([[ 5,-np.inf, 15, 45], [ 9, -np.inf, 11, 60],
[16,10, 19, 70],
[18, 26, 20, -np.inf],
[20, 7, 21, -np.inf]])
resarr = np.nan_to_num(ndarray,copy=True,neginf=0)
print(resarr)
Output
[[ 5. 0. 15. 45.]
[ 9. 0. 11. 60.]
[16. 10. 19. 70.]
[18. 26. 20. 0.]
[20. 7. 21. 0.]]
4. NumPy replace inf with zero using np. where()
In this Python program example, we are replacing inf with value 0. We have pass np.where(np.isinf(ndarray), 0, ndarray) as parameter to np.where() function of numpy library
import numpy as np
ndarray = np.array([[ 5,-np.inf, 15, 45], [ 9, -np.inf, 11, 60],
[16,10, 19, 70],
[18, 26, 20, -np.inf],
[20, 7, 21, -np.inf]])
resarr = np.where(np.isinf(ndarray), 0, ndarray)
print(resarr)
Output
[[ 5. 0. 15. 45.]
[ 9. 0. 11. 60.]
[16. 10. 19. 70.]
[18. 26. 20. 0.]
[20. 7. 21. 0.]]
5. NumPy replace inf with zero posinf or neginf
By using the np.nan_to_num() function we can change the negative or positive infinite value with different values in the below python program example we are converting the positive infinite value to zero or the negative infinite value to -5 with the help of arguments posinf=0 or neginf=-5 in nan_to_num() function. Let us understand with the below program.
import numpy as np
ndarray = np.array([[ 5,np.inf, 15, 45], [ 9, np.inf, 11, -np.inf],
[16,10, 19, 70],
[18, 26, 20, np.inf],
[20, 7, 21, np.inf]])
ndarray = np.nan_to_num(ndarray, copy=True, posinf=0, neginf=-5)
print(ndarray)
Output
[[ 5. 0. 15. 45.]
[ 9. 0. 11. -5.]
[16. 10. 19. 70.]
[18. 26. 20. 0.]
[20. 7. 21. 0.]]
Summary
In this post we have learned how to NumPy replace inf with zero with examples using NumPy library np.isinf(),np. nan_to_num() function.