(x, weights=None, max_val=None, minlength=0)
return bc, nans
Return counts of values in array X.

    Works kind of like np.bincount(), except that it also supports
    arrays with nans.

    Parameters
    ----------
    x : array_like, 1 dimension, nonnegative ints
        Input array.
    weights : array_like, optional
        Weights, array of the same shape as x.
    max_val : int, optional
        Indicates the maximum value we expect to find in X and sets the result
        array size accordingly. E.g. if we set `max_val=2` yet the largest
        value in X is 1, the result will contain a bin for the value 2, and
        will be set to 0. See examples for usage.
    minlength : int, optional
        A minimum number of bins for the output array. See numpy docs for info.

    Returns
    -------
    Tuple[np.ndarray, int]
        Returns the bincounts and the number of NaN values.

    Examples
    --------
    In case `max_val` is provided, the return shape includes bins for these
    values as well, even if they do not appear in the data. However, this will
    not truncate the bincount if values larger than `max_count` are found.
    >>> bincount([0, 0, 1, 1, 2], max_val=4)
    (array([ 2.,  2.,  1.,  0.,  0.]), 0.0)
    >>> bincount([0, 1, 2, 3, 4], max_val=2)
    (array([ 1.,  1.,  1.,  1.,  1.]), 0.0)
Return counts of values in array X.
Return counts of values in array X .
def bincount(x, weights=None, max_val=None, minlength=0):
    """Return counts of values in array X.

    Works kind of like np.bincount(), except that it also supports
    arrays with nans.

    Parameters
    ----------
    x : array_like, 1 dimension, nonnegative ints
        Input array.
    weights : array_like, optional
        Weights, array of the same shape as x.
    max_val : int, optional
        Indicates the maximum value we expect to find in X and sets the result
        array size accordingly. E.g. if we set `max_val=2` yet the largest
        value in X is 1, the result will contain a bin for the value 2, and
        will be set to 0. See examples for usage.
    minlength : int, optional
        A minimum number of bins for the output array. See numpy docs for info.

    Returns
    -------
    Tuple[np.ndarray, int]
        Returns the bincounts and the number of NaN values.

    Examples
    --------
    In case `max_val` is provided, the return shape includes bins for these
    values as well, even if they do not appear in the data. However, this will
    not truncate the bincount if values larger than `max_count` are found.
    >>> bincount([0, 0, 1, 1, 2], max_val=4)
    (array([ 2.,  2.,  1.,  0.,  0.]), 0.0)
    >>> bincount([0, 1, 2, 3, 4], max_val=2)
    (array([ 1.,  1.,  1.,  1.,  1.]), 0.0)

    """
    # Store the original matrix before any manipulation to check for sparse
    x_original = x
    if sp.issparse(x):
        if weights is not None:
            # Match weights and x axis so `indices` will be set appropriately
            if x.shape[0] == weights.shape[0]:
                x = x.tocsc()
            elif x.shape[1] == weights.shape[0]:
                x = x.tocsr()

            zero_weights = sparse_implicit_zero_weights(x, weights).sum()
            weights = weights[x.indices]
        else:
            zero_weights = sparse_count_implicit_zeros(x)

        x = x.data

    x = np.asanyarray(x, dtype=float)
    if bn.anynan(x):
        nonnan = ~np.isnan(x)
        x = x[nonnan]
        if weights is not None:
            nans = (~nonnan * weights).sum(axis=0)
            weights = weights[nonnan]
        else:
            nans = (~nonnan).sum(axis=0)
    else:
        nans = 0. if x.ndim == 1 else np.zeros(x.shape[1], dtype=float)

    if minlength == 0 and max_val is not None:
        minlength = max_val + 1

    bc = np.bincount(
        x.astype(np.int32, copy=False), weights=weights, minlength=minlength
    ).astype(float)
    # Since `csr_matrix.values` only contain non-zero values or explicit
    # zeros, we must count implicit zeros separately and add them to the
    # explicit ones found before
    if sp.issparse(x_original):
        # If x contains only NaNs, then bc will be an empty array
        if zero_weights and bc.size == 0:
            bc = [zero_weights]
        elif zero_weights:
            bc[0] += zero_weights

    return bc, nans
def bincount ( x , weights = None , max_val = None , minlength = 0 ) : # Store the original matrix before any manipulation to check for sparse x_original = x if sp . issparse ( x ) : if weights is not None : # Match weights and x axis so `indices` will be set appropriately if x . shape [ 0 ] == weights . shape [ 0 ] : x = x . tocsc ( ) elif x . shape [ 1 ] == weights . shape [ 0 ] : x = x . tocsr ( ) zero_weights = sparse_implicit_zero_weights ( x , weights ) . sum ( ) weights = weights [ x . indices ] else : zero_weights = sparse_count_implicit_zeros ( x ) x = x . data x = np . asanyarray ( x , dtype = float ) if bn . anynan ( x ) : nonnan = ~ np . isnan ( x ) x = x [ nonnan ] if weights is not None : nans = ( ~ nonnan * weights ) . sum ( axis = 0 ) weights = weights [ nonnan ] else : nans = ( ~ nonnan ) . sum ( axis = 0 ) else : nans = 0. if x . ndim == 1 else np . zeros ( x . shape [ 1 ] , dtype = float ) if minlength == 0 and max_val is not None : minlength = max_val + 1 bc = np . bincount ( x . astype ( np . int32 , copy = False ) , weights = weights , minlength = minlength ) . astype ( float ) # Since `csr_matrix.values` only contain non-zero values or explicit # zeros, we must count implicit zeros separately and add them to the # explicit ones found before if sp . issparse ( x_original ) : # If x contains only NaNs, then bc will be an empty array if zero_weights and bc . size == 0 : bc = [ zero_weights ] elif zero_weights : bc [ 0 ] += zero_weights return bc , nans