|
| 1 | +--- |
| 2 | +Title: '.nonzero()' |
| 3 | +Description: 'Returns the indices of the non-zero values in a given array.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Functions' |
| 10 | + - 'NumPy' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-python-3' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`.nonzero()`** function identifies and returns the indices of the non-zero elements in a NumPy array. This 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. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```psuedo |
| 21 | +numpy.nonzero(a) |
| 22 | +``` |
| 23 | + |
| 24 | +**Parameters:** |
| 25 | + |
| 26 | +- `a`: The input array. |
| 27 | + |
| 28 | +**Return value:** |
| 29 | + |
| 30 | +The `.nonzero()` function returns a tuple of multiple arrays, where each array represents a particular dimension of the input array. |
| 31 | + |
| 32 | +## Example 1: Using `.nonzero()` on a 1D Array |
| 33 | + |
| 34 | +This example demonstrates how to find the non-zero indices of a one-dimensional NumPy array and print the array of the non-zero values: |
| 35 | + |
| 36 | +```py |
| 37 | +import numpy as np |
| 38 | + |
| 39 | +# Create a 1D array |
| 40 | +array1 = np.array([0, 1, 0, 2, 3, 0, 0, 4, 0, 5]) |
| 41 | + |
| 42 | +# Compute the indices of the non-zero values and then the array of the non-zero values |
| 43 | +nonzero_indices = np.nonzero(array1) |
| 44 | +no_zero_array1 = array1[nonzero_indices] |
| 45 | + |
| 46 | +print("Array:", array1) |
| 47 | +print("Non-zero Indices:", nonzero_indices) |
| 48 | +print("Non-zero Values:", no_zero_array1) |
| 49 | +``` |
| 50 | + |
| 51 | +The output for this example will be: |
| 52 | + |
| 53 | +```shell |
| 54 | +Array: [0 1 0 2 3 0 0 4 0 5] |
| 55 | +Non-zero Indices: (array([1, 3, 4, 7, 9]),) |
| 56 | +Non-zero Values: [1 2 3 4 5] |
| 57 | +``` |
| 58 | + |
| 59 | +## Example 2: Using `.nonzero()` on a 2D Array |
| 60 | + |
| 61 | +This example demonstrates how to find the non-zero indices of a two-dimensional NumPy array and print the array of the nonzero values: |
| 62 | + |
| 63 | +```py |
| 64 | +import numpy as np |
| 65 | + |
| 66 | +# Create a 2D array |
| 67 | +array2 = np.array([[1, 2], [0, 3]]) |
| 68 | + |
| 69 | +# Compute the indices of the non-zero values and then the array without the zeros |
| 70 | +nonzero_indices = np.nonzero(array2) |
| 71 | +no_zero_array2 = array2[nonzero_indices] |
| 72 | + |
| 73 | +print("Array:", array2) |
| 74 | +print("Non-zero Indices:", nonzero_indices) |
| 75 | +print("Non-zero Values:", no_zero_array2) |
| 76 | +``` |
| 77 | + |
| 78 | +The outut for this example will be: |
| 79 | + |
| 80 | +```shell |
| 81 | +Array: [[1 2] |
| 82 | + [0 3]] |
| 83 | +Non-zero Indices: (array([0, 0, 1]), array([0, 1, 1])) |
| 84 | +Non-zero Values: [1 2 3] |
| 85 | +``` |
0 commit comments