code: def chi2(X, y):
      """Compute chi-squared stats between each non-negative feature and class.
  
      This score can be used to select the n_features features with the
      highest values for the test chi-squared statistic from X, which must
      contain only non-negative features such as booleans or frequencies
      (e.g., term counts in document classification), relative to the classes.
  
      Recall that the chi-square test measures dependence between stochastic
      variables, so using this function "weeds out" the features that are the
      most likely to be independent of class and therefore irrelevant for
      classification.
  
      Read more in the :ref:`User Guide <univariate_feature_selection>`.
  
      Parameters
      ----------
      X : {array-like, sparse matrix}, shape = (n_samples, n_features_in)
          Sample vectors.
  
      y : array-like, shape = (n_samples,)
          Target vector (class labels).
  
      Returns
      -------
      chi2 : array, shape = (n_features,)
          chi2 statistics of each feature.
      pval : array, shape = (n_features,)
          p-values of each feature.
  
      Notes
      -----
      Complexity of this algorithm is O(n_classes * n_features).
  
      See also
      --------
      f_classif: ANOVA F-value between label/feature for classification tasks.
      f_regression: F-value between label/feature for regression tasks.
      """
  
      # XXX: we might want to do some of the following in logspace instead for
      # numerical stability.
      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, X)  # n_classes * n_features
  
      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)
  
imports: import numpy
  import warnings
  from scipy import special, stats
  from scipy.sparse import issparse
  from ..base import BaseEstimator
  from ..preprocessing import LabelBinarizer
  from ..utils import as_float_array, check_array, check_X_y, safe_sqr, safe_mask
  from ..utils.extmath import norm, safe_sparse_dot, row_norms
  from ..utils.validation import check_is_fitted
  from .base import SelectorMixin
label_desc: Use image node_id 5 for calling a global function with example usage: chi2(X, y) and returns: _chisquare