Built with Tutify.io

    AIStudyLabs

    Python NumPy nonzero() Function

    Python NumPy nonzero() Function

    In NumPy, the nonzero() function is used to return the indices (the index numbers or index positions) of the elements that are non-zero in a given array. It is particularly useful when you want to find the indices of non-zero elements in an array without actually iterating through the array manually. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension.

    <a id="h-1-quick-examples-of-numpy-nonzero-function"></a>

    1. Quick Examples of 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.

    
    # Quick examples of nonzero() function
    
    # 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() 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))
    

    <a id="h-2-syntax-of-numpy-nonzero"></a>

    2. Syntax of NumPy nonzero()

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

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

    <a id="h-2-1-parameters-of-numpy-nonzeros"></a>

    2.1 Parameters of NumPy nonzeros()

    <a id="h-2-2-return-value"></a>

    2.2 Return Value

    It returns Indices of elements that are non-zero.

    <a id="h-3-get-indices-of-nonzero-elements-use-nonzero"></a>

    3. Get Indices of nonzero Elements Use nonzero()

    If you want to directly get the indices of non-zero elements using numpy.nonzero() function. Here, arr2 is a tuple containing one array, which represents the indices of the non-zero elements in the original array arr. The output indicates that non-zero elements are at positions 0, 1, 2, 4, 6, and 8 in the array. You can also use NumPy nonzero() function to get the index of all elements which are non-zero.

    
    # Import numpy
    import numpy as np
    
    # Create one-dimensional array with nonzero
    arr = np.array([8, 23, 45, 0, 60, 0, 85, 0, 101])
    print("Original array:\n",arr)
    
    # Indices of non zero elements  
    arr2 = np.nonzero(arr)
    print("Indices of non-zero elements:\n",arr2)
    
    # Output:
    # Original array:
    #  [  8  23  45   0  60   0  85   0 101]
    # Indices of non-zero elements:
    # (array([0, 1, 2, 4, 6, 8], dtype=int64),)
    
    # All nonzero elements
    arr3 = arr[arr2]
    print("All nonzero elements:\n", arr3)
    

    Yields below output.

    To find the indices of elements in a NumPy array that are greater than 50, you can use boolean indexing. Here, arr2 is a tuple containing one array, which represents the indices of elements in the original array arr that are greater than 50. The output indicates that elements greater than 50 are at positions 4, 6, and 8 in the array.

    
    # Index of all elements which are greater than 50
    arr2 = np.nonzero(arr > 50)
    print("Indices of elements greater than 50:\n",arr2)
    
    # Output:
    # Indices of elements greater than 50:
    # (array([4, 6, 8], dtype=int64),)
    

    <a id="h-4-use-nonzero-function-to-2-d-array"></a>

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

    You can use the numpy.nonzero() function to find the indices of non-zero elements in a two-dimensional NumPy array. For instance, the first array represents the row indices, and the second array represents the column indices of the non-zero elements in the original two-dimensional array arr.

    
    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("Indices of non-zero elements:\n",arr2)
    
    # Output:
    Indices of non-zero elements:
    (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("All nonzero elements:\n", arr3) 
    
    # Output:
    # All nonzero elements:
    #  [ 4  6 -7 15]
    

    If you want to access the non-zero elements directly, you can use these indices. Now, arr3 contains the values of the non-zero elements in the original two-dimensional array.

    <a id="h-5-use-numpy-transpose-function-to-indices-of-non-zero-number"></a>

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

    To use numpy.transpose() to get the indices of non-zero elements in a 2D array, you can first use numpy.nonzero() to obtain the indices and then transpose the result.

    In the below example, arr2 is a tuple containing two arrays representing the row and column indices of non-zero elements. Using numpy.transpose(), you transpose these indices, so each row now represents a pair of (row, column) indices for the non-zero elements in the original array.

    
    # Create two-dimensional array with nonzero
    arr = np.array([[4, 0, 0], [0, 6, 0], [-7, 15, 0]])
    
    # Use numpy.transpose() function to indices of non-zero number 
    arr2 = np.transpose(np.nonzero(arr))
    print("Transposed indices of non-zero elements:\n",arr2)
    
    # Output:
    # Transposed indices of non-zero elements:
    #  [[0 0]
    #  [1 1]
    #  [2 0]
    #  [2 1]]
    

    <a id="h-frequently-asked-questions"></a>

    Frequently Asked Questions

    The numpy.nonzero() function in NumPy returns the indices of elements that are non-zero in an array. It is particularly useful for finding the positions of non-zero elements without manually iterating through the array.

    For multi-dimensional arrays, numpy.nonzero() returns a tuple of arrays, where each array corresponds to the indices along a particular dimension of the non-zero elements.

    You can use numpy.nonzero() in combination with boolean indexing to find the indices of elements that are greater than a certain value. For example, arr > 10 creates a boolean array where each element is True if the corresponding element in arr is greater than 10, and False otherwise. np.nonzero() then returns the indices of the True values, giving you the indices of elements greater than 10 in the original array.

    To access the non-zero elements using the indices obtained from numpy.nonzero(), you can use the indices to index the original array.

    You can use numpy.transpose() to swap the row and column indices obtained from numpy.nonzero(). This can be useful for certain applications, such as when you want to transform the indices for further processing.

    numpy.nonzero() works for arrays of any data type. It is designed to find the indices of non-zero elements in an array regardless of the data type. Whether your array contains integers, floats, or other types, numpy.nonzero() will behave the same way.

    <a id="h-conclusion"></a>

    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!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References