Python Numpy Library Examples


#1

numpy.where() method
numpy.where() function in Python returns the indices of items in the input array when the given condition is satisfied.

# Python program illustrating where() function  
  
import numpy as np 
  
# items array. 
items = np.array([[0, 10, 20], [30, 40, 50]]) 

# print items array
print(items) 
  
# now print item elements which are greater than 20
print ('All indices of elements >20') 
  
result = np.where(items>20) 
print(result) 
  
print("All elements which are >20") 
print(items[result]) 

numpy.zeroes() method
Numpy zeros np.zeros() function in python is used to get an array of given shape and type filled with zeros. You can pass three parameters inside function np.zeros shape, dtype and order. Numpy zeros function returns an array of the given shape.

import numpy as np 
  
first = np.zeros(2, dtype = int) 
print("Matrix first : \n", first) 
  
second = np.zeros([2, 2], dtype = int) 
print("\nMatrix second : \n", second)