def argmaxMulti(a, groupKeys, assumeSorted=False):
  """
  This is like numpy's argmax, but it returns multiple maximums.

  It gets the indices of the max values of each group in 'a', grouping the
  elements by their corresponding value in 'groupKeys'.

  @param a (numpy array)
  An array of values that will be compared

  @param groupKeys (numpy array)
  An array with the same length of 'a'. Each entry identifies the group for
  each 'a' value.

  @param assumeSorted (bool)
  If true, group keys must be organized together (e.g. sorted).

  @return (numpy array)
  The indices of one maximum value per group

  @example
    _argmaxMulti([5, 4, 7, 2, 9, 8],
                 [0, 0, 0, 1, 1, 1])
  returns
    [2, 4]
  """

  if not assumeSorted:
    # Use a stable sort algorithm
    sorter = np.argsort(groupKeys, kind="mergesort")
    a = a[sorter]
    groupKeys = groupKeys[sorter]

  _, indices, lengths = np.unique(groupKeys, return_index=True,
                                  return_counts=True)

  maxValues = np.maximum.reduceat(a, indices)
  allMaxIndices = np.flatnonzero(np.repeat(maxValues, lengths) == a)

  # Break ties by finding the insertion points of the the group start indices
  # and using the values currently at those points. This approach will choose
  # the first occurrence of each max value.
  indices = allMaxIndices[np.searchsorted(allMaxIndices, indices)]

  if assumeSorted:
    return indices
  else:
    return sorter[indices]
def argmaxMulti ( a , groupKeys , assumeSorted = False ) : if not assumeSorted : sorter = np . argsort ( groupKeys , kind = "mergesort" ) a = a [ sorter ] groupKeys = groupKeys [ sorter ] _ , indices , lengths = np . unique ( groupKeys , return_index = True , return_counts = True ) maxValues = np . maximum . reduceat ( a , indices ) allMaxIndices = np . flatnonzero ( np . repeat ( maxValues , lengths ) == a ) indices = allMaxIndices [ np . searchsorted ( allMaxIndices , indices ) ] if assumeSorted : return indices else : return sorter [ indices ]
This is like numpy's argmax, but it returns multiple maximums.

  It gets the indices of the max values of each group in 'a', grouping the
  elements by their corresponding value in 'groupKeys'.

  @param a (numpy array)
  An array of values that will be compared

  @param groupKeys (numpy array)
  An array with the same length of 'a'. Each entry identifies the group for
  each 'a' value.

  @param assumeSorted (bool)
  If true, group keys must be organized together (e.g. sorted).

  @return (numpy array)
  The indices of one maximum value per group

  @example
    _argmaxMulti([5, 4, 7, 2, 9, 8],
                 [0, 0, 0, 1, 1, 1])
  returns
    [2, 4]