The np.isreal() is a numpy library function that tests elements, whether a real number or not(not infinity or not, Not a Number), and returns the result as a boolean array.
The Numpy isreal() function tests whether an element is a real number.
The isreal() function only checks for real numbers, not Infinity or NaN values. The isreal() function returns the Boolean values in a Boolean array. The numpy isreal function takes one required parameter.
Syntax
numpy.isreal(input array)
Parameters
The isreal() function takes only one parameter. The only parameter is the input array or the input for which we want to check whether it is a real number.
Return Value
The np.isreal() function returns a Boolean array, which determines whether the number passed in the input array is a real number or not.
Example programs of Python isreal() method
See the following code.
# app.py import numpy as np # Scalar Values print("Real - 933: ", np.isreal(933), "\n") print("Real - [444, 555]: ", np.isreal([444, 555]), "\n") # checking for complex numbers print("Real - [2+3j, 3+1j]", np.isreal([2+3j, 3+1j]), "\n") print("Real - [2+1j, 4+2j]", np.isreal([2+1j, 4+2j]), "\n")
Output
python3 app.py Real - 933: True Real - [444, 555]: [ True True] Real - [2+3j, 3+1j] [False False] Real - [2+1j, 4+2j] [False False]
In this example, we have seen bypassing two scalar values in the isreal() function, and we get True as it represents real numbers.
When we have passed the complex numbers, it shows False as complex numbers are not real numbers.
If an element has a complex type with zero complex parts, the return value for that element is True.
See the following code.
# app.py import numpy as np print("Real - [2+0j, 4+0j]", np.isreal([2+0j, 4+0j]), "\n")
Output
python3 app.py Real - [2+0j, 4+0j] [ True True]
You can see that we have passed a complex number with 0 complex parts, and the output is True.
Example 2: Write a program to use the arange() function and create an array, then check if the array elements are real.
See the following code.
# app.py import numpy as np a = np.arange(12).reshape(3, 4) print(a) print("\n") print("Is real: \n", np.isreal(a))
Output
python3 app.py [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] Is real: [[ True True True True] [ True True True True] [ True True True True]]
In this example, we can see that after creating an array using the np.arange() function, we checked for every element of the array if it’s real or not.
That’s it.