(x, return_counts=False)
Equivalent of np.unique that supports sparse or dense matrices.
Equivalent of np.unique that supports sparse or dense matrices.
Equivalent of np . unique that supports sparse or dense matrices .
def unique(x, return_counts=False):
    """ Equivalent of np.unique that supports sparse or dense matrices. """
    if not sp.issparse(x):
        return np.unique(x, return_counts=return_counts)

    implicit_zeros = sparse_count_implicit_zeros(x)
    explicit_zeros = not np.all(x.data)
    r = np.unique(x.data, return_counts=return_counts)
    if not implicit_zeros:
        return r

    if return_counts:
        zero_index = np.searchsorted(r[0], 0)
        if explicit_zeros:
            r[1][r[0] == 0.] += implicit_zeros
            return r
        return np.insert(r[0], zero_index, 0), np.insert(r[1], zero_index, implicit_zeros)
    else:
        if explicit_zeros:
            return r
        zero_index = np.searchsorted(r, 0)
        return np.insert(r, zero_index, 0)
def unique ( x , return_counts = False ) : if not sp . issparse ( x ) : return np . unique ( x , return_counts = return_counts ) implicit_zeros = sparse_count_implicit_zeros ( x ) explicit_zeros = not np . all ( x . data ) r = np . unique ( x . data , return_counts = return_counts ) if not implicit_zeros : return r if return_counts : zero_index = np . searchsorted ( r [ 0 ] , 0 ) if explicit_zeros : r [ 1 ] [ r [ 0 ] == 0. ] += implicit_zeros return r return np . insert ( r [ 0 ] , zero_index , 0 ) , np . insert ( r [ 1 ] , zero_index , implicit_zeros ) else : if explicit_zeros : return r zero_index = np . searchsorted ( r , 0 ) return np . insert ( r , zero_index , 0 )