ContrastEncoder: transform throws DimensionMismatch when a categorical feature contains missing values
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- data
Research direction
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.
Written by the indexing model from the issue text.
Description
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...))]
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 (
devat d3e3db3) - MLJBase v1.15.3, MLJModelInterface v1.12.2, CategoricalArrays v1.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
-
TargetEncoder: m = :auto uses the inverse of the empirical Bayes ratio and gives NaN for single-row levelsPossibly taken @EssamWisam claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
JuliaAI/MLJTransforms.jl#113 · 1 reaction · 1 assignee ·
Maintainers usually reply within 1 day
-
ContinuousEncoder docstring: wrong `drop_last` default and nonexistent `ordered_factors` optionPossibly taken @ablaom claimed this 2 days 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
-
Difficulty 2/5 1-3 hours Newbie friendliness 57/100
JuliaAI/MLJTransforms.jl#116 ·
Maintainers usually reply within 1 day
All issues in JuliaAI/MLJTransforms.jl
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
oscar-system/Oscar.jl#6572 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
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 ·