NumPy Array Searching

In this article, we will explore how NumPy array search works for a specific values or elements and perform various search-related operations.



Search Arrays

Numpy array search scans an array and displays the indexes that match a particular value.

Invoke the where() method to find an array.

You can locate the indexes where the value is 20499 as follows:

Example: 

import numpy as npy id_arr = npy.array([19847, 19399, 20478, 21561, 18212, 20499, 20504, 21061]) mrx = npy.where(id_arr == 20499) print(mrx)

As a result of the example above, a tuple will be displayed: (array([5]),) Therefore, index 5 holds the value 20499.

Identify the index of cube_arr that contains the data 27:

Example: 

import numpy as npy cube_arr = npy.array([1, 8, 27, 64, 125, 216]) mrx = npy.where(cube_arr == 27) print(mrx)

The following tuple will be generated as an outcome of the example above: (array([2]),)
Accordingly, index 2 contains the value 27.

You need to identify the indexes odd_arr array where the numbers are odd:

Example: 

import numpy as npy odd_arr = npy.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) mrx = npy.where(odd_arr%2 != 0) print(mrx)

NumPy Array Searching

Search for numbers that are perfectly divisible by four:

Example: 

import numpy as npy divisible_arr = npy.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) mrx = npy.where(divisible_arr%4 == 1) print(mrx)

Check out the indexes of the even_arr array that have even numbers:

Example: 

import numpy as npy even_arr = npy.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) mrx = npy.where(even_arr%2 == 0) print(mrx)

Look for numbers that are completely divisible by the odd number 7:

Example: 

import numpy as npy divisible_arr = npy.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) mrx = npy.where(divisible_arr%7 == 1) print(mrx)

NumPy Array Search Sorted

The searchsorted() method in Numpy performs a binary search in an array and returns the index where the searched value will be added.

Arrays with sorted items are expected to be retrieved through the searchsorted() method.

Determine the location of the gap_arr array where number 40 should be placed:

Example: 

import numpy as npy gap_arr = npy.array([10, 20, 30, 40, 50, 60, 70]) mrx = npy.searchsorted(gap_arr, 40) print(mrx)

Above example explanation: To maintain the sort order, number 40 should be placed on index 3.

According to Numpy array search, the method begins the lookup from the left and displays the first index where 40 is no more larger than the next value.

Where should number 3.7 be positioned in the point_arr array:

Example: 

import numpy as npy point_arr = npy.array([2.8, 3.7, 3.9, 4.2, 4.5, 6.9, 7]) mrx = npy.searchsorted(point_arr, 3.7) print(mrx)

Above example explanation: Number 3.7 must be positioned on index 1 to keep the sort order.

Numpy array search begins from the left and shows the first index where 3.7 is no larger than the value after it.

Search From the Right Side

We can set side=’right’ to retrieve the rightmost index rather than the leftmost index by default.

Beginning from the right, locate the indexes where the value 50 should be added:

Example: 

import numpy as npy gap_arr = npy.array([10, 20, 30, 40, 50, 60, 70]) mrx = npy.searchsorted(gap_arr, 50, side='right') print(mrx)

Above example explanation: To maintain the sort order, add 50 on index 5.

Numpy array search begins the query from the right and finds the first index where 50 is no longer less than the next value.

Identify the indexes where 4.5 should be placed starting from the right:

Example: 

import numpy as npy point_arr = npy.array([2.8, 3.7, 3.9, 4.2, 4.5, 6.9, 7]) mrx = npy.searchsorted(point_arr, 4.5, side='right') print(mrx)

Example Explanation

On index 5, insert 4.2 to continue the sequence order.

From right to left, Numpy array search locates the first index where 4.2 is no longer less than 4.3.


Multiple Values

An array with multiple values can be utilized to look for more than one value.

The numbers 8 and 10 should be placed at the following indexes:

Example: 

import numpy as npy odd_arr = npy.array([1, 3, 5, 7, 9, 11]) mrx = npy.searchsorted(odd_arr, [8, 10]) print(mrx)
Explanation: The above code generates an array [4 5] with the two positions where 8 and 10 would be placed in the original array to keep it sorted.

Following are the locations where 4.1, 4.3, and 6.3 should be added:

Example: 

import numpy as npy point_arr = npy.array([2.8, 3.7, 3.9, 4.2, 4.5, 6.9, 7]) mrx = npy.searchsorted(point_arr, [4.1, 4.3, 6.3]) print(mrx)
Explanation: For sorting, the above code prints an array [3 4 5] with the three positions where the initial array would place 4.1, 4.3, and 6.3.

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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