Reputation: 177
I have an array containing some negative values. how can i find the absolute value?
for eg suppose my array is
arr = [-2,-5,0,1,2]
and i want an array
arr_out =[2,5,0,1,2]
Upvotes: 0
Views: 2033
Reputation: 195438
Without numpy
using list comprehension:
arr = [-2,-5,0,1,2]
arr_out = [abs(i) for i in arr]
print(arr_out)
Output:
[2, 5, 0, 1, 2]
Upvotes: 1
Reputation: 589
import numpy as np
arr = [-2,-5,0,1,2]
print("Absolute value: ", np.absolute(arr))
Upvotes: 0