如何通过索引列表过滤numpy数组? [英] How to filter numpy array by list of indices?

查看:108
本文介绍了如何通过索引列表过滤numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,一直在尝试学习如何使用numpy和scipy.我有一个由LAS数据[x,y,z,强度,分类]组成的numpy数组.我已经创建了点的cKDTree,并使用

I am relatively new to python and have been trying to learn how to use numpy and scipy. I have a numpy array comprised of LAS data [x, y, z, intensity, classification]. I have created a cKDTree of points and have found nearest neighbors using query_ball_point. I would like to find standard deviation of the z values for the neighbors returned by query_ball_point, which returns a list of indices for the point and its neighbors.

是否有一种方法可以过滤filtered__rows以创建仅包含其索引在query_ball_point返回的列表中的点的数组?请参阅下面的代码.我可以将值附加到列表中并从中计算std dev,但我认为使用numpy在单轴上计算std dev会更容易.预先感谢.

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? See code below. I can append the values to a list and calculate std dev from that, but I think it would be easier to use numpy to calculate std dev on a single axis. Thanks in advance.

# 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)

推荐答案

ummmm很难说出所要询问的内容(那是一堵墙)

ummmm Its hard to tell whats being asked (thats quite the wall of text)

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

可能是您要问的内容

这篇关于如何通过索引列表过滤numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆