When we watch how a model works over time, we want to see if it keeps making good predictions. The main metrics to check are accuracy, precision, recall, and F1 score. These tell us if the model is still correct and balanced in its guesses. We also look at loss to see if the modelโs errors are growing. Monitoring these helps catch problems early, like if the model starts to forget or if the data changes.
Monitoring model performance in ML Python - Model Metrics & Evaluation
Actual \ Predicted | Positive | Negative
-------------------|----------|---------
Positive | 80 | 20
Negative | 10 | 90
Total samples = 200
Precision = 80 / (80 + 10) = 0.89
Recall = 80 / (80 + 20) = 0.80
Accuracy = (80 + 90) / 200 = 0.85
This matrix helps us see if the model is confusing positive and negative cases. Watching it over time shows if mistakes increase.
Sometimes, improving one metric lowers the other. For example:
- Spam filter: High precision means fewer good emails marked as spam. We want to avoid losing important emails.
- Cancer detection: High recall means catching most cancer cases. Missing a case is very bad.
When monitoring, if precision drops but recall stays high, the model may mark too many good cases as bad. If recall drops, it may miss important cases. We watch these to keep the balance right.
Good: Metrics stay stable or improve over time. For example, accuracy around 90%, precision and recall both above 85%, and loss stays low or decreases.
Bad: Metrics suddenly drop or slowly decline. For example, accuracy falls below 70%, precision or recall drop below 50%, or loss increases. This means the model may be confused or data has changed.
- Accuracy paradox: High accuracy can be misleading if data is unbalanced. For example, 95% accuracy on 95% negative data means the model ignores positives.
- Data leakage: If future data leaks into training, metrics look great but fail in real use.
- Overfitting indicators: Training metrics improve but test or live metrics worsen, showing the model learned noise, not real patterns.
- Ignoring metric trends: Small drops over time can signal problems before big failures.
Your model has 98% accuracy but only 12% recall on fraud cases. Is it good for production? Why or why not?
Answer: No, it is not good. Even though accuracy is high, the model misses 88% of fraud cases (low recall). This means many frauds go undetected, which is risky. For fraud detection, high recall is critical.
Practice
Solution
Step 1: Understand model deployment context
After deployment, models face new data that may differ from training data.Step 2: Importance of monitoring
Monitoring helps detect if the model's accuracy or other metrics drop over time.Final Answer:
To ensure the model continues to perform well on new data -> Option AQuick Check:
Monitoring = Ensure performance [OK]
- Confusing monitoring with training speed
- Thinking monitoring changes model algorithm automatically
- Assuming monitoring reduces model size
y_true and predicted labels y_pred?Solution
Step 1: Understand accuracy formula
Accuracy = number of correct predictions divided by total predictions.Step 2: Match formula to code
sum(y_true == y_pred)counts correct predictions; dividing bylen(y_true)gives accuracy.Final Answer:
accuracy = sum(y_true == y_pred) / len(y_true) -> Option CQuick Check:
Correct predictions / total = accuracy [OK]
- Dividing total by correct instead of correct by total
- Using inequality instead of equality for correct count
- Summing predictions instead of comparing labels
accuracies = [0.9, 0.85, 0.8, 0.75] threshold = 0.8 alerts = [acc < threshold for acc in accuracies] print(alerts)
Solution
Step 1: Evaluate each accuracy against threshold
Check if each accuracy is less than 0.8: 0.9 < 0.8? False; 0.85 < 0.8? False; 0.8 < 0.8? False; 0.75 < 0.8? True.Step 2: Create alerts list
List comprehension results in [False, False, False, True].Final Answer:
[False, False, False, True] -> Option DQuick Check:
Compare each value to threshold [OK]
- Misunderstanding < vs <= in comparisons
- Confusing True/False order in list
- Assuming 0.8 < 0.8 is True
losses = [0.3, 0.25, 0.4, 0.35]
for i in range(len(losses))
if losses[i] > 0.3:
print(f"High loss at index {i}: {losses[i]}")Solution
Step 1: Identify syntax error in for loop
The for loop line is missing a colon at the end, which is required in Python syntax.Step 2: Fix syntax by adding colon
Add ':' afterrange(len(losses))to correct the syntax and allow the loop to run.Final Answer:
SyntaxError due to missing colon after for loop; add ':' after 'range(len(losses))' to fix -> Option AQuick Check:
Missing colon = SyntaxError [OK]
- Ignoring missing colon causing syntax error
- Confusing indentation error with syntax error
- Assuming variable 'i' is undefined
Solution
Step 1: Understand monitoring automation
Automated scheduled jobs can regularly check model metrics like F1 score.Step 2: Choose alerting based on threshold
Sending alerts when F1 score drops below 0.7 helps catch performance issues early.Final Answer:
Set up a scheduled job that calculates F1 score daily and sends an alert if below 0.7 -> Option BQuick Check:
Automate checks + alerts = Set up a scheduled job that calculates F1 score daily and sends an alert if below 0.7 [OK]
- Retraining without checking performance first
- Manual checks too infrequent for timely alerts
- Ignoring key metrics like F1 score
Ready to prove it?
Pass the test and this pattern moves unknown
