How to get matrix rows by indexes?
<p>Assuming that we have a matrix X and a target column y as following:</p>

<pre><code>import numpy as np
X = np.ones([10,2])
for i in range(0,X.shape[0]):
    X[i][0] = i
y = [0,1,2,1,0,0,1,2,3,3]
</code></pre>

<p>I want to get rows of X depending on the value of y. From the small example above:</p>

<p>For y == 0, I want to get rows of X as:</p>

<pre><code>[[0 1]
[4 1]
[5 1]]
</code></pre>

<p>For y == 3, I want to get rows of X as:</p>

<pre><code>[[8 1]
[9 1]]
</code></pre>

<p>And so on. 
How can I solve this problem?</p>

<p>I also tried</p>

<pre><code>print(X[y == 0][:])
</code></pre>

<p>But it did not work.</p>

<p>You were close, you just need <code>y</code> to be a numpy array instead of a list:</p>

<pre><code>&gt;&gt;&gt; y = np.array(y)
&gt;&gt;&gt; X[y==0]
array([[0, 1],
       [4, 1],
       [5, 1]])

&gt;&gt;&gt; X[y==3]
array([[8, 1],
       [9, 1]])
</code></pre>

python matrix indexing