Return the indices of the elements that are non-zero. x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]]) np.nonzero(x) (array([0, 1, 2, 2], dtype=int64), array([0, 1, 0, 1], dtype=int64)) Counts the number of non-zero values in the array a. np.count_nonzero(np.eye(4)) 4. import numpy as np x = np.array([2,5,1,9,0,3,8,11,-4,-3,-8,6,10]) Basic Indexing. Let’s do some simple slicing. Just a reminder, arrays are zero-indexed, so count starts from zero. x[0] will return the first element of the array and x[1] will return the second element of the array. x[0] #output: 2 x[3] #output: 9 x[4] #output: 0. Basic Slicing We iterated over each row of the 2D numpy array and for each row we checked if all elements in that row are zero or not, by comparing all items in that row with the 0. Find columns with only zeros in a matrix or 2D Numpy array # Check row wise result = np.all((arr_2d == 0), axis=0)

numpy.zeros¶ numpy.zeros (shape, dtype=float, order='C') ¶ Return a new array of given shape and type, filled with zeros. Parameters shape int or tuple of ints. Shape of the new array, e.g., (2, 3) or 2. dtype data-type, optional. The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64. order {‘C’, ‘F’}, optional, default: ‘C’