RobustScaler
Module: strata.preprocessing • Kind: struct • Traits: Copyable, Movable, Serializable, Transformer
Source: strata/preprocessing/scaler.mojo
struct RobustScaler[compute_dtype: DType = DType.float64](Copyable, Movable, Serializable, Transformer)from strata.preprocessing import RobustScalerScale features using statistics that are robust to outliers.
Centers the data on the median and scales by the Interquartile Range (IQR):
where (by default 75th percentile minus 25th percentile).
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_centering: Bool = True, with_scaling: Bool = True, quantile_min: Float64 = 25.0, quantile_max: Float64 = 75.0,)Initialize the RobustScaler.
| Argument | Type | Description |
|---|---|---|
with_centering |
Bool |
Whether to center data by subtracting the median. Default True. |
with_scaling |
Bool |
Whether to scale data to the quantile range. Default True. |
quantile_min |
Float64 |
Lower quantile percentage of the scaling range. Default 25.0. |
quantile_max |
Float64 |
Upper quantile percentage of the scaling range. Default 75.0. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
center_ |
Median value for each feature in the training set. |
scale_ |
Interquartile range scaling factor for each feature. |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
RobustScaler.fit() |
Compute fitted parameters from input matrix X. |
RobustScaler.transform() |
Transform input features using the fitted parameters. |
RobustScaler.fit_transform() |
Fit the estimator to X and return the transformed output. |
RobustScaler.inverse_transform() |
Undoes the centering and scaling of X. |
RobustScaler.serialize() |
Serializes RobustScaler parameters and fitted state into BufferWriter. |
RobustScaler.deserialize() |
Deserializes RobustScaler from BufferReader. |
Method Details
Section titled “Method Details”RobustScaler.fit()
Section titled “RobustScaler.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) |
RobustScaler.transform()
Section titled “RobustScaler.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]
RobustScaler.fit_transform()
Section titled “RobustScaler.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]
RobustScaler.inverse_transform()
Section titled “RobustScaler.inverse_transform()”def inverse_transform[in_dtype: DType]( self, X: Matrix[in_dtype],) -> Matrix[in_dtype]Undoes the centering and scaling of X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[in_dtype] |
Feature matrix. |
Returns: Matrix[in_dtype]
RobustScaler.serialize()
Section titled “RobustScaler.serialize()”def serialize(self, mut writer: BufferWriter)Serializes RobustScaler parameters and fitted state into BufferWriter.
RobustScaler.deserialize()
Section titled “RobustScaler.deserialize()”def deserialize(mut reader: BufferReader) -> SelfDeserializes RobustScaler from BufferReader.
Returns: Self
Section titled “Returns: Self”Example
Section titled “Example”from strata.preprocessing import RobustScalerfrom strata.core import Matrix
var scaler = RobustScaler[DType.float64]()scaler.fit(X_train)var X_scaled = scaler.transform(X_train)