Searching in a NumPy array
×


Searching in a NumPy array

784

Searching in a NumPy Array

Introduction

Efficient data retrieval is crucial in data analysis and scientific computing. NumPy, a powerful library for numerical computations in Python, offers several methods to search for elements within arrays. This article explores two essential functions: numpy.where() and numpy.searchsorted().

1. numpy.where(): Conditional Indexing

The numpy.where() function returns the indices of elements in an input array where the given condition is satisfied. It's particularly useful for conditional selection and manipulation of array elements.

Syntax:

numpy.where(condition[, x, y])
  • condition: A condition to evaluate.
  • x, y: Optional values to choose from. If specified, the output array contains elements of x where the condition is true, and elements from y elsewhere.

Example:

import numpy as np

arr = np.array([10, 32, 30, 50, 20, 82, 91, 45])
print("arr =", arr)

indices = np.where(arr == 30)
print("Indices of 30:", indices[0])

Output:

arr = [10 32 30 50 20 82 91 45]
Indices of 30: [2]

2. numpy.searchsorted(): Finding Insertion Indices

The numpy.searchsorted() function finds the indices into a sorted array such that, if elements are inserted before the indices, the order of the array would be preserved. It uses binary search to find the required insertion indices.

Syntax:

numpy.searchsorted(arr, num, side='left', sorter=None)
  • arr: The sorted input array.
  • num: The values to insert into arr.
  • side: {'left', 'right'}, optional. If 'left', the index of the first suitable location found is given. If 'right', return the last such index.
  • sorter: Optional. An array of indices that sort arr.

Example:

import numpy as np

arr = np.array([1, 2, 2, 3, 3, 3, 4, 5, 6, 6])
print("arr =", arr)

left_index = np.searchsorted(arr, 3, side='left')
right_index = np.searchsorted(arr, 3, side='right')

print("Left-most index of 3:", left_index)
print("Right-most index of 3:", right_index)

Output:

arr = [1 2 2 3 3 3 4 5 6 6]
Left-most index of 3: 3
Right-most index of 3: 6

Conclusion

NumPy provides efficient methods for searching within arrays. The numpy.where() function allows for conditional indexing, while numpy.searchsorted() helps in finding insertion indices in sorted arrays. Understanding these functions enhances data manipulation capabilities in Python.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat