Skip to main content

variance_threshold

Function variance_threshold 

Source
pub fn variance_threshold(
    x: &[Vec<f64>],
    threshold: f64,
) -> Result<Selection, FeatureSelectionError>
Expand description

Selects features whose population variance exceeds threshold.

Computes each feature’s population variance (ddof = 0, divisor n) and keeps the feature when variance > threshold, exactly as sklearn.feature_selection.VarianceThreshold does (it drops features whose variance is ≤ threshold). With threshold = 0.0 this removes only the zero-variance (constant) features.

§Arguments

  • x — the feature matrix, one inner Vec<f64> per sample; must be non-empty, rectangular, and finite.
  • threshold — the variance below or equal to which a feature is dropped; must be finite (0.0 keeps every non-constant feature).

§Returns

A Selection whose scores are the per-feature population variances and whose mask flags variance > threshold.

§Errors

Returns FeatureSelectionError::EmptyInput, FeatureSelectionError::NoFeatures, FeatureSelectionError::RaggedRows, FeatureSelectionError::NonFinite, or FeatureSelectionError::InvalidThreshold.

§Examples

use stats_claw::algorithms::feature_selection::variance_threshold;

// Feature 0 is constant (variance 0); features 1 and 2 vary.
let x = vec![
    vec![0.0, 1.0, 2.0],
    vec![0.0, 4.0, 3.0],
    vec![0.0, 7.0, 10.0],
];
let sel = variance_threshold(&x, 1.0)?;
// Variance of feature 0 is 0 (≤ 1, dropped); features 1, 2 exceed 1 (kept).
assert_eq!(sel.mask(), &[false, true, true]);
assert_eq!(sel.selected_indices(), vec![1, 2]);