...

Web Technologies - NumPy

Back to Course

Lesson Description


Lession - #304 NumPy Array Search


Searching Arrays

You can search an array for a specific value, and return the indexes that get a match.
To search through an array, utilize the where(>
strategy.

Example
Find the indexes where the value is 4:
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 4, 4]>
x = np.where(arr == 4>
print(x>


Search Sorted

There is a strategy called searchsorted(>
which plays out a binary search in the array, and returns the index where the predetermined worth would be embedded to keep up with the search order.

Example
Find the indexes where the value 7 should be inserted:
import numpy as np

arr = np.array([6, 7, 8, 9]>
x = np.searchsorted(arr, 7>
print(x>


Search From the Right Side

As a matter of course the left most index is returned, however we can give side='right' to return the right most index all things being equal.

Example
Find the indexes where the worth 7 ought to be embedded, beginning from the right:
import numpy as np

arr = np.array([6, 7, 8, 9]>
x = np.searchsorted(arr, 7, side='right'>
print(x>


Multiple Values

To search for more than one value, utilize a array with the predetermined values.

Example
Find the indexes where the values 2, 4, and 6 ought to be embedded:
import numpy as np

arr = np.array([1, 3, 5, 7]>
x = np.searchsorted(arr, [2, 4, 6]>
print(x>









The Python List insert(>
method is an inbuilt function in Python that inserts a given element at a given index in a list.
The simplest way to convert a Python list to NumPy array is to use the np.array(>
function that takes an iterable and returns a NumPy array.
numpy average ... Compute the weighted average along the specified axis. ... Axis or axes along which to average.
A tutorial on using NumPy GitHub Gist: instantly share code, notes, and snippets.