Normalizer
Module: strata.preprocessing • Kind: struct • Traits: Copyable, Movable, Transformer
Source: strata/preprocessing/normalizer.mojo
struct Normalizer[compute_dtype: DType = DType.float64](Copyable, Movable, Transformer)from strata.preprocessing import NormalizerNormalize samples individually to unit norm.
Each sample (i.e. each row of the data matrix) with at least one non-zero component is rescaled independently of other samples so that its norm (, or ) equals one.
where is the chosen vector norm:
- :
- :
- : Rows of all zeros remain all zeros.
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, norm: String = "l2")Initialize the Normalizer.
| Argument | Type | Description |
|---|---|---|
norm |
String |
The norm to normalize each sample with (‘l1’, ‘l2’, or ‘max’). Default ‘l2’. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
n_features_in_ |
Number of features seen during fit. |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
Normalizer.fit() |
Fit the transformer on feature matrix X. |
Normalizer.transform() |
Scale each non-zero sample in X to unit norm. |
Normalizer.fit_transform() |
Fit to data, then transform it. |
Method Details
Section titled “Method Details”Normalizer.fit()
Section titled “Normalizer.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.
Fit the transformer on feature matrix X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[in_dtype] |
Training data matrix with shape (n_samples, n_features). |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix. (Can be provided alternatively in place of X) |
Normalizer.transform()
Section titled “Normalizer.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.
Scale each non-zero sample in X to unit norm.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[in_dtype] |
Data matrix with shape (n_samples, n_features). |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix. (Can be provided alternatively in place of X) |
Returns: Matrix[in_dtype] — Matrix[in_dtype]: Normalized feature matrix.
Normalizer.fit_transform()
Section titled “Normalizer.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 to data, then transform it.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[in_dtype] |
Input matrix with shape (n_samples, n_features). |
dataset |
Dataset[feat_dtype, target_dtype] |
Dataset container holding feature matrix. (Can be provided alternatively in place of X) |
Returns: Matrix[in_dtype] — Matrix[in_dtype]: Normalized feature matrix.
Section titled “Returns: Matrix[in_dtype] — Matrix[in_dtype]: Normalized feature matrix.”Example
Section titled “Example”from strata.preprocessing import Normalizerfrom strata.core import Matrix
var normalizer = Normalizer[DType.float64](norm="l2")normalizer.fit(X_train)var X_norm = normalizer.transform(X_train)