Skip to content

unique_counts

Returns the unique elements of an input array x, and the corresponding counts for each unique element in x.

Parameters:

Name Type Description Default
x COO

Input COO array. It will be flattened if it is not already 1-D.

required

Returns:

Name Type Description
out namedtuple

The result containing: * values - The unique elements of an input array. * counts - The corresponding counts for each unique element.

Raises:

Type Description
ValueError

If the input array is in a different format than COO.

Examples:

>>> import sparse
>>> x = sparse.COO.from_numpy([1, 0, 2, 1, 2, -3])
>>> sparse.unique_counts(x)
UniqueCountsResult(values=array([-3,  0,  1,  2]), counts=array([1, 1, 2, 2]))
Source code in sparse/numba_backend/_coo/common.py
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
def unique_counts(x, /):
    """
    Returns the unique elements of an input array `x`, and the corresponding
    counts for each unique element in `x`.

    Parameters
    ----------
    x : COO
        Input COO array. It will be flattened if it is not already 1-D.

    Returns
    -------
    out : namedtuple
        The result containing:
        * values - The unique elements of an input array.
        * counts - The corresponding counts for each unique element.

    Raises
    ------
    ValueError
        If the input array is in a different format than COO.

    Examples
    --------
    >>> import sparse
    >>> x = sparse.COO.from_numpy([1, 0, 2, 1, 2, -3])
    >>> sparse.unique_counts(x)
    UniqueCountsResult(values=array([-3,  0,  1,  2]), counts=array([1, 1, 2, 2]))
    """

    x = _validate_coo_input(x)

    x = x.flatten()
    values, counts = np.unique(x.data, return_counts=True, equal_nan=False)
    fill_count = x.size - x.nnz
    if fill_count > 0:
        if np.isnan(x.fill_value):
            # Per the Array API spec, NaNs compare as False, so each NaN is distinct.
            values = np.concatenate([values, np.full(fill_count, x.fill_value)])
            counts = np.concatenate([counts, np.ones(fill_count, dtype=counts.dtype)])
        else:
            values = np.concatenate([[x.fill_value], values])
            counts = np.concatenate([[fill_count], counts])
            sorted_indices = np.argsort(values)
            values = values[sorted_indices]
            counts = counts[sorted_indices]

    return UniqueCountsResult(values, counts)