np.abs() in Python [4 Examples]

Do you want to find the absolute value of the items in an array? In this NumPy article, I will provide a comprehensive overview of the np.abs() in Python, its syntax, parameters required, functionality, and applications.

To effectively calculate the absolute values of elements in different types of arrays, np.abs() in Python is a versatile tool. It handles arrays of integers, floats, complex numbers, and even 2D arrays, converting all elements into their non-negative equivalents.

np.abs() in Python

The np.abs() in Python is essentially an alias or shorthand for np.absolute() in Python. It is designed to calculate the absolute value of each element in an array.

The absolute value of a number refers to its distance from zero on the number line, regardless of direction, making all negative numbers positive.

Syntax and Parameters required for Python np.abs() function

The syntax of the Python np.abs function:

numpy.abs(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)


xInput array in Python. Can contain complex values as well.
outA location where the result in Python is stored.
whereA condition on which to apply the operation in Python.
casting, order, dtype, subokThese parameters control the type casting rules, memory layout, data type, and sub-classing behavior respectively.
List of parameters required in the np.abs() function in Python.

np.abs function in Python use cases

Let’s see some demonstrative examples related to the np.abs() in Python:

1. NumPy absolute value of an integer array in Python

The np.abs() in Python is used to convert each element in an array of integers, including negative values, to their positive counterparts, effectively calculating the absolute value of each number.

import numpy as np

temperature_fluctuations = np.array([-5, 32, -3, 28, 45, -10])


[ 5 32  3 28 45 10]

After executing the code in Pycharm, one can see the output in the below screenshot.

np.abs() in Python

2. np.abs in Python on an array with float values

To effectively compute the absolute value of each floating-point number present in an array, removing any negative signs and leaving the magnitude intact.

Here’s how np.abs() in Python can be used in this context:

import numpy as np

portfolio_changes = np.array([-1.5, 2.3, -0.8, 1.7, -2.0])
absolute_changes = np.abs(portfolio_changes)


[1.5 2.3 0.8 1.7 2. ]

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

numpy abs in Python

3. absolute value NumPy array of complex

For an array of complex numbers, np.abs() in Python calculates the magnitude (or the absolute value) of each complex number, disregarding the phase or direction of the number in the complex plane.

import numpy as np

complex_impedances = np.array([1+2j, 3+4j, 5-6j])


[2.23606798 5.         7.81024968]

After executing the code in Pycharm, one can see the output in the below screenshot.

np.absolute python

4. absolute value Python NumPy of 2D array

When we apply np.abs() in Python to a 2D array, it computes the absolute value for each element, turning all negative numbers in the array into their positive equivalents.

import numpy as np

daily_financials = np.array([[20, -15], [-25, 30], [10, -5]])


[[20 15]
 [25 30]
 [10  5]]

After the implementation of the code in the Pycharm editor, the screenshot is mentioned below.

Python np.abs function


Here, we have learned how the np.abs() in Python is a highly useful function for getting the absolute, or positive, values from different types of data, whether they are simple numbers, complex numbers, or elements in a multi-dimensional array.

You may also like to read: