#!/usr/bin/env python3
# Source: q1-ml-fundamentals.html
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold

def target_encode_oof(cat: pd.Series, y: pd.Series, m: float = 20.0, k: int = 5, seed: int = 0):
    """Out-of-fold, smoothed target encoding. Each row never sees its own label."""
    if len(cat) != len(y) or not cat.index.equals(y.index):
        raise ValueError("Categories and labels must have matching row indexes")
    if not np.isfinite(m) or m < 0:
        raise ValueError("Smoothing must be finite and nonnegative")
    # Fit this encoder inside each outer training fold for model evaluation.
    out = np.zeros(len(cat))
    for tr, va in KFold(k, shuffle=True, random_state=seed).split(cat):
        prior = y.iloc[tr].mean()  # No validation labels enter the prior.
        stats = y.iloc[tr].groupby(cat.iloc[tr]).agg(["sum", "count"])
        enc = (stats["sum"] + m * prior) / (stats["count"] + m)
        out[va] = cat.iloc[va].map(enc).fillna(prior).to_numpy()   # unseen -> prior
    return out
cat = pd.Series(['bee', 'honey', 'bee', 'wax', 'honey', 'wax'])
y = pd.Series([1, 0, 1, 0, 1, 0])
print(target_encode_oof(cat, y, k=3))
