0

I have a numpy array which is 300 rows and 5 columns

X[X[:,0]==1,[1,2]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-154-de5e74bc1a0b> in <module>()
----> 1 X[X[:,0]==1,[1,2]]

ValueError: shape mismatch: objects cannot be broadcast to a single shape 

How to make this work? I want to filter the first column (equals 1) but returns a copy of second and third column.

1 Answer 1

1

It's simplest to separate the two indexing cases. Look at column one first, select the appropriate rows, and then pick out columns 1 and 2 from that array:

>>> a = np.random.randint(0, 2, (3, 5))
>>> a
array([[0, 0, 0, 0, 1],
       [1, 0, 1, 0, 1],
       [0, 0, 1, 1, 0]])

>>> a[a[:,0] == 1][:,[1,2]]
array([[0, 1]])

The code in the question, a[a[:,0] == 1, [1,2]], look syntactically similar but is doing something different.

For example a[a[:,0] == 0, [1,2]] is, in the case of a, equivalent to a[[0,2], [1,2]]. This indexing picks out exactly two elements from a: the element at row 0, column 1 and the element at row 2, column 2. It does not select the rows and columns of the array.

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

1 Comment

@DoctorLai: I think this is by far the simplest way. Instead of a[:,0] == 1 you could write np.where(a[:,0]==1)[0] but this is less concise. Your approach looks syntactically very similar to mine but it is actually doing something different. For example, with my array a, a[a[:,0]==0, [1,2]] is equivalent to a[[0,2], [1,2]] which picks two elements out of the array: the element at (0, 1) and the element at (2, 2).

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.