Replace all numbers in a given array that is equal or greater than a given number

In this python numpy program, we will replace all numbers in a given array that is equal or greater than a given number

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Replace all numbers in a given array that is equal to or greater than a given number using Indexing and logic.
  4. Print the output,
				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])
print("Original array: ",x)
x[x >= 7] = 0
print("New matrix")
print(x)
				
			

Output :

				
					Original array:  [[ 6  5]
 [10  9]
 [ 8  7]]
New matrix
[[6 5]
 [0 0]
 [0 0]]
				
			

extract all numbers from a given array that are greater than a specified number.

swap rows of a given array using NumPy.

Leave a Comment