Skip to content

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 OneHotEncoder

Encode 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.


Parameter Description
compute_dtype Computational precision data type. Default DType.float64.

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’.

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.

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 ’_’ pairs.
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.

def n_features_out(self) -> Int

Number of indicator columns produced by transform.

Returns: Int


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: Matrix or a unified dataset: Dataset container.

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)

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: Matrix or a unified dataset: Dataset container.

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]


def get_feature_names_out(
self,
input_features: List[String] = List[String](),
) -> List[String]

Output column names as ’_’ pairs.

Parameter Type Description
input_features List[String] —

Returns: List[String]


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: Matrix or a unified dataset: Dataset container.

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]


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.
from strata.preprocessing import OneHotEncoder
from strata.core import Matrix
var encoder = OneHotEncoder[DType.float64](drop="if_binary")
encoder.fit(X_cat)
var X_encoded = encoder.transform(X_cat)