Sunday, May 31, 2020

Numpy sorting ndarray

NumPy sorting arrays

The sort() method

We can sort arrays(Numpy ndarrays) very easily using the sort() method. The sorting operation are used to place the items in some defined order such as ascending, descending, alphabetically, etc.

The syntax for this method is,

numpy.sort(ary, axis=-1, type='quicksort', order=None)

The parameters are,
ary : Array to be sorted.

axis : int or None, optional
Axis along which to sort. If None, the array is flattened before sorting. 
The default is -1, which sorts along the last axis.

type : {'quicksort', 'mergesort', 'heapsort'}, optional
Sorting algorithm. By default 'quicksort'.

order : list, optional
When ary is a structured array, this argument specifies which 
fields to compare first, second, and so on.
This list does not need to include all of the fields.
This method returns ndarray of the same size and shape in sorted order.

For example, simply sorting arrays in ascending order

import numpy as np

nums = np.array([16,11,22,12,16,42,17,55,12,12])

langs = np.array(['Python','Java','C','C++','Fortran','Pascal','Lisp']);

bools = np.array([True, False,True, False])

[11 12 12 12 16 16 17 22 42 55]
['C' 'C++' 'Fortran' 'Java' 'Lisp' 'Pascal' 'Python']
[False False  True  True]

Sorting a 2-D array

import numpy as np

sortednums=np.sort(nums, axis=1)

sortednums=np.sort(nums, axis=None)

sortednums=np.sort(nums, axis=0)

 [[11 16 22]
 [12 16 42]
 [12 17 55]]
 [11 12 12 16 16 17 22 42 55]
 [[12 11 12]
 [16 16 22]
 [17 55 42]]

Sorting by defined order

We can specify the field name of the Numpy ndarray dtype by which we want to sort the array, against the order keyword in method, for customized sort operation. For example,

import numpy as np
dtype = [('name', 'S10'), ('height', float), ('age', int)]
values = [('Shiva', 1.8, 41), ('Akshar', 1.9, 38),
('Virat', 1.7, 38)]
a = np.array(values, dtype=dtype)  #create a structured array
#sort by name
print('by name===========\n',np.sort(a, order='name'))     
#sort by age
print('by age===========\n',np.sort(a, order='age'))    

by name===========
 [(b'Akshar', 1.9, 38) (b'Shiva', 1.8, 41) (b'Virat', 1.7, 38)]
by age===========
 [(b'Akshar', 1.9, 38) (b'Virat', 1.7, 38) (b'Shiva', 1.8, 41)]