make negative values positive in numpy array

Numpy – Make All Negative Values Positive

The Numpy library in Python comes with a number of useful functions and methods to work with and manipulate the data in arrays. In this tutorial, we will look at how to make all the negative values in a Numpy array positive with the help of some examples.

Steps to make negative values positive in Numpy

make negative values positive in numpy array

You can use boolean indexing to make all the negative values in a Numpy array positive. The following is the syntax –

# make negative values positive
ar[ar < 0] = -1 * ar[ar < 0]

It replaces the negative values in the array ar with the corresponding positive values.

Let’s now look at a step-by-step example of using this syntax –

Step 1 – Create a Numpy array

First, we will create a Numpy array that we will use throughout this tutorial.

import numpy as np

# create numpy array
ar = np.array([-3, -2, -1, 0, 1, 2, 3])
# display the array
print(ar)

Output:

[-3 -2 -1  0  1  2  3]

Here, we used the numpy.array() function to create a one-dimensional Numpy array containing some numbers. You can see that the array has both positive and negative values (along with a zero).

Step 2 – Make negative values positive using boolean indexing

Using boolean indexing identify the values that are less than zero and then set them to their corresponding positive values (by multiplying by -1).

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

First, we will specify our boolean expression ar < 0 and then make the array values satisfying this condition positive.

For example, let’s replace all the negative values in the above array with the corresponding positive values.

# make negative values positive
ar[ar < 0] = -1*ar[ar < 0]
# display the array
print(ar)

Output:

[3 2 1 0 1 2 3]

The resulting array has the negative values replaced with the positive values.

To understand what’s happening here, let’s look under the hood. Let’s see what we get from the expression ar < 0

# create a numpy array
ar = np.array([-3, -2, -1, 0, 1, 2, 3])
# result of boolean expression ar < 0
ar < 0

Output:

array([ True,  True,  True, False, False, False, False])

We get a boolean array. The boolean values in this array represent whether a value at a particular index satisfies the given condition or not (in our case whether the element is less than 0 or not).

When we do ar[ar < 0] = -1*ar[ar < 0], we are essentially setting the values in the array where the condition evaluates to True to its value multiplied by -1.

You can similarly filter a Numpy array for other conditions as well.

Summary – Make negative values positive in Numpy

In this tutorial, we looked at how to replace all the negative values in a Numpy array with their corresponding positive values. The following is a short summary of the steps mentioned –

  1. Create a Numpy array (skip this step if you already have an array to operate on).
  2. Use boolean indexing to find the negative values and then make them positive ar[ar < 0] = -1*ar[ar < 0]

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top