How to understand numpy's combined slicing and indexing example
<p>I am trying to understand numpy's combined slicing and indexing concept, however I am not sure how to correctly get the below results from numpy's output (by hand so that we can understand how numpy process combined slicing and indexing, which one will be process first?):</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; a=np.arange(12).reshape(3,4)
&gt;&gt;&gt; a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
&gt;&gt;&gt; i=np.array([[0,1],[2,2]])
&gt;&gt;&gt; a[i,:]
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [ 8,  9, 10, 11]]])
&gt;&gt;&gt; j=np.array([[2,1],[3,3]])
&gt;&gt;&gt; a[:,j]
array([[[ 2,  1],
        [ 3,  3]],

       [[ 6,  5],
        [ 7,  7]],

       [[10,  9],
        [11, 11]]])
&gt;&gt;&gt; aj=a[:,j]
&gt;&gt;&gt; aj.shape
(3L, 2L, 2L)
</code></pre>

<p>I am bit confused about how aj's shape becomes (3,2,2) with the above output, any detailed explanations are very appreciated, thanks!</p>

<p>Whenever you use an array of indices, the result has the <em>same shape as the indices</em>; for example:</p>

<pre><code>&gt;&gt;&gt; x = np.ones(5)
&gt;&gt;&gt; i = np.array([[0, 1], [1, 0]])
&gt;&gt;&gt; x[i]
array([[ 1.,  1.],
       [ 1.,  1.]])
</code></pre>

<p>We've indexed with a 2x2 array, and the result is a 2x2 array.
When combined with a slice, the size of the slice is preserved. For example:</p>

<pre><code>&gt;&gt;&gt; x = np.ones((5, 3))
&gt;&gt;&gt; x[i, :].shape
(2, 2, 3)
</code></pre>

<p>Where the first example was a 2x2 array of items, this example is a 2x2 array of (length-3) rows.</p>

<p>The same is true when you switch the order of the slice:</p>

<pre><code>&gt;&gt;&gt; x = np.ones((5, 3))
&gt;&gt;&gt; x[:, i].shape
(5, 2, 2)
</code></pre>

<p>This can be thought of as a list of five 2x2 arrays.</p>

<p>Just remember: when any dimension is indexed with a list or array, the result has the shape of the <em>indices</em>, not the shape of the input.</p>

python arrays numpy indexing