NumPy ndarray.nonzero()

The numpy.ndarray.nonzero() method returns the indices of non-zero elements in an array. It is useful for extracting positions where values are non-zero in a NumPy array.

Syntax

</>
Copy
ndarray.nonzero()

Parameters

This method does not take any parameters.

Return Value

Returns a tuple of arrays, one for each dimension of ndarray, containing the indices of non-zero elements.


Examples

1. Finding Non-Zero Elements in a 1D Array

In this example, we find the indices of non-zero elements in a one-dimensional NumPy array.

</>
Copy
import numpy as np

# Creating a 1D array
arr = np.array([0, 3, 0, 4, 5, 0, 7])

# Finding indices of non-zero elements
indices = arr.nonzero()

# Printing the result
print("Indices of non-zero elements:", indices)

Output:

Indices of non-zero elements: (array([1, 3, 4, 6]),)

The method returns a tuple containing an array of indices where the values are non-zero.

2. Finding Non-Zero Elements in a 2D Array

Here, we find the indices of non-zero elements in a two-dimensional NumPy array.

</>
Copy
import numpy as np

# Creating a 2D array
arr = np.array([[0, 1, 0], 
                [2, 0, 3], 
                [0, 4, 5]])

# Finding indices of non-zero elements
indices = arr.nonzero()

# Printing the results
print("Row indices of non-zero elements:", indices[0])
print("Column indices of non-zero elements:", indices[1])

Output:

Row indices of non-zero elements: [0 1 1 2 2]
Column indices of non-zero elements: [1 0 2 1 2]

The method returns two arrays: the first contains row indices, and the second contains column indices of non-zero elements.

3. Using Non-Zero Indices to Access Elements

We can use the indices returned by nonzero() to access non-zero elements directly.

</>
Copy
import numpy as np

# Creating a 2D array
arr = np.array([[0, 1, 0], 
                [2, 0, 3], 
                [0, 4, 5]])

# Getting indices of non-zero elements
indices = arr.nonzero()

# Extracting the non-zero values
non_zero_values = arr[indices]

# Printing non-zero values
print("Non-zero elements:", non_zero_values)

Output:

Non-zero elements: [1 2 3 4 5]

Using the returned indices, we directly extract the non-zero elements from the array.

4. Applying Non-Zero to a Condition-Based Array

We can use nonzero() with a condition to find indices of elements meeting specific criteria.

</>
Copy
import numpy as np

# Creating a numerical array
arr = np.array([[10, 25, 30], 
                [5, 0, 15], 
                [20, 35, 40]])

# Finding indices where elements are greater than 20
indices = (arr > 20).nonzero()

# Extracting the values that satisfy the condition
values = arr[indices]

# Printing indices and values
print("Row indices:", indices[0])
print("Column indices:", indices[1])
print("Values greater than 20:", values)

Output:

Row indices: [0 0 2 2]
Column indices: [1 2 1 2]
Values greater than 20: [25 30 35 40]

Here, we apply a condition to find elements greater than 20 and extract their indices and values.