Change language

Replace negative value with zero in Numpy array

Given numpy array, the goal is to replace negative values with zeros in numpy array. Let’s check up some examples of this issue.

Numpy replace negative with 0: Naive Method

# Python code to demonstrate
# to replace negative value with 0
import numpy as np
  
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
  
# printing initial arrays
print("initial array", ini_array1)
  
# code to replace all negative value with 0
ini_array1[ini_array1<0] = 0
  
# printing result
print("New resulting array: ", ini_array1)

Output:

initial array [ 1  2 -3  4 -5 -6]
New resulting array:  [1 2 0 4 0 0]

Numpy replace negative values with zero Using np.clip

# Python code to demonstrate
# to replace negative values with 0
import numpy as np
  
# supposing maxx value array can hold
maxx = 1000
  
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
  
# printing initial arrays
print("initial array", ini_array1)
  
# code to replace all negative value with 0
result = np.clip(ini_array1, 0, 1000)
  
# printing result
print("New resulting array: ", result)

Output:

initial array [ 1  2 -3  4 -5 -6]
New resulting array:  [1 2 0 4 0 0]

Numpy replace negative with 0 Using np.where

# Python code to demonstrate
# to replace negative values with 0
import numpy as np
  
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
  
# printing initial arrays
print("initial array", ini_array1)
  
# code to replace all negative value with 0
result = np.where(ini_array1<0, 0, ini_array1)
  
# printing result
print("New resulting array: ", result)

Output:

initial array [ 1  2 -3  4 -5 -6]
New resulting array:  [1 2 0 4 0 0]

Method #4: Comparing the given array with an array of zeros and write in the maximum value from the two arrays as the output

# Python code to demonstrate
# to replace negative values with 0
import numpy as np
   
ini_array1 = np.array([1, 2, -3, 4, -5, -6])
   
# printing initial arrays
print("initial array", ini_array1)
   
# Creating a array of 0
zero_array = np.zeros(ini_array1.shape, dtype=ini_array1.dtype)
print("Zero array", zero_array)
  
# code to replace all negative value with 0
ini_array2 = np.maximum(ini_array1, zero_array)
  
# printing result
print("New resulting array: ", ini_array2)

Output:

initial array [ 1  2 -3  4 -5 -6]
Zero array [0 0 0 0 0 0]
New resulting array:  [1 2 0 4 0 0]

Replace negative values in an numpy array

StackOverflow question

Is there a simple way of replacing all negative values in an array with 0?

I’m having a complete block on how to do it using a NumPy array.

E.g.

a = array([1, 2, 3, -4, 5])

I need to return

[1, 2, 3, 0, 5]

a < 0 gives:

[False, False, False, True, False]

This is where I’m stuck - how to use this array to modify the original array.

Answer:

In [4]: a[a < 0] = 0

In [5]: a
Out[5]: array([1, 2, 3, 0, 5])

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically