OneHotEncoder
Module: strata.preprocessing • Kind: struct • Traits: Copyable, Movable, Transformer
Source: strata/preprocessing/encoders.mojo
struct OneHotEncoder[compute_dtype: DType = DType.float64](Copyable, Movable, Transformer)from strata.preprocessing import OneHotEncoderEncode categorical features as a one-hot numeric array.
The input to this transformer should be a 2D matrix of integer or float categorical features. The features are encoded using a one-hot (also known as ‘one-of-K’ or ‘dummy’) encoding scheme.
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, drop: String = "none", handle_unknown: String = "error",)Initialize the OneHotEncoder.
| Argument | Type | Description |
|---|---|---|
drop |
String |
Category dropping strategy (‘none’, ‘first’, ‘if_binary’). Default ‘none’. |
handle_unknown |
String |
Behavior for unseen categories (‘error’, ‘ignore’). Default ‘error’. |
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
categories_ |
Categories of each feature determined during fitting. |
drop_idx_ |
Indices of dropped categories for each feature. |
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 |
|---|---|
OneHotEncoder.n_features_out() |
Number of indicator columns produced by transform. |
OneHotEncoder.fit() |
Compute fitted parameters from input matrix X. |
OneHotEncoder.transform() |
Transform input features using the fitted parameters. |
OneHotEncoder.get_feature_names_out() |
Output column names as ’ |
OneHotEncoder.fit_transform() |
Fit the estimator to X and return the transformed output. |
OneHotEncoder.inverse_transform() |
Recovers the original categorical values from a one-hot matrix. |
Method Details
Section titled “Method Details”OneHotEncoder.n_features_out()
Section titled “OneHotEncoder.n_features_out()”def n_features_out(self) -> IntNumber of indicator columns produced by transform.
Returns: Int
OneHotEncoder.fit()
Section titled “OneHotEncoder.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) |
OneHotEncoder.transform()
Section titled “OneHotEncoder.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]
OneHotEncoder.get_feature_names_out()
Section titled “OneHotEncoder.get_feature_names_out()”def get_feature_names_out( self, input_features: List[String] = List[String](),) -> List[String]Output column names as ’
| Parameter | Type | Description |
|---|---|---|
input_features |
List[String] |
— |
Returns: List[String]
OneHotEncoder.fit_transform()
Section titled “OneHotEncoder.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]
OneHotEncoder.inverse_transform()
Section titled “OneHotEncoder.inverse_transform()”def inverse_transform[in_dtype: DType]( self, X: Matrix[in_dtype],) -> Matrix[in_dtype]Recovers the original categorical values from a one-hot matrix.
| Parameter | Type | Description |
|---|---|---|
X |
Matrix[in_dtype] |
Feature matrix. |
Returns: Matrix[in_dtype]
Section titled “Returns: Matrix[in_dtype]”Example
Section titled “Example”from strata.preprocessing import OneHotEncoderfrom strata.core import Matrix
var encoder = OneHotEncoder[DType.float64](drop="if_binary")encoder.fit(X_cat)var X_encoded = encoder.transform(X_cat)