# custom_preprocessor.py from sklearn.base import BaseEstimator, TransformerMixin import pandas as pd class CustomPreprocessor(BaseEstimator, TransformerMixin): def __init__(self): # No hyperparameters right now; keep for sklearn compatibility pass def fit(self, X, y=None): # Stateless: nothing to fit, but keep method for Pipeline API return self def transform(self, X): # Work on a copy to avoid mutating caller data df = X.copy() # Step 1: Replace 'reg' with 'Regular' if "Product_Sugar_Content" in df.columns: df.loc[df["Product_Sugar_Content"] == "reg", "Product_Sugar_Content"] = "Regular" # Step 2: Process object columns obj_cols = df.select_dtypes(["object"]).columns for col in obj_cols: # Lowercase, replace spaces, then cast to category df[col] = ( df[col] .astype(str) .str.lower() .str.replace(" ", "_") .astype("category") ) # Step 3: Convert numeric columns to float32 for col in df.columns: if df[col].dtype.kind in "biufc": df[col] = df[col].astype("float32") return df