Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-331

Leave-One-Out Target Encoding

Problem

Leave-one-out target encoding: for each sample whose category appears more than once, encode it as (sum of the category's targets minus its own target) / (count - 1). Categories seen once fall back to the global target mean. Return encodings in the original order; empty input returns [].

Hint

Excluding the sample's own target prevents the encoding from leaking its label.

Test cases

case 1
in: [["a","a","b","b","b"],[1,0,1,1,0]]
expected: [0,1,0.5,0.5,1]
case 2
in: [["x"],[2]]
expected: [2]
case 3
in: [[],[]]
expected: []
case 4
in: [["a","b","a"],[1,1,1]]
expected: [1,1,1]
Starter codePython
def loo_target_encode(categories, targets):
    # Your code here
    pass

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