Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-087

Target Encoding with Smoothing

Problem

Target-encode categories with additive smoothing: encoding(c) = (sum of targets in c + m * prior) / (count_c + m), where prior is the global target mean and m is the smoothing strength.

Return a dict mapping str(category) to encoding. Empty input returns {}.

Hint

Smoothing pulls rare categories toward the global mean; keys are stringified labels.

Test cases

case 1
in: [["a","a","b","b","b"],[1,0,1,1,0],2]
expected: {"a":0.55,"b":0.64}
case 2
in: [["a","a","b","b","b"],[1,0,1,1,0],0]
expected: {"a":0.5,"b":0.6666666666666666}
case 3
in: [["x"],[2],1]
expected: {"x":2}
case 4
in: [[],[],1]
expected: {}
Starter codePython
def target_encode(categories, targets, smoothing):
    # Your code here
    pass

4 test cases · run your code against them in the editor