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 |
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'))
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)
print ('Applying argsort() to x:')
y = (np.argsort(x))
print (y)
print ('Reconstruct original array in sorted order:')
print (x[y])
print ('Reconstruct the original array using loop:')
for i in y:
print (x[i])
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])
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)
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))
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')
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')
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
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.
If you want to