-
-
Notifications
You must be signed in to change notification settings - Fork 155
added naive bayes classifier #3799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file added
BIN
+6.88 KB
docs/Machine Learning/Naive Bayes classifier/Images/thomas bayes.webp
Binary file not shown.
96 changes: 96 additions & 0 deletions
96
docs/Machine Learning/Naive Bayes classifier/Naive-Bayes.md
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Naive Bayes Classifier | ||
|
||
## Introduction | ||
|
||
Naive Bayes is a probabilistic machine learning algorithm based on Bayes' theorem, with an assumption of independence between predictors. It's particularly useful for classification tasks and is known for its simplicity and effectiveness, especially with high-dimensional datasets. | ||
|
||
<p align="center"> | ||
<img src= "Images/thomas bayes.webp"> | ||
</p> | ||
|
||
|
||
## Theory | ||
|
||
### Bayes' Theorem | ||
|
||
The foundation of Naive Bayes is Bayes' theorem, which describes the probability of an event based on prior knowledge of conditions that might be related to the event. | ||
|
||
|
||
$$ P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)} $$ | ||
|
||
Where: | ||
- $P(A|B)$ is the posterior probability | ||
- $P(B|A)$ is the likelihood | ||
- $P(A)$ is the prior probability | ||
- $P(B)$ is the marginal likelihood | ||
|
||
<p align="center"> | ||
<img src= "Images/equation.webp"> | ||
</p> | ||
|
||
### Naive Bayes Classifier | ||
|
||
The Naive Bayes classifier extends this to classify data points into categories. It assumes that the presence of a particular feature in a class is unrelated to the presence of any other feature (the "naive" assumption). | ||
|
||
For a data point $X = (x_1, x_2, ..., x_n)$ and a class variable $$: | ||
|
||
$$ P(C|X) = \frac{P(X|C) \cdot P(C)}{P(X)} $$ | ||
|
||
The classifier chooses the class with the highest posterior probability: | ||
|
||
$$ C^* = \underset{c \in C}{argmax} P(X|c) \cdot P(c) $$ | ||
|
||
### Mathematical Formulation | ||
|
||
For a given set of features $(x_1, x_2, ..., x_n)$: | ||
|
||
$$ P(C|x_1, x_2, ..., x_n) \propto P(C) \cdot P(x_1|C) \cdot P(x_2|C) \cdot ... \cdot P(x_n|C) $$ | ||
|
||
Where: | ||
- $P(C|x_1, x_2, ..., x_n)$ is the posterior probability of class $C$ given the features | ||
- $P(C)$ is the prior probability of class $C$ | ||
- $P(x_i|C)$ is the likelihood of feature $x_i$ given class $C$ | ||
|
||
## Types of Naive Bayes Classifiers | ||
|
||
1. **Gaussian Naive Bayes**: Assumes continuous values associated with each feature are distributed according to a Gaussian distribution. | ||
|
||
2. **Multinomial Naive Bayes**: Typically used for discrete counts, like word counts in text classification. | ||
|
||
3. **Bernoulli Naive Bayes**: Used for binary feature models (0s and 1s). | ||
|
||
## Example: Gaussian Naive Bayes in scikit-learn | ||
|
||
Here's a simple example of using Gaussian Naive Bayes in scikit-learn: | ||
|
||
```python | ||
from sklearn.naive_bayes import GaussianNB | ||
from sklearn.datasets import load_iris | ||
from sklearn.model_selection import train_test_split | ||
from sklearn.metrics import accuracy_score | ||
|
||
# Load the iris dataset | ||
iris = load_iris() | ||
X, y = iris.data, iris.target | ||
|
||
# Split the data into training and testing sets | ||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) | ||
|
||
# Initialize and train | ||
gnb = GaussianNB() | ||
gnb.fit(X_train, y_train) | ||
|
||
# Predictions | ||
y_pred = gnb.predict(X_test) | ||
|
||
# Accuracy | ||
accuracy = accuracy_score(y_test, y_pred) | ||
print(f"Accuracy: {accuracy:.2f}") | ||
|
||
``` | ||
|
||
## Applications of Naive Bayes Algorithm | ||
- Real-time Prediction. | ||
- Multi-class Prediction. | ||
- Text classification/ Spam Filtering/ Sentiment Analysis. | ||
- Recommendation Systems. |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace
to
or
try
