Numpy.sort() function – How to sort arrays using numpy in python

Spread the love

In this program, you will learn how to write a program to sort arrays using numpy.

First we need to import the numpy library then we have to use numpy.sort function to sort the values.

Syntax: numpy.sort(a, axis=-1, kind=’quicksort’, order=None)
This function return a sorted copy of an array.

Parameters:
a : array_like
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.

kind : {‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional
Sorting algorithm. Default is ‘quicksort’.

order : str or list of str, optional
When a is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties.

Returns:
sorted_array : ndarray
Array of the same type and shape as a.

Sorting first axis using numpy:

import numpy as np # sort along the first axis a = np.random.rand(3,2) print('array values before sort',a) arr1 = np.sort(a, axis = 0) print ("\nAlong first axis : \n", arr1)

Output:

array values before sort 
[[0.698065 0.18923593]
[0.24840437 0.98040561]
[0.83910185 0.44233389]]

Along first axis : 
[[0.24840437 0.18923593]
[0.698065 0.44233389]
[0.83910185 0.98040561]]

Sorting last axis using numpy:

in this below program we are passing axis value to -1 to sort the last axis in the sort function along with array.

import numpy as np # sort along the last axis a = np.random.rand(3,2) print('\narray values (3,2) before sort',a) arr2 = np.sort(a, axis = -1) print ("\nsort along the last axis : \n", arr2)

Output:

array values (3,2) before sort

[[0.27207839 0.31552827]
[0.53569892 0.67902567]
[0.84425215 0.87191413]]

sort along the last axis : 
[[0.27207839 0.31552827]
[0.53569892 0.67902567]
[0.84425215 0.87191413]]

Sorting with no axis using numpy:

import numpy as np a = np.random.rand(3,2,3) print('\narray values (3,2,3) before sort',a) arr1 = np.sort(a, axis = None) print ("\nAlong none axis : \n", arr1)

Output:

array values (3,2,3) before sort [[[0.37763006 0.71538084 0.3231002 ]
[0.73052758 0.31552531 0.2722308 ]]

[[0.03596215 0.76927319 0.5312672 ]
[0.21909521 0.0600554 0.52591726]]

[[0.96099544 0.60639497 0.40885825]
[0.55006936 0.28812491 0.83349558]]]

Along none axis : 
[0.03596215 0.0600554 0.21909521 0.2722308 0.28812491 0.31552531
0.3231002 0.37763006 0.40885825 0.52591726 0.5312672 0.55006936
0.60639497 0.71538084 0.73052758 0.76927319 0.83349558 0.96099544]

Reverse sorting using numpy arrays:

Program:

import numpy as np a = np.random.randint(1,10, 10) print('Regular sorting',np.sort(a)) reverse = np.sort(a)[::-1] print('Reverse sorting',reverse)

Output:

Regular sorting [1 2 3 4 5 5 6 7 9 9]
Reverse sorting [9 9 7 6 5 5 4 3 2 1]

Sorting using argsort() function.

In this program, we will use argsort() function to sort values. It returns the indices that would sort an array.

Program:

import numpy as np x = np.array([3, 1, 2]) print('argsort',np.argsort(x, axis=0)) x = np.array([[0, 3], [2, 2]]) print('\nargsort with axis=0',np.argsort(x, axis=0)) print('\nargsort with axis=0',np.argsort(x, axis=1))

Output:

argsort [1 2 0]

argsort with axis=0 [[0 1]
[1 0]]

argsort with axis=0 [[0 1]
[0 1]]

 

admin

admin

Leave a Reply

Your email address will not be published. Required fields are marked *