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 of imaginary numbers array = np.array([ complex (np.nan, np.inf)]) print (array) # shape of the array is print ( "Shape of the array is : " ,array.shape) # dimension of the array print ( "The dimension of the array is : " ,array.ndim) # Datatype of the array print ( "Datatype of our Array is : " ,array.dtype) # np.nan is replaced with 0.0 and np.inf # is replaced with 999999 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 # Creating an array of imaginary numbers array = np.array([ complex (np.nan, np.inf)]) print (array) # shape of the array is print ( "Shape of the array is : " ,array.shape) # dimension of the array print ( "The dimension of the array is : " ,array.ndim) # Datatype of the array print ( "Datatype of our Array is : " ,array.dtype) # np.nan is replaced with 100 and np.inf is # replaced with 999999 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 package import numpy as np # Creating an array of imaginary numbers array = np.array([ complex (np.nan, np.inf), - np.inf]) print (array) # shape of the array is print ( "Shape of the array is : " ,array.shape) # dimension of the array print ( "The dimension of the array is : " ,array.ndim) # Datatype of the array print ( "Datatype of our Array is : " ,array.dtype) # np.nan is replaced with 100 and np.inf is # replaced with 999999 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]