Storing and Querying ML Results
Persisting model predictions, experiment logs, and feature data in relational databases.
Storing and Querying ML Results is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Tracking ML Experiments
As you train models you produce dozens of runs with different settings and scores. Without tracking, you lose which configuration worked best.
A small experiments database records every run so you can query, compare, and reproduce the winner.
Designing an Experiments Table
A good schema captures the run identity, the model, key hyperparameters, the metric, and a timestamp. Store flexible hyperparameters as a JSON text column.
import sqlite3
conn = sqlite3.connect("experiments.db")
conn.execute("""
CREATE TABLE IF NOT EXISTS runs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
model TEXT NOT NULL,
params TEXT,
accuracy REAL,
f1 REAL,
created_at TEXT DEFAULT (datetime("now"))
)
""")
conn.commit()Why a Timestamp and ID
The auto-increment id gives each run a stable handle, and created_at lets you order runs over time or find the latest.
The DEFAULT (datetime("now")) fills the timestamp automatically on insert.
Logging a Run
After training, insert the results. Serialize the hyperparameter dict to JSON so the schema stays flexible across models.
import json, sqlite3
params = {"n_estimators": 200, "max_depth": 8}
conn.execute(
"INSERT INTO runs (model, params, accuracy, f1) VALUES (?, ?, ?, ?)",
("random_forest", json.dumps(params), 0.913, 0.902),
)
conn.commit()A Reusable log_run Helper
Wrap insertion into a function you call at the end of every training script. Consistent logging is what makes later comparison possible.
def log_run(conn, model, params, accuracy, f1):
conn.execute(
"INSERT INTO runs (model, params, accuracy, f1) VALUES (?, ?, ?, ?)",
(model, json.dumps(params), accuracy, f1),
)
conn.commit()Selecting the Best Runs
Find your top models by ordering on the metric. ORDER BY accuracy DESC with LIMIT returns the leaders.
cur = conn.execute(
"SELECT model, accuracy, f1 FROM runs ORDER BY accuracy DESC LIMIT 5"
)
for row in cur.fetchall():
print(row)Filtering by Model or Threshold
Narrow comparisons with WHERE: only one model family, or only runs above a target score.
cur = conn.execute(
"SELECT model, accuracy FROM runs WHERE model = ? AND accuracy > ? ORDER BY accuracy DESC",
("random_forest", 0.90),
)
print(cur.fetchall())Aggregating Across Runs
SQL aggregates summarize many runs at once: best, average, and count per model with GROUP BY.
cur = conn.execute("""
SELECT model,
COUNT(*) AS n,
MAX(accuracy) AS best,
AVG(accuracy) AS mean
FROM runs
GROUP BY model
ORDER BY best DESC
""")
print(cur.fetchall())Loading Results into pandas
For richer comparison, pull the table into a DataFrame and analyze with pandas — sort, pivot, and plot.
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine("sqlite:///experiments.db")
df = pd.read_sql("SELECT * FROM runs", engine)
print(df.sort_values("accuracy", ascending=False).head())Comparing Experiments in pandas
Expand the JSON params into columns to correlate hyperparameters with the metric — exactly what guides your next experiment.
import json, pandas as pd
params_df = df["params"].apply(json.loads).apply(pd.Series)
full = pd.concat([df, params_df], axis=1)
print(full.groupby("max_depth")["accuracy"].mean())Reproducing the Winner
Because every run stored its hyperparameters, reproducing the best model is just reading its row and re-instantiating with those params.
best = df.sort_values("accuracy", ascending=False).iloc[0]
import json
best_params = json.loads(best["params"])
print("Reproduce", best["model"], "with", best_params)Quick Check: Best Runs
You want the five highest-accuracy runs from the table.
Recap: Storing and Querying ML Results
You built an experiment tracking workflow:
- A
runstable with model, JSON params, metrics, and timestamp - A
log_runhelper to record every training run ORDER BY ... DESC LIMITto find best runs,WHEREto filterGROUP BYaggregates and pandas for deeper comparison- Stored params make the winner reproducible
Next: vector databases for embedding search.
Frequently asked questions
Is the “Storing and Querying ML Results” lesson free?
Yes — the full text of “Storing and Querying ML Results” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Storing and Querying ML Results”?
Persisting model predictions, experiment logs, and feature data in relational databases. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Storing and Querying ML Results” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- SQLite with Python's sqlite3 Module
- Pandas and SQL Integration
- Storing and Querying ML Results
- Introduction to Vector Databases