The LabelEncoder
class from scikit-learn's preprocessing
module converts non-numerical labels into numerical labels. It provides three key methods: fit()
, transform()
, and inverse_transform()
, which are used to encode, transform, and decode labels respectively.
from sklearn.preprocessing import LabelEncoder
"""
Module Description:
This module provides utility functions for data preprocessing.
Author:
Llama (Large Language Model)
"""
import sklearn.preprocessing as le # Import Label Encoder as le for readability
from sklearn.exceptions import NotFittedError # Import NotFittedError for type hint
class LabelEncoderWrapper:
"""Wrapper class for LabelEncoder with additional functionality."""
def __init__(self):
"""
Initialize LabelEncoderWrapper instance.
Attributes:
- le (LabelEncoder): The LabelEncoder instance.
"""
self.le = le.LabelEncoder() # Initialize LabelEncoder instance
def fit_transform(self, y: list) -> list:
"""
Fit LabelEncoder to data and transform it.
Args:
- y (list): The data to fit and transform.
Returns:
- list: The transformed data.
Raises:
- NotFittedError: If the LabelEncoder is not fitted.
"""
try:
self.le.fit(y) # Try to fit the LabelEncoder
except NotFittedError:
self.le.fit(y) # If not fitted, fit it
return self.le.transform(y) # Return the transformed data
def transform(self, y: list) -> list:
"""
Transform data using the fitted LabelEncoder.
Args:
- y (list): The data to transform.
Returns:
- list: The transformed data.
Raises:
- NotFittedError: If the LabelEncoder is not fitted.
"""
if not self.le.fit_available: # Check if the LabelEncoder is fitted
raise NotFittedError("LabelEncoder is not fitted")
return self.le.transform(y) # Return the transformed data
The LabelEncoder
class from scikit-learn's preprocessing
module is used to convert non-numerical labels (such as class labels) into numerical labels.
fit()
: Fits the encoder to the data, i.e., finds the classes and their order.transform()
: Transforms the labels by assigning a numerical value to each class label.inverse_transform()
: Transforms the numerical labels back to the original class labels.