/    /  Numpy – Sort, Search & Counting Functions

Numpy – Sort, Search & Counting Functions:

In Numpy, we can perform various Sorting, Searching, Counting operations using the various functions that are provided in the library like sort, argmax, argmin, count_nonzero etc.

sort: This will return a sorted copy of an array.
argmax: This will return the indices of the maximum values along an axis.
argmin: This will return the indices of the minimum values along an axis.
count_nonzero: This will return count of number of non-zero values in the array.

Example:

>>> import numpy as np

>>> x=np.array([[1,4],[3,2]])

>>> np.sort(x)
array([[1, 4],
[2, 3]])

>>> np.sort(x,axis=None)
array([1, 2, 3, 4])

>>> np.sort(x,axis=0)
array([[1, 2],
[3, 4]])

>>> np.argmax(x)
1

>>> x
array([[1, 4],
[3, 2]])

>>> np.argmax(x, axis=0)
array([1, 0], dtype=int32)

>>> np.argmax(x, axis=1)
array([1, 0], dtype=int32)

>>> np.argmin(x)
0

>>> np.argmin(x, axis=0)
array([0, 1], dtype=int32)

>>> np.count_nonzero(np.eye(4))
4

>>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]])
5