Skip to content

Created nonzero.md #6665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions content/numpy/concepts/built-in-functions/terms/nonzero/nonzero.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
Title: '.nonzero()'
Description: 'Returns the indices of the nonzero elements in a given array.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Arrays'
- 'Functions'
- 'NumPy'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The **`.nonzero()`** function function identifies and returns the indices of the non-zero elements in a NumPy array. The `.nonzero()` function is commonly used in data preprocessing and analysis to filter out or extract meaningful, non-zero elements from datasets. It is also valuable in sparse matrix operations and machine learning workflows, where zero values often represent missing, default, or irrelevant data that needs to be handled separately.

## Syntax

```psuedo
numpy.nonzero(a)
```

**Parameters:**

- `a`: The input array.

**Return value:**

The `.nonzero()` function returns a tuple of arrays. There's one array for each dimension of the input array.

## Example 1: Using `.nonzero()` with a 1D Array

The following demonstrates how to find the nonzero indices of a one-dimensional NumPy array and print the array of the nonzero values:

```py
import numpy as np

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

# Compute the indices of the nonzero values and then the array of the nonzero values
nonzero_indices = np.nonzero(array1)
no_zero_array1 = array1[nonzero_indices]

print("Array:", array1)
print("Nonzero Indices:", nonzero_indices)
print("Nonzero Values:", no_zero_array1)
```

This example results in the following output:

```shell
Array: [0 1 0 2 3 0 0 4 0 5]
Nonzero Indices: (array([1, 3, 4, 7, 9]),)
Nonzero Values: array([1, 2, 3, 4, 5])
```

## Example 2: Using `.nonzero()` with a 2D Array

The following demonstrates how to find the nonzero indices of a two-dimensional NumPy array and print the array of the nonzero values:

```py
import numpy as np

# Create a 2D array
array2 = np.array([[1, 2], [0, 3]])

# Compute the indices of the nonzero values and then the array without the zeros
nonzero_indices = np.nonzero(array2)
no_zero_array2 = array2[nonzero_indices]

print("Array:", array2)
print("Nonzero Indices:", nonzero_indices)
print("Nonzero Values:", no_zero_array2)
```

This example results in the following output:

```shell
Array: [[1 2]
[0 3]]
Nonzero Indices: (array([0, 0, 1]), array([0, 1, 1]))
Nonzero Values: [1, 2, 3]
```