title: Numpy indexing set 1 to max value and zero's to all others
body: <p>I think I've misunderstood something with indexing in numpy.</p>
  
  <p>I have a 3D-numpy array of shape <code>(dim_x, dim_y, dim_z)</code> and I want to find the maximum along the third axis <code>(dim_z)</code>, and set its value to 1 and all the others to zero.</p>
  
  <p>The problem is that I end up with several 1 in the same row, even if values are different.</p>
  
  <p>Here is the code :</p>
  
  <pre><code>&gt;&gt;&gt; test = np.random.rand(2,3,2)
  &gt;&gt;&gt; test
  array([[[ 0.13110146,  0.07138861],
          [ 0.84444158,  0.35296986],
          [ 0.97414498,  0.63728852]],
  
         [[ 0.61301975,  0.02313646],
          [ 0.14251848,  0.91090492],
          [ 0.14217992,  0.41549218]]])
  
  &gt;&gt;&gt; result = np.zeros_like(test)
  &gt;&gt;&gt; result[:test.shape[0], np.arange(test.shape[1]), np.argmax(test, axis=2)]=1
  &gt;&gt;&gt; result
  array([[[ 1.,  0.],
          [ 1.,  1.],
          [ 1.,  1.]],
  
         [[ 1.,  0.],
          [ 1.,  1.],
          [ 1.,  1.]]])
  </code></pre>
  
  <p>I was expecting to end with :</p>
  
  <pre><code>array([[[ 1., 0.],
          [ 1., 0.],
          [ 1., 0.]],
  
         [[ 1., 0.],
          [ 0., 1.],
          [ 0., 1.]]])
  </code></pre>
  
  <p>Probably I'm missing something here. From what I've understood, <code>0:dim_x, np.arange(dim_y)</code> returns <code>dim_x</code> of <code>dim_y</code> tuples and <code>np.argmax(test, axis=dim_z)</code> has the shape <code>(dim_x, dim_y)</code> so if the indexing is of the form <code>[x, y, z]</code> a couple <code>[x, y]</code> is not supposed to appear twice.</p>
  
  <p>Could someone explain me where I'm wrong ? Thanks in advance.</p>