NumPy Sorting and Searching

Numpy provides a variety of functions for sorting and searching. There are various sorting algorithms like quicksort, merge sort and heapsort which is implemented using the numpy.sort() function.

The kind of the sorting algorithm to be used in the sort operation must be mentioned in the function call.

Let's discuss the sorting algorithm which is implemented in numpy.sort()

SN Algorithm Worst case complexity
1 Quick Sort O (n ^ 2)
2 Merge Sort O (n * log(n))
3 Heap Sort O (n * log(n))

The syntax to use the numpy.sort() function is given below.

snippet
numpy.sort(a, axis, kind, order)

It accepts the following parameters.

SN Parameter Description
1 input It represents the input array which is to be sorted.
2 axis It represents the axis along which the array is to be sorted. If the axis is not mentioned, then the sorting is done along the last available axis.
3 kind It represents the type of sorting algorithm which is to be used while sorting. The default is quick sort.
4 order It represents the filed according to which the array is to be sorted in the case if the array contains the fields.

Consider the following example.

Example

snippet
import numpy as np

a = np.array([[10,2,3],[4,5,6],[7,8,9]])

print("Sorting along the columns:")
print(np.sort(a))

print("Sorting along the rows:")
print(np.sort(a, 0))

data_type = np.dtype([('name', 'S10'),('marks',int)])

arr = np.array([('Mukesh',200),('John',251)],dtype = data_type)

print("Sorting data ordered by name")

print(np.sort(arr,order = 'name'))

Output:

Output
Sorting along the columns: [[ 2 3 10] [ 4 5 6] [ 7 8 9]] Sorting along the rows: [[ 4 2 3] [ 7 5 6] [10 8 9]] Sorting data ordered by name [(b'John', 251) (b'Mukesh', 200)]

numpy.argsort() function

This function is used to perform an indirect sort on an input array that is, it returns an array of indices of data which is used to construct the array of sorted data.

Consider the following example.

Example

snippet
import numpy as np

a = np.array([90, 29, 89, 12])

print("Original array:\n",a)

sort_ind = np.argsort(a)

print("Printing indices of sorted data\n",sort_ind)

sort_a = a[sort_ind]

print("printing sorted array")

for i in sort_ind:
    print(a[i],end = " ")

Output:

Output
Original array: [90 29 89 12] Printing indices of sorted data [3 1 2 0] printing sorted array 12 29 89 90

numpy.lexsort() function

This function is used to sort the array using the sequence of keys indirectly. This function performs similarly to the numpy.argsort() which returns the array of indices of sorted data.

Consider the following example.

Example

snippet
import numpy as np

a = np.array(['a','b','c','d','e'])

b = np.array([12, 90, 380, 12, 211])

ind = np.lexsort((a,b))

print("printing indices of sorted data")

print(ind)

print("using the indices to sort the array")

for i in ind:
    print(a[i],b[i])

Output:

Output
printing indices of sorted data [0 3 1 4 2] using the indices to sort the array a 12 d 12 b 90 e 211 c 380

numpy.nonzero() function

This function is used to find the location of the non-zero elements from the array.

Consider the following example.

Example

snippet
import numpy as np

b = np.array([12, 90, 380, 12, 211])

print("printing original array",b)

print("printing location of the non-zero elements")

print(b.nonzero())

Output:

Output
printing original array [ 12 90 380 12 211] printing location of the non-zero elements (array([0, 1, 2, 3, 4]),)

numpy.where() function

This function is used to return the indices of all the elements which satisfies a particular condition.

Consider the following example.

Example

snippet
import numpy as np

b = np.array([12, 90, 380, 12, 211])

print(np.where(b>12))

c = np.array([[20, 24],[21, 23]])

print(np.where(c>20))

Output:

Output
(array([1, 2, 4]),) (array([0, 1, 1]), array([1, 0, 1]))
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +