NumPy Search: 5 Different NumPy Searching Techniques

Filed Under: NumPy
NumPy Searching Techniques

Hello, readers! In this article, we will be focusing on NumPy Search techniques, in detail.

So, let us begin!! 馃檪


NumPy module – Quick Overview

Python provides us with different modules to deal with various types of data and also manipulate the same. One such module is the Python NumPy module.

With the NumPy module, the base of mathematical modeling is built. It provides us with various functions to manipulate data and perform statistical calculations on it.

5 NumPy Search Techniques

Suppose your code requires you to identify the minimum and maximum values from a particular set of values. While it is easy with some of the built-in functions, Numpy offers more advanced versions of the checks. Let’s look at 5 different ways to look for the maximum and minimum values.

  1. argmax() function
  2. nanargmax() function
  3. argmin() function
  4. nanargmin() function
  5. search() function

1. The argmax() function

With argmax() function, the NumPy module provides us a way easy way to get the maximum value limit from the Array elements at ease.

The argmax() function returns the index of the largest element present in the array. We can make use of this index to be applied to other functions to consider the maximum value in terms of the position.

Syntax–

numpy.argmax(array)

Example–

In this example, the argmax() function returns the index of the largest element present in the entire array/matrix.

import numpy as np
data = np.array([[6,9,2,11],[1,2,3,4]])
op =  np.argmax(data) 
print("Max element's index:", op)

Output–

Max element's index: 3

2. The argmin() function

NumPy argmin() function searches for the smallest element from the array and returns the index of the same. That is, it would find the minimum element from the array and then returns its position.

Syntax–

numpy.argmin(array)

Example–

In this example, the argmin() function returns the index of the smallest element present in the entire array/matrix.

import numpy as np
data = np.array([[6,9,2,11],[11,2,3,1]])
op =  np.argmin(data) 
print("Min element's index:", op)

Output–

Min element's index: 7

3. The nanargmax() & nanargmin() function

The argmin() and argmax() functions tend to fail when the data contains impurities. If the data contains NULL or NA values, the functions would be affected by them disturbing the search cycle.

For the same, we have the below functions that stay unaffected by NA values and perform the search efficiently.

  • nanargmax() function: It searches for the maximum(largest) element from the array and returns the index
  • nanargmin() function: It searches for the smallest(minimum) element from the array and returns the index

NOTE: NA values do not affect the search and stay still.

Example–

import numpy as np
data = np.array([[6,9,np.nan,11],[11,2,3,np.nan]])
op1 =  np.nanargmax(data) 
print("Max element's index:", op1)
op2 =  np.nanargmin(data) 
print("Min element's index:", op2)

Output–

Max element's index: 3
Min element's index: 5

5. The NumPy where() function

NumPy where() function brings in dynamic search and observations. With the where() function we can search elements according to the customized conditions that we pass to the function.

Once the condition is found/met, the where() function returns the index of that search element according to the condition.

import numpy as np
a = np.arange(10)
print(a)
print(np.where(a>5))

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to Python programming, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content