Skip to main content

mlprep_minmax_scale/
minmax_scale.rs

1//! # Companion example: min-max scale feature columns (matten-mlprep)
2//!
3//! Run: cargo run -p matten-mlprep --example mlprep_minmax_scale
4//!
5//! ## What this shows
6//! Scaling each feature column of a `[samples, features]` matrix into `[0, 1]`.
7//!
8//! ## Teaching points
9//! - rows are samples, columns are features;
10//! - per column, the minimum maps to 0 and the maximum to 1;
11//! - deterministic, with no fitted model state.
12
13use matten::Tensor;
14use matten_mlprep::minmax_scale_columns;
15
16fn main() {
17    // 3 samples, 2 features.
18    let x = Tensor::new(vec![0.0, 100.0, 5.0, 150.0, 10.0, 200.0], &[3, 2]);
19    let s = minmax_scale_columns(&x).expect("two non-constant columns");
20    println!("input  shape {:?}: {:?}", x.shape(), x.as_slice());
21    println!("scaled shape {:?}: {:?}", s.shape(), s.as_slice());
22
23    // Per column: min -> 0.0, max -> 1.0 (column 0 = [0,5,10], column 1 = [100,150,200]).
24    assert_eq!(s.shape(), x.shape());
25    let ss = s.as_slice();
26    assert_eq!(ss[0], 0.0); // col 0 min
27    assert_eq!(ss[4], 1.0); // col 0 max
28    assert_eq!(ss[1], 0.0); // col 1 min
29    assert_eq!(ss[5], 1.0); // col 1 max
30    println!("minmax_scale: OK");
31}