The absolute()
function in NumPy computes the absolute values of all the elements of an array.
The absolute value of a number is the magnitude, or the distance, between the number and zero. It is always a positive number.
numpy.fabs(x, /, out=None, *, where=True)
The fabs()
function takes the following parameter values:
x
: This represents an input array of values. It is a required parameter.out
: This represents the location where the result is stored. It is an optional parameter.where
: This represents the condition over which the input is being broadcast. At a given location where this condition is True
, the resulting array is set to the ufunc
result. Otherwise, the resulting array retains its original value. This is an optional parameter.**kwargs
: This represents other keyword arguments. It is an optional parameter.The fabs()
function returns an array of the same shape as the input array passed to it. This array holds the absolute values of the elements of the input array.
import numpy as np# Creating an input array of complex valuesx = np.array([-1, 6, -2.5, 2])# Implementing the fabs() functionmyarray = np.fabs(x)print(x)print(myarray)
numpy
module.x
using the array()
function.fabs()
function on the input array. We assign the result to a variable myarray
.x
.myarray
.