37. How to Check If Your ML Model Is Good
37.4 Small sklearn Metrics Demo
Why. Glue metrics to a real fit/predict path so the habit sticks.
What. Train a tiny LogisticRegression, then report accuracy, precision, recall, and the confusion matrix on the test set only.
How.
Metrics demo
- Fit on train.
- Predict on test.
- Call accuracy_score, precision_score, recall_score, classification_report.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, classification_report
df = pd.DataFrame({
"orders_last_30d": [2, 8, 1, 12, 3, 9, 0, 11, 4, 7, 1, 10, 5, 13, 2, 9],
"complaints": [1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 2, 0],
"churned": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
})
X = df[["orders_last_30d", "complaints"]]
y = df["churned"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=7, stratify=y
)
clf = LogisticRegression(max_iter=200).fit(X_train, y_train)
pred = clf.predict(X_test)
print("accuracy", accuracy_score(y_test, pred))
print("precision", precision_score(y_test, pred, zero_division=0))
print("recall", recall_score(y_test, pred, zero_division=0))
print(classification_report(y_test, pred, zero_division=0))
What you should see. A small report. On tiny data, numbers jump if you change random_state — that teaches humility, not despair.
Fictional data
Churn table is synthetic for the classroom.