OrdinalEncoder
Module: strata.preprocessing • Kind: struct • Traits: Copyable, Movable, Transformer
Source: strata/preprocessing/encoders.mojo
struct OrdinalEncoder[compute_dtype: DType = DType.float64](Copyable, Movable, Transformer)from strata.preprocessing import OrdinalEncoderEncode categorical features as an integer array.
The input to this transformer should be a 2D matrix of integer or float categorical features. The features are converted to ordinal integers () corresponding to the sorted order of unique categories per feature.
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, handle_unknown: String = "error", unknown_value: Scalar[Self.compute_dtype] = -1.0,)Initialize the OrdinalEncoder.
| Argument | Type | Description |
|---|---|---|
handle_unknown |
String |
Behavior for unseen categories (‘error’, ‘use_encoded_value’). Default ‘error’. |
unknown_value |
Scalar[Self.compute_dtype] |
Numerical value assigned to unseen categories when handle_unknown=‘use_encoded_value’. Default -1.0. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
categories_ |
Categories of each feature determined during fitting. |
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 |
|---|---|
OrdinalEncoder.fit() |
Fit the OrdinalEncoder on feature matrix X. |
OrdinalEncoder.transform() |
Transform X to ordinal integer codes. |
OrdinalEncoder.fit_transform() |
Fit to data, then transform it. |
OrdinalEncoder.inverse_transform() |
Convert the ordinal codes back to original category values. |
Method Details
Section titled “Method Details”OrdinalEncoder.fit()
Section titled “OrdinalEncoder.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 OrdinalEncoder on feature matrix X.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[in_dtype] |
Categorical 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) |
OrdinalEncoder.transform()
Section titled “OrdinalEncoder.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 X to ordinal integer codes.
| 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]: Encoded ordinal matrix.
OrdinalEncoder.fit_transform()
Section titled “OrdinalEncoder.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]: Encoded ordinal matrix.
OrdinalEncoder.inverse_transform()
Section titled “OrdinalEncoder.inverse_transform()”def inverse_transform[in_dtype: DType]( self, X: Matrix[in_dtype],) -> Matrix[in_dtype]Convert the ordinal codes back to original category values.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[in_dtype] |
Encoded data matrix with shape (n_samples, n_features). |
Returns: Matrix[in_dtype] — Matrix[in_dtype]: Decoded matrix containing reconstructed categories.
Section titled “Returns: Matrix[in_dtype] — Matrix[in_dtype]: Decoded matrix containing reconstructed categories.”Example
Section titled “Example”from strata.preprocessing import OrdinalEncoderfrom strata.core import Matrix
var encoder = OrdinalEncoder[DType.float64]()encoder.fit(X_cat)var X_ord = encoder.transform(X_cat)