title: Why does indexing a numpy array using an array change the shape?
body: <p>I'm trying to index a 2-dimensional array to certain values using numpy.where(), but unless I am indexing in the first index without a <code>:</code> slice it always increases the dimension. I can't seem to find an explanation for this in the documentation.</p>
  
  <p>For example, say I have an array <code>a</code>:</p>
  
  <pre><code>a = np.arange(20)
  a = np.reshape(a,(4,5))
  print("a = ",a)
  print("a shape = ", a.shape)
  </code></pre>
  
  <p>Output: </p>
  
  <pre><code>a =  [[ 0  1  2  3  4]
   [ 5  6  7  8  9]
   [10 11 12 13 14]
   [15 16 17 18 19]]
  a shape =  (4, 5)
  </code></pre>
  
  <p>If I have two indexing arrays, one in the 'x' direction and one in the 'y' direction: </p>
  
  <pre><code>x = np.arange(5)
  y = np.arange(4)
  xindx = np.where((x&gt;=2)&amp;(x&lt;=4))
  yindx = np.where((y&gt;=1)&amp;(y&lt;=2))
  </code></pre>
  
  <p>and then index <code>a</code> using the 'y' index like so there's no problem:</p>
  
  <pre><code>print(a[yindx])
  print(a[yindx].shape)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>[[ 5  6  7  8  9]
   [10 11 12 13 14]]
  (2, 5)
  </code></pre>
  
  <p>But if I have <code>:</code> in one of the indices then I have an extra dimension of size 1:</p>
  
  <pre><code>print(a[yindx,:])
  print(a[yindx,:].shape)
  print(a[:,xindx])
  print(a[:,xindx].shape)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>[[[ 5  6  7  8  9]
    [10 11 12 13 14]]]
  (1, 2, 5)
  [[[ 2  3  4]]
   [[ 7  8  9]]
   [[12 13 14]]
   [[17 18 19]]]
  (4, 1, 3)
  </code></pre>
  
  <p>I run into this issue with one-dimensional arrays, too. How do I fix this? </p>