Skip to content

Add MultinomialNaiveBayesClassifier Implementation#7532

Open
Rosander0 wants to merge 1 commit into
TheAlgorithms:masterfrom
Rosander0:add-multinomial-naive-bayes
Open

Add MultinomialNaiveBayesClassifier Implementation#7532
Rosander0 wants to merge 1 commit into
TheAlgorithms:masterfrom
Rosander0:add-multinomial-naive-bayes

Conversation

@Rosander0

Copy link
Copy Markdown
Contributor

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

@codecov-commenter

codecov-commenter commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.36%. Comparing base (742162c) to head (e3a8329).
⚠️ Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rosander0
Rosander0 force-pushed the add-multinomial-naive-bayes branch from 5cbc382 to e3a8329 Compare July 20, 2026 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants