What is the numpy.imag() function in NumPy?
Overview
The imag() function in NumPy returns the imaginary part of the complex argument passed to it.
Syntax
numpy.imag(val)
Syntax for the imag() function
Parameter value
The imag() function takes a single parameter value (val) that represents an input with complex values. This val value can be a variable or an array.
Return value
The imag() function returns the imaginary component of the input (val) passed to it.
Example
import numpy as np# creating a complex variable and an array of complex valuesmy_var = 1+4jmy_array = np.array([1+2j, 2+3j, 1+5j])# implementing the imag() functionimag_var = np.imag(my_var)imag_array = np.imag(my_array)print(my_var)print(imag_var)print(my_array)print(imag_array)
Explanation
- Line 1: We import the
numpymodule. - Lines 4–5: We create a complex-valued variable,
my_varAnd an array,my_arraythat contains complex values. - Lines 8–9: We implement the
imag()function on the inputs. We assign the result ofmy_varto a variable,imag_varAnd the result ofmy_arraytoimag_array. - Line 11: We print
my_var, which contains the complex argument. - Line 12: We print the variable,
imag_varwhich only contains the imaginary value ofmy_var.
- Line 13: We print
my_array, which contains the complex values. - Line 14: We print the array,
imag_arraywhich only contains the imaginary values ofmy_array.