NumPy | fabs method
Start your free 7-days trial now!
NumPy's np.fabs(~) method returns a NumPy array with the absolute value applied to each of its value.
There is a difference between np.abs() and np.fabs() methods. The f in fabs() denotes float, which means that the return type for fabs() is always float. On the other hand, np.abs() returns the same data type as the input array.
Parameter
1. x | array-like
The source NumPy array.
Return Value
A NumPy array of floats with the absolute value applied to each of its value.
Examples
Basic Usage
Suppose we have the following NumPy array:
x
array([[-1, 2], [3, -4]])
To convert all negatives to positives, use the abs() method like follows:
np.fabs(x)
array([[1., 2.], [3., 4.]])
Notice the numbers have a . in them - this means that they are of data-type float.
Also, note that the source NumPy array is left intact, that is, x in this example would still have negative values.