FrequencyEncoder(normalize=true) counts missing values in the denominator, so frequencies no longer sum to 1

Open
#116 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
57/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
data

Research direction

Read src/encoders/frequency_encoding/frequency_encoding.jl around line 31 and src/transformers/cardinality_reducer/cardinality_reducer.jl around line 48, where the issue identifies the calculations involving missing values. Check the relevant tests for both models and verify normalized frequencies and float min_frequency use non-missing values while missing entries remain unchanged. Done means the reported frequency sums and level collapsing match the non-missing category values.

Written by the indexing model from the issue text.

Description

FrequencyEncoder(normalize = true) computes the frequencies with proportionmap(col) (src/encoders/frequency_encoding/frequency_encoding.jl, line 31), and col still contains the missing entries. So missing rows end up in the denominator, even though missing never gets its own encoded value (it just passes through as missing). The result is that the per-level frequencies don't add up to 1, which the docstring says they should:

normalize=false: Whether to use normalized frequencies that sum to 1 over category values or to use raw counts.

It also means the encoding of a non-missing level changes depending on how many missing values were in the training data.

using MLJBase, MLJTransforms, CategoricalArrays

X = (color = categorical(["red", "red", "blue", missing, missing, missing]),)

mach = machine(FrequencyEncoder(normalize = true), X)
fit!(mach, verbosity = 0)

freqs = fitted_params(mach).statistic_given_feat_val[:color]
@show freqs
@show sum(values(freqs))
@show transform(mach, X).color

# same non-missing values, no missing rows
X2 = (color = categorical(["red", "red", "blue"]),)
mach2 = machine(FrequencyEncoder(normalize = true), X2)
fit!(mach2, verbosity = 0)
@show fitted_params(mach2).statistic_given_feat_val[:color]

Output:

freqs = Dict{String, Float32}("red" => 0.33333334, "blue" => 0.16666667)
sum(values(freqs)) = 0.5f0
(transform(mach, X)).color = Union{Missing, Float32}[0.33333334, 0.33333334, 0.16666667, missing, missing, missing]
(fitted_params(mach2)).statistic_given_feat_val[:color] = Dict{String, Float32}("red" => 0.6666667, "blue" => 0.33333334)

I expected red => 2/3 and blue => 1/3 in both cases, i.e. frequencies over the observed category values, which is what the docstring describes (and what e.g. pandas value_counts(normalize=True) gives by default). Instead they sum to 0.5 here.

CardinalityReducer has the same pattern when min_frequency is a float (src/transformers/cardinality_reducer/cardinality_reducer.jl, line 48), and there it changes which levels get collapsed. Below, "red" is 3 of the 4 non-missing values (0.75) but is still mapped to "Other" with min_frequency = 0.4, because it is compared as 3/8:

Y = (color = categorical(["red", "red", "red", "blue", missing, missing, missing, missing]),)
mach3 = machine(CardinalityReducer(min_frequency = 0.4), Y)
fit!(mach3, verbosity = 0)
@show unwrap.(transform(mach3, Y).color)
unwrap.((transform(mach3, Y)).color) = Union{Missing, String}["Other", "Other", "Other", "Other", missing, missing, missing, missing]

Since both models already leave missing untouched in transform, I think the frequencies should be computed over the non-missing values only, e.g. proportionmap(collect(skipmissing(col))) / countmap(skipmissing(col)). If counting missing rows in the denominator is intentional, it would be good to mention that in the docstrings instead.

Versions: Julia 1.13.4, MLJTransforms 0.2.4 (dev at d3e3db3), MLJBase 1.15.4, CategoricalArrays 1.1.1.

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaAI/MLJTransforms.jl

All issues in JuliaAI/MLJTransforms.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.