Replace Negative Value with Zero in NumPy Array
If an array has negative numbers, you change them to 0, while keeping all non-negative numbers the same. For example: Suppose you have this array: A=[−3, 5, −7, 2, 0, −1], after replacing negatives with zero: A′=[0, 5, 0, 2, 0, 0].
Using boolean mask (in-place)
This is the most efficient method if you want to update the original array directly. It uses NumPy’s boolean indexing.
Example: In this example, we create a boolean mask for all negative elements and assign them the value 0 directly inside the same array.
import numpy as np
a = np.array([1, 2, -3, 4, -5, -6])
print("Input:", a)
a[a < 0] = 0
print("Result:", a)
Output
Input: [ 1 2 -3 4 -5 -6] Result: [1 2 0 4 0 0]
Explanation:
- a < 0: produces a boolean mask identifying negative positions.
- a[a < 0] = 0: selects those positions and assigns 0 in-place.
- The original array a is modified; no new array is created.
Using np.maximum
This method uses NumPy’s universal function (ufunc) to compare each element with 0.
Example: Here we apply np.maximum to take the larger of each element and 0, ensuring negatives are replaced.
import numpy as np
a = np.array([1, 2, -3, 4, -5, -6])
res = np.maximum(a, 0)
print("Input:", a)
print("Result:", res)
Output
Input: [ 1 2 -3 4 -5 -6] Result: [1 2 0 4 0 0]
Explanation:
- np.maximum(a, 0) compares each element of a with 0.
- For each position it returns the larger of the two values effectively replacing negatives with 0.
- res is a new array (original a unchanged).
Using np.clip
np.clip clamps all values to lie within a given range. Setting the lower bound to 0 forces negatives up to 0.
Example: In this example, we clip values of the array to the range [0, upper], so anything below 0 becomes 0.
import numpy as np
a = np.array([1, 2, -3, 4, -5, -6])
res = np.clip(a, 0, max(a.max(), 0))
print("Input:", a)
print("Result:", res)
Output
Input: [ 1 2 -3 4 -5 -6] Result: [1 2 0 4 0 0]
Explanation:
- a.max() finds the largest element; max(a.max(), 0) ensures the upper bound ≥ 0.
- np.clip(a, 0, upper) forces every value below 0 to become 0, leaving others unchanged.
- The result is a new array.
Using np.where
np.where lets you select between two values based on a condition.
Example: This code creates a new array: if an element is negative, it becomes 0; otherwise, it keeps its original value.
import numpy as np
a = np.array([1, 2, -3, 4, -5, -6])
res = np.where(a < 0, 0, a)
print("Input:", a)
print("Result:", res)
Output
Input: [ 1 2 -3 4 -5 -6] Result: [1 2 0 4 0 0]
Explanation:
- a < 0 produces a boolean mask.
- np.where(mask, 0, a) chooses 0 where mask is True, otherwise the original element.
- Returns a new array; dtype follows broadcasting rules.
Using np.vectorize
This method wraps a Python function to apply elementwise. It is slower but sometimes useful for readability.
Example: In this program, a lambda function replaces negative elements with 0 and keeps others unchanged.
import numpy as np
a = np.array([1, 2, -3, 4, -5, -6])
vfunc = np.vectorize(lambda x: 0 if x < 0 else x)
res = vfunc(a)
print("Input:", a)
print("Result:", res)
Output
Input: [ 1 2 -3 4 -5 -6] Result: [1 2 0 4 0 0]
Explanation:
- np.vectorize applies the lambda to each element.
- For negatives, lambda returns 0. Otherwise, it returns the value unchanged.
- The result is correct but slower than ufunc-based methods like maximum or clip.