📩 Message Intelligence System
This project evaluates understanding of supervised machine learning classification with a focus on probability foundations (conditional probability, Bayes' Theorem), instance-based, margin-based and probabilistic classifiers (KNN, SVM, Naive Bayes), and model comparison with classification metrics. Students learn how to build a spam-vs-ham message classifier, analyse how hyperparameters and kernels change predictions, and choose the best model for a real-world deployment.
📄 Problem Statement
You are working on a messaging platform's trust & safety team. The company holds a message dataset of 5,200 messages and wants a Message Intelligence System that can predict whether a message is spam (1) or legitimate / ham (0) from its text statistics, sender behaviour and timing attributes.
Your task is to prepare the data, build a KNN baseline, tune it, compare it with an SVM and a Naive Bayes classifier, manually verify Bayes' Theorem on the data, evaluate all models with accuracy, precision, recall and F1, and deliver a final business recommendation for deployment.
♻️ Workflow
🌐 Live Demo
📂 Project Files
| 📄 File / Folder |
📌 Description |
📓 Message_Intelligence_Dataset.ipynb |
Main notebook — the complete, annotated KNN, SVM, Naive Bayes and model-comparison pipeline |
📊 Message_Intelligence_Dataset.csv.csv |
Raw message dataset (5,200 records) |
📘 README.md |
Project documentation and workflow guide |
📂 Visuals |
Folder of the output visuals/graphs |
📄 Part A :- Conceptual_Foundation.pdf |
Project documentation of Part A (Theory) |
📋 Part G Final_Analysis_&_Reporting (Report).pdf |
Report documentation of Part G (Final Report) |
🛠️ Tools Used
🎬 Project Demo
📹 Click on the badge to watch the video .
🧬 Dataset Structure — Message Intelligence Dataset
| Field Name |
Data Type |
Description |
Notes |
message_id |
Integer |
Unique identifier for each message |
Identifier only — not a real feature |
message_text |
Object |
Raw text of the message |
Not used directly as a model feature |
message_length |
Integer |
Number of characters in the message |
Model feature |
word_count |
Integer |
Number of words in the message |
Model feature |
num_urls |
Integer |
Number of URLs in the message |
Model feature (0 or 1) |
num_digits |
Integer |
Number of digits in the message |
Model feature |
num_special_chars |
Integer |
Number of special characters |
Dropped from the model |
spam_keyword_score |
Integer |
Count of spam-style keywords |
Dropped from the model |
legit_keyword_score |
Integer |
Count of legitimate-style keywords |
Dropped from the model |
sender_activity_score |
Float |
Activity score of the sender |
Model feature — has missing values |
sender_account_age_days |
Float |
Age of the sender account in days |
Model feature — has missing values |
messages_sent_last_24h |
Float |
Messages sent by the sender in 24 hours |
Model feature — has missing values |
timestamp |
Object (date) |
Date and time the message was sent |
Not used as a model feature |
hour_of_day |
Integer |
Hour the message was sent (0–23) |
Model feature |
day_of_week |
Integer |
Day of week (0–6) |
Model feature |
spam_label |
Integer |
1 = Spam, 0 = Ham |
🎯 Target variable (imbalanced: ~18.7% spam) |
🧠 Part B : Dataset Understanding & Preparation
📋 Dataset Overview
df = pd.read_csv("Message_Intelligence_Dataset.csv.csv")
df.head(); df.tail(); df.info(); df.describe(); df.isnull().sum(); df.shape
Output: 5,200 rows × 16 columns · missing values in 3 columns
💡 Insight: sender_activity_score (106), sender_account_age_days (113) and messages_sent_last_24h (162) have missing values, all around 2–3% of the rows. The target spam_label has a mean of 0.187, so about 18.7% of messages are spam. 📊
6️⃣ Identify input features and target variable
X = df.drop(columns=[
'message_id', 'message_text', 'timestamp', 'spam_label',
'num_special_chars', 'spam_keyword_score', 'legit_keyword_score'
])
y = df['spam_label']
Output: 9 input features · target = spam_label
💡 Insight: The target is binary (1 = spam, 0 = ham). ID, raw text and timestamp are dropped, and the three keyword/special-character columns are excluded so the models must rely on length, URLs, digits, sender behaviour and timing. This removes some highly predictive signals, so the task is harder but more realistic. 🎯
7️⃣ Perform basic data preprocessing (scaling where required)
imputer = SimpleImputer(strategy='median')
scaler = StandardScaler()
💡 Insight: Median imputation fills missing sender values without being pulled by outliers, and StandardScaler puts all features on the same scale — a must for KNN and SVM, which are distance/margin based. 📐
8️⃣ Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
X_train = imputer.fit_transform(X_train); X_test = imputer.transform(X_test)
X_train = scaler.fit_transform(X_train); X_test = scaler.transform(X_test)
Output: Training data (4160, 9) · Testing data (1040, 9) · 0 missing values after imputation
💡 Insight: The 80/20 split uses stratify=y to preserve the spam ratio. The imputer and scaler are fit on the training set only and then applied to the test set, so no test information leaks into training. Before imputation the three sender columns had 106, 113 and 162 missing values; after it, all counts are 0. 🔀
📊 Part C : Baseline Model – K-Nearest Neighbors
9️⃣ Implement K-Nearest Neighbors (KNN) classifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
y_pred_knn = knn.predict(X_test)
💡 Insight: KNN with K=5 is a solid baseline, trained on the scaled training data and evaluated on the held-out test set. 🎯
🔟 Experiment with different values of K
| K |
Accuracy |
| 3 |
0.9846 |
| 5 |
0.9865 |
| 7 |
0.9865 |
| 9 |
0.9865 |
💡 Insight: Accuracy rises from 0.9846 at K=3 to 0.9865 at K=5 and then stays flat for K=7 and K=9, so K=5 is the smallest K that reaches the plateau and is a stable choice with less noise than K=3. 📈
1️⃣1️⃣ Analyze how distance metrics affect predictions
for metric in ['euclidean', 'manhattan', 'minkowski']:
model = KNeighborsClassifier(n_neighbors=5, metric=metric)
Output: Euclidean = Manhattan = Minkowski = 0.9865 accuracy
💡 Insight: The distance metric made no difference on this scaled dataset, so the model is stable with respect to this choice. (Minkowski with the default p=2 is identical to Euclidean.) 📏
1️⃣2️⃣ Identify cases where KNN misclassifies messages
Output: 14 of 1,040 test messages misclassified
💡 Insight: 12 are spam that KNN missed — messages with phrases like "refund", "cash prize", "₹50,000" and "URGENT" — and 2 are legitimate messages (an invoice and a report request) wrongly flagged as spam. KNN therefore errs mostly on the side of missing spam. ⚠️
⚙️ Part D : Support Vector Machine Classifier
1️⃣3️⃣ Implement SVM with Linear kernel and RBF kernel
svm_linear = SVC(kernel='linear').fit(X_train, y_train)
svm_rbf = SVC(kernel='rbf').fit(X_train, y_train)
💡 Insight: Both kernels are trained on the same scaled data so linear and non-linear separation can be compared directly. 🔀
1️⃣4️⃣ Analyze margin separation and support vectors
| Kernel |
Support vectors (ham / spam) |
Total |
| Linear |
43 / 40 |
83 |
| RBF |
118 / 112 |
230 |
💡 Insight: The linear kernel needs only 83 support vectors against 230 for RBF, meaning the classes are separated by a clean, simple boundary with few critical points. 🎯
1️⃣5️⃣ Compare SVM performance with KNN
| Model |
Accuracy |
| KNN (K=5) |
0.9865 |
| SVM Linear |
0.9942 |
| SVM RBF |
0.9933 |
💡 Insight: Both SVM variants beat KNN, with the linear kernel highest at 0.9942. The linear and RBF kernels differ by only about one message in 1,040. 🏆
🧮 Part E : Naive Bayes Classifier & Probability
1️⃣6️⃣ Implement Naive Bayes Classifier
nb = GaussianNB()
nb.fit(X_train, y_train)
y_pred_nb = nb.predict(X_test)
💡 Insight: GaussianNB estimates a class prior and a Gaussian likelihood per feature, then predicts the class with the higher posterior. 🧠
1️⃣7️⃣ Manually compute conditional probabilities for sample messages
| Probability |
Value |
| P(Keyword | Spam) |
0.7657 |
| P(Keyword | Legitimate) |
0.0 |
| P(URL | Spam) |
0.7390 |
| P(URL | Legitimate) |
0.0840 |
💡 Insight: Spam messages contain spam keywords and URLs far more often than legitimate ones. These are strong single signals for Naive Bayes. 📊
1️⃣8️⃣ Demonstrate how Bayes' Theorem is applied to compute class probabilities
p_spam_given_keyword = (p_keyword_spam * p_spam) / p_keyword
p_legit_given_keyword = (p_keyword_legit * p_legit) / p_keyword
Output: P(Spam | Keyword) = 1.0 · P(Legitimate | Keyword) = 0.0
💡 Insight: Because no legitimate message contains a spam keyword, seeing one makes a message certainly spam in this dataset. Note that this keyword feature was excluded from the models. ⚖️
1️⃣9️⃣ Compare theoretical probability calculations with model predictions
Output: Theoretical P(Spam | no keyword) = 0.0512 · Naive Bayes predicts ham with probability 0.999997
💡 Insight: Both the manual Bayes calculation and the model point to ham for the sample message. The model is far more confident because it multiplies evidence from all nine features, not one keyword feature. 🔍
📈 Part F : Model Comparison & Evaluation
2️⃣0️⃣ Evaluate all models using Accuracy, Precision, Recall and F1 Score
| Model |
Accuracy |
Precision |
Recall |
F1 Score |
| KNN |
0.9865 |
0.9892 |
0.9385 |
0.9632 |
| SVM (RBF) |
0.9933 |
0.9947 |
0.9692 |
0.9818 |
| Naive Bayes |
0.9683 |
0.8857 |
0.9538 |
0.9185 |
💡 Insight: SVM leads on every metric. KNN has high precision but the lowest recall, and Naive Bayes has the lowest precision and F1. 🥇
2️⃣1️⃣ Compare KNN vs SVM vs Naive Bayes
💡 Insight: SVM is the most consistent model across all four metrics. KNN is conservative (few false alarms, more missed spam), while Naive Bayes flags many legitimate messages as spam, which lowers its precision. 📊
2️⃣2️⃣ Identify which model performs best for high precision and high recall
Output: Highest Precision 0.9947 → SVM · Highest Recall 0.9692 → SVM
💡 Insight: SVM is best on both metrics, so it keeps false alarms and missed spam lowest at the same time. 🏆
📂 Project Workflow