简体   繁体   English

给定相同长度的布尔向量,最快的方法来屏蔽2D numpy数组的行?

[英]Fastest way to mask rows of a 2D numpy array given a boolean vector of same length?

I have a numpy boolean vector of shape 1 x N, and an 2d array with shape 160 x N. What is a fast way of subsetting the columns of the 2d array such that for each index of the boolean vector that has True in it, the column is kept, and for each index of the boolean vector that has False in it, the column is discarded? 我有一个形状为1 x N的numpy布尔向量,以及一个形状为160 x N的2d数组。对子数组2d的列进行子集设置的一种快速方法是,对于其中具有True的布尔值向量的每个索引,保留该列,并且对于其中具有False的布尔向量的每个索引,该列是否被丢弃?

If you call the vector mask and the array features, i've found the following to be far too slow: np.array([f[mask] for f in features]) 如果调用矢量蒙版和数组特征,我发现以下操作太慢了: np.array([f[mask] for f in features])

Is there a better way? 有没有更好的办法? I feel like there has to be, right? 我觉得一定有吧?

You can try this, 你可以试试看

    new_array = 2d_array[:,bool_array==True]

So depending on the axes you can select which one you want to remove. 因此,可以根据轴选择要删除的轴。 In case you get a 1-d array, then you can just reshape it and get the required array. 如果得到一维阵列,则可以对其进行整形并获得所需的阵列。 This method will be faster also. 此方法也将更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM