Add MultinomialNaiveBayesClassifier Implementation#7532
Open
Rosander0 wants to merge 1 commit into
Open
Conversation
Rosander0
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
July 20, 2026 12:31
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7532 +/- ##
============================================
+ Coverage 80.32% 80.36% +0.04%
- Complexity 7404 7422 +18
============================================
Files 812 813 +1
Lines 23911 23970 +59
Branches 4704 4714 +10
============================================
+ Hits 19206 19264 +58
Misses 3945 3945
- Partials 760 761 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Rosander0
force-pushed
the
add-multinomial-naive-bayes
branch
from
July 20, 2026 13:07
5cbc382 to
e3a8329
Compare
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.
Add Multinomial Naive Bayes classifier
Adds a MultinomialNaiveBayesClassifier implementation to com.thealgorithms.machinelearning, following the same package structure as the existing Linear Regression implementation.
What's included:
Multinomial Naive Bayes with Laplace (add-alpha) smoothing, configurable via constructor (defaults to alpha = 1.0)
Log-probability arithmetic throughout predict() to avoid numerical underflow from multiplying small probabilities directly
Single-sample and batch predict() overloads
Guard clauses for unfitted-classifier use, invalid alpha, and feature/sample dimension mismatches
JUnit 5 test suite covering prediction correctness on a separable toy dataset, batch/single-sample consistency, smoothing behavior on zero-count features, and all exception paths
Notes for reviewers:
This is the Multinomial variant specifically (suited to count-based features like word frequencies), not Gaussian or Bernoulli — happy to add those as follow-ups if there's interest
One test (laplaceSmoothingKeepsZeroCountFeatureLogProbabilityFinite) only asserts the prediction doesn't collapse to an invalid state, not the exact expected class — flagging in case a maintainer wants that tightened