NumPy | fabs method
np.fabs(~) method returns a NumPy array with the absolute value applied to each of its value.
There is a difference between
np.fabs() methods. The
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.
The source NumPy array.
A NumPy array of floats with the absolute value applied to each of its value.
Suppose we have the following NumPy array:
x = np.array([[-1,2], [3,-4]])xarray([[-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.