3 Easy Sorting Techniques in NumPy

sorting techniques in NumPy

Hello, readers! In this article, we will be focusing on 3 Sorting techniques in NumPy, in detail.

So, let us begin! 🙂

Python NumPy library offers us various functions to create an array and manipulate elements of a similar type in an array structure. Along with this, NumPy offers us various functions that can enable us to sort the elements present in the array structure.

Sorting Techniques in NumPy

We’ll learn the below sorting techniques in NumPy.

  1. NumPy sort() function
  2. NumPy argsort() function
  3. NumPy lexsort() function

So, let us begin!

1. NumPy sort() function

In order to sort the various elements present in the array structure, NumPy provides us with sort() function. With sort() function, we can sort the elements and segregate them in ascending to descending order, respectively.

Have a look at the below syntax!

Syntax:

numpy.sort(array, axis)

The parameter ‘axis’ specifies the manner in which sorting needs to be performed. So when we set axis = NONE, the sorting occurs in the traditional fashion, and the resultant array is a single row of elements. On the other hand, if we set axis = 1, the sorting occurs in a row-wise fashion, that is each and every row gets sorted individually.

Example 1:

In this example, we have created an array, further we have sorted the array using the sort() function and with axis = NONE i.e. it sorts the elements in ascending order.

import numpy as np
data = np.array([[22, 55], [0, 10]])
res = np.sort(data, axis = None)        
print ("Data before sorting:", data)
print("Data after sorting:", res)

Output:

Data before sorting: [[22 55]
 [ 0 10]]
Data after sorting: [ 0 10 22 55]

Example 2:

In this example, we have created an array and sorted the same using sort() function, here we have set axis = 1 i.e. row wise sorting has been performed.

import numpy as np
data = np.array([[66, 55, 22], [0, 10, -1]])
res = np.sort(data, axis = 1)        
print ("Data before sorting:", data)
print("Row wise sorting:", res)

Output:

Data before sorting: [[66 55 22]
 [ 0 10 -1]]
Row wise sorting: [[22 55 66]
 [-1  0 10]]

2. NumPy argsort()

Apart from the sort() method, we also have argsort() function that is used as a sorting techniques in NumPy which returns an array of indices of the sorted elements. From those sorted index values, we can get the sorted array elements in ascending order.

Thus, with argsort() function, we can sort the array values and get the index values of the same as a separate array.

Example:

import numpy as np
data = np.array([66, 55, 22,11,-1,0,10])
res_index = np.argsort(data)        
print ("Data before sorting:", data)
print("Sorted index values of the array:", res_index)

x = np.zeros(len(res_index), dtype = int)
for i in range(0, len(x)):
    x[i]= data[res_index[i]]
print('Sorted array from indexes:', x)

Output:

In the above example, we have performed argsort() function on the data values and have obtained sorted index values of the elements. Further, we have utilized the same array index values to get the sorted array elements.

Data before sorting: [66 55 22 11 -1  0 10]
Sorted index values of the array: [4 5 6 3 2 1 0]
Sorted array from indexes: [-1  0 10 11 22 55 66]

3. NumPy lexsort() function

The lexsort() function enables us to sort the data values using sequence of keys i.e. by columns. With lexsort() function, we sort the two arrays taking one at a time into consideration. As a result, we get the index values of the sorted elements.

import numpy as np
data = np.array([66, 55, 22,11,-1,0,10])
data1 = np.array([1,2,3,4,5,0,-1])
res_index = np.lexsort((data1, data))        
print("Sorted index values of the array:", res_index)

Output:

Sorted index values of the array: [4 5 6 3 2 1 0]

Conclusion

Feel free to comment below, in case you come across any question. For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!!