Esempio n. 1
0
def mode(keys,
         axis=semantics.axis_default,
         weights=None,
         return_indices=False):
    """compute the mode, or most frequent occuring key in a set

    Parameters
    ----------
    keys : ndarray, [n_keys, ...]
        input array. elements of 'keys' can have arbitrary shape or dtype
    weights : ndarray, [n_keys], optional
        if given, the contribution of each key to the mode is weighted by the given weights
    return_indices : bool
        if True, return all indices such that keys[indices]==mode holds

    Returns
    -------
    mode : ndarray, [...]
        the most frequently occuring key in the key sequence
    indices : ndarray, [mode_multiplicity], int, optional
        if return_indices is True, all indices such that points[indices]==mode holds
    """
    index = as_index(keys, axis)
    if weights is None:
        unique, weights = count(index)
    else:
        unique, weights = group_by(index).sum(weights)
    bin = np.argmax(weights)
    _mode = unique[
        bin]  # FIXME: replace with index.take for lexindex compatibility?
    if return_indices:
        indices = index.sorter[index.start[bin]:index.stop[bin]]
        return _mode, indices
    else:
        return _mode
Esempio n. 2
0
 def max(self, values, default=None):
     if default is None:
         try:
             info = np.iinfo(values.dtype)
             default = info.min
         except:
             default = -np.inf
     table = self.allocate(values.dtype, default)
     keys, values = group_by(self.keys).max(values)
     table[self.get_inverses(keys)] = values
     return tuple(self.uniques), table
Esempio n. 3
0
 def last(self, values):
     table = self.allocate(np.float, np.nan)
     keys, values = group_by(self.keys).last(values)
     table[self.get_inverses(keys)] = values
     return tuple(self.uniques), table
Esempio n. 4
0
 def sum(self, values):
     table = self.allocate(values.dtype)
     keys, values = group_by(self.keys).sum(values)
     table[self.get_inverses(keys)] = values
     return tuple(self.uniques), table