mojolearn.preprocessing#

Feature scaling: StandardScaler and MinMaxScaler.

Both scalers take dense two-dimensional float32 input, compute their statistics in float32, treat NaN as a missing value and support the numeric_mode setting. Other preprocessing estimators (encoders, imputers, transformers) are available from the top-level mojolearn namespace.

Classes

MinMaxScaler([feature_range, copy, clip, ...])

Scale each feature to a given range.

StandardScaler(*[, copy, with_mean, ...])

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

class mojolearn.preprocessing.MinMaxScaler(feature_range=(0, 1), *, copy=True, clip=False, numeric_mode=None)[source]#

Bases: _ScalerProtocol

Scale each feature to a given range.

Computes each feature’s minimum and maximum and maps them linearly onto feature_range. 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. The endpoints of feature_range are rounded to float32. A feature whose range is below 10 float32 epsilons (including a constant feature) uses a denominator of one.

NaN is treated as missing: fit ignores it (a feature with no values gets NaN statistics) and transform keeps it. Infinity raises ValueError. The numeric mode is resolved at fit and kept for later transforms, including after pickling.

Parameters:
  • feature_range (tuple of two floats, default (0, 1)) – Target range (min, max); both finite in float32, with min < max after rounding.

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

  • clip (bool, default False) – Clip transformed values to feature_range. Values lost to clipping are not recovered by inverse_transform.

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

Variables:
  • data_min_ (Array of shape (n_features,), float32) – Per-feature minimum seen.

  • data_max_ (Array of shape (n_features,), float32) – Per-feature maximum seen.

  • data_range_ (Array of shape (n_features,), float32) – data_max_ - data_min_.

  • scale_ (Array of shape (n_features,), float32) – Per-feature scale factor.

  • min_ (Array of shape (n_features,), float32) – Per-feature offset.

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

  • n_samples_seen_ (int) – Number of rows processed.

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

  • feature_range_ (tuple of float) – feature_range as rounded to float32.

  • clip_ (bool) – The clip setting captured by the fit.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> scaler = mojolearn.MinMaxScaler().fit(X)
>>> Z = scaler.transform(X)
fit(X, y=None, sample_weight=None)[source]#

Compute the per-feature minimum and maximum.

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

  • y (ignored)

  • sample_weight (None) – Not supported; any value raises NotImplementedError.

Returns:

self (MinMaxScaler) – The fitted scaler.

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

Update the running minimum and maximum with a batch of rows.

On an unfitted scaler this is fit. A feature with NaN statistics stays NaN.

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

  • y (ignored)

Returns:

self (MinMaxScaler) – The updated scaler.

Raises:

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

transform(X)[source]#

Scale X to feature_range.

Parameters:

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

Returns:

X_scaled (Array of shape (n_samples, n_features), float32) – The scaled 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)[source]#

Undo the scaling.

Parameters:

X (array-like of shape (n_samples, n_features), float32) – Scaled data.

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 MinMaxScaler.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 MinMaxScaler.save.

Returns:

scaler (MinMaxScaler) – The restored scaler.

Raises:

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

class mojolearn.preprocessing.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)
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.