Updated On : Jul-21,2020 Time Investment : ~10 mins

Learning Numpy - Simple Tutorial For Beginners - NumPy - Sort, Search & Counting Functions Part 17

A variety of sorting related functions are available in NumPy. These sorting functions implement different sorting algorithms, each of them characterized by the speed of execution, worst case performance, the workspace required and the stability of algorithms. Following table shows the comparison of three sorting algorithms.

Kind Speed Worst Case Work Space Stable
quicksort 1 O(n^2) 0 no
mergesort 2 O(n*log(n)) ~n/2 yes
heapsort 3 O(n*log(n)) 0 no

numpy.sort()

The sort() function returns a sorted copy of the input array. It has the following parameters −

numpy.sort(a, axis, kind, order)
Parameter Description
a Array to be sorted
axis The axis along which the array is to be sorted. If none, the array is flattened, sorting on the last axis
kind Default is quicksort
order If the array contains fields, the order of fields to be sorted
import numpy as np
a = np.array([[3,7],[9,1]])

print ('Our array is:')
print (a)


print ('Applying sort() function:')
print (np.sort(a))

print ('Sort along axis 0:')
print (np.sort(a, axis = 0))


# Order parameter in sort function 
dt = np.dtype([('name', 'S10'),('age', int)])
a = np.array([("raju",21),("anil",25),("ravi", 17), ("amar",27)], dtype = dt)

print ('Our array is:')
print (a)


print ('Order by name:')
print (np.sort(a, order = 'name'))
Our array is:
[[3 7]
 [9 1]]
Applying sort() function:
[[3 7]
 [1 9]]
Sort along axis 0:
[[3 1]
 [9 7]]
Our array is:
[(b'raju', 21) (b'anil', 25) (b'ravi', 17) (b'amar', 27)]
Order by name:
[(b'amar', 27) (b'anil', 25) (b'raju', 21) (b'ravi', 17)]

numpy.argsort()

The numpy.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.

import numpy as np
x = np.array([3, 1, 2])
print ('Our array is:')
print (x)
Our array is:
[3 1 2]
print ('Applying argsort() to x:')
y = (np.argsort(x))
print (y)
Applying argsort() to x:
[1 2 0]
print ('Reconstruct original array in sorted order:')
print (x[y])
Reconstruct original array in sorted order:
[1 2 3]
print ('Reconstruct the original array using loop:')
for i in y:
   print (x[i])
Reconstruct the original array using loop:
1
2
3

numpy.lexsort()

This 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.

import numpy as np

nm = ('raju','anil','ravi','amar')
dv = ('f.y.', 's.y.', 's.y.', 'f.y.')
ind = np.lexsort((dv,nm))

print ('Applying lexsort() function:')
print (ind)


print ('Use this index to get sorted data:')
print ([nm[i] + ", " + dv[i] for i in ind])
Applying lexsort() function:
[3 1 0 2]
Use this index to get sorted data:
['amar, f.y.', 'anil, s.y.', 'raju, f.y.', 'ravi, s.y.']

numpy.argmax() and numpy.argmin()

These two functions return the indices of maximum and minimum elements respectively along the given axis.

import numpy as np
a = np.array([[30,40,70],[80,20,10],[50,90,60]])

print ('Our array is:')
print (a)


print ('Applying argmax() function:')
print (np.argmax(a))


print ('Index of maximum number in flattened array')
print (a.flatten())


print ('Array containing indices of maximum along axis 0:')
maxindex = np.argmax(a, axis = 0)
print (maxindex)


print ('Array containing indices of maximum along axis 1:')
maxindex = np.argmax(a, axis = 1)
print (maxindex)


print ('Applying argmin() function:')
minindex = np.argmin(a)
print (minindex)


print ('Flattened array:')
print (a.flatten()[minindex])


print ('Flattened array along axis 0:')
minindex = np.argmin(a, axis = 0)
print (minindex)


print ('Flattened array along axis 1:')
minindex = np.argmin(a, axis = 1)
print (minindex)
Our array is:
[[30 40 70]
 [80 20 10]
 [50 90 60]]
Applying argmax() function:
7
Index of maximum number in flattened array
[30 40 70 80 20 10 50 90 60]
Array containing indices of maximum along axis 0:
[1 2 0]
Array containing indices of maximum along axis 1:
[2 0 1]
Applying argmin() function:
5
Flattened array:
10
Flattened array along axis 0:
[0 1 1]
Flattened array along axis 1:
[0 2 0]

numpy.nonzero()

The numpy.nonzero() function returns the indices of non-zero elements in the input array.

import numpy as np
a = np.array([[30,40,0],[0,20,10],[50,0,60]])

print ('Our array is:')
print (a)
print ('\n')

print ('Applying nonzero() function:')
print (np.nonzero (a))
Our array is:
[[30 40  0]
 [ 0 20 10]
 [50  0 60]]


Applying nonzero() function:
(array([0, 0, 1, 1, 2, 2]), array([0, 1, 1, 2, 0, 2]))

numpy.where()

The where() function returns the indices of elements in an input array where the given condition is satisfied.

import numpy as np
x = np.arange(9.).reshape(3, 3)

print ('Our array is:')
print (x)
print ('\n')

print ('Indices of elements > 3')
y = np.where(x > 3)
print (y)
print ('\n')

print ('Use these indices to get elements satisfying the condition')
print (x[y])
print ('\n')
Our array is:
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]


Indices of elements > 3
(array([1, 1, 2, 2, 2]), array([1, 2, 0, 1, 2]))


Use these indices to get elements satisfying the condition
[4. 5. 6. 7. 8.]


numpy.extract()

The extract() function returns the elements satisfying any condition.

import numpy as np
x = np.arange(9.).reshape(3, 3)

print ('Our array is:')
print (x)
print ('\n')

# define a condition 
condition = np.mod(x,2) == 0

print ('Element-wise value of condition')
print (condition)
print ('\n')

print ('Extract elements using condition')
print (np.extract(condition, x))
print ('\n')
Our array is:
[[0. 1. 2.]
 [3. 4. 5.]
 [6. 7. 8.]]


Element-wise value of condition
[[ True False  True]
 [False  True False]
 [ True False  True]]


Extract elements using condition
[0. 2. 4. 6. 8.]


Dolly Solanki  Dolly Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.


Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription