TargetEncoder: m = :auto uses the inverse of the empirical Bayes ratio and gives NaN for single-row levels
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 78/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
src/encoders/target_encoding/target_encoding.jl の compute_m_auto と、test/encoders/target_encoding.jl の「Compute_m_auto tests」から始めてください。分散比と、1行のみまたは退化した目的変数レベルが縮小にどう影響するかを確認してください。修正後の動作を網羅するようテストを更新してください。完了条件は、m = :auto が指定された比率を使用し、1行のみのレベルに対して有限のエンコード値を生成することです。
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Julia
- スター
- 1
- フォーク
- 5
- 平均マージ
- 1日 4時間
- マージ済み PR(30日)
- 6
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
JuliaAI/MLJTransforms.jl のほかの issue
-
ContinuousEncoder docstring: wrong `drop_last` default and nonexistent `ordered_factors` option対応中かも @ablaom が 1 日前に担当しました。 オープンdocumentation
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
JuliaAI/MLJTransforms.jl#109 · コメント 1 件 · リアクション 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
JuliaAI/MLJTransforms.jl#108 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
JuliaAI/MLJTransforms.jl#107 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
next breaking release
難易度 1/5 1時間未満 初心者へのやさしさ 58/100
JuliaAI/MLJTransforms.jl#111 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
JuliaAI/MLJTransforms.jl#106 ·
メンテナーはふだん 1 日以内に返信
JuliaAI/MLJTransforms.jl の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
-
question
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
brown-ccv/Chamber.jl#272 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
JuliaAstro/SolarPosition.jl#143 ·
-
distributions
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
JuliaBayes/Wren.jl#404 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信