mojolearn.chi2#

mojolearn.chi2(X, y)[source]#

Compute chi-squared statistics between each nonnegative feature and the class labels.

Follows scikit-learn’s chi2: the per-class sums of each feature are compared with their expected values. X must be nonnegative (ValueError otherwise). An all-zero feature scores NaN with p-value NaN, as in scikit-learn. Computation is float32.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Nonnegative feature matrix, such as counts or frequencies.

  • y (array-like of shape (n_samples,)) – Class labels.

Returns:

  • chi2 (array of shape (n_features,)) – Chi-squared statistic of each feature.

  • p_values (array of shape (n_features,)) – p-value of each statistic.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.integers(0, 10, (1000, 8)).astype(np.float32)
>>> y = rng.integers(0, 2, 1000)
>>> stat, p = mojolearn.chi2(X, y)