Python - Replace negative value with zero in numpy array

In NumPy arrays, replacing negative values with zero is a common preprocessing step in data analysis. Python offers several efficient methods to accomplish this task, from basic list comprehension to NumPy's built-in functions.

Using List Comprehension

This approach converts the array to a list, applies conditional logic, and converts back to NumPy array ?

import numpy as np

arr = np.array([-12, 32, -34, 42, -53, 88])
result = np.array([0 if x < 0 else x for x in arr])
print("Original array:", arr)
print("Modified array:", result)
Original array: [-12  32 -34  42 -53  88]
Modified array: [ 0 32  0 42  0 88]

Using numpy.where()

The most efficient and readable approach using NumPy's conditional selection ?

import numpy as np

arr = np.array([-12, 32, -34, 42, -53, 88])
result = np.where(arr < 0, 0, arr)
print("Original array:", arr)
print("Modified array:", result)
Original array: [-12  32 -34  42 -53  88]
Modified array: [ 0 32  0 42  0 88]

Using numpy.clip()

Clips values to a specified range, setting a minimum bound of 0 ?

import numpy as np

arr = np.array([-12, 32, -34, 42, -53, 88])
result = np.clip(arr, 0, None)
print("Original array:", arr)
print("Modified array:", result)
Original array: [-12  32 -34  42 -53  88]
Modified array: [ 0 32  0 42  0 88]

Using numpy.maximum()

Compares each element with 0 and returns the maximum value ?

import numpy as np

arr = np.array([-12, 32, -34, 42, -53, 88])
result = np.maximum(arr, 0)
print("Original array:", arr)
print("Modified array:", result)
Original array: [-12  32 -34  42 -53  88]
Modified array: [ 0 32  0 42  0 88]

Performance Comparison

Here's a performance comparison for large arrays ?

import numpy as np
import time

# Create large array for performance testing
large_arr = np.random.randint(-100, 100, size=1000000)

# Test numpy.where()
start = time.time()
result1 = np.where(large_arr < 0, 0, large_arr)
time1 = time.time() - start

# Test numpy.maximum()
start = time.time()
result2 = np.maximum(large_arr, 0)
time2 = time.time() - start

# Test numpy.clip()
start = time.time()
result3 = np.clip(large_arr, 0, None)
time3 = time.time() - start

print(f"np.where(): {time1:.6f} seconds")
print(f"np.maximum(): {time2:.6f} seconds")
print(f"np.clip(): {time3:.6f} seconds")
np.where(): 0.012453 seconds
np.maximum(): 0.003821 seconds
np.clip(): 0.004156 seconds

Comparison Table

Method Performance Readability Best For
List Comprehension Slowest Good Small arrays, learning
np.where() Medium Excellent Complex conditions
np.clip() Fast Good Range-based operations
np.maximum() Fastest Good Simple threshold operations

Conclusion

For replacing negative values with zero, np.maximum() offers the best performance, while np.where() provides the most readable and flexible solution. Choose np.clip() when working with value ranges.

Updated on: 2026-03-27T14:39:01+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements