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
- Import the numpy library as np.
- Create an array using np.array().
- Replace all numbers in a given array that is equal to or greater than a given number using Indexing and logic.
- 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]]