Selecting matrix elements from a column matrix
<p>I want to select a element from each row in a matrix according a column matrix.
The column matrix thus contains the indexes to pick. </p>

<pre><code>(Pdb) num_samples
15000
(Pdb) probs.shape
(15000, 26)
(Pdb) y.shape
(15000, 1)
(Pdb) (probs[np.arange(num_samples),y]).shape
(15000, 15000)
(Pdb) # this should (15000,)
</code></pre>

<p>It may be that your <code>y</code> variable is a numpy matrix. Your code works fine when <code>y</code> is a list:</p>

<pre><code>&gt;&gt;&gt; num_samples = 3
&gt;&gt;&gt; data = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
&gt;&gt;&gt; y = [0, 1, 2]
&gt;&gt;&gt; print((data[np.arange(num_samples), y]).shape)
(1, 3)
</code></pre>

<p>and the result can be easily rearranged to be a column matrix.</p>

python numpy