如何在NumPy的ndarray中筛选列

8

我有一个布尔数组,它表示我需要删除另一个数组的哪些列。

例如:

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

我很乐意提供以下翻译:

我希望你能够提供以下内容:

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

在Python 2.7中,所有的数组都是用numpy.array定义的。

1个回答

9

一旦你实际使用 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]])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接