


How to Find Row Indices of Multiple Values in a NumPy Array?
Find the Row Indexes of Several Values in a Numpy Array
Problem:
We are given a NumPy array X and a set of values searched_values. The objective is to determine the row indices in X that correspond to each of the values in searched_values.
For instance, for the following input arrays:
X = np.array([[4, 2], [9, 3], [8, 5], [3, 3], [5, 6]]) searched_values = np.array([[4, 2], [3, 3], [5, 6]])
The desired output should be:
[0, 3, 4]
Approach #1: NumPy Broadcasting
This approach utilizes NumPy broadcasting to perform element-wise comparisons between X and each row of searched_values:
np.where((X == searched_values[:, None]).all(-1))[1]
Approach #2: Memory Efficient Conversion using np.in1d
To conserve memory, we can convert each row of X and searched_values into linear index equivalents and then apply np.in1d for intersection:
dims = X.max(0) + 1 out = np.where(np.in1d(np.ravel_multi_index(X.T, dims), np.ravel_multi_index(searched_values.T, dims)))[0]
Approach #3: Memory Efficient Conversion using np.searchsorted
Another memory-efficient approach using np.searchsorted and the same philosophy of linear index conversion:
dims = X.max(0) + 1 X1D = np.ravel_multi_index(X.T, dims) searched_valuesID = np.ravel_multi_index(searched_values.T, dims) sidx = X1D.argsort() out = sidx[np.searchsorted(X1D, searched_valuesID, sorter=sidx)]
Understanding np.ravel_multi_index
np.ravel_multi_index converts each row of X into a unique linear index equivalent. It operates on a 2D array of n-dimensional indices and the shape of the n-dimensional grid that these indices are to be mapped onto.
For instance, in our example, each row of X represents an indexing tuple for a 2D grid with dimensions dims. np.ravel_multi_index maps each of these tuples to a unique linear index.
The above is the detailed content of How to Find Row Indices of Multiple Values in a NumPy Array?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
