How can negative values be replaced with zero in NumPy?

How can negative values be replaced with zero in NumPy?

NumPy is a popular Python library used for scientific computing and data analysis. It provides efficient numerical operations on multi-dimensional arrays and matrices. In certain scenarios, it may be necessary to replace negative values within a NumPy array with zero. This can be achieved using the built-in function “np.where()”, which takes in three parameters – a condition, a value to replace the elements that satisfy the condition, and the original array. By setting the value to zero and specifying the condition as the presence of negative values, the function can effectively replace all negative values with zero in the NumPy array. This method is useful in data cleaning and manipulation tasks, where negative values may skew the results or cause errors in calculations.

Replace Negative Values with Zero in NumPy


You can use the following basic syntax to replace negative values with zero in NumPy:

my_array[my_array <0] = 0

This syntax works with both 1D and 2D NumPy arrays.

The following examples show how to use this syntax in practice.

Example 1: Replace Negative Values with Zero in 1D NumPy Array

The following code shows how to replace all negative values with zero in a NumPy array:

import numpy as np

#create 1D NumPy array
my_array = np.array([4, -1, 6, -3, 10, 11, -14, 19, 0])

#replace negative values with zero in array
my_array[my_array <0] = 0

#view updated array
print(my_array)

[ 4  0  6  0 10 11  0 19  0]

Notice that each negative value in the original array has been replaced with zero.

Example 2: Replace Negative Values with Zero in 2D NumPy Array

Suppose we have the following 2D NumPy array:

import numpy as np

#create 2D NumPy array
my_array = np.array([3, -5, 6, 7, -1, 0, -5, 9, 4, 3, -5, 1]).reshape(4,3)

#view 2D NumPy array
print(my_array)

[[ 3 -5  6]
 [ 7 -1  0]
 [-5  9  4]
 [ 3 -5  1]]

We can use the following code to replace all negative values with zero in the NumPy array:

#replace all negative values with zero in 2D array
my_array[my_array <0] = 0#view updated array
print(my_array)

[[3 0 6]
 [7 0 0]
 [0 9 4]
 [3 0 1]]

Notice that all negative values in the original 2D array have been replaced with zero.

The following tutorials explain how to perform other common tasks in NumPy:

Cite this article

stats writer (2024). How can negative values be replaced with zero in NumPy?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-negative-values-be-replaced-with-zero-in-numpy/

stats writer. "How can negative values be replaced with zero in NumPy?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-negative-values-be-replaced-with-zero-in-numpy/.

stats writer. "How can negative values be replaced with zero in NumPy?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-negative-values-be-replaced-with-zero-in-numpy/.

stats writer (2024) 'How can negative values be replaced with zero in NumPy?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-negative-values-be-replaced-with-zero-in-numpy/.

[1] stats writer, "How can negative values be replaced with zero in NumPy?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can negative values be replaced with zero in NumPy?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top