Python NumPy – Replace NaN with zero and fill positive infinity for complex input values
Last Updated :
25 Apr, 2022
In this article, we will see how to replace NaN with zero and fill positive infinity for complex input values in Python.
Numpy package provides us with the numpy.nan_to_num() method to replace NaN with zero and fill positive infinity for complex input values in Python. This method substitutes a nan value with a number and replaces positive infinity with the number of our choice. Let’s see the syntax of the numpy.nan_to_num() in detail.
Syntax: numpy.nan_to_num(x, copy=True, nan=0.0, posinf=None, neginf=None)
Parameters:
- x: array like or scalar object. data given as input.
- copy: optional value, boolean. pass ‘true’ to create a copy of x , or ‘false’ to replace the values inplace. by default ‘true’.
- nan: optional value, int or float.Fill NaN values with this value. NaN values will be substituted with 0.0 if no value is given.
- posinf: optional value, int or float. Fill positive infinity values with this value. Positive infinity values will be replaced with an extremely big number if no value is given.
- neginf: optional value, int or float.Fill in negative infinity values with this value. Negative infinity values will be replaced with a very small integer if no value is passed.
Returns: an array object.
Example 1:
In this example, we created an array of imaginary numbers with the help of np.nan and np.inf. The shape of the array is defined by the .shape attribute and the dimension of the array is defined by .ndim. Now we will use the posinf parameter to replace np.inf with the 999999 value.
Python3
import numpy as np
array = np.array([ complex (np.nan, np.inf)])
print (array)
print ( "Shape of the array is : " ,array.shape)
print ( "The dimension of the array is : " ,array.ndim)
print ( "Datatype of our Array is : " ,array.dtype)
print ( "After replacement the array is : " ,np.nan_to_num(array, posinf = 999999 ))
|
Output:
[nan+infj]
Shape of the array is : (1,)
The dimension of the array is : 1
Datatype of our Array is : complex128
After replacement the array is : [0.+999999.j]
Example 2:
In this example, we are replacing nan with the value of 100.
Python3
import numpy as np
array = np.array([ complex (np.nan, np.inf)])
print (array)
print ( "Shape of the array is : " ,array.shape)
print ( "The dimension of the array is : " ,array.ndim)
print ( "Datatype of our Array is : " ,array.dtype)
print ( "After replacement the array is : " ,
np.nan_to_num(array,nan = 100 , posinf = 999999 ))
|
Output:
[nan+infj]
Shape of the array is : (1,)
The dimension of the array is : 1
Datatype of our Array is : complex128
After replacement the array is : [100.+999999.j]
Example 3:
In this example, we are replacing nan= 100, posinf = 999999, neginf=0.
Python3
import numpy as np
array = np.array([ complex (np.nan, np.inf), - np.inf])
print (array)
print ( "Shape of the array is : " ,array.shape)
print ( "The dimension of the array is : " ,array.ndim)
print ( "Datatype of our Array is : " ,array.dtype)
print ( "After replacement the array is : " ,
np.nan_to_num(array,nan = 100 , posinf = 999999 , neginf = 0 ))
|
Output:
[ nan+infj -inf +0.j]
Shape of the array is : (2,)
The dimension of the array is : 1
Datatype of our Array is : complex128
After replacement the array is : [100.+999999.j 0. +0.j]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...