NumPy fabs()

The numpy.fabs() function computes the absolute values (positive magnitude) of each element in an input array, element-wise. Unlike numpy.abs(), fabs() does not support complex numbers.

Syntax

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

Parameters

ParameterTypeDescription
xarray_likeAn array of real numbers for which absolute values are computed. Complex numbers are not supported.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where results are stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying where to compute fabs(). Elements where where=False retain their original value.
castingstr, optionalDefines casting behavior when computing fabs().
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 containing the absolute values of the input. If the input is a scalar, a scalar is returned.


Examples

1. Computing the Absolute Value of a Single Number

Here, we compute the absolute value of a single negative number.

</>
Copy
import numpy as np

# Define a negative number
num = -10.5

# Compute its absolute value
result = np.fabs(num)

# Print the result
print("Absolute value of -10.5:", result)

Output:

Absolute value of -10.5: 10.5

2. Computing Absolute Values for an Array

We compute the absolute values for an array of positive and negative numbers.

</>
Copy
import numpy as np

# Define an array with both positive and negative numbers
arr = np.array([-3.2, 4.5, -2.0, -8.9, 0])

# Compute the absolute values
abs_values = np.fabs(arr)

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

Output:

Original array: [-3.2  4.5 -2.  -8.9  0. ]
Absolute values: [3.2 4.5 2.  8.9 0. ]

3. Using the out Parameter

We store the computed absolute values in a pre-allocated output array.

</>
Copy
import numpy as np

# Define an input array
arr = np.array([-1.5, -2.3, 3.8, -7.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.fabs(arr, out=output_array)

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

Output:

Computed absolute values: [1.5 2.3 3.8 7.1]

4. Using the where Parameter

Computing absolute values only for selected elements using a boolean mask.

</>
Copy
import numpy as np

# Define an input array
arr = np.array([-5.2, -3.0, 4.2, -6.1])

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

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

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

Output:

Computed absolute values with mask: [5.2 0.  4.2 0. ]

Only the elements where mask=True are computed, while others retain their initial value.