Built with Tutify.io

    AIStudyLabs

    How to Use NumPy Argsort() in Python

    How to Use NumPy Argsort() in Python

    NumPy argsort() function in Python is used to calculate an indirect sort along the specified axis using the algorithm specified by the kind keyword. It returns an index of an array of elements of the same shape as arr that would sort the array. Note that this doesn't sort the elements of the array instead it just returns the indices of the sorted elements.

    In this article, I will explain Python NumPy argsort() function syntax and using its parameters how you can get the indices of sorted elements of an array.

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

    1. Quick Examples of Argsort Function

    If you are in a hurry, below are some quick examples of Python numpy.argsort() function.

    
    # Quick examples of argsort function
    
    # Example 1: Get the argsort() of 1-D array
    arr = np.array([6, 8, 3, 9, 5, 7])
    arr1 = np.argsort(arr)
    
    # Example 2: Get the argsort() 1-D array 
    # In descending order
    arr = np.array([6, 8, 3, 9, 5, 7])
    arr1 = np.argsort(arr)[::-1]
    
    # Example 3: Compute argsort of 2-D array 
    # Along axis = 0
    arr = np.array([[6, 8, 3],[ 9, 5, 7]]) 
    arr1 = np.argsort(arr, axis = 0)
    
    # Example 4: Compute argsort of 2-D array 
    # Along axis = 1
    arr = np.array([[6, 8, 3],[ 9, 5, 7]]) 
    arr1 = np.argsort(arr, axis = 1)
    
    # Example 5: Using 'quicksort' algorithm
    # Get the indices that would sort each row 
    arr = np.array([[6, 8, 3],[ 9, 5, 7]]) 
    arr1 = np.argsort(arr, kind ='quicksort', axis = 1)
    
    # Example 6: Using 'heapsort' algorithm
    # Get the indices that would sort each column 
    arr = np.array([[6, 8, 3],[ 9, 5, 7]]) 
    arr2 = np.argsort(arr, axis=0, kind='heapsort')
    

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

    2. Syntax of argsort()

    Following is the syntax of argsort().

    
    # Syntax of argsort()
    numpy.argsort(arr, axis=- 1, kind=None, order=None)
    

    <a id="h-2-1-parameters-of-argsort-in"></a>

    2.1 Parameters of argsort() in

    Following are the parameters of the argsort() function.

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

    2.2 Return Value of argsort()

    It returns the indices of array elements that would sort an array.

    <a id="h-3-usage-of-numpy-argsort-function"></a>

    3. Usage of NumPy argsort() Function

    The numpy.argsort() is available in the NumPy module package in Python and it returns the indices that would sort an array by performing an indirect sort along the given axis using the algorithm specified by the kind keyword. In Numpy Python argsort() means to sort the elements of an array with the given axis of the same shape.

    You can create a NumPy array using the numpy.array() function and then use the numpy.argsort() function to obtain the indices of the sorted elements. For example, the numpy.argsort() function returns the indices [2, 4, 5, 0, 1, 3] which correspond to the sorted elements of the original array [3, 5, 7, 6, 8, 9].

    
    # Import numpy
    import numpy as np  
    
    # Create NumPy 1-D array
    arr = np.array([6, 8, 3, 9, 5, 7])
    print("Original array:",arr)
    
    # Get the argsort of 1-D array
    arr1 = np.argsort(arr)
    print("Sorted indices:",arr1)
    

    Yields below output.

    <a id="h-4-get-the-numpy-argsort-in-descending-order"></a>

    4. Get the NumPy argsort() in Descending Order

    You are using numpy.argsort() to get the indices that would sort the input array in ascending order and then reversing these indices to obtain the descending order.

    By default argsort() function sorts the NumPy array elements in ascending order and returns their corresponding indices, moreover, you can easily sort the indices of a given array in descending order. Along with the syntax [::-1]. For example, arr1 contains the indices that would sort the arr array in descending order, elements 9, 8, 7, 6, 5, and 3.

    
    import numpy as np  
    
    # Create NumPy 1-D array
    arr = np.array([6, 8, 3, 9, 5, 7])
    print("Original array:",arr)
    
    # Get the argsort 1-D array 
    # In decending order
    arr1 = np.argsort(arr)[::-1]
    print("Sorted indices in descending order:",arr1)
    

    Yields below output.

    <a id="h-5-argsort-of-two-dimensional-array"></a>

    5. Argsort of Two-Dimensional Array

    You can also apply argsort() with a two-dimensional array along with the specified axis, for that, you have to pass the axis parameter along with the array. Now you can specify axis=0, then this function will return the column-wise indices of the sorted array. For instance, arr1 contains the indices that would sort each column of the arr array in ascending order.

    
    # Initialize 2-D numpy array
    arr = np.array([[6, 8, 3],[ 9, 5, 7]]) 
    print("Original array:\n",arr)
    
    # Compute argsort of 2-D array 
    # Along axis = 0
    arr1 = np.argsort(arr, axis = 0)
    print("Sorted indices along columns:\n",arr1)
    
    # Output:
    # Original array:
    #  [[6 8 3]
    #  [9 5 7]]
    # Sorted indices along columns:
    #  [[0 1 0]
    #  [1 0 1]]
    

    To specify axis=1, then this function will return the row-wise indices of the sorted array. For example, arr1 contains the indices that would sort each row of the arr array in ascending order.

    
    # Initialize 2-D numpy array
    arr = np.array([[6, 8, 3],[ 9, 5, 7]]) 
    print("Original array:\n",arr)
    
    # Compute argsort of 2-D array 
    # Along axis = 1
    arr1 = np.argsort(arr, axis = 1)
    print("Sorted indices along rows:\n",arr1)
    
    # Output:
    # Original array:
    #  [[6 8 3]
    #  [9 5 7]]
    # Sorted indices along rows:
    #  [[2 0 1]
    #  [1 2 0]]
    

    <a id="h-6-argsort-of-two-dimensional-array-along-with-kind-parameter"></a>

    6. Argsort of Two-Dimensional Array Along with Kind Parameter

    You can use the kind parameter with numpy.argsort() to specify the sorting algorithm you want to use. For example, kind='quicksort' is used for sorting rows, and kind='heapsort' is used for sorting columns. You can choose from different sorting algorithms like 'quicksort', 'mergesort', and 'heapsort' based on your specific requirements. The kind parameter allows you to control the sorting algorithm used by numpy.argsort().

    
    # Initialize 2-D numpy array
    arr = np.array([[6, 8, 3],[ 9, 5, 7]]) 
    print("Original array:\n",arr)
    
    # Using 'quicksort' algorithm
    # Get the indices that would sort each row 
    arr1 = np.argsort(arr, kind ='quicksort', axis = 1)
    print("Sorted indices along rows using 'quicksort' algorithm:\n",arr1)
    
    # Output:
    # Sorted indices along rows using 'quicksort' algorithm:
    #  [[2 0 1]
    #  [1 2 0]]
    
    # Using 'heapsort' algorithm
    # Get the indices that would sort each column 
    arr2 = np.argsort(arr, axis=0, kind='heapsort')
    print("Sorted indices along columns using 'heapsort' algorithm:\n", arr2)
    
    # Output:
    # Sorted indices along columns using 'heapsort' algorithm:
    #  [[0 1 0]
    #  [1 0 1]]
    

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

    Frequently Asked Questions

    numpy.argsort() is a function provided by the NumPy library in Python. It performs an indirect sort on an input array, returning an array of indices that would sort the input array. In simpler terms, it returns the indices that would arrange the elements of the input array in ascending order along the specified axis.

    You can use numpy.argsort() to sort a 1-D array by obtaining the indices that would sort the array in ascending order.

    You can sort a 2-D array using numpy.argsort() by specifying the axis parameter. Sorting along rows or columns can be achieved by setting the axis parameter accordingly.

    You can sort a 2-D array in descending order using numpy.argsort(). The key is to first get the sorted indices in ascending order, and then reverse these indices to obtain the descending order.

    You can specify a sorting algorithm when using numpy.argsort() by using the kind parameter. The kind parameter allows you to choose the sorting algorithm you want to use. The available options are 'quicksort', 'mergesort', and 'heapsort'.

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

    Conclusion

    In this article, I have explained how to use Python NumPy argsort() function to return the indexes of the sorted array and also learned how to sort by descending order.

    Happy Learning!!

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

    Related Articles

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

    References