TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun · 77.8K subscribers
Post #1726 8.42K
Let's start with Day 8 today

30 Days of Data Science Series: https://t.me/datasciencefun/1708

Let's learn about Naive Bayes Algorithm today

Concept: Naive Bayes is a family of probabilistic algorithms based on Bayes' Theorem with the "naive" assumption of independence between every pair of features. Despite this strong assumption, Naive Bayes classifiers have performed surprisingly well in many real-world applications, particularly for text classification.

#### Types of Naive Bayes Classifiers
1. Gaussian Naive Bayes: Assumes that the features follow a normal distribution.
2. Multinomial Naive Bayes: Typically used for discrete data (e.g., text classification with word counts).
3. Bernoulli Naive Bayes: Used for binary/boolean features.

#### Implementation

Let's consider an example using Python and its libraries.

##### Example
Suppose we have a dataset that records features of different emails, such as word frequencies, to classify them as spam or not spam.

# Import necessary libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

# Example data
data = {
'Feature1': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
'Feature2': [5, 4, 3, 2, 1, 5, 4, 3, 2, 1],
'Feature3': [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
'Spam': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
}
df = pd.DataFrame(data)

# Independent variables (features) and dependent variable (target)
X = df[['Feature1', 'Feature2', 'Feature3']]
y = df['Spam']

# Splitting the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Creating and training the Multinomial Naive Bayes model
model = MultinomialNB()
model.fit(X_train, y_train)

# Making predictions
y_pred = model.predict(X_test)

# Evaluating the model
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
class_report = classification_report(y_test, y_pred)

print(f"Accuracy: {accuracy}")
print(f"Confusion Matrix:\n{conf_matrix}")
print(f"Classification Report:\n{class_report}")

#### Explanation of the Code

1. Libraries: We import necessary libraries like numpy, pandas, and sklearn.
2. Data Preparation: We create a DataFrame containing features (Feature1, Feature2, Feature3) and the target variable (Spam).
3. Feature and Target: We separate the features and the target variable.
4. Train-Test Split: We split the data into training and testing sets.
5. Model Training: We create a MultinomialNB model and train it using the training data.
6. Predictions: We use the trained model to predict whether the emails in the test set are spam.
7. Evaluation: We evaluate the model using accuracy, confusion matrix, and classification report.

#### Evaluation Metrics

- Accuracy: The proportion of correctly classified instances among the total instances.
- Confusion Matrix: Shows the counts of true positives, true negatives, false positives, and false negatives.
- Classification Report: Provides precision, recall, F1-score, and support for each class.

#### Applications

Naive Bayes classifiers are widely used for:
- Text Classification: Spam detection, sentiment analysis, and document categorization.
- Medical Diagnosis: Predicting diseases based on symptoms.
- Recommendation Systems: Recommending products or services based on user behavior.

Credits: t.me/datasciencefun

ENJOY LEARNING 👍👍
  • 👍 19
  • ❤ 2
  • 👏 1
More from @datasciencefun
  1. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
  2. Oct 8, 2026🔬 Kandinsky 6.0 Video: Training Details and Architecture The new Kandinsky 6.0 Video line…
  3. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  4. Oct 7, 2026ORDER BY salary DESC LIMIT 5; Mistake 3 — Forgetting that LIMIT applies after sorting For…
  5. Oct 7, 2026🚀 Data Science Roadmap 2026 📍 Phase 3: SQL for Data Science 📖 Topic 4 — LIMIT LIMIT is…
  6. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →