Python ML Patterns (scikit-learn)
Pipeline Pattern
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import cross_val_score, StratifiedKFold
# Feature groups
numeric_features = ["age", "income", "tenure"]
categorical_features = ["gender", "country", "plan"]
# Preprocessing
numeric_transformer = Pipeline([
("imputer", SimpleImputer(strategy="median")),
("scaler", StandardScaler()),
])
categorical_transformer = Pipeline([
("imputer", SimpleImputer(strategy="most_frequent")),
("encoder", OneHotEncoder(handle_unknown="ignore", sparse_output=False)),
])
preprocessor = ColumnTransformer([
("num", numeric_transformer, numeric_features),
("cat", categorical_transformer, categorical_features),
])
# Full pipeline
model = Pipeline([
("preprocessor", preprocessor),
("classifier", GradientBoostingClassifier(n_estimators=100, random_state=42)),
])
Evaluation
import numpy as np
from sklearn.metrics import classification_report, roc_auc_score
# Cross-validation (never evaluate on training data)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(model, X_train, y_train, cv=cv, scoring="roc_auc")
print(f"AUC: {scores.mean():.3f} ± {scores.std():.3f}")
# After final model training on full train set
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_proba = model.predict_proba(X_test)[:, 1]
print(classification_report(y_test, y_pred))
print(f"Test AUC: {roc_auc_score(y_test, y_proba):.3f}")
Hyperparameter Tuning
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint, uniform
param_distributions = {
"classifier__n_estimators": randint(50, 300),
"classifier__max_depth": randint(3, 10),
"classifier__learning_rate": uniform(0.01, 0.3),
"classifier__subsample": uniform(0.6, 0.4),
}
search = RandomizedSearchCV(
model,
param_distributions,
n_iter=50,
cv=5,
scoring="roc_auc",
n_jobs=-1,
random_state=42,
)
search.fit(X_train, y_train)
print(f"Best params: {search.best_params_}")
print(f"Best AUC: {search.best_score_:.3f}")
Model Persistence
import joblib
from pathlib import Path
model_path = Path("models/churn_model_v1.joblib")
# Save
joblib.dump(model, model_path)
# Load
loaded_model = joblib.load(model_path)
# Always save with metadata
import json
metadata = {
"model_version": "1.0.0",
"trained_at": datetime.utcnow().isoformat(),
"features": numeric_features + categorical_features,
"target": "churned",
"metrics": {"test_auc": 0.892},
}
(model_path.parent / "metadata.json").write_text(json.dumps(metadata, indent=2))
Anti-Patterns
- Data leakage: fitting preprocessor on full dataset before split
- Not using Pipeline (manual preprocessing on test set introduces bugs)
- Evaluating on training data
- Using accuracy for imbalanced classes (use AUC or F1)
- Missing random_state for reproducibility
- Storing large datasets in git (use DVC or S3)