Add MultinomialNaiveBayesClassifier Implementation - #7532
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7532 +/- ##
============================================
+ Coverage 80.32% 80.40% +0.08%
- Complexity 7404 7443 +39
============================================
Files 812 814 +2
Lines 23911 24016 +105
Branches 4704 4727 +23
============================================
+ Hits 19206 19311 +105
+ Misses 3945 3944 -1
- Partials 760 761 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
5cbc382 to
e3a8329
Compare
|
Hey @DenizAltunkapan , just checking in on this PR — it's been a few days. Happy to make any changes if something needs to be adjusted. Let me know! |
|
@Rosander0 Thanks for the contribution. The implementation looks solid overall, but Please also add a regression test for refitting the same classifier instance. After that, this should be ready to approve. |
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