Skip to content

MinMaxScaler

Module: strata.preprocessing • Kind: struct • Traits: Copyable, Movable, Serializable, Transformer
Source: strata/preprocessing/scaler.mojo

struct MinMaxScaler[compute_dtype: DType = DType.float64](Copyable, Movable, Serializable, Transformer)
from strata.preprocessing import MinMaxScaler

Transform features by scaling each feature to a specified range.

Scales and translates each feature individually such that it is in the given range on the training set, e.g. between zero and one:

xscaled=x−xmin⁡xmax⁡−xmin⁡⋅(max−min)+minx_{\text{scaled}} = \frac{x - x_{\min}}{x_{\max} - x_{\min}} \cdot (\text{max} - \text{min}) + \text{min}
Parameter Description
compute_dtype Computational precision data type. Default DType.float64.

def __init__(
out self,
feature_range_min: Scalar[Self.compute_dtype] = 0.0,
feature_range_max: Scalar[Self.compute_dtype] = 1.0,
clip: Bool = False,
)

Initialize the MinMaxScaler.

Argument Type Description
feature_range_min Scalar[Self.compute_dtype] Lower bound of the transformed range. Default 0.0.
feature_range_max Scalar[Self.compute_dtype] Upper bound of the transformed range. Default 1.0.
clip Bool Whether to clip transformed values to the feature range. Default False.

Attribute Description
data_min_ Per-feature minimum seen in the training data.
data_max_ Per-feature maximum seen in the training data.
data_range_ Per-feature range (xmax⁡−xmin⁡x_{\max} - x_{\min}) seen in the data.
scale_ Per-feature relative scaling factor.
min_ Per-feature minimum adjustment.
is_fitted Boolean flag indicating if estimator has been fitted.

Method Description
MinMaxScaler.fit() Compute fitted parameters from input matrix X.
MinMaxScaler.transform() Transform input features using the fitted parameters.
MinMaxScaler.fit_transform() Fit the estimator to X and return the transformed output.
MinMaxScaler.inverse_transform() Undoes the scaling of X according to the fitted feature range.
MinMaxScaler.serialize() Serializes MinMaxScaler parameters and fitted state into BufferWriter.
MinMaxScaler.deserialize() Deserializes MinMaxScaler from BufferReader.

def fit[in_dtype: DType](mut self, X: Matrix[in_dtype])
def fit[feat_dtype: DType, target_dtype: DType](
mut self,
dataset: Dataset[feat_dtype, target_dtype],
)

Overload Note: This method accepts either a standard X: Matrix or a unified dataset: Dataset container.

Compute fitted parameters from input matrix X.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix. (Can be provided alternatively in place of X)

def transform[in_dtype: DType](
self,
X: Matrix[in_dtype],
) -> Matrix[in_dtype]
def transform[feat_dtype: DType, target_dtype: DType](
self,
dataset: Dataset[feat_dtype, target_dtype],
) -> Dataset[ feat_dtype, target_dtype ]

Overload Note: This method accepts either a standard X: Matrix or a unified dataset: Dataset container.

Transform input features using the fitted parameters.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix. (Can be provided alternatively in place of X)

Returns: Matrix[in_dtype]


def fit_transform[in_dtype: DType](
mut self,
X: Matrix[in_dtype],
) -> Matrix[in_dtype]
def fit_transform[feat_dtype: DType, target_dtype: DType](
mut self,
dataset: Dataset[feat_dtype, target_dtype],
) -> Dataset[ feat_dtype, target_dtype ]

Overload Note: This method accepts either a standard X: Matrix or a unified dataset: Dataset container.

Fit the estimator to X and return the transformed output.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.
dataset Dataset[feat_dtype, target_dtype] Dataset container holding feature matrix. (Can be provided alternatively in place of X)

Returns: Matrix[in_dtype]


def inverse_transform[in_dtype: DType](
self,
X: Matrix[in_dtype],
) -> Matrix[in_dtype]

Undoes the scaling of X according to the fitted feature range.

Parameter Type Description
X Matrix[in_dtype] Feature matrix.

Returns: Matrix[in_dtype]


def serialize(self, mut writer: BufferWriter)

Serializes MinMaxScaler parameters and fitted state into BufferWriter.


def deserialize(mut reader: BufferReader) -> Self

Deserializes MinMaxScaler from BufferReader.

from strata.preprocessing import MinMaxScaler
from strata.core import Matrix
var scaler = MinMaxScaler[DType.float64](feature_range_min=0.0, feature_range_max=1.0)
scaler.fit(X_train)
var X_scaled = scaler.transform(X_train)