mojolearn.StandardScaler#

class mojolearn.StandardScaler(*, copy=True, with_mean=True, with_std=True, numeric_mode=None)[source]#

Bases: _ScalerProtocol

Standardize features by removing the mean and scaling to unit variance.

Input must be a dense, nonempty two-dimensional float32 array: a float64 or integer array raises TypeError (nested Python lists of numbers are converted). Sparse input is not supported. Statistics are computed in float32, not in float64 as in scikit-learn. The variance is the population variance; a feature with exactly zero variance gets scale one, while a nearly constant feature is scaled by its (tiny) standard deviation.

NaN is treated as missing: fit ignores it per feature (a feature with no values gets NaN statistics) and transform keeps it. Infinity raises ValueError. The numeric mode and flags are captured by fit and kept through pickling.

Parameters:
  • copy (bool, default True) – If False, transform in place when X is already a writable C-contiguous float32 buffer.

  • with_mean (bool, default True) – Center the data before scaling.

  • with_std (bool, default True) – Scale the data to unit variance.

  • numeric_mode ({“fast”, “deterministic”, “identical”} or None, default None) – Numeric mode; None uses the library default.

Variables:
  • mean_ (Array of shape (n_features,), float32, or None) – Per-feature mean; None when both with_mean and with_std are False.

  • var_ (Array of shape (n_features,), float32, or None) – Per-feature variance; None when with_std=False.

  • scale_ (Array of shape (n_features,), float32, or None) – Per-feature scale; None when with_std=False.

  • n_features_in_ (int) – Number of features seen during fit.

  • n_samples_seen_ (int, float or Array) – Rows seen. With NaN values whose counts differ across features, a per-feature array; with sample_weight, the weight sum.

  • numeric_mode_ (str) – The numeric mode used by the fit.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> scaler = mojolearn.StandardScaler().fit(X)
>>> Z = scaler.transform(X)
__init__(*, copy=True, with_mean=True, with_std=True, numeric_mode=None)[source]#

Methods

fit(X[, y, sample_weight])

Compute the per-feature mean and variance.

fit_transform(X[, y])

Fit to X and return the standardized data.

get_params([deep])

inverse_transform(X[, copy])

Undo the standardization.

load(path)

Load a scaler written by save.

partial_fit(X[, y, sample_weight])

Update the running mean and variance with a batch of rows.

save(path)

Save the fitted scaler to path as an .npz file.

set_params(**params)

transform(X[, copy])

Standardize X.

fit(X, y=None, sample_weight=None)[source]#

Compute the per-feature mean and variance.

Parameters:
  • X (array-like of shape (n_samples, n_features), float32) – Training data; NaN is ignored, infinity is refused.

  • y (ignored)

  • sample_weight (array-like of shape (n_samples,), optional) – Weights for the mean and variance.

Returns:

self (StandardScaler) – The fitted scaler.

Raises:

ValueError – If X holds infinity or the statistics overflow float32.

fit_transform(X, y=None, **fit_params)[source]#

Fit to X and return the standardized data.

Parameters:
  • X (array-like of shape (n_samples, n_features), float32) – Training data.

  • y (ignored)

  • **fit_params – Passed to fit (for example sample_weight).

Returns:

X_scaled (Array of shape (n_samples, n_features), float32) – The standardized data.

partial_fit(X, y=None, sample_weight=None)[source]#

Update the running mean and variance with a batch of rows.

On an unfitted scaler this is fit. The batch’s count, mean and variance are merged into the running ones per feature; a feature the batch never saw keeps its values.

Parameters:
  • X (array-like of shape (n_samples, n_features), float32) – A batch of rows with the fitted feature count.

  • y (ignored)

  • sample_weight (array-like of shape (n_samples,), optional) – Weights for this batch.

Returns:

self (StandardScaler) – The updated scaler.

Raises:

ValueError – If with_mean or with_std changed since the first batch or the feature count differs.

transform(X, copy=None)[source]#

Standardize X.

Parameters:
  • X (array-like of shape (n_samples, n_features), float32) – Data with the fitted feature count; NaN is kept.

  • copy (bool or None, default None) – Overrides the copy parameter for this call.

Returns:

X_scaled (Array of shape (n_samples, n_features), float32) – The standardized data.

Raises:
  • NotFittedError – If the scaler is not fitted (a subclass of ValueError and AttributeError).

  • ValueError – If the feature count differs or the result overflows float32.

inverse_transform(X, copy=None)[source]#

Undo the standardization.

Parameters:
  • X (array-like of shape (n_samples, n_features), float32) – Standardized data.

  • copy (bool or None, default None) – Overrides the copy parameter for this call.

Returns:

X_original (Array of shape (n_samples, n_features), float32) – The data in its original units.

save(path)[source]#

Save the fitted scaler to path as an .npz file.

A scaler restored with StandardScaler.load or mojolearn.host_model(path) transforms, including on a CPU-only install.

Parameters:

path (str or path-like) – Destination file.

Raises:

RuntimeError – If the scaler is not fitted.

classmethod load(path)[source]#

Load a scaler written by save.

The loaded scaler transforms; arrays are read at their saved dtype and never cast.

Parameters:

path (str or path-like) – File written by StandardScaler.save.

Returns:

scaler (StandardScaler) – The restored scaler.

Raises:

ValueError – If the file is not a StandardScaler or its arrays are inconsistent.