python 21 lines · 1 tab

Baseline classifiers in scikit-learn for fast benchmark setting

1 tab
from sklearn.ensemble import RandomForestClassifier, HistGradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer

models = {
    'log_reg': LogisticRegression(max_iter=1000, class_weight='balanced'),
    'random_forest': RandomForestClassifier(n_estimators=400, max_depth=None, random_state=42, n_jobs=-1),
    'hist_gbm': HistGradientBoostingClassifier(max_depth=6, learning_rate=0.05, random_state=42),
}

for name, model in models.items():
    pipeline = Pipeline([
        ('imputer', SimpleImputer(strategy='median')),
        ('model', model),
    ])
    pipeline.fit(X_train, y_train)
    predictions = pipeline.predict_proba(X_valid)[:, 1]
    auc = roc_auc_score(y_valid, predictions)
    print(name, round(auc, 4))
1 file · python Explain with highlit

I like setting a few strong baselines before chasing complexity. A regularized logistic regression, a random forest, and a gradient boosting model usually tell me whether the problem is linearly separable, non-linear, or data-limited. Good baseline discipline saves weeks of unnecessary model experimentation.


Related snips

python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
from sklearn.linear_model import LogisticRegression

standard_pipeline = Pipeline([
    ('scaler', StandardScaler()),

Scaling and normalization choices for different model families

feature-scaling normalization machine-learning
by Dr. Elena Vasquez 1 tab
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 pandas as pd

df = pd.read_parquet('churn_training.parquet')

print('shape:', df.shape)
print('target balance:', df['churned'].value_counts(normalize=True).round(3))

Exploratory data analysis checklist for tabular ML projects

eda machine-learning tabular-data
by Dr. Elena Vasquez 1 tab

Share this code

Here's the card — post it anywhere.

Baseline classifiers in scikit-learn for fast benchmark setting — share card
Link copied