Machine
Learning
MODULE -2
WHAT IS MACHINE LEARNING?
A computer program is said to learn from experience E with respect to some class of tasks T
and performance measure P, if its performance at tasks in T, as measured by P, improves
with experience
A learning problem is said to be well-posed if:
[Link] is a clearly defined task (T)
2.A performance measure (P) to evaluate the task
[Link] (E) or data from which the system learns
Example:
•Task (T): Predict house prices
•Performance (P): Accuracy of prediction
•Experience (E): Historical house data
1. Data Input (Experience)
In Machine Learning, the data input is the raw information that the algorithm uses to learn
patterns.
This data is also called the experience (E) in a well-posed learning problem.
What it is:
•It consists of features (input variables) and sometimes labels (expected outputs).
•It can be structured (tables, numbers) or unstructured (text, images, audio, video).
•Data input provides the foundation for the model to learn.
2. Abstraction (Pattern Identification)
After inputting the data, the model learns an internal representation of it — this is called
abstraction.
What it is:
•Abstraction means finding hidden relationships or underlying structure in the data.
•The model simplifies the complex raw data into useful mathematical patterns or rules.
•It does not memorize the data; it captures the essence of it.
3. Generalization (Learning to Predict on New Data)
This is one of the most important goals of ML — not just to fit the training data, but to
perform well on new, unseen data.
What it is:
•Generalization is the model’s ability to apply what it learned to make correct predictions on
new inputs.
•If a model only memorizes training data → poor generalization and vice verca
What is clustering?
• Clustering is used in projects for companies that want to find common
aspects within their customers to apply customer segmentation, create
customer journey maps or find groups and focus products or services.
• Thus, if a significant percentage of customers have certain aspects in
common (age, type of family, etc.) the company can justify a particular
campaign, service or product. Clustering is also useful to obtain general
insights and information.
What is classification?
• On the other hand, classification belongs to supervised learning, which
means that we know the input data (labeled in this case) and we know the
possible output of the algorithm.
[Link] data :
•Training data is the portion of the dataset used to teach the model.
•The model learns patterns and relationships between input (features) and output (labels).
Example:
•Suppose you want to train a model to predict student grades based on hours studied.
• Input (X): hours studied
• Output (Y): grade
•The training data might contain 80% of all the student records.
•The model studies these examples to “learn” the relationship.
2. Testing Data
What it is:
•Testing data is the remaining part of the dataset used to check the model’s performance.
•The model has never seen this data before.
•It shows how well the model can generalize to new data.
• Example:
•The remaining 20% of student records are kept aside.
•After training, we give these new inputs to the model to predict grades.
•Then, we compare predicted results with actual results to find accuracy.
K-Fold Cross Validation
• It is a popular model evaluation technique used in machine learning to check how well a model will perform on unseen data.
It helps to reduce overfitting and gives a more reliable estimate of model performance.
How K-Fold Cross Validation Works
Let’s say k = 5 (a common choice):
[Link] the dataset into k equal parts (folds).
[Link] k − 1 folds for training and 1 fold for testing.
[Link] this process k times — each time a different fold is used as the test set.
[Link] the performance across all k tests to get the final accuracy (or error).
Advantages
•More reliable estimate of model performance
•Uses entire dataset for both training and testing (at different times)
•Reduces bias compared to single train-test split
•Overfitting happens when the model learns the training data too well, including noise and
random fluctuations.
•It performs very well on training data, but poorly on new (test) data.
Example:
•Suppose a student memorizes all the questions and answers from last year’s exam.
•If the same questions come, they’ll score 100%.
•But if new questions come, they’ll struggle — because they didn’t actually understand the
concept
Underfitting
Definition:
•Underfitting happens when the model is too simple and cannot capture the patterns in the data.
•It performs poorly on both training and test data.
Example:
•A student only studies basic formulas but doesn’t understand them.
•They can’t answer even the basic or new questions well.
Signs of Overfitting: Signs of Underfitting:
•High training accuracy •Low training accuracy
•Low testing accuracy •Low testing accuracy
•Model is too complex (too many features, parameters) •Model is too simple (not enough features, too few
parameters)
How to Reduce Overfitting:
•Use cross-validation (like K-Fold) How to Fix Underfitting:
•Simplify the model •Make the model more complex (add layers, features,
•Use regularization techniques etc.)
•Collect more data •Train longer
•Use early stopping during training •Use better feature engineering
Confusion Matrix
It is used to compares predicted vs actual values.
Predicted Positive Predicted Negative
Actual Positive True Positive (TP) False Negative (FN)
Actual Negative False Positive (FP) True Negative (TN)
• In machine learning classification problems, after training the model on training data and testing it on test
data, we use evaluation metrics to measure how well the model is performing.
1. Accuracy
Definition:
The ratio of correct predictions to total predictions.
Meaning:
How often the model is correct overall.
Example:
If 90 out of 100 predictions are correct, accuracy = 90%.
Use when:
Classes are balanced (e.g., equal positives and negatives).
•TP + TN = Total correct predictions
•TP + TN + FP + FN = Total predictions
So Accuracy tells:
Out of all predictions, how many did the model predict correctly?
2. Precision
Definition:
Out of all predicted positives, how many were actually positive?
Meaning:
“How precise” your positive predictions are.
Example:
If the model predicted 10 positives and 8 were correct → Precision = 0.8 (80%).
Use when:
False positives are costly (e.g., spam email detection — we don’t want to mark real emails
as spam).
•TP = Correctly predicted positive cases
•FP = Model said positive, but it was actually negative (wrong positive)
So Precision tells:
When the model predicts Positive, how often is it actually correct?
Recall (Sensitivity or True Positive Rate)
Definition:
Out of all actual positives, how many did the model correctly identify?
Meaning:
“How many actual positives did we catch?”
Example:
If there are 10 actual positives and we found 8 → Recall = 0.8 (80%).
Use when:
Missing positives is costly (e.g., detecting cancer — we don’t want to miss real cases).
•TP = Correctly predicted positives
•FN = Model said negative, but it was actually positive (missed positive)
So Recall tells:
Out of all the actual Positive cases, how many did the model correctly detect?
F1-Score
Definition:
The harmonic mean of Precision and Recall — balances both.
Meaning:
Good measure when you need a balance between precision and
recall.
Example:
If Precision = 0.8 and Recall = 0.6
•Precision and Recall sometimes conflict.
•F1 Score takes the harmonic mean, giving balanced performance.
So F1 Score tells:
How well the model balances Precision and Recall.
Metric Formula Best When Measures
(TP + TN) / (TP + Classes are Overall
Accuracy TN + FP + FN) balanced correctness
False positives are
Precision TP / (TP + FP) costly Exactness
False negatives are
Recall TP / (TP + FN) costly Completeness
Trade-off between
F1-score 2 × (P×R)/(P+R) Need balance P&R
Q1. Simple Confusion Matrix Calculation
A binary classifier produced the following results on 100 test samples:
•True Positives (TP) = 40
•True Negatives (TN) = 50
•False Positives (FP) = 5
•False Negatives (FN) = 5
Find: Accuracy, Precision, Recall, and F1-score.
Accuracy=TP+TN+FP+FNTP+TN=100/40+50=0.90
Answer: Accuracy = 90%, Precision = 88.9%, Recall = 88.9%, F1 =
88.9%
Q2. High False Positives
In a spam detection model:
•TP = 70
•TN = 900
•FP = 100
•FN = 30
Find: Accuracy, Precision, Recall, and F1.
Accuracy=70+900+100+3070+900=1100970=0.882
Answer: Accuracy = 88.2%, Precision = 41.2%, Recall =
70%, F1 = 51.8%
Q3. High False Negatives (Medical Diagnosis Example)
A disease detection system gave:
•TP = 90
•TN = 860
•FP = 40
•FN = 10
Find: Accuracy, Precision, Recall, F1.
Accuracy=100090+860=0.95
Answer: Accuracy = 95%, Precision = 69.2%, Recall =
90%, F1 = 78.2%
Q4. Model Comparison
Model TP TN FP FN
A 80 70 20 30
B 60 90 10 40
Which model is better based on F1-score?
Model A:
•Precision = 80/(80+20)=0.8
•Recall = 80/(80+30)=0.727
•F1 = 2×(0.8×0.727)/(0.8+0.727)=0.761
Model B:
•Precision = 60/(60+10)=0.857
•Recall = 60/(60+40)=0.6
•F1 = 2×(0.857×0.6)/(0.857+0.6)=0.705
Answer: Model A has higher F1 (0.761) → Model A is better (better balance).
1. Problem:
A model predicted whether 20 students passed or failed an exam.
Calculate accuracy
Actual \
Pass Fail
Predicted
Pass 8 2
Fail 1 9
TP = 8, FN = 2, FP = 1, TN = 9
Total = 8 + 2 + 1 + 9 = 20
2. Problem:
Answer: Accuracy = 85% A spam detection model predicted 15 emails as spam.
Out of those 15, only 12 were actually spam.
There were 5 more spam emails it missed. Calculate precision
TP = 12 (correct spam predictions)
FP = 3 (wrong spam predictions)
FN = 5 (missed spam emails)
Answer: Precision = 80%
A spam detection model analyzed 100 emails. It predicted 40 emails as spam, out of which 35 were actually spam. Out of
the 60 emails predicted as not spam, 5 were actually spam. Based on the given information, construct the confusion
matrix and calculate the model’s accuracy, precision, recall, and F1-score
•Predicted Not Spam = 60
•Total emails = 100
•Predicted Spam = 40
•Out of these, 35 were actually Spam → True Positive (TP) = 35
•Remaining were incorrectly predicted as spam → False Positive (FP) = 40 − 35 = 5
•Out of these, 5 were actually Spam → False Negative (FN) = 5
•Remaining were correctly predicted as not spam → True Negative (TN) = 60 − 5 = 55
Actual Spam Actual Not Spam
Predicted Spam TP = 35 FP = 5
Predicted Not Spam FN = 5 TN = 55
. Accuracy
2. Precision (How many predicted spam were actually spam?)
3. Recall (How many actual spam emails were correctly detected?)
4. F1-Score
THANK
YOU