NumPy Sort, Search and Count Functions

Free NumPy course with real-time projects Start Now!!

NumPy contains functions to perform search, sort, and count operations. There are a wide variety of functions to get the output. We can use the functions accordingly to produce the most optimum output. We can implement these functions on the array object.

The functions can be used along with each other on the same data. We use NumPy search to find a particular element from the array. We use Numpy sort function for arranging the array in a particular order. NumPy counting function returns the count of a particular value.

NumPy Searching Function

We perform NumPy search operation to determine the position of a given element or value inside an array. There are functions to find the maximum, minimum, or a value satisfying a particular condition. The search turns out to be successful if the value is found.

1. np.argmax()

This function returns the index of the maximum value in the array list. The search can be specific to a particular axis or the entire array.

import numpy as np
arr=np.array([[74,23],[56,98]])
print(np.argmax(arr))

Output

3

2. np.nanargmax()

This function returns the index of the maximum value element after ignoring ‘Not a Number’ (NaN) values present in the list.

import numpy as np
arr=np.array([[74,np.nan,59],[56,98,np.nan]])
print(np.nanargmax(arr))

Output

4

3. np.argmin()

This function returns the index of the minimum value in the array list. The search can be specific to a particular axis or the entire array.

import numpy as np
arr=np.array([[74,59],[56,98]])
print(np.argmin(arr))

Output

2

4. np.nanargmin()

This function returns the index of the minimum value element after ignoring ‘Not a Number’ (NaN) values present in the list.

import numpy as np
arr=np.array([[74,np.nan,59],[56,98,np.nan]])
print(np.nanargmin(arr))

Output

3

5. np.extract()

This function returns the array values that satisfy a particular condition.

import numpy as np
arr=np.array([[74,33],[59,98]])
a=np.mod(arr,2) ==0
print(np.extract(a,arr))

Output

[74 98]

6. np.nonzero()

This function search returns values that are non-zero.

import numpy as np
arr=np.array([0,56,89,0,23])
print(np.nonzero(arr))

Output

(array([1, 2, 4]))
FunctionDescription
np.argwhere()It is used to group elements having non-zero indices.
np.flatnonzero()It returns a flattened array consisting of elements with non-zero indices.
np.where()It returns elements following a condition.
np.searchsorted()It returns indices of elements that are in sorted manner.

NumPy Sorting Function

There are a wide variety of sorting functions in NumPy. These NumPy Sort functions arrange the data in a particular order. We choose the best sorting algorithm depending on the output criteria. We can apply for any order over the data. There are various sorting functions available.

1. np.sort()

This function returns an array in sorted format.

import numpy as np
arr = np.array([[16,1,47,53,87,28]])
print(np.sort(arr))
 

Output

[[ 1 16 28 47 53 87]]

2. np.argsort()

This function returns the indices of the sorted array. We can perform this along a particular axis.

import numpy as np
arr = np.array([[16,1,47,53,87,28]])
print(np.argsort(arr))

Output

[[1 0 5 2 3 4]]

3. np.lexsort()

This function returns the indices of array elements indirectly sorted by using a key sequence.We can consider the last key as the primary one.

import numpy as np
arr1 =np.array([[16,1,47,53,87,28]])
arr2 =np.array([[56,90,44,32,77,45]])
#sort by arr1 and then arr2
print(np.lexsort((arr2,arr1)))

Output

[[1 0 5 2 3 4]]
FunctionDescription
np.ndarraysort()It is for sorting an array in place.
np.msort()It returns an array which sorted along the first axis
np.partition()It returns a copy of array partition.
np.argpartition()It returns the partition of an aray along a specific axis.
np.sort_compex()It sorts the real and imaginary parts separately.

NumPy Counting Function

We can apply NumPy Count to count a specific kind of value from the array list.

1. np.count_nonzero()

This function returns the count of all the non-zero values from the array. We can apply this function along a specific axis.

import numpy as np
arr=np.array([[16,0,56],[2,34,0],[90,87,0]])
print(np.count_nonzero(arr))
print(np.count_nonzero(arr,axis=0))
 

Output

6
[3 2 1]

Summary

There are functions to perform searching, sorting, and counting on an array list in NumPy. These NumPy functions help in arranging the data according to the requirement. This is also useful to extract particular data in a sorted manner. It is also useful for further use of the sorted.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

Leave a Reply

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