(X, y, max_X=None, max_y=None, weights=None, mask=None)
return np.array(contingencies), np.array(nans_cols), nans_rows, nans
Compute the contingency matrices for each column of X (excluding the masked)
    versus the vector y.

    If the array is 1-dimensional, a 2d contingency matrix is returned. If the
    array is 2d, the function returns a 3d array, with the first dimension
    corresponding to column index (variable in the input array).

    The column of contingency matrix correspond to values of variables, the
    row correspond to values in vector `y`.

    Rows in the input array can be weighted (argument `weights`). A subset of
    columns can be selected by additional argument `mask`.

    The function also returns a count of NaN values per each value of `y`.

    Parameters
    ----------
    X : array_like
        With values in columns.
    y : 1d array
        Vector of true values.
    max_X : int
        The maximal value in the array
    max_y : int
        The maximal value in `y`
    weights : array_like
        Row weights. When not None, contingencies contain weighted counts
    mask : sequence
        Discrete columns of X.

    Returns
    -------
    contingencies: (m × ny × nx) array
        m number of masked (used) columns (all if mask=None), i.e.
        for each column of X;
        ny number of uniques in y,
        nx number of uniques in column of X.
    nans_cols : array_like
        Number of nans in each column of X for each unique value of y.
    nans_rows : array_like
        Number of nans in y for each unique value in columns of X.
    nans : array_like
        Number of nans in column of X and y at the same time.
Compute the contingency matrices for each column of X (excluding the masked)
    versus the vector y.
Compute the contingency matrices for each column of X ( excluding the masked ) versus the vector y .
def contingency(X, y, max_X=None, max_y=None, weights=None, mask=None):
    """
    Compute the contingency matrices for each column of X (excluding the masked)
    versus the vector y.

    If the array is 1-dimensional, a 2d contingency matrix is returned. If the
    array is 2d, the function returns a 3d array, with the first dimension
    corresponding to column index (variable in the input array).

    The column of contingency matrix correspond to values of variables, the
    row correspond to values in vector `y`.

    Rows in the input array can be weighted (argument `weights`). A subset of
    columns can be selected by additional argument `mask`.

    The function also returns a count of NaN values per each value of `y`.

    Parameters
    ----------
    X : array_like
        With values in columns.
    y : 1d array
        Vector of true values.
    max_X : int
        The maximal value in the array
    max_y : int
        The maximal value in `y`
    weights : array_like
        Row weights. When not None, contingencies contain weighted counts
    mask : sequence
        Discrete columns of X.

    Returns
    -------
    contingencies: (m × ny × nx) array
        m number of masked (used) columns (all if mask=None), i.e.
        for each column of X;
        ny number of uniques in y,
        nx number of uniques in column of X.
    nans_cols : array_like
        Number of nans in each column of X for each unique value of y.
    nans_rows : array_like
        Number of nans in y for each unique value in columns of X.
    nans : array_like
        Number of nans in column of X and y at the same time.
    """
    was_1d = False
    if X.ndim == 1:
        X = X[..., np.newaxis]
        was_1d = True

    contingencies, nans_cols, nans_rows, nans = [], [], [], []
    ny = np.unique(y).size if max_y is None else max_y + 1
    for i in range(X.shape[1]):
        if mask is not None and not mask[i]:
            contingencies.append(np.zeros((ny, max_X + 1)))
            nans_cols.append(np.zeros(ny))
            nans_rows.append(None)
            nans.append(0)
            continue
        col = X[..., i]
        nx = np.unique(col[~np.isnan(col)]).size if max_X is None else max_X + 1
        if sp.issparse(col):
            col = np.ravel(col.todense())
        contingencies.append(
            bincount(y + ny * col,
                     minlength=ny * nx,
                     weights=weights)[0].reshape(nx, ny).T)

        nan_col_mask = np.isnan(col)
        nan_row_mask = np.isnan(y)
        nan_mask = nan_col_mask & nan_row_mask
        weights_ = np.ones(len(y)) if weights is None else weights

        nans_cols.append(
            bincount(y[nan_col_mask], weights=weights_[nan_col_mask],
                     minlength=ny)[0])
        nans_rows.append(
            bincount(col[nan_row_mask], weights=weights_[nan_row_mask],
                     minlength=nx)[0])
        nans.append(np.sum(nan_mask * weights_))
    if was_1d:
        return contingencies[0], nans_cols[0], nans_rows[0], nans[0]
    return np.array(contingencies), np.array(nans_cols), nans_rows, nans
def contingency ( X , y , max_X = None , max_y = None , weights = None , mask = None ) : was_1d = False if X . ndim == 1 : X = X [ ... , np . newaxis ] was_1d = True contingencies , nans_cols , nans_rows , nans = [ ] , [ ] , [ ] , [ ] ny = np . unique ( y ) . size if max_y is None else max_y + 1 for i in range ( X . shape [ 1 ] ) : if mask is not None and not mask [ i ] : contingencies . append ( np . zeros ( ( ny , max_X + 1 ) ) ) nans_cols . append ( np . zeros ( ny ) ) nans_rows . append ( None ) nans . append ( 0 ) continue col = X [ ... , i ] nx = np . unique ( col [ ~ np . isnan ( col ) ] ) . size if max_X is None else max_X + 1 if sp . issparse ( col ) : col = np . ravel ( col . todense ( ) ) contingencies . append ( bincount ( y + ny * col , minlength = ny * nx , weights = weights ) [ 0 ] . reshape ( nx , ny ) . T ) nan_col_mask = np . isnan ( col ) nan_row_mask = np . isnan ( y ) nan_mask = nan_col_mask & nan_row_mask weights_ = np . ones ( len ( y ) ) if weights is None else weights nans_cols . append ( bincount ( y [ nan_col_mask ] , weights = weights_ [ nan_col_mask ] , minlength = ny ) [ 0 ] ) nans_rows . append ( bincount ( col [ nan_row_mask ] , weights = weights_ [ nan_row_mask ] , minlength = nx ) [ 0 ] ) nans . append ( np . sum ( nan_mask * weights_ ) ) if was_1d : return contingencies [ 0 ] , nans_cols [ 0 ] , nans_rows [ 0 ] , nans [ 0 ] return np . array ( contingencies ) , np . array ( nans_cols ) , nans_rows , nans