What is the absolute() function in NumPy?
Overview
In NumPy, the absolute() function is used to compute the
Syntax
numpy.absolute(x, /, out=None, *, where=True)
Syntax for the absolute() function in NumPy
Parameters
This function takes the following parameter values:
x: This represents an input array of values. This argument is mandatory.out: This represents the location where the result is stored. This is optional.where: This is the condition over which the input is being broadcast. At a given location where this condition isTrue, the resulting array will be set to theufuncresult. Otherwise, the resulting array will retain its original value. This is optional.
Note:ufuncis short for universal function and it operates on ndarrays, in an element-wise fashion. It also supports other standard features in NumPy.
**kwargs: This represents other keyword arguments. This is mandatory.
Return value
This function returns an array of the same shape as the input array passed to it holding the absolute values of the elements of the input array.
Example
import numpy as np# creating an input arrayx = np.array([-1, 6, -2.5, 2])# implementing the absolute() functionmyarray = np.absolute(x)print(x)print(myarray)
Explanation
- Line 1: We import the
numpymodule. - Line 4: We use the
array()function to create an arrayx. - Line 7: We call the
absolute()function by passing the input array,xas the argument. We assign the result to a variablemyarray. - Line 9: We print the array
x. - Line 10: We print the new array
myarray.