python
23 lines · 1 tab
Dr. Elena Vasquez
Apr 2026
1 tab
from sklearn.model_selection import StratifiedKFold, train_test_split, cross_validate
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
stratify=y,
random_state=42,
)
pipeline = Pipeline([
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler()),
('model', LogisticRegression(max_iter=1000)),
])
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_validate(pipeline, X_train, y_train, cv=cv, scoring=['roc_auc', 'f1', 'precision', 'recall'])
print(scores)
1 file · python
Explain with highlit
Evaluation goes wrong when data splitting is treated like boilerplate. I stratify imbalanced targets, guard time order when necessary, and make sure preprocessing lives inside cross-validation. This is the difference between a model that looks good in a notebook and one that behaves predictably in production.
Related snips
python
from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet
from sklearn.metrics import mean_absolute_error, root_mean_squared_error
models = {
'linear': LinearRegression(),
'ridge': Ridge(alpha=1.0),
Regression workflows with linear, ridge, lasso, and elastic net
scikit-learn
regression
ridge
by Dr. Elena Vasquez
1 tab
python
import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
Encoding categorical variables without creating leakage
categorical-encoding
preprocessing
scikit-learn
by Dr. Elena Vasquez
1 tab
python
import numpy as np
from sklearn.metrics import confusion_matrix
probabilities = model.predict_proba(X_valid)[:, 1]
thresholds = np.linspace(0.1, 0.9, 9)
Confusion matrix diagnostics for threshold selection
confusion-matrix
thresholding
evaluation
by Dr. Elena Vasquez
1 tab
python
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
grid_search = GridSearchCV(
estimator=RandomForestClassifier(random_state=42, n_jobs=-1),
param_grid={
Hyperparameter tuning with GridSearchCV and randomized search
hyperparameter-tuning
gridsearch
scikit-learn
by Dr. Elena Vasquez
1 tab
python
import joblib
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title='Churn Prediction API')
Serving scikit-learn models behind a FastAPI prediction API
fastapi
scikit-learn
model-serving
by Dr. Elena Vasquez
1 tab
python
from sklearn.metrics import (
average_precision_score,
classification_report,
precision_recall_curve,
roc_auc_score,
)
Classification metrics beyond accuracy for imbalanced problems
classification-metrics
imbalanced-data
evaluation
by Dr. Elena Vasquez
1 tab
Share this code
Here's the card — post it anywhere.