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.

Share this code

Here's the card — post it anywhere.

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