pub fn standardize_columns(x: &Tensor) -> Result<Tensor, MattenMlprepError>Expand description
Standardizes each column to zero mean and unit (population) standard
deviation: out[i,j] = (x[i,j] - mean_j) / std_j.
std_j uses the population formula (divide by n), matching scikit-learn’s
StandardScaler.
§Errors
MattenMlprepError::ExpectedMatrixifxis not rank-2.MattenMlprepError::ZeroVarianceif any column is constant.MattenMlprepError::DynamicTensor(with thedynamicfeature) ifxis dynamic.
use matten::Tensor;
use matten_mlprep::standardize_columns;
// Column 0: [1, 3] -> mean 2, std 1 -> [-1, 1]; column 1: [10, 20] -> [-1, 1].
let x = Tensor::new(vec![1.0, 10.0, 3.0, 20.0], &[2, 2]);
let z = standardize_columns(&x).unwrap();
assert_eq!(z.as_slice(), &[-1.0, -1.0, 1.0, 1.0]);Examples found in repository?
examples/standardize_columns.rs (line 20)
17fn main() {
18 // 3 samples, 2 features.
19 let x = Tensor::new(vec![1.0, 10.0, 2.0, 20.0, 3.0, 30.0], &[3, 2]);
20 let z = standardize_columns(&x).expect("two non-constant columns");
21 println!("input shape {:?}: {:?}", x.shape(), x.as_slice());
22 println!("z-score {:?}: {:?}", z.shape(), z.as_slice());
23
24 // Shape is preserved and each standardized column has (near) zero mean.
25 assert_eq!(z.shape(), x.shape());
26 let zs = z.as_slice();
27 let col0_mean = (zs[0] + zs[2] + zs[4]) / 3.0;
28 let col1_mean = (zs[1] + zs[3] + zs[5]) / 3.0;
29 assert!(col0_mean.abs() < 1e-9 && col1_mean.abs() < 1e-9);
30 println!("standardize_columns: OK");
31}More examples
examples/visual_standardize_summary.rs (line 43)
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}