.abs()
In NumPy, the .abs()
function calculates the absolute value of a given number or each element in an array. The absolute value of a number is its non-negative value or the number’s distance from zero. This function can be applied to both real and complex numbers.
Note:
numpy.abs()
is identical tonumpy.absolute()
, and they can be used interchangeably.
Syntax
numpy.abs(input_value, out=None, where=True)
or alternatively,
numpy.absolute(input_value, out=None, where=True)
input_value
: The input number or array for which the absolute value will be computed.out
(optional): A location where the result of the absolute value will be stored. If no value is provided, the default value ofNone
is used and a new array is returned.where
(optional): A boolean array that determines which elements of the input array should have the absolute value function applied:- If the condition is
True
at a given position, the absolute value is computed for that element. - If the condition is
False
, the original value is retained. - If no value is provided, the absolute value is computed for every element.
- If the condition is
Example 1
This example demonstrates using .abs()
function to calculate the absolute value of an array:
# Importing the 'numpy' library as 'np'import numpy as np# Creating a numpy arrayarr = np.array([1, -1.5, 0, -3])# Computing the absolute value of the arrayarr = np.abs(arr)print(arr)
The above example code results in the following output:
[1. 1.5 0. 3.]
Example 2
This example shows how the where
parameter of .abs()
function is used to specify which elements of the array undergo the absolute value function:
# Importing the 'numpy' library as 'np'import numpy as np# Creating a numpy arrayarr = np.array([-1, -2, -3, -4])# Computing the absolute value of only the elements that are less than -2np.abs(arr, out=arr, where=arr<-2)print(arr)
The above example code results in the following output:
[-1 -2 3 4]
Note: The
where
array must be broadcastable to the shape of the input array. Additionally, when using thewhere
parameter, it is recommended to use theout
parameter to specify where the result should be stored, which helps avoid errors with uninitialized memory.
Codebyte Example
In this codebyte example, the .abs()
method computes the absolute value of the elements in the array that are greater than -100
:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Skill path
Data Science Foundations
Learn to clean, analyze, and visualize data with Python and SQL.Includes 15 CoursesWith CertificateBeginner Friendly54 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours