Skip to content

StandardScaler

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

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

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

The standard score of a sample xx is calculated as:

z=x−μσz = \frac{x - \mu}{\sigma}

where μ\mu is the mean of the training samples and σ\sigma is the standard deviation.


Parameter Description
compute_dtype Computational precision data type. Default DType.float64.

def __init__(out self, with_mean: Bool = True, with_std: Bool = True)

Initialize the StandardScaler.

Argument Type Description
with_mean Bool Whether to center data by subtracting feature means. Default True.
with_std Bool Whether to scale data to unit variance. Default True.

Attribute Description
mean_ Mean value for each feature in the training set.
scale_ Per-feature standard deviation scaling factor.
is_fitted Boolean flag indicating if estimator has been fitted.

Method Description
StandardScaler.fit() Compute fitted parameters from input matrix X.
StandardScaler.transform() Transform input features using the fitted parameters.
StandardScaler.fit_transform() Fit the estimator to X and return the transformed output.
StandardScaler.serialize() Serializes StandardScaler parameters and fitted state into BufferWriter.
StandardScaler.deserialize() Deserializes StandardScaler 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 serialize(self, mut writer: BufferWriter)

Serializes StandardScaler parameters and fitted state into BufferWriter.


def deserialize(mut reader: BufferReader) -> Self

Deserializes StandardScaler from BufferReader.

from strata.preprocessing import StandardScaler
from strata.core import Matrix
var scaler = StandardScaler[DType.float64]()
scaler.fit(X_train)
var X_scaled = scaler.transform(X_train)