Skip to content

Target processor

fyt.core.processing.target_processor

TargetProcessor

Bases: BaseEstimator

TargetProcessor class for encoding target variables.

Supports various encoding strategies including: - LabelEncoder: Automatic label encoding - OrdinalEncoder: Encoding with optional explicit class-to-label mapping - Passthrough: No encoding applied

Example with explicit mapping

config = TargetProcessorConfig( ... target_encoding=TargetEncodingConfig( ... strategy=TargetEncodingStrategy.ORDINAL_ENCODER, ... class_mapping={"negative": 0, "positive": 1}, ... ) ... ) processor = TargetProcessor(config) y_encoded = processor.fit_transform(y_train)

class_mapping property

The class-to-label mapping.

Returns:

Type Description
dict[str, int] | None

Dictionary mapping class labels to integers, or None if not available.

Raises:

Type Description
RuntimeError

If the TargetProcessor has not been fitted yet.

classes property

The classes from the fitted encoder.

Returns:

Type Description
ndarray | None

Array of class labels or None if not available.

Raises:

Type Description
RuntimeError

If the TargetProcessor has not been fitted yet.

encoder property

The fitted encoder.

Returns:

Type Description

The fitted encoder instance or None for passthrough.

Raises:

Type Description
RuntimeError

If the TargetProcessor has not been fitted yet.

__init__(config, task=TaskType.CLASSIFICATION)

Initialize the TargetProcessor.

Parameters:

Name Type Description Default
config TargetProcessorConfig

Configuration for target processing.

required
task TaskType

Learning task type. For regression the target is passed through unencoded (a numeric dtype is required); any configured encoding strategy is ignored with a warning.

CLASSIFICATION

fit(y, X=None)

Fit the target encoder.

Parameters:

Name Type Description Default
y Series | ndarray

Target variable (pl.Series or array-like).

required
X DataFrame | ndarray | None

Ignored. Present for API consistency.

None

Returns:

Name Type Description
TargetProcessor TargetProcessor

Fitted TargetProcessor instance.

fit_transform(y, X=None, **fit_params)

Fit the target encoder and transform the target variable in one step.

Parameters:

Name Type Description Default
y Series | ndarray

Target variable (pl.Series or array-like).

required
X DataFrame | ndarray | None

Ignored. Present for API consistency.

None
**fit_params object

Ignored. Present for sklearn API consistency.

{}

Returns:

Type Description

Transformed target variable, same container type as the input.

inverse_transform(y)

inverse_transform(y: np.ndarray) -> np.ndarray
inverse_transform(y: pl.Series) -> pl.Series

Inverse transform the encoded target variable back to original labels.

Parameters:

Name Type Description Default
y Series | ndarray

Encoded target variable (pl.Series or array-like).

required

Returns:

Type Description
Series | ndarray

Original target labels. Returns pl.Series if input is pl.Series,

Series | ndarray

otherwise returns np.ndarray.

Raises:

Type Description
RuntimeError

If the TargetProcessor has not been fitted yet.

ValueError

If encoder doesn't support inverse_transform.

transform(y)

transform(y: np.ndarray) -> np.ndarray
transform(y: pl.Series) -> pl.Series

Transform the target variable.

Parameters:

Name Type Description Default
y Series | ndarray

Target variable to transform (pl.Series or array-like).

required

Returns:

Type Description
Series | ndarray

Transformed target variable. Returns pl.Series if input is pl.Series,

Series | ndarray

otherwise returns np.ndarray.

Raises:

Type Description
RuntimeError

If the TargetProcessor has not been fitted yet.