Skip to main content

standardize_columns

Function standardize_columns 

Source
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

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 8)
6fn main() {
7    let x = Tensor::new(vec![1.0, 10.0, 2.0, 20.0, 3.0, 30.0], &[3, 2]);
8    let z = standardize_columns(&x).expect("two non-constant columns");
9    println!("input  shape {:?}: {:?}", x.shape(), x.as_slice());
10    println!("z-score      {:?}: {:?}", z.shape(), z.as_slice());
11}