TargetEncoder: m = :auto uses the inverse of the empirical Bayes ratio and gives NaN for single-row levels
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
- Domain
- machine-learning
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
mand is barely shrunk toward the prior, - a very consistent level gets a huge
mand 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, notNaN. (The sameNaNwould show up for any degenerate case likey_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,
devat 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from JuliaAI/MLJTransforms.jl
-
ContinuousEncoder docstring: wrong `drop_last` default and nonexistent `ordered_factors` optionPossibly taken @ablaom claimed this 1 day ago. Opendocumentation
Difficulty 1/5 Under an hour Newbie friendliness 88/100
JuliaAI/MLJTransforms.jl#109 · 1 comment · 1 reaction · 1 assignee ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
JuliaAI/MLJTransforms.jl#108 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
JuliaAI/MLJTransforms.jl#107 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
next breaking release
Difficulty 1/5 Under an hour Newbie friendliness 58/100
JuliaAI/MLJTransforms.jl#111 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
JuliaAI/MLJTransforms.jl#106 ·
Maintainers usually reply within 1 day
All issues in JuliaAI/MLJTransforms.jl
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
question
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
brown-ccv/Chamber.jl#272 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
JuliaAstro/SolarPosition.jl#143 ·
-
distributions
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
JuliaBayes/Wren.jl#404 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day