Skip to content

Commit f1985ba

Browse files
added naive bayes classifier
1 parent 63d74a0 commit f1985ba

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Binary file not shown.
Binary file not shown.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Naive Bayes Classifier
2+
3+
## Introduction
4+
5+
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.
6+
7+
<p align="center">
8+
<img src= "Images/thomas bayes.webp">
9+
</p>
10+
11+
12+
## Theory
13+
14+
### Bayes' Theorem
15+
16+
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.
17+
18+
19+
$$ P(A|B) = \frac{P(B|A) \cdot P(A)}{P(B)} $$
20+
21+
Where:
22+
- $P(A|B)$ is the posterior probability
23+
- $P(B|A)$ is the likelihood
24+
- $P(A)$ is the prior probability
25+
- $P(B)$ is the marginal likelihood
26+
27+
<p align="center">
28+
<img src= "Images/equation.webp">
29+
</p>
30+
31+
### Naive Bayes Classifier
32+
33+
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).
34+
35+
For a data point $X = (x_1, x_2, ..., x_n)$ and a class variable $C$:
36+
37+
$$ P(C|X) = \frac{P(X|C) \cdot P(C)}{P(X)} $$
38+
39+
The classifier chooses the class with the highest posterior probability:
40+
41+
$$ C^* = \underset{c \in C}{\operatorname{argmax}} P(X|c) \cdot P(c) $$
42+
43+
### Mathematical Formulation
44+
45+
For a given set of features $(x_1, x_2, ..., x_n)$:
46+
47+
$$ 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) $$
48+
49+
Where:
50+
- $P(C|x_1, x_2, ..., x_n)$ is the posterior probability of class $C$ given the features
51+
- $P(C)$ is the prior probability of class $C$
52+
- $P(x_i|C)$ is the likelihood of feature $x_i$ given class $C$
53+
54+
## Types of Naive Bayes Classifiers
55+
56+
1. **Gaussian Naive Bayes**: Assumes continuous values associated with each feature are distributed according to a Gaussian distribution.
57+
58+
2. **Multinomial Naive Bayes**: Typically used for discrete counts, like word counts in text classification.
59+
60+
3. **Bernoulli Naive Bayes**: Used for binary feature models (0s and 1s).
61+
62+
## Example: Gaussian Naive Bayes in scikit-learn
63+
64+
Here's a simple example of using Gaussian Naive Bayes in scikit-learn:
65+
66+
```python
67+
from sklearn.naive_bayes import GaussianNB
68+
from sklearn.datasets import load_iris
69+
from sklearn.model_selection import train_test_split
70+
from sklearn.metrics import accuracy_score
71+
72+
# Load the iris dataset
73+
iris = load_iris()
74+
X, y = iris.data, iris.target
75+
76+
# Split the data into training and testing sets
77+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
78+
79+
# Initialize and train
80+
gnb = GaussianNB()
81+
gnb.fit(X_train, y_train)
82+
83+
# Predictions
84+
y_pred = gnb.predict(X_test)
85+
86+
# Accuracy
87+
accuracy = accuracy_score(y_test, y_pred)
88+
print(f"Accuracy: {accuracy:.2f}")
89+
90+
```
91+
92+
## Applications of Naive Bayes Algorithm
93+
- Real-time Prediction.
94+
- Multi-class Prediction.
95+
- Text classification/ Spam Filtering/ Sentiment Analysis.
96+
- Recommendation Systems.

0 commit comments

Comments
 (0)