Skip to main content

mlprep_visual_standardize_summary/
visual_standardize_summary.rs

1//! # Companion example: visual standardization summary (matten-mlprep)
2//!
3//! Run: cargo run -p matten-mlprep --example mlprep_visual_standardize_summary
4//!
5//! ## What this shows
6//! A compact before/after summary for `standardize_columns`: per-column mean,
7//! per-column standard deviation, and the fact that the shape is unchanged.
8//!
9//! ## Teaching points
10//! - rows are samples, columns are features;
11//! - standardization changes values, not shape;
12//! - the report is explanatory only: no model-quality or data-quality judgment.
13
14use matten::Tensor;
15use matten_mlprep::standardize_columns;
16
17fn format_values(values: &[f64]) -> String {
18    let values = values
19        .iter()
20        .map(|&value| {
21            let stable = if value.abs() < 0.0005 { 0.0 } else { value };
22            format!("{stable:.3}")
23        })
24        .collect::<Vec<_>>()
25        .join(", ");
26    format!("[{values}]")
27}
28
29fn print_stats(label: &str, t: &Tensor) {
30    let mean = t.mean_axis(0);
31    let std = t.std_axis(0);
32    println!("{label:<13} mean={}", format_values(mean.as_slice()));
33    println!("{:<13} std={}", "", format_values(std.as_slice()));
34}
35
36fn main() {
37    let input = Tensor::new(vec![8.0, 80.0, 10.0, 100.0, 12.0, 120.0], &[3, 2]);
38
39    println!("== Standardize columns ==");
40    println!("input shape   {:?}", input.shape());
41    print_stats("before", &input);
42
43    let standardized = standardize_columns(&input).expect("two non-constant columns");
44    println!("after shape   {:?}", standardized.shape());
45    print_stats("after", &standardized);
46    println!("meaning       standardize_columns changes values, not shape.");
47
48    assert_eq!(standardized.shape(), input.shape());
49    let after_mean = standardized.mean_axis(0);
50    let after_std = standardized.std_axis(0);
51    for &value in after_mean.as_slice() {
52        assert!(value.abs() < 1e-9);
53    }
54    for &value in after_std.as_slice() {
55        assert!((value - 1.0).abs() < 1e-9);
56    }
57
58    println!("summary complete");
59}