

def chi2(X, y):
    'Compute chi-squared stats between each non-negative feature and class.\n\n    This score can be used to select the n_features features with the\n    highest values for the test chi-squared statistic from X, which must\n    contain only non-negative features such as booleans or frequencies\n    (e.g., term counts in document classification), relative to the classes.\n\n    Recall that the chi-square test measures dependence between stochastic\n    variables, so using this function "weeds out" the features that are the\n    most likely to be independent of class and therefore irrelevant for\n    classification.\n\n    Read more in the :ref:`User Guide <univariate_feature_selection>`.\n\n    Parameters\n    ----------\n    X : {array-like, sparse matrix}, shape = (n_samples, n_features_in)\n        Sample vectors.\n\n    y : array-like, shape = (n_samples,)\n        Target vector (class labels).\n\n    Returns\n    -------\n    chi2 : array, shape = (n_features,)\n        chi2 statistics of each feature.\n    pval : array, shape = (n_features,)\n        p-values of each feature.\n\n    Notes\n    -----\n    Complexity of this algorithm is O(n_classes * n_features).\n\n    See also\n    --------\n    f_classif: ANOVA F-value between label/feature for classification tasks.\n    f_regression: F-value between label/feature for regression tasks.\n    '
    X = check_array(X, accept_sparse='csr')
    if np.any(((X.data if issparse(X) else X) < 0)):
        raise ValueError('Input X must be non-negative.')
    Y = LabelBinarizer().fit_transform(y)
    if (Y.shape[1] == 1):
        Y = np.append((1 - Y), Y, axis=1)
    observed = safe_sparse_dot(Y.T, feature_count)
    feature_count = X.sum(axis=0).reshape(1, (- 1))
    class_prob = Y.mean(axis=0).reshape(1, (- 1))
    expected = np.dot(class_prob.T, feature_count)
    return _chisquare(observed, expected)

dataset/ETHPy150Open scikit-learn/scikit-learn/sklearn/feature_selection/univariate_selection.py chi2/VarMisuse@110/646 `X`->`feature_count`