简体   繁体   English

如何过滤numpy ndarray中的列

[英]How to filter columns in numpy ndarray

I have an array of booleans that says which columns of another array I should drop. 我有一系列booleans ,说明我应该删除另一个数组的哪些列。

For example: 例如:

selections = [True, False, True]
data = [[ 1, 2, 3 ],
        [ 4, 5, 6 ]]

I would like to have the following one: 我想有以下一个:

new_data = [[ 1, 3 ],
            [ 4, 6 ]

All arrays are numpy.array in Python 2.7. Python 2.7中的所有数组都是numpy.array

Once you actually use numpy.array s, it all works: 实际使用numpy.array ,一切正常:

import numpy as np

selections = np.array([True, False, True])
data = np.array([[ 1, 2, 3 ],
        [ 4, 5, 6 ]])

>>> data[:, selections]
array([[1, 3],
       [4, 6]])

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

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