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 StandardScalerStandardize features by removing the mean and scaling to unit variance.
The standard score of a sample is calculated as:
where is the mean of the training samples and is the standard deviation.
Parameters (Compile-Time)
Section titled “Parameters (Compile-Time)”| Parameter | Description |
|---|---|
compute_dtype |
Computational precision data type. Default DType.float64. |
Constructors
Section titled “Constructors”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. |
Attributes
Section titled “Attributes”| 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. |
Methods Overview
Section titled “Methods Overview”| 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. |
Method Details
Section titled “Method Details”StandardScaler.fit()
Section titled “StandardScaler.fit()”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: Matrixor a unifieddataset: Datasetcontainer.
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) |
StandardScaler.transform()
Section titled “StandardScaler.transform()”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: Matrixor a unifieddataset: Datasetcontainer.
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]
StandardScaler.fit_transform()
Section titled “StandardScaler.fit_transform()”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: Matrixor a unifieddataset: Datasetcontainer.
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]
StandardScaler.serialize()
Section titled “StandardScaler.serialize()”def serialize(self, mut writer: BufferWriter)Serializes StandardScaler parameters and fitted state into BufferWriter.
StandardScaler.deserialize()
Section titled “StandardScaler.deserialize()”def deserialize(mut reader: BufferReader) -> SelfDeserializes StandardScaler from BufferReader.
Returns: Self
Section titled “Returns: Self”Example
Section titled “Example”from strata.preprocessing import StandardScalerfrom strata.core import Matrix
var scaler = StandardScaler[DType.float64]()scaler.fit(X_train)var X_scaled = scaler.transform(X_train)