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

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

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

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

評価

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

調査の方向性

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.

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

説明

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.

主要言語
Julia
スター
1
フォーク
5
平均マージ
1日 4時間
マージ済み PR(30日)
6

環境構築

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

はじめの一歩

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

JuliaAI/MLJTransforms.jl のほかの issue

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

似ている issue

Julia の issue をもっと見る

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

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