TargetEncoder: fit throws "unable to check bounds for indices of type Missing" when a categorical feature contains missing values
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
- 76/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Ambito
- machine-learning
Direzione di ricerca
Inizia da src/encoders/target_encoding/target_encoding.jl, in particolare dal codice feature_mapper vicino alla riga 173 segnalata, e segui come costruisce la maschera per ciascun livello categorico. Verifica come i valori mancanti delle feature dovrebbero influire sulle statistiche per livello, quindi esegui i test del target encoder e aggiungi test che coprano i valori mancanti. Il lavoro è completato quando il fitting riesce per i casi di classificazione e regressione segnalati oppure restituisce l’errore chiaro documentato se i valori mancanti non sono supportati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
TargetEncoder can't be fit on a categorical feature that contains missing. Fit fails with ArgumentError: unable to check bounds for indices of type Missing, and this happens for binary classification, multiclass and regression targets alike. The machine type checks pass (the column is Union{Missing, Multiclass{3}}, which generic_fit selects for encoding), so the error comes from inside fit.
using MLJTransforms, MLJBase, CategoricalArrays
X = (color = categorical(["red", "green", missing, "blue", "red", "green"]), x = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
schema(X) |> println # color is Union{Missing, Multiclass{3}}
y_bin = categorical(["a", "b", "a", "b", "a", "b"])
y_reg = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
for (task, y) in (("binary", y_bin), ("regression", y_reg))
try
MLJTransforms.target_encoder_fit(X, y)
catch e
println("target_encoder_fit, $task: ", sprint(showerror, e))
end
end
# through a machine
mach = machine(TargetEncoder(), X, y_bin)
try
fit!(mach, verbosity = 0)
catch e
println("fit!(mach): ", sprint(showerror, e))
end
# training on rows without missing works, and transform then propagates missing fine
keep = .!ismissing.(X.color)
Xclean = (color = X.color[keep], x = X.x[keep])
mach2 = fit!(machine(TargetEncoder(), Xclean, y_bin[keep]), verbosity = 0)
println("transform after clean fit: ", transform(mach2, X))
# the mask used in feature_mapper
println(X.color .== "red")
Output:
ScientificTypes.Schema{(:color, :x), Tuple{Union{Missing, Multiclass{3}}, Continuous}, Tuple{Union{Missing, CategoricalValue{String, UInt32}}, Float64}}(nothing, nothing, nothing)
target_encoder_fit, binary: ArgumentError: unable to check bounds for indices of type Missing
target_encoder_fit, regression: ArgumentError: unable to check bounds for indices of type Missing
┌ Error: Problem fitting the machine machine(TargetEncoder(features = Symbol[], …), …).
└ @ MLJBase ~/.julia/packages/MLJBase/CIZ7f/src/machines.jl:758
[ Info: Running type checks...
[ Info: Type checks okay.
fit!(mach): ArgumentError: unable to check bounds for indices of type Missing
transform after clean fit: (color = Union{Missing, Float64}[1.0, 0.0, missing, 0.0, 1.0, 0.0], x = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
Union{Missing, Bool}[1, 0, missing, 0, 1, 0]
(A multiclass target, e.g. categorical(["a", "b", "c", "b", "a", "c"]), gives the same error.)
Top of the stack trace:
ArgumentError: unable to check bounds for indices of type Missing
Stacktrace:
[1] checkindex(::Type{Bool}, inds::Base.OneTo{Int64}, i::Missing)
@ Base ./abstractarray.jl:754
...
[5] getindex
@ ~/.julia/packages/CategoricalArrays/pvykx/src/array.jl:743 [inlined]
[6] (::MLJTransforms.var"#feature_mapper#28"{...})(col::CategoricalVector{Union{Missing, String}, UInt32, String, CategoricalValue{String, UInt32}, Missing}, name::Symbol)
@ MLJTransforms src/encoders/target_encoding/target_encoding.jl:173
[7] generic_fit(...)
@ MLJTransforms src/generic.jl:91
...
[9] target_encoder_fit(...)
@ MLJTransforms src/encoders/target_encoding/target_encoding.jl:209
Cause
generic_fit selects columns whose element scitype is Union{Missing, Multiclass} (or Union{Missing, OrderedFactor}) for encoding (src/generic.jl#L84-L91). The target encoder's feature_mapper then collects the targets for each level with:
targets_for_level = y[col.==level]
(src/encoders/target_encoding/target_encoding.jl#L171-L173)
When col has a missing, col .== level is a Vector{Union{Missing, Bool}} (last line of the output above), and that can't be used as a logical index, so getindex throws. Something like y[isequal.(col, level)] (or coalesce.(col .== level, false)) would just leave the missing rows out of every level's statistics; I checked that both give [1.0, 5.0] for "red" on the column above. The priors (mean(y) / class frequencies) would still be computed over all rows, which seems reasonable, and as shown above transform already passes missing through via recode.
This is related to #106 but a separate code path: #106 is ContrastEncoder failing in transform (the multi-column hcat branch of generic_transform), while here TargetEncoder fails earlier, in fit, because of the boolean mask. FrequencyEncoder, OrdinalEncoder, CardinalityReducer and MissingnessEncoder all fit and transform the same X without errors.
Expected
Fit should either skip missing when computing the per-level statistics (so the encoded column is Union{Missing, Float64} like the other single-column encoders), or throw a clear error saying TargetEncoder doesn't support missing values in features and pointing to MissingnessEncoder/FillImputer.
Versions
- MLJTransforms v0.2.4 (
devat d3e3db3) - MLJBase v1.15.3, CategoricalArrays v1.1.1
- Julia 1.13.3
- Lingua principale
- Julia
- Stelle
- 1
- Fork
- 5
- Merge medio
- 1g 20m
- PR unite (30g)
- 7
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
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
JuliaAI/MLJTransforms.jl#108 ·
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
-
Difficoltà 3/5 Mezza giornata Idoneità per principianti 66/100
JuliaAI/MLJTransforms.jl#105 ·
I maintainer di solito rispondono entro 1 giorno
-
Add a `Transformer` wrapper for models that supervised that transformForse di nuovo libera @ablaom l’ha presa 240 giorni fa e non c’è nessuna pull request aperta. Apertaenhancement
JuliaAI/MLJTransforms.jl#52 · 3 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
JuliaAI/MLJTransforms.jl#39 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di JuliaAI/MLJTransforms.jl
Issue simili
-
The entrypoint deletes its manifest runfiles tree at exit even under TEST_TMPDIRForse già presa @TrapsterDK l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
periareon/rules_julia#64 ·
I maintainer di solito rispondono entro 1 giorno
-
tagbot-manual
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
Add to "Why Wren.jl"Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
JuliaBayes/Wren.jl#370 · 3 commenti · 2 reazioni ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
CliMA/Oceananigans.jl#6603 ·
I maintainer di solito rispondono entro 1 giorno