ADVERTISEMENT
ADVERTISEMENT

NumPy -  Sorting

NumPy, a Python package, can perform many mathematical operations on arrays in addition to scientific computing. It can sort a variety of things. Sorting is the process of organising data in specific order. Arranged data helps us  to understand and compute better.

NumPy Sorting Methods

  1. np.sort():

The np.sort() function is a simple sorting function that sorts the elements in an array in an ascending order.

Example:

import numpy as np

arr = np.array([3, 2, 0, 1])
print(np.sort(arr))  # Output: [0 1 2 3]

 

  1. Order Function:

The order parameter in sort function is used to impose a specific sort order. If the array is structured, it will be sorted according to the fields.

Example:

import numpy as np

dt = np.dtype([('name', 'S10'), ('age', int)])
arr = np.array([("John", 20), ("catie", 30), ("Bob", 22)], dtype=dt)
print(np.sort(arr, order='name'))
# Output: [(b'Bob', 22) (b'John', 20) (b'catie', 30)]

 Please note that the actual output of byte strings in python3 looks like this: b'John'

  1. Axis Sorting:

NumPy allows you to sort arrays along specific axes using the axis parameter in the sort function. By default, sort() doesn't consider the axis.

Example:

import numpy as np

arr = np.array([[3, 2], [0, 1]])
print(np.sort(arr, axis=0))
# Output: [[0 1] [3 2]]
  1. np.argsort():

The np.argsort() function performs an indirect sort on input array, along the given axis and using a specified kind of sort to return the array of indices of data. This indices array is used to construct the sorted array.

Example:

import numpy as np

arr = np.array([3, 2, 0, 1])
print(np.argsort(arr))  # Output: [2 3 1 0]
  1. np.lexsort():

The np.lexsort() function performs an indirect sort using a sequence of keys. The keys can be seen as a column in a spreadsheet. The function returns an array of indices, using which the sorted data can be obtained. Note that the last key happens to be the primary key of sort.

Example:

import numpy as np

first_name = ('Bob', 'Jane', 'John')
last_name = ('Johnson', 'Adams', 'Stewart')

ind = np.lexsort((first_name, last_name))  # sort by last_name, then by first_name
print([last_name[i] + ", " + first_name[i] for i in ind])
# Output: ['Adams, Jane', 'Johnson, Bob', 'Stewart, John']

 


ADVERTISEMENT

ADVERTISEMENT