NumPy: Calculate the absolute value element-wise (np.abs, np.fabs)
You can calculate the absolute value element-wise in a NumPy array (ndarray) using np.abs(), np.absolute(), or np.fabs(). Note that np.abs() is simply an alias for np.absolute(). Additionally, the built-in abs() function can be used.
As of NumPy version 1.26.2, these functions are not available as methods of ndarray.
The NumPy version used in this article is as follows. Note that functionality may vary between versions.
import numpy as np
print(np.__version__)
# 1.26.2
How to use np.abs() and np.absolute()
np.abs() is an alias for np.absolute()
np.abs() is an alias for np.absolute().
print(np.abs)
# <ufunc 'absolute'>
print(np.abs is np.absolute)
# True
In the following sample code, np.abs() is used, but np.absolute() can be used in the same way.
Basic usage
Passing an ndarray to np.abs() returns a new ndarray, with each element converted to its absolute value.
a = np.array([-2, -1, 0, 1, 2])
print(np.abs(a))
# [2 1 0 1 2]
print(type(np.abs(a)))
# <class 'numpy.ndarray'>
You can specify not only ndarray but also array-like objects such as lists to np.abs(). The return value is always an ndarray.
l = [-2, -1, 0, 1, 2]
print(np.abs(l))
# [2 1 0 1 2]
print(type(np.abs(l)))
# <class 'numpy.ndarray'>
For conversion between ndarray and lists, see the following article.
You can also specify scalar values to np.abs().
print(np.abs(-100))
# 100
print(type(np.abs(-100)))
# <class 'numpy.int64'>
Returns int for int, float for float
np.abs() preserves the data type (dtype) of the input ndarray. It returns an integer (int) ndarray for integer inputs and a floating point (float) ndarray for floating point inputs.
a_int = np.array([-2, -1, 0, 1, 2])
print(a_int.dtype)
# int64
print(np.abs(a_int))
# [2 1 0 1 2]
print(np.abs(a_int).dtype)
# int64
a_float = np.array([-2.0, -1.0, 0, 1.0, 2.0])
print(a_float.dtype)
# float64
print(np.abs(a_float))
# [2. 1. 0. 1. 2.]
print(np.abs(a_float).dtype)
# float64
For details about the data type (dtype) in NumPy, see the following article.
Also supports complex numbers
In Python, complex numbers (complex) can be represented using j as the imaginary unit.
np.abs() also supports complex numbers (complex), converting them to their absolute values (modulus or magnitude). The result is always a floating point number (float), even if the absolute value is an integer.
a_complex = np.array([3 + 4j, 5 + 12j])
print(a_complex.dtype)
# complex128
print(np.abs(a_complex))
# [ 5. 13.]
print(np.abs(a_complex).dtype)
# float64
Built-in abs() function for ndarray
Since ndarray implements the special method __abs__(), it can be used with Python's built-in abs() function.
a = np.array([-2, -1, 0, 1, 2])
print(a.__abs__())
# [2 1 0 1 2]
print(abs(a))
# [2 1 0 1 2]
print(type(abs(a)))
# <class 'numpy.ndarray'>
For ndarray, abs() can be used in the same way as np.absolute() (= np.abs()).
The
absfunction can be used as a shorthand fornp.absoluteon ndarrays. numpy.absolute — NumPy v1.26 Manual
Like np.abs(), it also supports complex numbers (complex).
a_complex = np.array([3 + 4j, 5 + 12j])
print(abs(a_complex))
# [ 5. 13.]
Difference between np.fabs() and np.abs()
np.fabs() calculates the absolute value element-wise in an ndarray, similar to np.abs(). However, it consistently returns a floating point number (float), even if the input is an integer (int).
a_int = np.array([-2, -1, 0, 1, 2])
print(a_int.dtype)
# int64
print(np.fabs(a_int))
# [2. 1. 0. 1. 2.]
print(np.fabs(a_int).dtype)
# float64
math.fabs() supports only real numbers and results in an error for complex numbers (complex).
a_complex = np.array([3 + 4j, 5 + 12j])
# print(np.fabs(a_complex))
# TypeError: ufunc 'fabs' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''