2

How can you select only the columns of a 2-d numpy array that correspond to a conditional boolean vector?

Say you have a 10x10 matrix, generated by, say:

a = np.random.randint(0,1,(10,10))
a = 
array([[4, 9, 1, 9, 5, 2, 1, 7, 6, 5],
       [5, 4, 2, 4, 8, 1, 5, 5, 7, 5],
       [3, 8, 7, 4, 3, 4, 8, 8, 8, 3],
       [5, 4, 4, 4, 9, 6, 7, 1, 6, 8],
       [8, 3, 2, 1, 7, 5, 8, 8, 4, 9],
       [9, 5, 6, 8, 6, 8, 1, 4, 4, 5],
       [5, 4, 3, 2, 8, 3, 2, 2, 8, 6],
       [2, 5, 4, 5, 9, 7, 9, 2, 5, 6],
       [4, 5, 9, 7, 3, 1, 5, 7, 4, 8],
       [6, 1, 3, 8, 8, 3, 2, 6, 6, 7]])

and you want to cut out all the rows corresponding to a vector containing (True/False or 0/1), like, say:

b = np.random.randint(0,2,10)
b = 
array([0, 1, 1, 0, 1, 0, 1, 1, 1, 1])

1 Answer 1

2

I spent some time trying to find the simple syntax to return only specified colummns in a numpy array in python 3 and finally have it figured out. There are a number of other threads which show more complicated ways to do this, so I thought I would put the simple solution here. This will be very obvious to more experienced python users, but for a beginner like me, it would have been useful.

The simplest way is:

new_matrix = a[:,b==1]

which yields:

new_matrix = 

array([[9, 1, 5, 1, 7, 6, 5],
       [4, 2, 8, 5, 5, 7, 5],
       [8, 7, 3, 8, 8, 8, 3],
       [4, 4, 9, 7, 1, 6, 8],
       [3, 2, 7, 8, 8, 4, 9],
       [5, 6, 6, 1, 4, 4, 5],
       [4, 3, 8, 2, 2, 8, 6],
       [5, 4, 9, 9, 2, 5, 6],
       [5, 9, 3, 5, 7, 4, 8],
       [1, 3, 8, 2, 6, 6, 7]])

This would have saved me a lot of time.

Sign up to request clarification or add additional context in comments.

1 Comment

That expression selects columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.