Machine
Learning
BY:
[Link] DHARSHINI
II MCA
REG NO:24801907
Filtering mobile phone spam with the naive
Bayes algorithm
Evaluate
0 Collect Data 0 model
1 4 performance
SMS Data Collection Model Evaluation
Preparing Improving
0 0 Model
Data
2 Data Preprocessing 5 performance
Model Optimization
Train the
0 model 0 Conclusion
3 Model Training 6
Machine Learning
Machine
learning
● It’s an application of AI
● Computers observe and
analyze
● Predict based on previous
patterns
● Pre-programmed algorithms
Machine Learning Infographics
Supervised Unsupervised Reinforcement
learning learning learning
Classification Reduction
● Fraud detection ● Text mining ● Finances
● Email spam ● Data visualization ● Manufacturing
detection ● Face detection ● Stock
● Diagnostics ● Voice detection management
● Image ● Autonomous cars
classification
Regression Regression
● Risk assessment ● City planning
● Score prediction ● Targeted
marketing
Machine Learning Infographics
Machine learning advantages vs
disadvantages
Advantages Disadvantages
● Efficiency data managing ● Data acquisition
● Continuous improvement ● Time and space
● Lots of applications ● Time-consuming
● Trend identification ● High error possibilities
● Pattern identification ● Algorithm selection
Machine Learning
Spam
filtering
Some machine
learning
applications Mobile
banking
Risk
assessment
Social
networks
Filtering Mobile Phone Spam with Naive
Bayes
The SMS Spam Problem
• SMS spam = unwanted advertising via text
• Users often pay per message
• Spam detection is critical for user experience and
cost control
Why Use Naive Bayes?
• Proven effective for email spam
• Fast and simple probabilistic model
• Works well with text classification
Filtering Mobile Phone Spam with Naive
Bayes
Challenges in SMS Filtering
• Limited message length (160 characters)
• Use of shorthand/slang
• Less text = less context for classification
• Need for robust text preprocessing
SMS Spam Collection Dataset
• ~5,500 labeled SMS messages
• Two classes: spam or ham
Examples:
• Ham: “Are we meeting today?”
• Spam: “Free entry in a prize draw…”
Filtering Mobile Phone Spam with Naive
Bayes
How Naive Bayes Works
• Based on Bayes’ Theorem
• Calculates:
P(Spam | Words) ∝ P(Words | Spam) ×
P(Spam)
• Assumes word independence
Word Patterns in Spam
Spam often includes words like: “free”, “win”, “call now”
Ham Example Spam Example
“free on Sunday” “FREE ringtones!”
“going to work now” “Win cash now!”
Filtering Mobile Phone Spam with Naive
Bayes
Classification Performance
• Model: Multinomial Naive Bayes
• Accuracy: ~98–99%
• High precision and recall for spam detection
Step 1- Collection Data
• Use the SMS Spam Collection dataset containing labeled SMS
messages as spam or ham.
Examples:
• Ham: Informal, friendly messages, e.g., "I’m gonna be late for
meeting…"
• Spam: Promotional, keyword-heavy messages, e.g.,
"Congratulations! You’ve won free vouchers!"
• Ham messages often mention specific days and use casual
language.
• Spam messages frequently contain words like “FREE,” “prize,”
and “win.”
• The Naive Bayes classifier leverages these word frequency
patterns to calculate the probability of a message being spam or
ham.
Step 3- Train the Model
Apply the Naive Bayes Algorithm
Now that the SMS data is preprocessed into a numerical format, we
train a Naive Bayes classifier using the e1071 R package.
Install & Load Package
[Link]("e1071")
library(e1071)
Step 3- Train the Model
Build the Classifier
model <- naiveBayes(train_data, target_class, laplace = 0)
• train_data: Data frame or matrix of training features
• target_class: Factor vector of class labels (spam, ham, etc.)
• Laplace is a number to control the laplace estimator(by default
0)
Make Predictions
predictions <- predict(model, test_data, type = "class")
• model: Trained model from naiveBayes()
• test_data: It is a Data frame contains same features.
Type:
• "class" → predicted labels
• "raw" → raw probabilities for each class
Step 3- Train the Model
Build the Classifier
sms_classifier <- naiveBayes(sms_train, sms_raw_train$type)
• sms_train: matrix of preprocessed SMS messages
• sms_raw_train$type: target variable (spam or ham)
• Returns a Naive Bayes model object
Optional Laplace smoothing:
naiveBayes(train, class, laplace = 1)
Make Predictions
sms_predictions <- predict(sms_classifier, sms_test, type = "class")
• sms_test: test data with the same structure as training data
• type = "class" returns the most probable label (spam or ham)
Summary of the Train Model
• Trained using word presence/absence
• Learns probabilistic patterns
• Efficient for text classification
Step 4- Evaluating Model Performance
Prediction on Test Data
sms_test_pred <- predict(sms_classifier, sms_test)
• Uses trained model to classify unseen SMS messages
• sms_test = test features
• sms_test_pred = predicted labels
Compare Predictions vs. Actual Labels
library(gmodels)
CrossTable(sms_test_pred, sms_raw_test$type,
[Link] = FALSE, prop.t = FALSE,
dnn = c('predicted', 'actual’))
Displays confusion matrix for evaluating accuracy
Step 4- Evaluating Model Performance
Confusion Matrix Results
Actual: Ham Actual: Spam Total
Predicted: 1203 32
1235
Ham 0.997 0.175
Predicted: 4 151
155
Spam 0.003 0.825
1207 183
Total 1390
0.868 0.132
Performance Summary
• Accuracy: ~98%
• False Positives (Ham → Spam): 4 (0.3%)
• False Negatives (Spam → Ham): 32 (17.5%)
Step 5- Improving Model Performance
Enhancing Naive Bayes with Laplace Smoothing
• Without Laplace smoothing, words that appear only in spam or
ham dominate classification
• E.g., word “ringtone” seen only in spam → every message with
it is treated as spam.
• Risk of overfitting and misclassification.
Apply Laplace Smoothing (laplace = 1)
sms_classifier2 <- naiveBayes(sms_train, sms_raw_train$type,
laplace = 1)
sms_test_pred2 <- predict(sms_classifier2, sms_test)
Step 5- Improving Model Performance
New Confusion Matrix
Actual: Ham Actual: Spam Total
1204 31
Predicted: Ham 1235
0.998 0.169
3 152
Predicted: Spam 155
0.002 0.831
1207 183
Total 1390
0.868 0.132
Performance Gains
False Positives reduced: 4 → 3
False Negatives reduced: 32 → 31
Slight but meaningful improvement
Shows the value of smoothing techniques
Even small improvements reduce risk of important messages being
missed.
Laplace smoothing helps generalize better to unseen data.
CONCLUSI
ON