pub fn chi2<F: Float + Send + Sync + 'static>(
x: &Array2<F>,
y: &Array1<usize>,
) -> Result<(Array1<F>, Array1<F>), FerroError>Expand description
Compute chi-squared statistics between each non-negative feature and the class labels.
For each feature the observed and expected frequencies per class are computed, then:
chi2 = sum_class (observed - expected)^2 / expectedwhere observed is the sum of feature values for samples of that class,
and expected is the expected sum under the null hypothesis (proportional
to the class frequency and the overall feature sum).
§Returns
(chi2_statistics, p_values) — two Array1<F> of length n_features.
§Errors
FerroError::InsufficientSamplesifxhas zero rows.FerroError::ShapeMismatchifx.nrows() != y.len().FerroError::InvalidParameterif any feature value is negative.
§Examples
use ferrolearn_preprocess::feature_scoring::chi2;
use ndarray::{array, Array1};
let x = array![[1.0_f64, 0.0], [0.0, 1.0], [1.0, 0.0], [0.0, 1.0]];
let y: Array1<usize> = array![0, 1, 0, 1];
let (chi2_stats, _p_vals) = chi2(&x, &y).unwrap();
assert_eq!(chi2_stats.len(), 2);