TargetEncoder: m = :auto uses the inverse of the empirical Bayes ratio and gives NaN for single-row levels
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- machine-learning
Direzione di ricerca
Inizia da compute_m_auto in src/encoders/target_encoding/target_encoding.jl e dai «test di Compute_m_auto» in test/encoders/target_encoding.jl. Verifica come il rapporto tra le varianze e i livelli target con una sola riga o degeneri influiscono sullo shrinkage. Aggiorna i test per coprire il comportamento corretto; il lavoro è completato quando m = :auto usa il rapporto indicato e produce valori codificati finiti per i livelli con una sola riga.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Julia
- Stelle
- 1
- Fork
- 5
- Merge medio
- 1g 4h
- PR unite (30g)
- 6
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di JuliaAI/MLJTransforms.jl
-
ContinuousEncoder docstring: wrong `drop_last` default and nonexistent `ordered_factors` optionForse già presa @ablaom l’ha presa 1 giorno fa. Apertadocumentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
JuliaAI/MLJTransforms.jl#109 · 1 commento · 1 reazione · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
JuliaAI/MLJTransforms.jl#108 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
JuliaAI/MLJTransforms.jl#107 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
next breaking release
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 58/100
JuliaAI/MLJTransforms.jl#111 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 72/100
JuliaAI/MLJTransforms.jl#106 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di JuliaAI/MLJTransforms.jl
Issue simili
-
Overload == for ZGroupAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
-
question
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
brown-ccv/Chamber.jl#272 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
JuliaAstro/SolarPosition.jl#143 ·
-
distributions
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
JuliaBayes/Wren.jl#404 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno