如何过滤numpy ndarray中的列

Dan*_*kov 5 python arrays numpy

我有一个数组,booleans说明我应该删除另一个数组的哪些列.

例如:

selections = [True, False, True]
data = [[ 1, 2, 3 ],
        [ 4, 5, 6 ]]
Run Code Online (Sandbox Code Playgroud)

我想有以下一个:

new_data = [[ 1, 3 ],
            [ 4, 6 ]
Run Code Online (Sandbox Code Playgroud)

所有数组都 numpy.array在Python 2.7中.

Ami*_*ory 5

一旦你真正使用了numpy.arrays,一切正常:

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]])
Run Code Online (Sandbox Code Playgroud)