mojolearn.MinMaxScaler#
- class mojolearn.MinMaxScaler(feature_range=(0, 1), *, copy=True, clip=False, numeric_mode=None)[source]#
Bases:
_ScalerProtocolScale 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 raisesTypeError(nested Python lists of numbers are converted). Sparse input is not supported. The endpoints offeature_rangeare 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:
fitignores it (a feature with no values gets NaN statistics) andtransformkeeps it. Infinity raisesValueError. The numeric mode is resolved atfitand 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, withmin < maxafter rounding.copy (bool, default True) – If False, transform in place when
Xis already a writable C-contiguous float32 buffer.clip (bool, default False) – Clip transformed values to
feature_range. Values lost to clipping are not recovered byinverse_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_rangeas rounded to float32.clip_ (bool) – The
clipsetting 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)
Methods
fit(X[, y, sample_weight])Compute the per-feature minimum and maximum.
fit_transform(X[, y])get_params([deep])Undo the scaling.
load(path)Load a scaler written by
save.partial_fit(X[, y])Update the running minimum and maximum with a batch of rows.
save(path)Save the fitted scaler to
pathas an.npzfile.set_params(**params)transform(X)Scale
Xtofeature_range.- 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_rangechanged since the first batch or the feature count differs.
- transform(X)[source]#
Scale
Xtofeature_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
ValueErrorandAttributeError).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
pathas an.npzfile.A scaler restored with
MinMaxScaler.loadormojolearn.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.