Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
Keeping Your Machine Learning Model Healthy, Relevant,
and Actionable
🧠 Introduction
Once your machine learning model is deployed, the job isn’t
done. In fact, it’s just beginning.
A model that works today might become useless tomorrow if
not monitored, maintained, and reported properly.
This chapter focuses on:
Let’s explore how to ensure your model keeps delivering
value over time.
🔄 1. Why Monitoring
Matters
Without Monitoring:
Risk |
Result |
Data drift |
Poor predictions |
Model decay |
Increasing
error over time |
Latency spikes |
Poor user experience |
No alerts |
Failure goes
unnoticed |
📡 2. Key Monitoring
Metrics
Metric |
Description |
Prediction Accuracy |
Is the model still
performing as expected? |
Latency |
How long does
the model take to respond? |
Input Drift |
Is the input data
distribution changing? |
Output Drift |
Are
predictions changing abnormally? |
Error Rate |
% of predictions that
failed or were incorrect |
Resource Usage |
CPU, RAM,
storage consumed by model API |
🛠️ 3. How to Implement
Monitoring
✅ Logging Predictions
python
import
datetime
import
json
def
log_prediction(input_data, prediction):
log = {
'timestamp':
str(datetime.datetime.now()),
'input': input_data,
'prediction': prediction
}
with open('logs.jsonl', 'a') as f:
f.write(json.dumps(log) + '\n')
✅ Track API Health with
Prometheus + Grafana
bash
#
Example Flask app endpoint
@app.route("/health")
def
health():
return {"status":
"ok"}, 200
Integrate
Prometheus exporters like prometheus_flask_exporter.
✅ Alerting with Watchdog or
Sentry
Get alerts when:
bash
#
Sample email alert trigger in Python
if
accuracy < 0.75:
send_email("Model accuracy dropped
below threshold!")
📈 4. Detecting Model
Drift
✅ What is Drift?
Type |
Meaning |
Data Drift |
Input data changes
(e.g., new user behavior) |
Concept Drift |
Target
meaning changes (e.g., fraud evolves) |
Label Shift |
Distribution of labels
changes |
✅ Tools to Detect Drift:
Tool |
Use Case |
Evidently.ai |
Data & concept
drift, dashboards |
River (Python) |
Streaming
model monitoring |
Alibi Detect |
Drift, outlier,
adversarial detection |
♻️ 5. Model Maintenance Best
Practices
🔁 Retraining Triggers:
Trigger Type |
Example |
Scheduled |
Weekly or monthly
retraining |
Performance-based |
Accuracy <
80% over last 1000 predictions |
Data threshold |
10,000 new rows
collected |
Manual update |
Human-inspected
model updates |
✅ Retraining Script Example
python
def
retrain_model():
new_data = load_new_data()
X_train, y_train = preprocess(new_data)
model.fit(X_train, y_train)
joblib.dump(model, 'model_updated.pkl')
Schedule this with cron or use Airflow for orchestration.
📄 6. Reporting for
Stakeholders
Machine learning results should be made digestible for
non-technical audiences.
✅ Include in Your Report:
Section |
Description |
Executive Summary |
3–5 line business
impact overview |
Model Performance |
Accuracy, F1,
AUC, etc. |
Changes Since Last |
What’s been updated or
improved |
Recommendations |
Whether to
retrain, adjust features, etc. |
Visualizations |
ROC curve, confusion
matrix, drift plots |
📊 Visualization Tips
python
import
seaborn as sns
sns.heatmap(df.corr(),
annot=True)
📁 Report Format Options
Format |
Use Case |
PDF (via Jupyter
nbconvert) |
Static reports |
HTML Dashboard |
Interactive
report (Plotly, Dash) |
Google Slides |
Business/marketing
presentation |
Excel Export |
Table-based
tracking |
💡 7. Tools and Services
for Maintenance
Tool/Service |
Use Case |
MLflow |
Model tracking,
versioning |
Airflow |
Schedule
retraining pipelines |
Docker |
Reproducibility of
deployed models |
Streamlit |
Quick updates
to data dashboards |
DVC |
Dataset versioning |
🔄 8. Automating the MLOps
Loop
Create a CI/CD pipeline for ML:
Step |
Tool |
New data uploaded |
GitHub Actions,
Airflow |
Retrain model |
Python +
joblib |
Evaluate
performance |
Cross-validation,
drift testing |
Update model in prod |
Docker +
CI/CD |
Notify stakeholders |
Slack API, email
alerts |
Answer: The data science workflow is a structured step-by-step process used to turn raw data into actionable insights or solutions. It ensures clarity, efficiency, and reproducibility from problem definition to deployment.
Answer: Not necessarily. While there is a general order, data science is iterative. You may go back and forth between stages (like EDA and feature engineering) as new insights emerge.
Answer: Data cleaning prepares the dataset by fixing errors and inconsistencies, while EDA explores the data to find patterns, trends, and relationships to inform modeling decisions.
Answer: You can build a baseline model early, but robust feature engineering often improves performance significantly. It's best to iterate and refine after EDA and feature transformations.
Answer: Popular tools include Python libraries like scikit-learn, XGBoost, LightGBM, and TensorFlow for building models, and metrics functions within sklearn.metrics for evaluation.
Answer: It depends on the problem:
Answer: Start with lightweight options like:
Answer: Use logging for predictions, track performance metrics over time, and set alerts for significant drops. Tools like MLflow, Prometheus, and AWS CloudWatch are commonly used.
Answer: Yes. For learning or portfolio-building, it's okay to stop after model evaluation. But deploying at least one model enhances your understanding of real-world applications.
Answer: Choose a simple dataset (like Titanic or housing prices), go through every workflow step end-to-end, and document your process. Repeat with different types of problems to build experience.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)