Skip to content

Conversation

@MeKaustubh07
Copy link
Contributor

🧠 Naive Bayes Classifier (R Implementation)

Overview

Naive Bayes is a probabilistic machine learning algorithm based on Bayes’ Theorem, assuming that all features are independent given the class.
It’s simple, fast, and works well for classification tasks like text analysis, spam detection, and document categorization.


Algorithm Concept

For a given input ( X = (x_1, x_2, ..., x_n) ), the probability of belonging to a class ( C_k ) is:

[
P(C_k | X) \propto P(C_k) \prod_{i=1}^{n} P(x_i | C_k)
]

The algorithm predicts the class with the highest posterior probability.


Variants Implemented

  1. Gaussian Naive Bayes (GaussianNB)

    • For continuous data
    • Assumes features follow a normal distribution
  2. Multinomial Naive Bayes (MultinomialNB)

    • For count-based features (e.g., word frequencies)
    • Commonly used in text classification
  3. Bernoulli Naive Bayes (BernoulliNB)

    • For binary features (e.g., word presence/absence)
    • Ideal for spam detection and document filtering

Key Functions

# Train the model
fit(X, y)

# Predict class labels
predict(X)

# Predict class probabilities
predict_proba(X)

# Evaluate accuracy
score(X, y)

@MeKaustubh07 MeKaustubh07 requested a review from siriak as a code owner October 18, 2025 20:11
Copilot AI review requested due to automatic review settings October 18, 2025 20:11
@MeKaustubh07 MeKaustubh07 requested a review from acylam as a code owner October 18, 2025 20:11
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a comprehensive implementation of the Naive Bayes classification algorithm to the machine learning directory. The implementation includes three variants of Naive Bayes classifiers optimized for different types of data.

  • Implements Gaussian, Multinomial, and Bernoulli Naive Bayes variants using R6 classes
  • Provides comprehensive examples demonstrating each variant with appropriate datasets
  • Includes proper probability calculations with numerical stability considerations

Copilot AI review requested due to automatic review settings October 20, 2025 06:16
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

@MeKaustubh07 MeKaustubh07 requested a review from Copilot October 20, 2025 06:23
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@siriak siriak merged commit c7d41e7 into TheAlgorithms:master Oct 25, 2025
2 checks passed
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