From 340b01d1fd74f1b07fc600fd866ec7a24f752165 Mon Sep 17 00:00:00 2001 From: Haritha Yerukonda Date: Fri, 7 Nov 2025 10:30:57 +0000 Subject: [PATCH 1/2] docs: add nonzero() term under NumPy ndarray concept --- .../concepts/ndarray/terms/nonzero/nonzero.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/nonzero/nonzero.md diff --git a/content/numpy/concepts/ndarray/terms/nonzero/nonzero.md b/content/numpy/concepts/ndarray/terms/nonzero/nonzero.md new file mode 100644 index 00000000000..0faf2633b52 --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/nonzero/nonzero.md @@ -0,0 +1,39 @@ +--- +Title: nonzero() +Concept: ndarray +--- + +# nonzero() + +The `nonzero()` method in NumPy returns the indices of the array elements that are non-zero (or `True` when the array is treated as a boolean). It is often used to identify or extract the locations of non-zero elements in an array. + +## Syntax + +```python +numpy.nonzero(a) + +```markdown +## Example + +```python +import numpy as np + +a = np.array([[0, 2, 0], + [3, 0, 4]]) + +result = np.nonzero(a) +print(result) + +```markdown +## Codebyte + +```codebyte/python +import numpy as np + +a = np.array([[0, 5, 0], + [7, 0, 9]]) + +rows, cols = a.nonzero() + +for r, c in zip(rows, cols): + print(f"Non-zero element {a[r, c]} found at position ({r}, {c})") From 045cdf81d04f0da23f7e5dd948a7c052c364685c Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 10 Nov 2025 12:14:26 +0530 Subject: [PATCH 2/2] Update nonzero.md with example descriptions, meta data, and backlinks --- .../concepts/ndarray/terms/nonzero/nonzero.md | 59 +++++++++++++------ 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/nonzero/nonzero.md b/content/numpy/concepts/ndarray/terms/nonzero/nonzero.md index 0faf2633b52..3c45c8f2323 100644 --- a/content/numpy/concepts/ndarray/terms/nonzero/nonzero.md +++ b/content/numpy/concepts/ndarray/terms/nonzero/nonzero.md @@ -1,31 +1,55 @@ --- -Title: nonzero() -Concept: ndarray +Title: 'nonzero()' +Description: 'Returns the indices of the array elements that are non-zero.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Array' + - 'Data' + - 'NumPy' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' --- -# nonzero() - -The `nonzero()` method in NumPy returns the indices of the array elements that are non-zero (or `True` when the array is treated as a boolean). It is often used to identify or extract the locations of non-zero elements in an array. +The **`numpy.ndarray.nonzero()`** method returns the indices of elements in an [array](https://www.codecademy.com/resources/docs/numpy/ndarray) that are non-zero or evaluate to `True`. It is often used to locate positions of meaningful or valid data within an array. ## Syntax -```python -numpy.nonzero(a) +```pseudo +ndarray.nonzero() +``` -```markdown -## Example +**Parameters:** -```python -import numpy as np +This method takes no parameters. + +**Return value:** + +Returns a [tuple](https://www.codecademy.com/resources/docs/python/tuples) of arrays, one for each dimension, containing the indices of elements that are non-zero. + +## Example: Finding Non-Zero Elements in a 1D Array -a = np.array([[0, 2, 0], - [3, 0, 4]]) +In this example, the indices of all non-zero elements in a 1D NumPy array are returned: -result = np.nonzero(a) +```py +import numpy as np + +arr = np.array([0, 2, 0, 4, 5]) +result = arr.nonzero() print(result) +``` + +The output of this code is: + +```shell +(array([1, 3, 4]),) +``` + +## Codebyte Example -```markdown -## Codebyte +In this example, the positions of non-zero elements in a 2D array are identified: ```codebyte/python import numpy as np @@ -36,4 +60,5 @@ a = np.array([[0, 5, 0], rows, cols = a.nonzero() for r, c in zip(rows, cols): - print(f"Non-zero element {a[r, c]} found at position ({r}, {c})") + print(f"Non-zero element {a[r, c]} found at position ({r}, {c})") +```