I tried to find in stackoverflow a thread answering this question, but I could not find. Thus, if it is duplicate, please provide the link.
The use case is very common:
I have two arrays: X which contains two dimensional datapoints and y which contains labels either 0 or 1.
X has shape (307, 2)
y has shape (307, 1)
I want to have all rows in X where the corresponding row in y has value of 1.
I tried the following code:
X[y==1]
But it raises the following error:
IndexError: boolean index did not match indexed array along dimension 1; dimension is 2 but corresponding boolean dimension is 1
How can I do that?
X[y, :]yhave a value of 1 -> shape = (9, 2)yis not an array of boolean values as described in the question. Thus to mask, you have to write a condition which then results in theIndexErrorI also mentioned in the question and have found the reason which is stated in the answer to this questionX[y.ravel().astype(np.bool), :]