Numpy nonzero() Function



The Numpy nonzero() function is used to return the indices of the non-zero elements in a array. It can be used to quickly locate the positions of non-zero elements, which is helpful in various operations, such as masking or extracting specific elements from the array. The function works with arrays of any shape (1D, 2D, etc.).

In the Numpy nonzero() function, the result is a tuple of arrays, one for each dimension of the input array a, containing the indices of the non-zero elements along that dimension. The indices are returned in a row-major (C-style) order, ensuring that the first array in the tuple represents the row indices, the second array represents the column indices, and so on for higher-dimensional arrays.

Syntax

Following is the syntax of the Numpy nonzero() function −

numpy.nonzero(a)

Parameters

Following is the parameter of the Numpy nonzero() function −

  • a: The input array. It can be of any shape and contains both zero and non-zero elements.

Return Type

This function returns indices of elements that are non-zero in the input array.

Example

Following is a basic example to find the indices of non-zero elements in an array using the Numpy nonzero() function −

import numpy as np
my_array = np.array([3, 0, 0, 5, 6, 0])
nonzero_indices = np.nonzero(my_array)
print("Original Array:", my_array)
print("Indices of Non-zero Elements:", nonzero_indices)

Output

Following is the output of the above code −

Original Array: [3 0 0 5 6 0]
Indices of Non-zero Elements: (array([0, 3, 4]),)

Example: Non-zero Indices in a 2D Array

The numpy.nonzero() function works with multi-dimensional arrays as well. It returns a tuple of arrays containing indices of non-zero elements along each dimension.

In the following example, we find the non-zero indices in a 2D array of shape (3,3) and the indices of the non-zero elements are returned using numpy.nonzero()

import numpy as np
my_array = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
nonzero_indices = np.nonzero(my_array)
print("Original Array:\n", my_array)
print("Indices of Non-zero Elements:", nonzero_indices)

Output

Following is the output of the above code −

Original Array:
 [[3 0 0]
  [0 4 0]
  [5 6 0]]
Indices of Non-zero Elements: (array([0, 1, 2, 2]), array([0, 1, 0, 1]))

Example: Finding Indices from a Boolean Array

Here, we have used the Numpy nonzero() function to find the indices of elements greater than 3 in a 2D array. The condition a > 3 creates a boolean array, and nonzero() returns the indices where the condition is True −

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Boolean_Array = a > 3
print("Boolean Array:\n", Boolean_Array)
result = np.nonzero(Boolean_Array)
print("Indices of elements greater than 3:\n", result)

Output

Following is the output of the above code −

Boolean Array:
 [[False False False]
 [ True  True  True]
 [ True  True  True]]
Indices of elements greater than 3:
 (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
numpy_array_manipulation.htm
Advertisements