ContrastEncoder: transform throws DimensionMismatch when a categorical feature contains missing values

オープン
#106 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

まだ誰も着手していません。

評価

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

調査の方向性

The stack trace points to src/generic.jl in generic_transform, with the call coming through src/encoders/contrast_encoder/contrast_encoder.jl and src/encoders/contrast_encoder/interface_mlj.jl. Read the multi-column branch and compare how other encoders handle missing values. Done means transforming data with missing categorical values either propagates missing values across the encoded columns or returns a clear, actionable error.

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

説明

ContrastEncoder fits fine on a categorical column that has missing values, but transform then fails with an unhelpful DimensionMismatch. This happens for every mode I tried, and also when the training data has no missing values but the new data does.

using MLJTransforms, MLJBase, CategoricalArrays

X = (color = categorical(["red", "green", missing, "blue", "red"]), x = [1.0, 2.0, 3.0, 4.0, 5.0])
schema(X)   # color is Union{Missing, Multiclass{3}}

for mode in (:dummy, :sum, :helmert)
    mach = machine(ContrastEncoder(mode = mode), X)
    fit!(mach, verbosity = 0)
    println("fit ok for mode=$mode: ", fitted_params(mach).vector_given_value_given_feature)
    try
        transform(mach, X)
    catch e
        println("transform, mode=$mode: ", sprint(showerror, e))
    end
end

Output:

fit ok for mode=dummy: Dict{Any, Any}(:color => OrderedCollections.OrderedDict("blue" => [1.0, 0.0], "green" => [0.0, 1.0], "red" => [0.0, 0.0]))
transform, mode=dummy: DimensionMismatch: mismatch in dimension 1 (expected 2 got 1)
fit ok for mode=sum: Dict{Any, Any}(:color => OrderedCollections.OrderedDict("blue" => [1.0, 0.0], "green" => [0.0, 1.0], "red" => [-1.0, -1.0]))
transform, mode=sum: DimensionMismatch: mismatch in dimension 1 (expected 2 got 1)
fit ok for mode=helmert: Dict{Any, Any}(:color => OrderedCollections.OrderedDict("blue" => [-1.0, -1.0], "green" => [1.0, -1.0], "red" => [0.0, 2.0]))
transform, mode=helmert: DimensionMismatch: mismatch in dimension 1 (expected 2 got 1)

Training on clean data and transforming data with a missing gives the same error:

Xtrain = (color = categorical(["red", "green", "blue", "red"]),)
Xnew   = (color = categorical(["red", missing], levels = ["blue", "green", "red"]),)
mach = fit!(machine(ContrastEncoder(), Xtrain), verbosity = 0)
transform(mach, Xnew)
# DimensionMismatch: mismatch in dimension 1 (expected 2 got 1)

Top of the stack trace:

ERROR: LoadError: DimensionMismatch: mismatch in dimension 1 (expected 2 got 1)
Stacktrace:
  ...
  [9] hcat(::Vector{Float64}, ::Vector{Float64}, ::Missing, ::Vararg{Any})
    @ Base ./abstractarray.jl:2019
 [10] generic_transform(X::@NamedTuple{color::CategoricalVector{Union{Missing, String}, UInt32, String, CategoricalValue{String, UInt32}, Missing}, x::Vector{Float64}}, mapping_per_feat_level::Dict{Any, Any}; single_feat::Bool, ignore_unknown::Bool, use_levelnames::Bool, custom_levels::Nothing, ensure_categorical::Bool)
    @ MLJTransforms src/generic.jl:236
 [11] generic_transform
    @ src/generic.jl:192 [inlined]
 [12] contrast_encoder_transform
    @ src/encoders/contrast_encoder/contrast_encoder.jl:166 [inlined]
 [13] transform(transformer::ContrastEncoder{Symbol, Vector{Symbol}, Nothing}, fitresult::Dict{Any, Any}, Xnew::...)
    @ MLJTransforms src/encoders/contrast_encoder/interface_mlj.jl:52

For comparison, the other encoders accept the same table and just propagate missing:

julia> transform(fit!(machine(OneHotEncoder(), X), verbosity=0), X)
(color__blue = Union{Missing, Float64}[0.0, 0.0, missing, 1.0, 0.0], color__green = Union{Missing, Float64}[0.0, 1.0, missing, 0.0, 0.0], color__red = Union{Missing, Float64}[1.0, 0.0, missing, 0.0, 1.0], x = [1.0, 2.0, 3.0, 4.0, 5.0])

julia> transform(fit!(machine(OrdinalEncoder(), X), verbosity=0), X)
(color = Union{Missing, Float32}[3.0, 2.0, missing, 1.0, 3.0], x = [1.0, 2.0, 3.0, 4.0, 5.0])
Cause

generic_fit deliberately selects columns with element scitype Union{Missing, Multiclass} (and Union{Missing, OrderedFactor}) for encoding (src/generic.jl#L85-L88), and the contrast feature_mapper builds its dict from rawlevels(col), so missing never gets a vector. In the multi-column branch of generic_transform:

level2vector = mapping_per_feat_level[feat_name]
new_multi_col = map(x -> get(level2vector, x, x), col)
new_multi_col = [col for col in eachrow(hcat(new_multi_col...))]

(src/generic.jl#L234-L237)

get(level2vector, missing, missing) returns a scalar missing for that row, so hcat gets a mix of length-k-1 vectors and a scalar and throws. The single-column encoders don't hit this because they go through recode, which keeps missing as is.

Expected

Either propagate missing into each of the spawned columns for that row (like OneHotEncoder does, giving Union{Missing, Float64} columns), or throw a clear error saying ContrastEncoder doesn't support missing values and pointing to MissingnessEncoder/FillImputer. Right now fit accepts the column and transform fails with an error that doesn't say what's wrong.

Versions
  • MLJTransforms v0.2.4 (dev at d3e3db3)
  • MLJBase v1.15.3, MLJModelInterface v1.12.2, CategoricalArrays v1.1.1
  • Julia 1.13.3
主要言語
Julia
スター
1
フォーク
5
平均マージ
1日 20分
マージ済み PR(30日)
7

環境構築

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

はじめの一歩

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

JuliaAI/MLJTransforms.jl のほかの issue

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

似ている issue

Julia の issue をもっと見る

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

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