Thursday, December 26, 2024
Google search engine
HomeLanguagesPython | Replace negative value with zero in numpy array

Python | Replace negative value with zero in numpy array

Given numpy array, the task is to replace negative value with zero in numpy array. Let’s see a few examples of this problem. 

Method #1: Naive Method 

Python3




# 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]

The time complexity of this code is O(n), where n is the size of the ini_array1. 

The auxiliary space complexity of this code is O(1), which means it uses a constant amount of extra space, regardless of the input size. 

Method #2: Using np.where 

Python3




# 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 #3: Using np.clip 

Python3




# 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]

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

Python3




# 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]

The time complexity of the given Python code is O(n), where n is the size of the input array ini_array1

The auxiliary space complexity of the code is O(n), as it creates a new array of the same size as the input array to store the 0 values.

Method #5: Using np.vectorize

You could use a lambda function to transform the elements of the array and replace negative values with zeros. This can be done using the NumPy vectorize function.

Python3




import numpy as np
 
# Initialize the array
arr = np.array([1, 2, -3, 4, -5, -6])
 
# Print the initial array
print("Initial array:", arr)
 
# Replace negative values with zeros using a lambda function
replace_negatives = np.vectorize(lambda x: 0 if x < 0 else x)
result = replace_negatives(arr)
 
# Print the resulting array
print("Resulting array:", result)
#This code is contributed by Edula Vinay Kumar Reddy


Output:

Initial array: [ 1  2 -3  4 -5 -6]
Resulting array: [1 2 0 4 0 0]
 

Time complexity: O(n) where n is the number of elements in the array
Auxiliary Space: O(n) as a new array with the transformed elements is created
 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments