TargetEncoder reuses the first level's shrinkage factor for all levels when m > 0

オープン 初心者向け
#95 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@parth-patel010 がすでに取り組んでいます。

2026年11月3日 から。

  • #96 @parth-patel010 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発

調査の方向性

target_encoder_fitから始め、特にfeature_mapperクロージャ内のレベルごとのループを確認し、既存のTargetEncoderテストを読んでください。有限のmとm = :autoを対象とする回帰テストを追加し、各レベルが独自の縮小係数を取得することを確認してください。再現されたレベルのエンコーディングがレベルごとの期待値と一致し、既存のテストも引き続き通れば完了です。

索引モデルが issue の本文から書いたものです。

説明

TargetEncoder with m > 0 (or m = :auto) is supposed to compute the shrinkage factor λ = n_i / (m + n_i) separately for every level, but only the first level of the first encoded column gets its own λ. Every other level (and every other column) reuses that same value.

To reproduce

using MLJBase, MLJTransforms, CategoricalArrays, Statistics

X = (A = categorical(["a", "a", "b", "b", "b", "b", "b", "b"]),)
y = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]

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

prior = mean(y)
for lvl in ("a", "b")
    t = y[X.A .== lvl]
    λ = length(t) / (2 + length(t))
    println(lvl, ": n=", length(t), "  λ=", λ, "  expected=", λ * mean(t) + (1 - λ) * prior)
end

Output:

encoded A: [3.0, 3.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0]
a: n=2  λ=0.5  expected=3.0
b: n=6  λ=0.75  expected=5.25

Level "b" should be encoded as 0.75 * 5.5 + 0.25 * 4.5 = 5.25, but it gets 5.0, which is what you get with level "a"'s λ = 0.5. Same thing with m = :auto: the m estimated for the first level is reused for all the others (for the data above but with y = [1, 2, 3, 5, 8, 13, 21, 34], level "b" comes out as 10.8985 instead of 13.5697). Binary and multiclass targets are affected the same way.

Cause

In target_encoder_fit, the per-level loop inside the feature_mapper closure assigns back to the captured outer variables:

m == :auto && (m = compute_m_auto(task, targets_for_level; y_var = y_var))
lambda = compute_shrinkage(targets_for_level; m = m, λ = lambda)

After the first level, lambda is no longer 1.0, so compute_shrinkage treats it as a user-supplied λ and just returns it. With m = :auto, m is overwritten by a number on the first level so it is never re-estimated. The existing tests only use m = Inf, where λ is 0 for every level anyway, so they don't catch this.

Versions

  • Julia 1.13.3
  • MLJTransforms 0.2.1 (current dev, 1f79daf)
  • MLJBase 1.15.0, CategoricalArrays 1.1.1
主要言語
Julia
スター
1
フォーク
5
平均マージ
1日 16時間
マージ済み PR(30日)
8

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

JuliaAI/MLJTransforms.jl のほかの issue

JuliaAI/MLJTransforms.jl の issue をすべて見る

似ている issue

Julia の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。