How to find Absolute Values of the NumPy Array
To find absolute values of the elements of NumPy array we use ndarray.__abs__() method of the NumPy library in Python.
Let’s understand it better with an example:
Example:
Python3
# import the important module in python import numpy as np # make an array with numpy gfg = np.array([[ 1.22 , 2.25 , - 3.21 , 4.45 , 5.56 , 6 ], [ - 6.65 , 5.55 , 4.32 , 3.33 , 2.12 , - 1.05 ]]) # applying ndarray.__abs__() method print (gfg.__abs__()) |
Output
[[ 1 2 3 4 5 6 ] [ 6 5 4 3 2 1]]
NumPy ndarray.__abs__() | Find Absolute Value of Elements in NumPy Array
The ndarray.__abs__() method returns the absolute value of every element in the NumPy array.
It is automatically invoked when we use Python’s built-in method abs() on a NumPy array.
Example
Python3
import numpy as np gfg = np.array([ 1.45 , 2.32 , 3.98 , 4.41 , 5.55 , 6.12 ]) print (gfg.__abs__()) |
Output
[ 1 2 3 4 5 6]