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}