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><a href="http://docs.scipy.org/doc/numpy-1.10.0/reference/arrays.indexing.html#integer-array-indexing" rel="nofollow">integer array indexing</a> may be helpful.
Suppose you have this <code>numpy</code> array:</p>

<pre><code>myArray = numpy.array([[2, 3, 4],
                       [6, 7, 8],
                       [9, 1, 5]])
</code></pre>

<p>If your array of indices to select is</p>

<pre><code>indices = numpy.array([2, 0, 1])
</code></pre>

<p>Then</p>

<pre><code>rowSelector = numpy.arange(myArray.shape[0])
myArray[rowSelector, indices]
</code></pre>

<p>returns the values of the selected elements of the array:</p>

<pre><code>array([4, 6, 1]) 
</code></pre>

python numpy