Реализуйте Target Encoding с регуляризацией (smoothing).
encoding = (count × category_mean + smooth × global_mean) / (count + smooth)
Задача основана на вопросах с собеседований в Сбер.
def target_encode(categories: list[str], targets: list[float], smooth: float) -> list[float]:
Результат округлён до 4 знаков.
target_encode(["A","B","A","B","A","C"], [1,0,1,0,0,1], 1)
→ [0.625, 0.1667, 0.625, 0.1667, 0.625, 0.75]categories = ["A","B","A","B","A","C"]targets = [1,0,1,0,0,1]smooth = 1[0.625,0.1667,0.625,0.1667,0.625,0.75]categories = ["X","X","X","Y","Y"]targets = [10,20,30,40,50]smooth = 2[24,24,24,37.5,37.5]categories = ["A","B","C"]targets = [1,0,1]smooth = 0[1,0,1]