Python NumPy nonzero() Function

Spread the love

Python NumPy nonzero() function is used to return the indices (the index numbers or index positions) of the elements of an array that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension. In this article, I will explain numpy.nonzero() function and use this syntax, parameters, and return indices of elements that are non-zero with examples.

1. Quick Examples of Python NumPy nonzero() Function

If you are in a hurry, below are some quick examples of how to use Python NumPy nonzero() function. For more creators of ndarray refer NumPy Tutorial.


# Below are the quick examples

# Example 1: Indices of non zero elements 
arr = np.array([8, 23, 45, 0, 60, 0, 85, 0, 101])
arr2 = np.nonzero(arr)

# Example 2: All nonzero elements
arr3 = arr[arr2]

# Example 3: Index of all elements which are greater than 50
arr2 = np.nonzero(arr > 50)

# Example 4: Use nonzero() function to 2-d array 
arr = np.array([[4, 0, 0], [0, 6, 0], [-7, 15, 0]])
arr2 = np.nonzero(arr)

# Example 5: Get the corresponding non-zero number                
arr3 = arr[np.nonzero(arr)]

# Example 6: Use numpy.transpose() to indices of non-zero number 
arr = np.array([[4, 0, 0], [0, 6, 0], [-7, 15, 0]])             
arr3 = np.transpose(np.nonzero(arr))

2. Syntax of Python numpy.nonzero()

Following is the syntax to create numpy.nonzero() function.


# Python numpy.nonzero() Syntax
numpy.nonzero(arr)

2.1 Parameters of NumPy nonzeros()

  • arr : Input array.

2.2 Return Value of nonzeros()

It returns Indices of elements that are non-zero.

3. Get Indices of nonzero Elements Use nonzero()

We can use NumPy nonzero() function is used to get index of all elements which are non-zero.


import numpy as np

# Create one-dimensional array with nonzero
arr = np.array([8, 23, 45, 0, 60, 0, 85, 0, 101])

# Indices of non zero elements  
arr2 = np.nonzero(arr)
print (arr2) 

# Output:
# (array([0, 1, 2, 4, 6, 8], dtype=int64),)

# All nonzero elements
arr3 = arr[arr2]
print(arr3)

# Output:
# [  8  23  45  60  85 101]

# Index of all elements which are greater than 50
arr2 = np.nonzero(arr > 50)
print (arr2) 

# Output:
# (array([4, 6, 8], dtype=int64),)

4. Use nonzero() Function to 2-D Array


import numpy as np

# Create two-dimensional array with nonzero
arr = np.array([[4, 0, 0], [0, 6, 0], [-7, 15, 0]])

# Use nonzero() function to 2-d array 
arr2 = np.nonzero(arr)
print (arr2) 

# Output:
(array([0, 1, 2, 2], dtype=int64), array([0, 1, 0, 1], dtype=int64))

# Get the corresponding non-zero number                
arr3 = arr[np.nonzero(arr)]
print(arr3) 

# Output:
# [ 4  6 -7 15]

5. Use numpy.transpose() Function to Indices of non-zero Number


# Use numpy.transpose() function to indices of non-zero number 
arr = np.array([[4, 0, 0], [0, 6, 0], [-7, 15, 0]])             
arr3 = np.transpose(np.nonzero(arr))
print(arr3) 

# Output:
# [[0 0]
#  [1 1]
#  [2 0]
# [2 1]]

6. Conclusion

In this article, I have explained how to use Python NumPy nonzero() function using indices of elements that are non-zero with examples.

Happy Learning!!

References

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing Python NumPy nonzero() Function