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)``

Here,

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])
print(np.abs(temperature_fluctuations))``````

Output:

``[ 5 32  3 28 45 10]``

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

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)
print(absolute_changes)``````

Output:

``[1.5 2.3 0.8 1.7 2. ]``

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

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])
print(np.abs(complex_impedances))``````

Output:

``[2.23606798 5.         7.81024968]``

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

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]])
print(np.abs(daily_financials))``````

Output:

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

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

Conclusion

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: