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.35% 80.40% +0.04%
- Complexity 7425 7444 +19
============================================
Files 813 814 +1
Lines 23955 24016 +61
Branches 4717 4727 +10
============================================
+ Hits 19250 19309 +59
- Misses 3944 3945 +1
- Partials 761 762 +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