Home > Back-end >  Make negative values of numpy array positive
Make negative values of numpy array positive

Time:12-17

I have [6, -1, -3, -5]. I want to make the negative values positive, e.g. [6, 1, 3, 5]. Is there an easy way to do this?

Thank you so much, in advance!

CodePudding user response:

Simply use the builtin abs function:

>>> a = np.array( [6, -1, -3, -5])
>>> a
array([ 6, -1, -3, -5])

>>> abs(a)
array([6, 1, 3, 5])

CodePudding user response:

If you don't want to change the type of data.

data = [6, -1, -3, -5]
print([abs(d) for d in data])

And the output will be [6, 1, 3, 5].

  • Related