Skip to main content

apply_scaler

Function apply_scaler 

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

Apply an already-fitted Scaler to a feature matrix.

Use this to replay a training-fitted scaler onto the test rows, or onto later data, without refitting. Refitting would give the two sets different transforms and leak test statistics into training.

Non-finite entries stay untouched, matching the fitting functions.

§Parameters

  • features - The numeric feature matrix to transform, shape (n_samples, n_features).
  • scaler - Statistics from a previous standardize or min_max_scale call.

§Returns

  • Array2<f64> - The transformed matrix, with the same shape as features.

§Errors

  • DatasetError::LengthMismatch - Returns this when features has a different number of columns than the scaler expects.

§Example

use dataset_ml::preprocessing::{apply_scaler, standardize};
use ndarray::array;

let train = array![[1.0], [2.0], [3.0]];
let (_scaled_train, scaler) = standardize(&train).unwrap();

// This transforms the test rows with the training statistics, not their own.
let test = array![[2.0], [4.0]];
let scaled_test = apply_scaler(&test, &scaler).unwrap();
assert_eq!(scaled_test[[0, 0]], 0.0); // 2.0 was the training mean