Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter numpy array by list of indices?

I have a numpy array, filtered__rows, comprised of LAS data [x, y, z, intensity, classification]. I have created a cKDTree of points and have found nearest neighbors, query_ball_point, which is a list of indices for the point and its neighbors.

Is there a way to filter filtered__rows to create an array of only points whose index is in the list returned by query_ball_point?

like image 226
Barbarossa Avatar asked Nov 06 '13 19:11

Barbarossa


People also ask

How do you select an indices of an array in Python?

To select an element from Numpy Array , we can use [] operator i.e. It will return the element at given index only.

How do I filter an element in a NumPy array?

In NumPy, you filter an array using a boolean index list. A boolean index list is a list of booleans corresponding to indexes in the array. If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array.

How do you extract items that satisfy a given condition from 1d array?

If you want to extract elements that meet the condition, you can use ndarray[conditional expression] . Even if the original ndarray is a multidimensional array, a flattened one-dimensional array is returned. A new ndarray is returned, and the original ndarray is unchanged.


2 Answers

It looks like you just need a basic integer array indexing:

filter_indices = [1,3,5] np.array([11,13,155,22,0xff,32,56,88])[filter_indices]  
like image 130
Joran Beasley Avatar answered Sep 19 '22 19:09

Joran Beasley


numpy.take can be useful and works well for multimensional arrays.

import numpy as np  filter_indices = [1, 2] array = np.array([[1, 2, 3, 4, 5],                    [10, 20, 30, 40, 50],                    [100, 200, 300, 400, 500]])  axis = 0 print(np.take(array, filter_indices, axis)) # [[ 10  20  30  40  50] #  [100 200 300 400 500]]  axis = 1 print(np.take(array, filter_indices, axis)) # [[  2   3] #  [ 20  30] # [200 300]] 
like image 26
Keunwoo Choi Avatar answered Sep 19 '22 19:09

Keunwoo Choi