Repository navigation
Count NGBClassifier classes with Dist.n_classes (fixes BetaBernoulli after #413) - #448
Open
alejandroschuler wants to merge 2 commits into
Open
alejandroschuler wants to merge 2 commits into
alejandroschuler wants to merge 2 commits into
Conversation
PR #413 counts the classes of an NGBClassifier as Dist.n_params + 1. BetaBernoulli has 2 parameters for 2 classes, so NGBClassifier(Dist=BetaBernoulli).fit(X, y) with binary y raised "NGBClassifier Dist expects 3 classes, got 2". ClassificationDistn now has an n_classes class attribute. k_categorical and the SymPy factory set it. For other subclasses, __init_subclass__ sets it to n_params + 1. _fit_label_encoder and __setstate__ use it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…asses # Conflicts: # RELEASE_NOTES.md
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On master,
NGBClassifier(Dist=BetaBernoulli).fit(X, y)with binaryyraisesPR #413 added
NGBClassifier._fit_label_encoder, which counts the classes asDist.n_params + 1. The__setstate__fallback uses the same count. That holds forBernoulliandk_categorical(K), butBetaBernoullihas 2 parameters (alpha and beta) for 2 classes. The example indocs/sympy_distributions.mdfails for this reason. #413 is not in a release yet, so no user has seen this.Fix
ClassificationDistnnow has ann_classesclass attribute. A subclass can set it. If it does not,__init_subclass__sets it ton_params + 1, so custom classification distributions keep the count they had before.k_categorical(K)setsn_classes = K.n_classes = len(class_prob_exprs)on classification distributions.NGBClassifier._fit_label_encoderandNGBClassifier.__setstate__useDist.n_classes.I checked the other places that use
n_params + 1.NGBoost.__getstate__(ngboost/ngboost.py:152) andCategorical.params(ngboost/distns/categorical.py:91) apply only tok_categoricaldistributions, where the count is correct, so I did not change them.Tests
test_classification_distns_set_n_classeschecksn_classesforBernoulli,k_categorical(4),BetaBernoulliand a custom subclass that uses the default.test_classifier_beta_bernoulli_binary_labelsfitsNGBClassifier(Dist=BetaBernoulli)on string labels and checksclasses_,predictand the(n, 2)shape ofpredict_proba.There is no pickle round trip test for this model yet. Factory distributions cannot be pickled until #445 merges. After that, a round trip of a fitted
NGBClassifier(Dist=BetaBernoulli)is a good test to add.black, isort, flake8 and pylint pass on the changed files, and
pytest tests --slowpasses.🤖 Generated with Claude Code