关于python:如何通过索引列表过滤numpy数组?

How to filter numpy array by list of indices?

我对python比较陌生,一直在努力学习如何使用numpy和scipy。我有一个由las数据组成的numpy数组[X,Y,Z,强度,分类]。我创建了一个点的CKDTree,并使用查询球点找到了最近的邻居。我想找出由查询球点返回的相邻点的z值的标准偏差,它返回该点及其相邻点的索引列表。

有没有一种方法可以过滤过滤掉的_uuu行来创建一个只有索引在查询_Ball_u Point返回的列表中的点的数组?请参见下面的代码。我可以将这些值附加到一个列表中,然后根据这个列表计算std dev,但我认为使用numpy计算单个轴上的std dev会更容易一些。事先谢谢。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Import modules
from liblas import file
import numpy as np
import scipy.spatial

if __name__=="__main__":
    '''Read LAS file and create an array to hold X, Y, Z values'''
    # Get file
    las_file = r"E:\Testing\kd-tree_testing\LE_K20_clipped.las"
    # Read file
    f = file.File(las_file, mode='r')
    # Get number of points from header
    num_points = int(f.__len__())
    # Create empty numpy array
    PointsXYZIC = np.empty(shape=(num_points, 5))
    # Load all LAS points into numpy array
    counter = 0
    for p in f:
        newrow = [p.x, p.y, p.z, p.intensity, p.classification]
        PointsXYZIC[counter] = newrow
        counter += 1

    '''Filter array to include classes 1 and 2'''
    # the values to filter against
    unclassified = 1
    ground = 2
    # Create an array of booleans
    filter_array = np.any([PointsXYZIC[:, 4] == 1, PointsXYZIC[:, 4] == 2], axis=0)
    # Use the booleans to index the original array
    filtered_rows = PointsXYZIC[filter_array]

    '''Create a KD tree structure and segment the point cloud'''
    tree = scipy.spatial.cKDTree(filtered_rows, leafsize=10)

    '''For each point in the point cloud use the KD tree to identify nearest neighbors,
       with a K radius'''

    k = 5 #meters
    for pntIndex in range(len(filtered_rows)):
        neighbor_list = tree.query_ball_point(filtered_rows[pntIndex], k)
        zList = []
        for neighbor in neighbor_list:
            neighbor_z = filtered_rows[neighbor, 2]
            zList.append(neighbor_z)

嗯,很难说别人在问什么(这简直就是文字的墙)

1
2
filter_indices = [1,3,5]
print numpy.array([11,13,155,22,0xff,32,56,88])[filter_indices]

可能是你要的