Nivi
Nivi

Reputation: 1137

Indexing numpy array using boolean vector

I have a numpy.ndarray and I have a boolean list. I want to use the list to access the columns in the array.

X = [[1,2,3,4],[5,6,7,8]]
Y = [True,False,False,True]

I want the result to be

[[1,4][5,8]]

I guess I am doing this inefficiently and would like to know if there is a straightforward method.

Upvotes: 1

Views: 72

Answers (1)

utengr
utengr

Reputation: 3345

You have to convert it to numpy first.

import numpy as np

X = np.array([[1,2,3,4],[5,6,7,8]])
Y = np.array([True,False,False,True])

print(X[:,Y])

Upvotes: 2

Related Questions