pub fn target_encode(
values: &[String],
y: &[f64],
expected: &[f64],
exposure: Option<&[f64]>,
splits: &[Split],
k: f64,
) -> Result<(Vec<f64>, TargetEncoding)>Expand description
The out-of-fold encoding of each training row, and the encoding from all the rows for new data.
values are the factor’s levels per row, y the actual responses,
expected the expected responses, exposure the credibility weights
(default 1 per row), and every row is in exactly one split’s test set.
A row is encoded from its split’s training rows; within those, a level
with no expected loss gets 1.
use prospicio_models::encode::target_encode;
use prospicio_models::resample::Split;
let values: Vec<String> = ["a", "a", "b", "b"].iter().map(|s| s.to_string()).collect();
let splits = vec![
Split { train: vec![1, 3], test: vec![0, 2] },
Split { train: vec![0, 2], test: vec![1, 3] },
];
let (rows, all) =
target_encode(&values, &[2.0, 2.0, 0.0, 1.0], &[1.0; 4], None, &splits, 1.0).unwrap();
// Row 0 sees only row 1 of level a: z = 1/2, ratio 2, so 1.5.
assert_eq!(rows[0], 1.5);
// From all rows, a has n = 2, z = 2/3 and ratio 2.
assert!((all.encode(&["a".to_string()])[0] - 5.0 / 3.0).abs() < 1e-15);Fails when the lengths differ, k is negative, or a row is in no test
set or several.