Skip to main content

standardize

Function standardize 

Source
pub fn standardize(
    features: &Array2<f64>,
) -> Result<(Array2<f64>, Scaler), DatasetError>
Expand description

Standardize each feature column to zero mean and unit variance.

This is the classic z-score transform, (value - mean) / std_dev, applied per column. It is what distance-based and gradient-based models want from the raw numeric matrices these loaders return. Those columns routinely differ by orders of magnitude: for example, adult’s fnlwgt runs to the hundreds of thousands, while education-num goes no higher than 16.

This function computes the mean and standard deviation (population, that is, divided by n) over the finite values of each column. Non-finite entries stay untouched, so a NaN marking a missing value stays a NaN. A column with no variation gets a scale of 1 and maps to all zeros rather than dividing by 0.

§Parameters

  • features - The numeric feature matrix, shape (n_samples, n_features).

§Returns

  • (Array2<f64>, Scaler) - The standardized matrix, and the fitted per-column statistics to replay on later data with apply_scaler.

§Errors

  • DatasetError::ValidationError - Returns this when features has no rows or no columns.

§Example

use dataset_ml::preprocessing::standardize;
use ndarray::array;

let features = array![[1.0, 10.0], [2.0, 20.0], [3.0, 30.0]];
let (scaled, scaler) = standardize(&features).unwrap();

assert_eq!(scaler.center, array![2.0, 20.0]);
assert_eq!(scaled[[1, 0]], 0.0); // the mean row maps to 0
assert!((scaled[[0, 0]] + scaled[[2, 0]]).abs() < 1e-12); // symmetric about it