We use evaluation metrics to see how well a model is doing. They help us understand if the model makes good predictions or not.
Evaluation metrics (accuracy, F1, confusion matrix) in NLP
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NLP
from sklearn.metrics import accuracy_score, f1_score, confusion_matrix # accuracy = accuracy_score(true_labels, predicted_labels) # f1 = f1_score(true_labels, predicted_labels, average='binary') # cm = confusion_matrix(true_labels, predicted_labels)
accuracy_score measures the percentage of correct predictions.
f1_score balances precision and recall, useful when classes are uneven.
Examples
NLP
accuracy = accuracy_score([1,0,1,1], [1,0,0,1]) f1 = f1_score([1,0,1,1], [1,0,0,1]) cm = confusion_matrix([1,0,1,1], [1,0,0,1])
NLP
f1_macro = f1_score([0,1,2,2], [0,2,1,2], average='macro')
Sample Model
This program shows how to calculate accuracy, F1 score, and confusion matrix for a simple binary classification example.
NLP
from sklearn.metrics import accuracy_score, f1_score, confusion_matrix # True labels for 8 samples true_labels = [0, 1, 0, 1, 0, 1, 1, 0] # Model predictions predicted_labels = [0, 0, 0, 1, 0, 1, 0, 1] # Calculate accuracy accuracy = accuracy_score(true_labels, predicted_labels) # Calculate F1 score (binary) f1 = f1_score(true_labels, predicted_labels) # Calculate confusion matrix cm = confusion_matrix(true_labels, predicted_labels) print(f"Accuracy: {accuracy:.2f}") print(f"F1 Score: {f1:.2f}") print("Confusion Matrix:") print(cm)
Important Notes
Accuracy can be misleading if classes are imbalanced.
F1 score is better when you care about both false positives and false negatives.
The confusion matrix shows counts of true negatives, false positives, false negatives, and true positives.
Summary
Accuracy tells how many predictions were correct overall.
F1 score balances precision and recall, useful for uneven classes.
Confusion matrix helps see where the model makes mistakes.
Practice
1. What does the accuracy metric measure in a classification model?
easy
Solution
Step 1: Understand accuracy definition
Accuracy is defined as the number of correct predictions divided by the total number of predictions made.Step 2: Compare options with definition
Only The proportion of correct predictions out of all predictions correctly describes accuracy as the proportion of correct predictions out of all predictions.Final Answer:
The proportion of correct predictions out of all predictions -> Option AQuick Check:
Accuracy = Correct predictions / Total predictions [OK]
Hint: Accuracy = correct predictions divided by total predictions [OK]
Common Mistakes:
- Confusing accuracy with F1 score
- Thinking accuracy measures only false positives
- Believing accuracy counts number of classes
2. Which of the following is the correct formula for F1 score?
easy
Solution
Step 1: Recall F1 score formula
F1 score is the harmonic mean of precision and recall, calculated as 2 times their product divided by their sum.Step 2: Match formula with options
2 * (Precision * Recall) / (Precision + Recall) matches the correct formula: 2 * (Precision * Recall) / (Precision + Recall).Final Answer:
2 * (Precision * Recall) / (Precision + Recall) -> Option BQuick Check:
F1 = 2PR/(P+R) [OK]
Hint: F1 score = 2 * Precision * Recall / (Precision + Recall) [OK]
Common Mistakes:
- Adding precision and recall instead of harmonic mean
- Using true positives over total samples as F1
- Confusing F1 with specificity
3. Given the confusion matrix below for a binary classifier:
What is the accuracy of the model?
[[50, 10], [5, 35]]
What is the accuracy of the model?
medium
Solution
Step 1: Identify confusion matrix values
True Positives (TP) = 50, False Positives (FP) = 10, False Negatives (FN) = 5, True Negatives (TN) = 35.Step 2: Calculate accuracy
Accuracy = (TP + TN) / (TP + FP + FN + TN) = (50 + 35) / (50 + 10 + 5 + 35) = 85 / 100 = 0.85 or 85%.Final Answer:
85% -> Option DQuick Check:
Accuracy = (TP+TN)/Total = 85/100 = 85% [OK]
Hint: Accuracy = (TP + TN) / total samples [OK]
Common Mistakes:
- Adding false positives or false negatives to numerator
- Calculating only TP / total samples
- Mixing up TP and TN values
4. You have this confusion matrix:
Which line of code correctly calculates precision for the positive class?
[[40, 20], [10, 30]]
Which line of code correctly calculates precision for the positive class?
medium
Solution
Step 1: Recall precision formula
Precision is the ratio of true positives to all predicted positives: TP / (TP + FP).Step 2: Match formula with options
precision = TP / (TP + FP) correctly uses TP / (TP + FP). precision = TP / (TP + FN) uses recall formula, C and D are incorrect.Final Answer:
precision = TP / (TP + FP) -> Option AQuick Check:
Precision = TP / (TP + FP) [OK]
Hint: Precision = true positives / predicted positives [OK]
Common Mistakes:
- Using TP / (TP + FN) which is recall
- Confusing TN with TP in precision
- Dividing by TP + TN instead of TP + FP
5. A model has precision = 0.8 and recall = 0.5. What is the F1 score? Choose the closest value.
hard
Solution
Step 1: Recall F1 score formula
F1 = 2 * (Precision * Recall) / (Precision + Recall) = 2 * (0.8 * 0.5) / (0.8 + 0.5).Step 2: Calculate F1 score
Calculate numerator: 2 * 0.4 = 0.8. Calculate denominator: 1.3. F1 = 0.8 / 1.3 ≈ 0.615.Final Answer:
0.62 -> Option CQuick Check:
F1 ≈ 0.62 from 0.8 precision and 0.5 recall [OK]
Hint: F1 is harmonic mean: 2PR/(P+R), plug values carefully [OK]
Common Mistakes:
- Averaging precision and recall instead of harmonic mean
- Mixing up precision and recall values
- Rounding too early causing wrong final answer
