LabelEncoder
Module: strata.preprocessing • Kind: struct • Traits: Copyable, Movable
Source: strata/preprocessing/encoders.mojo
struct LabelEncoder[compute_dtype: DType = DType.float64](Copyable, Movable)from strata.preprocessing import LabelEncoderEncode target labels with value between 0 and n_classes-1.
Used to transform non-numerical or non-consecutive 1D target labels into continuous integer labels for classification tasks.
Parameters (Compile-Time)
Section titled “Parameters (Compile-Time)”| Parameter | Description |
|---|---|
compute_dtype |
Precision used for internal class representation. Default DType.float64. |
Constructors
Section titled “Constructors”def __init__(out self)Initialize the LabelEncoder.
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
classes_ |
Distinct classes seen during fit, in sorted order. |
is_fitted |
Boolean flag indicating if estimator has been fitted. |
Methods Overview
Section titled “Methods Overview”| Method | Description |
|---|---|
LabelEncoder.fit() |
Fit label encoder on target vector y. |
LabelEncoder.transform() |
Transform target labels to normalized encoding indices. |
LabelEncoder.fit_transform() |
Fit label encoder and return encoded integer labels. |
LabelEncoder.inverse_transform() |
Transform integer labels back to original encoding. |
Method Details
Section titled “Method Details”LabelEncoder.fit()
Section titled “LabelEncoder.fit()”def fit[in_dtype: DType](mut self, y: List[Scalar[in_dtype]])Fit label encoder on target vector y.
| Parameter | Type | Description |
|---|---|---|
y |
List[Scalar[in_dtype]] |
Target label vector. |
LabelEncoder.transform()
Section titled “LabelEncoder.transform()”def transform[in_dtype: DType](self, y: List[Scalar[in_dtype]]) -> List[Int]Transform target labels to normalized encoding indices.
| Parameter | Type | Description |
|---|---|---|
y |
List[Scalar[in_dtype]] |
Target label vector to transform. |
Returns: List[Int] — List[Int]: Encoded integer labels in range [0, n_classes - 1].
LabelEncoder.fit_transform()
Section titled “LabelEncoder.fit_transform()”def fit_transform[in_dtype: DType]( mut self, y: List[Scalar[in_dtype]],) -> List[Int]Fit label encoder and return encoded integer labels.
| Parameter | Type | Description |
|---|---|---|
y |
List[Scalar[in_dtype]] |
Target label vector. |
Returns: List[Int] — List[Int]: Encoded integer labels.
LabelEncoder.inverse_transform()
Section titled “LabelEncoder.inverse_transform()”def inverse_transform[out_dtype: DType = Self.compute_dtype]( self, y: List[Int],) -> List[Scalar[out_dtype]]Transform integer labels back to original encoding.
| Parameter | Type | Description |
|---|---|---|
y |
List[Int] |
Encoded integer labels. |
Returns: List[Scalar[out_dtype]] — List[Scalar[out_dtype]]: Reconstructed original labels.
Section titled “Returns: List[Scalar[out_dtype]] — List[Scalar[out_dtype]]: Reconstructed original labels.”Example
Section titled “Example”from strata.preprocessing import LabelEncoder
var encoder = LabelEncoder[DType.float64]()encoder.fit(y_train)var y_encoded = encoder.transform(y_train)var y_original = encoder.inverse_transform(y_encoded)