- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate the absolute value element-wise in Numpy
To calculate the absolute value, we can use two methods, fabs() and absolute(). To return the absolute value element-wise, use the numpy.fabs() method in Python Numpy. This displays the output in float.
To return the absolute value element-wise, you can also use the numpy.absolute() method in Python Numpy. The out is a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.
The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.
Steps
At first, import the required library −
import numpy as np
Create an array −
arr = np.array([76, -81, 91, -100, -120, 150, 200])
Display the arrays −
print("Array...
", arr)
Get the type of the arrays −
print("
Our Array type...
", arr.dtype)
Get the dimensions of the Arrays −
print("
Our Array Dimension...
",arr.ndim)
Get the shape of the Arrays −
print("
Our Array Shape...
",arr.shape)
To return the absolute value element-wise, use the numpy.fabs() method in Python Numpy. This displays the output in float −
print("
Result...
",np.fabs(arr))
To return the absolute value element-wise, use the numpy.absolute() method in Python Numpy −
print("
Result...
",np.absolute(arr))
Example
import numpy as np # Create an array arr = np.array([76, -81, 91, -100, -120, 150, 200]) # Display the array print("Array...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimension...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # To return the absolute value element-wise, use the numpy.fabs() method in Python Numpy # This displays the output in float print("
Result...
",np.fabs(arr)) # To return the absolute value element-wise, use the numpy.absolute() method in Python Numpy print("
Result...
",np.absolute(arr))
Output
Array... [ 76 -81 91 -100 -120 150 200] Our Array type... int64 Our Array Dimension... 1 Our Array Shape... (7,) Result... [ 76. 81. 91. 100. 120. 150. 200.] Result... [ 76 81 91 100 120 150 200]
- Related Articles
- Calculate the absolute value of float values in Numpy
- Calculate the absolute value of complex numbers in Numpy
- Compute the absolute values element-wise and store the result in a new location in Numpy
- Compute the truth value of NOT an array element-wise in Numpy
- Subtract arguments element-wise in Numpy
- Compute the bit-wise AND of two arrays element-wise in Numpy
- Compute the bit-wise NOT of an array element-wise in Numpy
- True Divide arguments element-wise in Numpy
- Test element-wise for NaN in Numpy
- Compute the truth value of an array XOR another array element-wise in Numpy
- Return the truth value of an array equal to another element-wise in Numpy
- Return the truth value of an array greater than another element-wise in Numpy
- Return the truth value of an array less than another element-wise in Numpy
- Compute the bit-wise OR of two Numpy arrays element-wise
- Compute the bit-wise XOR of two Numpy arrays element-wise
