Skip to content

Add ChiMerge constraint mode#158

Merged
Secbone merged 3 commits into
amphibian-dev:masterfrom
Atwood706:chimerge-constraint-mode
Apr 13, 2026
Merged

Add ChiMerge constraint mode#158
Secbone merged 3 commits into
amphibian-dev:masterfrom
Atwood706:chimerge-constraint-mode

Conversation

@Atwood706
Copy link
Copy Markdown
Contributor

这个 PR 为 ChiMerge 增加了一个兼容式新参数 constraint_mode。

Fixes #12

若在fit中选择‘chi’, 且同时设置min_sample参数和n_bins参数,ChiMerge的逻辑:在满足其中一个条件后就break
d95327b017333d1f4a178711778499aa

例如设置参数n_bins = 5, min_samples=0.04(1.原意是希望分箱结果同时满足两个条件;2.此参数设置是合法且可行的),分箱会出现以下情况(以下图片的模拟数据集来源 https://www.kaggle.com/competitions/gmsc-thebridge-dsft2109/data ):

1.先满足 min_samples,此时虽然每箱样本占比都足够大,但箱数仍然大于 n_bins,算法却提前停止。
image

2.先满足 n_bins,此时箱数已经不超过目标,但仍存在极小箱,算法也会提前停止。
image

此PR新增constraint_mode,将这个参数从 Python 包装层一路透传到了 Rust 的 ChiMerge 核心逻辑,并在 all 模式下保留了 empty_separate=True 的现有语义。默认 constraint_mode='any' 保持旧行为不变,也就是 n_bins、min_samples、min_threshold 任一条件满足就停止;新增的 constraint_mode='all' 则把 n_bins 和 min_samples 当作硬约束,只有“箱数 <= n_bins”且“每个普通箱样本数/占比 >= min_samples”同时满足时才停止。
image

改动后同参数同特征的分箱结果如图所示:
image

image

测试了修改的部分
image

@Secbone Secbone self-requested a review April 10, 2026 03:54
@Atwood706 Atwood706 force-pushed the chimerge-constraint-mode branch from 5d6d4c9 to a17f175 Compare April 11, 2026 10:24
@Secbone
Copy link
Copy Markdown
Member

Secbone commented Apr 13, 2026

Thanks for the great contribution! The feature looks solid and the core logic is correct.

We've just merged some pandas 3.x compatibility fixes to master (#159), which conflict with the impute.py changes in this PR. Could you please rebase onto the latest master and remove the impute.py changes (they're already on master)?

git fetch upstream
git rebase upstream/master
# resolve conflicts, drop impute.py changes
git push --force-with-lease

Once CI is green after the rebase, we'll merge this.

@Secbone Secbone merged commit 8738990 into amphibian-dev:master Apr 13, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

combiner的n_bins问题

2 participants