Perfect. Since this is for your course, we'll keep every algorithm in the exact same format for consistency.
For every Machine Learning algorithm, we'll use only these sections:
Definition
Purpose
When to Use
Complete Python Code (using Pandas DataFrame with dummy data)
Chapter 9 — Naive Bayes
1. Definition
Naive Bayes is a Supervised Machine Learning Classification Algorithm that predicts the category (class) of new data using probability.
It is based on Bayes Theorem and assumes that all input features are independent of each other.
It is mainly used for classification problems, such as predicting Yes/No, Spam/Not Spam, or Positive/Negative.
Example
A company wants to predict whether a customer will purchase a product.
Input:
Age
Monthly Income
Website Visits
Previous Purchases
Output:
Purchased = Yes
or
Purchased = No
2. Purpose
The main purpose of Naive Bayes is to classify data into different categories based on probability.
It helps computers answer questions like:
Will the customer buy the product?
Is the email spam?
Is the review positive?
Does the patient have a disease?
Naive Bayes is widely used because it is:
Very fast
Easy to train
Easy to understand
Works well on small datasets
Excellent for text classification
3. When to Use
Use Naive Bayes when:
The problem is Classification.
The output is categorical (Yes/No, Pass/Fail, Spam/Not Spam).
The dataset contains numerical or text features.
Fast prediction is required.
The features are mostly independent.
Real-Life Applications
Email Spam Detection
Sentiment Analysis
News Classification
Disease Prediction
Product Purchase Prediction
Customer Classification
4. Complete Python Example (Using Pandas DataFrame)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import (
confusion_matrix,
accuracy_score,
precision_score,
recall_score,
f1_score
)
# -----------------------------
# Create Dataset
# -----------------------------
data = {
"Age": [
22,25,28,30,35,
40,45,50,27,32,
24,29,36,42,48,
23,31,38,44,52
],
"MonthlyIncome": [
25000,30000,35000,42000,50000,
56000,62000,70000,32000,45000,
28000,38000,52000,60000,68000,
26000,43000,55000,63000,72000
],
"WebsiteVisits": [
2,3,4,5,7,
8,9,10,3,6,
2,4,7,8,9,
2,5,7,9,10
],
"PreviousPurchases": [
0,1,1,2,3,
4,5,6,1,2,
0,1,3,4,5,
0,2,3,5,6
],
"Purchased": [
0,0,0,0,1,
1,1,1,0,1,
0,0,1,1,1,
0,1,1,1,1
]
}
df = pd.DataFrame(data)
print("="*50)
print("DATASET")
print("="*50)
print(df.head())
# -----------------------------
# Features and Target
# -----------------------------
X = df[
[
"Age",
"MonthlyIncome",
"WebsiteVisits",
"PreviousPurchases"
]
]
y = df["Purchased"]
# -----------------------------
# Train-Test Split
# -----------------------------
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.30,
random_state=42
)
# -----------------------------
# Train Model
# -----------------------------
model = GaussianNB()
model.fit(X_train, y_train)
# -----------------------------
# Prediction
# -----------------------------
y_pred = model.predict(X_test)
probability = model.predict_proba(X_test)
# -----------------------------
# Evaluation
# -----------------------------
cm = confusion_matrix(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
print("\nConfusion Matrix")
print(cm)
print("\nAccuracy :", round(accuracy,4))
print("Precision:", round(precision,4))
print("Recall :", round(recall,4))
print("F1 Score :", round(f1,4))
print("\nPrediction Probabilities")
print(probability)
# -----------------------------
# New Customer
# -----------------------------
new_customer = [[34,50000,7,3]]
prediction = model.predict(new_customer)
new_probability = model.predict_proba(new_customer)
print("\nNew Customer")
if prediction[0] == 1:
print("Prediction : PURCHASE")
else:
print("Prediction : NOT PURCHASE")
print("Not Purchase Probability:",
round(new_probability[0][0]*100,2),"%")
print("Purchase Probability:",
round(new_probability[0][1]*100,2),"%")
Summary
| Section | Description |
|---|---|
| Definition | Naive Bayes is a supervised classification algorithm based on Bayes Theorem. |
| Purpose | Predict the category (class) of new data using probabilities. |
| When to Use | Use for classification problems such as spam detection, sentiment analysis, disease prediction, and purchase prediction. |
| Code Example | Uses GaussianNB, a Pandas DataFrame, train-test split, evaluation metrics, prediction probabilities, and new customer prediction. |
0 Comments