w3resource logo
Python Exercises

Python NumPy: Calculate the absolute value element-wise

Python NumPy Math, Statistics, Trigonometry and Linear algebra: Exercise-7 with Solution

Write a Python program to calculate the absolute value element-wise.

Sample Solution:-

Python Code:

import numpy as np
x = np.array([-10.2, 122.2, .20])
print("Original array:")
print(x)
print("Element-wise absolute value:")
print(np.absolute(x))

Sample Output:

Original array:                                                        
[ -10.2  122.2    0.2]                                                 
Element-wise absolute value:                                           
[  10.2  122.2    0.2]

Python Code Editor:

Improve this sample solution and post your code through Disqus