NumPy absolute()

The numpy.absolute() function computes the absolute value of each element in an input array. It works element-wise, making all values non-negative.

Syntax

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

Parameters

ParameterTypeDescription
xarray_likeInput array containing numeric values (real or complex).
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the absolute function.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the absolute values of the input array elements. If the input is a scalar, a scalar is returned.


Examples

1. Computing Absolute Values of an Array

Here, we compute the absolute values of elements in an array containing both positive and negative numbers.

</>
Copy
import numpy as np

# Define an array with positive and negative values
arr = np.array([-5, -3, 0, 3, 5])

# Compute the absolute values
result = np.absolute(arr)

# Print the result
print("Original array:", arr)
print("Absolute values:", result)

Output:

Original array: [-5 -3  0  3  5]
Absolute values: [5 3 0 3 5]

2. Computing Absolute Values for Complex Numbers

For complex numbers, the absolute value returns the magnitude of each complex number.

</>
Copy
import numpy as np

# Define an array of complex numbers
complex_arr = np.array([3 + 4j, -1 - 2j, 0 + 1j])

# Compute the absolute values (magnitudes)
result = np.absolute(complex_arr)

# Print the results
print("Original complex numbers:", complex_arr)
print("Magnitudes:", result)

Output:

Original complex numbers: [ 3.+4.j -1.-2.j  0.+1.j]
Magnitudes: [5. 2.23606798 1.]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array with negative values
arr = np.array([-7, -2, 4, -1])

# Create an output array with the same shape
output_array = np.empty_like(arr)

# Compute absolute values and store them in output_array
np.absolute(arr, out=output_array)

# Print the results
print("Computed absolute values:", output_array)

Output:

Computed absolute values: [7 2 4 1]

4. Using the where Parameter

Using a condition to compute absolute values only for selected elements.

</>
Copy
import numpy as np

# Define an array with positive and negative values
arr = np.array([-6, -4, 2, -8])

# Define a mask (compute absolute only where mask is True)
mask = np.array([True, False, True, False])

# Compute absolute values where mask is True
result = np.absolute(arr, where=mask)

# Print the results
print("Computed absolute values with mask:", result)

Output:

Computed absolute values with mask: [6 0 2 0]

The absolute values are computed only for elements where mask=True. The other values remain unchanged.