TargetEncoder: m = :auto uses the inverse of the empirical Bayes ratio and gives NaN for single-row levels

Open Beginner friendly
#113 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active

Research direction

Start with compute_m_auto in src/encoders/target_encoding/target_encoding.jl and the “Compute_m_auto tests” in test/encoders/target_encoding.jl. Check how the variance ratio and single-row or degenerate target levels affect shrinkage. Update the tests to cover the corrected behavior; done means m = :auto uses the stated ratio and produces finite encoded values for single-row levels.

Written by the indexing model from the issue text.

Description

With m = :auto, TargetEncoder computes the per-level m the wrong way round, and it returns NaN for any level that appears only once in the training data.

In src/encoders/target_encoding/target_encoding.jl#L87-L93:

function compute_m_auto(task, targets_for_level; y_var)
    ...
    y_var_level = std(targets_for_level)^2
    m = y_var / y_var_level
    return m
end

and then λ = n / (m + n) in compute_shrinkage.

In the empirical Bayes estimate from Micci-Barreca (the reference in the docstring) m is the within-level variance over the overall variance, i.e. m = σ²_level / τ² (this is also what scikit-learn's TargetEncoder(smooth="auto") uses, citing the same paper). The current code uses the inverse, so:

  • a noisy level (large within-level variance) gets a small m and is barely shrunk toward the prior,
  • a very consistent level gets a huge m and is shrunk almost entirely to the global mean.

That's the opposite of what the shrinkage is meant to do.

Second problem: std of a single value is NaN, so a level with one row gets m = NaN, λ = NaN, and the encoded value is NaN.

Example
using MLJBase, MLJTransforms, CategoricalArrays, Statistics

X = (A = categorical(["a", "a", "a", "a", "b", "b", "b", "b", "c"]),)
y = [0.0, 10.0, 0.0, 10.0, 4.0, 4.1, 3.9, 4.0, 7.0]

mach = fit!(machine(TargetEncoder(m = :auto), X, y), verbosity = 0)
Xt = MLJBase.transform(mach, X)
println("encoded: ", Xt.A)

# what the encoder does per level (m = y_var / y_var_level)
y_var = var(y)
for lvl in ["a", "b", "c"]
    t = y[X.A .== lvl]
    n = length(t)
    m_cur = y_var / var(t)
    m_eb  = var(t) / y_var                      # within-level variance / overall variance
    λ_cur = n / (m_cur + n)
    λ_eb  = n / (m_eb + n)
    println(rpad(lvl, 2), " n=$n  level mean=$(round(mean(t), digits=3))  level var=$(round(var(t), digits=4))",
            "  | current: m=$(round(m_cur, digits=3)) λ=$(round(λ_cur, digits=3))",
            "  | var_level/var_y: m=$(round(m_eb, digits=3)) λ=$(round(λ_eb, digits=3))")
end
println("overall mean = ", round(mean(y), digits=3), ", overall var = ", round(y_var, digits=3))

Output:

encoded: [4.979641611790194, 4.979641611790194, 4.979641611790194, 4.979641611790194, 4.776238417574363, 4.776238417574363, 4.776238417574363, 4.776238417574363, NaN]
a  n=4  level mean=5.0  level var=33.3333  | current: m=0.403 λ=0.908  | var_level/var_y: m=2.479 λ=0.617
b  n=4  level mean=4.0  level var=0.0067  | current: m=2017.042 λ=0.002  | var_level/var_y: m=0.0 λ=1.0
c  n=1  level mean=7.0  level var=NaN  | current: m=NaN λ=NaN  | var_level/var_y: m=NaN λ=NaN

Level b (values 3.9–4.1) is encoded as 4.776, basically the global mean, while the noisy level a (0, 10, 0, 10) keeps ~91% of its own mean. Level c comes out as NaN.

Expected
  • m = y_var_level / y_var, so consistent levels keep their own mean and noisy ones are pulled toward the prior.
  • A single-row level should give a finite value. scikit-learn uses the population variance (sum of squared deviations / n), which is 0 for one row and gives λ = 1; alternatively fall back to the prior. Either way, not NaN. (The same NaN would show up for any degenerate case like y_var == 0.)

The existing unit tests in test/encoders/target_encoding.jl ("Compute_m_auto tests") assert the current ratio (2.0 / 2.5, 100.0 / 250.0), so they'd need updating along with the fix.

Versions
  • MLJTransforms v0.2.4, dev at d3e3db3
  • MLJBase 1.15.4, CategoricalArrays 1.1.1
  • Julia 1.13.3
Dominant language
Julia
Stars
1
Forks
5
Avg merge
1d 4h
Merged PRs (30d)
6

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaAI/MLJTransforms.jl

All issues in JuliaAI/MLJTransforms.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.