python 30 lines · 1 tab

Hyperparameter tuning with GridSearchCV and randomized search

1 tab
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={
        'n_estimators': [200, 400],
        'max_depth': [None, 10, 20],
        'min_samples_leaf': [1, 3, 5],
    },
    scoring='roc_auc',
    cv=5,
    n_jobs=-1,
    verbose=1,
)

randomized_search = RandomizedSearchCV(
    estimator=RandomForestClassifier(random_state=42, n_jobs=-1),
    param_distributions={
        'n_estimators': [200, 300, 400, 500],
        'max_depth': [None, 8, 12, 16, 24],
        'min_samples_split': [2, 5, 10],
        'min_samples_leaf': [1, 2, 4],
    },
    n_iter=15,
    scoring='roc_auc',
    cv=5,
    n_jobs=-1,
    random_state=42,
)
1 file · python Explain with highlit

Hyperparameter search should be targeted, not theatrical. I usually combine a strong baseline, a compact search space, and a metric aligned with business cost. GridSearchCV is good for interpretable sweeps; randomized search is better when the space gets large and the budget is fixed.


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 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.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report

pipeline = Pipeline([

Text vectorization with TF-IDF for strong classical baselines

tf-idf nlp text-classification
by Dr. Elena Vasquez 1 tab
python
from sklearn.compose import ColumnTransformer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.ensemble import RandomForestClassifier

ColumnTransformer pipelines that keep preprocessing honest

scikit-learn pipelines columntransformer
by Dr. Elena Vasquez 1 tab
python
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

Train test split and stratified cross validation done properly

cross-validation evaluation scikit-learn
by Dr. Elena Vasquez 1 tab

Share this code

Here's the card — post it anywhere.

Hyperparameter tuning with GridSearchCV and randomized search — share card
Link copied