Skip to main content

compute_sample_weights

Function compute_sample_weights 

Source
pub fn compute_sample_weights(
    targets: &[f64],
    class_weight: &ClassWeight,
) -> Vec<f64>
Expand description

Compute per-sample weights from target labels and a class weighting strategy.

Returns a vector with one weight per sample. For Uniform, all weights are 1.0. For Balanced, uses the sklearn formula: weight_c = n_samples / (n_classes × count_c).

§Example

use scry_learn::weights::{ClassWeight, compute_sample_weights};

// 8 samples of class 0, 2 samples of class 1
let targets = vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0];
let weights = compute_sample_weights(&targets, &ClassWeight::Balanced);

// n_samples=10, n_classes=2
// weight_0 = 10 / (2 × 8) = 0.625
// weight_1 = 10 / (2 × 2) = 2.5
assert!((weights[0] - 0.625).abs() < 1e-6);
assert!((weights[8] - 2.5).abs() < 1e-6);