🧠 AI with Python – 📝 Logging Predictions with Timestamps
Posted on: June 2, 2026
Description:
Building a machine learning model is only one part of a real-world ML system. Once a model is deployed, it becomes important to monitor what the model is predicting, when predictions are made, and how the system behaves over time. This is where prediction logging becomes critical.
In this project, we explore how to log machine learning predictions along with timestamps and metadata for monitoring and operational visibility.
Why Prediction Logging Matters
In production systems, predictions are continuously generated for:
- customer actions
- fraud checks
- recommendations
- healthcare decisions
- business workflows
Without proper logging, it becomes difficult to:
- debug incorrect predictions
- monitor model behaviour
- analyse failures
- detect drift
- audit historical decisions
Prediction logging provides visibility into the ML system.
What Should Be Logged?
A production ML system often stores:
- timestamp
- prediction output
- prediction probability
- input features
- model version
- request IDs
- latency metrics
These logs help teams understand how the model behaves in real-world usage.
1. Train the Model
We first train a machine learning model.
model = LogisticRegression()
model.fit(X_train, y_train)
This creates the prediction engine.
2. Generate Predictions
We make predictions on sample input data.
predictions = model.predict(sample_data)
We also capture prediction probabilities.
prediction_probs = model.predict_proba(sample_data)[:, 1]
Probabilities are extremely useful for monitoring model confidence.
3. Create Structured Logs
We create structured log entries containing prediction information.
log_entry = {
"timestamp": datetime.now(),
"prediction": prediction,
"prediction_probability": probability
}
This creates machine-readable logs suitable for monitoring systems.
4. Store Logs
Logs can be stored in:
- CSV files
- databases
- cloud logging systems
- monitoring platforms
In this example:
logs_df.to_csv("prediction_logs.csv")
Why Timestamps Are Important
Timestamps help answer questions like:
- When did prediction quality degrade?
- Did behaviour change after deployment?
- Are predictions drifting over time?
- Which predictions happened during incidents?
Without timestamps, operational analysis becomes difficult.
Real-World ML Monitoring
Modern ML systems often integrate logging with:
- Grafana
- ELK Stack
- Prometheus
- Datadog
- cloud observability systems
Logging becomes the foundation for production ML observability.
Where Prediction Logging Is Used
Prediction logging is essential in:
- fraud detection systems
- recommendation engines
- healthcare ML
- fintech systems
- customer analytics platforms
These systems require traceability and auditing.
Key Takeaways
- Prediction logging is critical in production ML systems.
- Timestamps help monitor model behaviour over time.
- Logging supports debugging, auditing, and monitoring.
- Structured logs improve operational visibility.
- Production ML requires engineering workflows beyond training models.
Conclusion
Machine learning systems do not stop at predictions. Real-world ML requires monitoring, traceability, and operational visibility to ensure reliability over time. Logging predictions with timestamps is one of the foundational practices in building production-ready ML systems.
This strengthens the ML Systems track in the AI with Python series — focusing on the engineering and operational side of machine learning workflows.
Code Snippet:
# 📦 Import Required Libraries
import pandas as pd
from datetime import datetime
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
# 🧩 Load Dataset
data = load_breast_cancer()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = data.target
# ✂️ Split Data
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.3,
random_state=42,
stratify=y
)
# =========================================================
# 🤖 Train Model
# =========================================================
model = LogisticRegression(max_iter=5000)
model.fit(X_train, y_train)
# =========================================================
# 📊 Generate Predictions
# =========================================================
sample_data = X_test.iloc[:5]
predictions = model.predict(sample_data)
prediction_probs = model.predict_proba(sample_data)[:, 1]
# =========================================================
# 📝 Create Prediction Logs
# =========================================================
logs = []
for i in range(len(sample_data)):
log_entry = {
"timestamp": datetime.now(),
"prediction": int(predictions[i]),
"prediction_probability": round(
float(prediction_probs[i]),
4
),
# Example input features
"mean_radius": sample_data.iloc[i]["mean radius"],
"mean_texture": sample_data.iloc[i]["mean texture"]
}
logs.append(log_entry)
# =========================================================
# 📂 Convert Logs to DataFrame
# =========================================================
logs_df = pd.DataFrame(logs)
print("Prediction Logs:\n")
print(logs_df)
# =========================================================
# 💾 Save Logs to CSV
# =========================================================
logs_df.to_csv(
"prediction_logs.csv",
index=False
)
print("\nPrediction logs saved successfully.")
# =========================================================
# 🔍 Read Saved Logs
# =========================================================
saved_logs = pd.read_csv("prediction_logs.csv")
print("\nSaved Logs Preview:\n")
print(saved_logs.head())
No comments yet. Be the first to comment!