title: Find position of maximum per unique bin (binargmax)
body: <h2>Setup</h2>
  
  <p>Suppose I have</p>
  
  <pre><code>bins = np.array([0, 0, 1, 1, 2, 2, 2, 0, 1, 2])
  vals = np.array([8, 7, 3, 4, 1, 2, 6, 5, 0, 9])
  k = 3
  </code></pre>
  
  <p>I need the position of maximal values by unique bin in <code>bins</code>.</p>
  
  <pre><code># Bin == 0
  #  ↓ ↓           ↓
  # [0 0 1 1 2 2 2 0 1 2]
  # [8 7 3 4 1 2 6 5 0 9]
  #  ↑ ↑           ↑
  #  ⇧
  # [0 1 2 3 4 5 6 7 8 9]
  # Maximum is 8 and happens at position 0
  
  (vals * (bins == 0)).argmax()
  
  0
  </code></pre>
  
  <hr>
  
  <pre><code># Bin == 1
  #      ↓ ↓         ↓
  # [0 0 1 1 2 2 2 0 1 2]
  # [8 7 3 4 1 2 6 5 0 9]
  #      ↑ ↑         ↑
  #        ⇧
  # [0 1 2 3 4 5 6 7 8 9]
  # Maximum is 4 and happens at position 3
  
  (vals * (bins == 1)).argmax()
  
  3
  </code></pre>
  
  <hr>
  
  <pre><code># Bin == 2
  #          ↓ ↓ ↓     ↓
  # [0 0 1 1 2 2 2 0 1 2]
  # [8 7 3 4 1 2 6 5 0 9]
  #          ↑ ↑ ↑     ↑
  #                    ⇧
  # [0 1 2 3 4 5 6 7 8 9]
  # Maximum is 9 and happens at position 9
  
  (vals * (bins == 2)).argmax()
  
  9
  </code></pre>
  
  <hr>
  
  <p>Those functions are hacky and aren't even generalizable for negative values.</p>
  
  <h2>Question</h2>
  
  <p>How do I get all such values in the most efficient manner using Numpy?</p>
  
  <h2>What I've tried.</h2>
  
  <pre><code>def binargmax(bins, vals, k):
    out = -np.ones(k, np.int64)
    trk = np.empty(k, vals.dtype)
    trk.fill(np.nanmin(vals) - 1)
  
    for i in range(len(bins)):
      v = vals[i]
      b = bins[i]
      if v &gt; trk[b]:
        trk[b] = v
        out[b] = i
  
    return out
  
  binargmax(bins, vals, k)
  
  array([0, 3, 9])
  </code></pre>
  
  <hr>
  
  <p><a href="https://colab.research.google.com/drive/1kMbEf8A0cawoQX-R74lGlaNeKBMHFfsS" rel="noreferrer">LINK TO TESTING AND VALIDATION</a></p>