feat(naive_bayes): add CategoricalNB classifier#1936
Open
uttam12331 wants to merge 1 commit into
Open
Conversation
MaxHalford
reviewed
Jun 29, 2026
| for c in self.classes_ | ||
| } | ||
|
|
||
| def learn_many(self, X: pd.DataFrame, y: pd.Series): |
Member
There was a problem hiding this comment.
Could you support Narwhals? We're in the process of moving all mini-batch methods to Narwhals instead of pandas
Comment on lines
+138
to
+139
| for (_, row), label in zip(X.iterrows(), y): | ||
| self.learn_one(row.to_dict(), label) |
Member
There was a problem hiding this comment.
We do not want mini-batch methods to be for loops over the inputs. Mini-batch methods must use vectorization, else they're not bringing anything to the table.
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.
Closes #1399.
Adds an online
CategoricalNBnaive Bayes classifier for categorical features (e.g.{"weather": "sunny", "humidity": "high"}), mirroring scikit-learn'sCategoricalNBbut learning incrementally. It fills the gap alongside the existingMultinomialNB/BernoulliNB/ComplementNB/GaussianNB.Model
For each feature
f, categoryvand classc:with the empirical class prior
P(c) = class_count[c] / N. New categories seen after the first observations are handled gracefully (online setting).Implementation
learn_one/learn_many(mini-batch viaMiniBatchClassifier),joint_log_likelihood/joint_log_likelihood_many, plusp_class/p_feature_given_classhelpers — consistent with the other NB classes.naive_bayes/__init__.py.Verification
CategoricalNBto machine precision (maxpredict_probadifference~4e-16across alphas) — covered by a newtest_categorical_vs_sklearn(parametrized over alpha).learn_manyproduces an identical model to repeatedlearn_one(test_categorical_learn_many_vs_learn_one).test_categorical_handles_unseen_feature_value).river/naive_bayessuite passes (50 tests), the generic estimator checks pass (test_estimators.py -k CategoricalNB, 53 checks), andruff check/ruff formatare clean.