In this tutorial, we will look at how to get the imaginary part of a complex number in a Numpy array. For example for the complex number `a + bj`

, `a`

is the real part and `b`

is the imaginary part.

## How to get the imaginary part of complex values in a Numpy array?

You can use the `numpy.imag()`

function to get (element-wise) the imaginary part of complex numbers in a Numpy array. The following is the syntax –

# get the imaginary part - pass scaler value or numpy array np.imag(a)

The `numpy.imag()`

function essentially returns (element-wise) the imaginary component of the complex argument.

If you pass a scalar value, it returns the imaginary part of that value. And, if you pass a Numpy array, it returns the array of imaginary parts of the array elements.

## Examples

Let’s now look at some examples of using the above function to extract the imaginary part of complex numbers.

### Example 1 – Extract the imaginary part for a scalar value using `numpy.imag()`

First, let’s pass scaler values to the `numpy.imag()`

function.

Let’s apply the `numpy.imag()`

function on three scalar values – a real number, a complex number with a non-zero real part, and a complex number with the real part as 0.

import numpy as np # get the imaginary part print(np.imag(14)) print(np.imag(2 + 4j)) print(np.imag(3j))

Output:

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

0 4.0 3.0

We get the imaginary component for all the scalar values irrespective of whether we passed a real or a complex number as an argument.

### Example 2 – Element-wise extract the imaginary part in a Numpy array using `numpy.imag()`

If you apply the `numpy.imag()`

function on an array, it will return a Numpy array of the extracted imaginary components for the elements inside the array.

Let’s create a 1-D array and apply the `numpy.imag()`

function to it.

# create a numpy array ar = np.array([1, 2+3j, 2+0j, 0+4j, 5.7, np.nan, np.inf, 1j, 0+0j]) # element-wise extract the imaginary part in ar np.imag(ar)

Output:

array([0., 3., 0., 4., 0., 0., 0., 1., 0.])

We get a Numpy array containing the imaginary component of each value in the array `ar`

.

## Summary – Imaginary part of complex numbers in Numpy array

In this tutorial, we looked at how we can use the `numpy.imag()`

function to extract the imaginary component of a complex number in a Numpy array. Keep in mind that if you apply this function on a scalar value, it returns the imaginary component whereas if you apply it on a Numpy array, it returns the array of imaginary components.

You might also be interested in –

- Practical Guide to Working with Complex Numbers in Python
- Python – Add Two Complex Numbers
- Python – Subtract Two Complex Numbers
- Python – Multiply Two Complex Numbers
- Python – Divide Two Complex Numbers
- Python – Generate a Random Complex Number
- Python – Convert Complex Number to Polar Form
- Python – Get the Phase Angle of a Complex Number
- Python – Get the Absolute Value of a Complex Number
- Numpy – Get the Complex Conjugate
- Numpy – Get the Real Part of a Complex Number
- Numpy – Check If a Number is Real
- Numpy – Check If a Number is Complex

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**