Skip to content
v0.1.3latest release121 skills4 categories6 sub-categories247 tested scripts397 reference docsContact us

optuna-study-writer

Write correct, resumable Optuna hyperparameter studies for scikit-learn, LightGBM, XGBoost and CatBoost: an objective with a search space sized to the data, fixed CV folds built once (stratified, grouped or time-ordered), early stopping inside each fold, trial.report and should_prune for MedianPruner / HyperbandPruner, seeded multivariate TPESampler, SQLite storage with study_name and load_if_exists for resuming, stale-study stopping callbacks, user attributes (best iteration, fold std), trials export, parameter importances, conditional spaces, multi-objective studies, and refitting the best configuration with a lower learning rate. Generates verified study scripts for six model families and lints Optuna code: unprunable objectives, inconsistent parameters, per-trial folds, wrong direction, constant-returning handlers, missing seeds or storage, deprecated APIs. Use when someone writes or reviews an Optuna study, wants Bayesian hyperparameter optimisation, pruning, or a resumable tuning job.

AI & Machine Learning / Classical Machine Learningv1.0.0Updated OCT 25 20262 references2 scripts

Optuna makes Bayesian search easy to write and easy to get subtly wrong: folds that change between trials, pruners that never fire because nothing is reported, a parameter whose range silently changes, an exception handler that feeds the sampler fake scores, or a two-hour study that cannot resume after a crash. scripts/gen_optuna_study.py writes a complete study script (verified end to end for LightGBM, XGBoost, CatBoost, HistGradientBoosting, random forest and logistic regression, grouped and time-ordered folds, both pruners, and resuming from storage); scripts/optuna_lint.py reviews existing Optuna code for eleven classes of mistakes.

Trigger Conditions

Activate this skill when the user:

  • Wants to tune a model with Optuna, or asks for Bayesian / TPE hyperparameter optimisation
  • Needs pruning, resumable studies, parallel workers, or multi-objective (accuracy vs latency) search
  • Shares an Optuna objective for review, or sees pruning never happen / results that do not reproduce
  • Wants the final model refit from the best trial

Strategy and budget questions: hyperparameter-search-planner; GBM parameter meaning: xgboost-lightgbm-tuner; fold design: cross-validation-strategy-designer.

Workflow

text
1. FOLDS      -> decide the splitter (stratified / grouped / time); build folds ONCE
2. GENERATE   -> python scripts/gen_optuna_study.py data.csv --target y --model lgbm --metric average_precision -o study.py
3. REVIEW     -> python scripts/optuna_lint.py study.py      (also for hand-written studies)
4. SMOKE      -> python study.py --trials 5        (seconds-scale check that everything runs)
5. RUN        -> python study.py --trials 200 --timeout 14400   (rerun the same command to resume)
6. ANALYSE    -> study_trials.csv -> hyperparameter-search-planner/scripts/analyze_search.py; importances
7. REFIT      -> best params, lower learning rate, scaled iterations; confirm on a holdout

Anatomy of a Correct Study

python
import numpy as np, optuna
FOLDS = list(StratifiedKFold(5, shuffle=True, random_state=0).split(X, y))      # fixed once

def objective(trial):
    params = {"num_leaves": trial.suggest_int("num_leaves", 7, 127, log=True),
              "min_child_samples": trial.suggest_int("min_child_samples", 5, 300, log=True),
              "reg_lambda": trial.suggest_float("reg_lambda", 1e-3, 30.0, log=True)}
    scores, iters = [], []
    for k, (tr, te) in enumerate(FOLDS):
        m = lgb.LGBMClassifier(**params, learning_rate=0.05, n_estimators=5000, verbose=-1, random_state=0)
        m.fit(X.iloc[tr], y.iloc[tr], eval_set=[(X.iloc[te], y.iloc[te])],
              callbacks=[lgb.early_stopping(100, verbose=False)])
        scores.append(average_precision_score(y.iloc[te], m.predict_proba(X.iloc[te])[:, 1]))
        iters.append(m.best_iteration_)
        trial.report(float(np.mean(scores)), step=k)        # intermediate value per fold
        if trial.should_prune():
            raise optuna.TrialPruned()
    trial.set_user_attr("best_iteration_mean", float(np.mean(iters)))
    return float(np.mean(scores))

study = optuna.create_study(study_name="lgbm_churn", storage="sqlite:///lgbm_churn.db", load_if_exists=True,
                            direction="maximize",
                            sampler=optuna.samplers.TPESampler(seed=0, multivariate=True, n_startup_trials=10),
                            pruner=optuna.pruners.MedianPruner(n_startup_trials=10, n_warmup_steps=1))
study.optimize(objective, n_trials=200, timeout=4 * 3600, gc_after_trial=True)
ElementWhy
Folds built once outside the objectivetrials are compared on identical splits; pruning compares like with like
trial.report + should_prune + raise TrialPrunedthe pruner needs all three
Seeded TPESampler(multivariate=True)reproducible; models parameter interactions
storage + study_name + load_if_exists=Truecrash-safe and resumable; several workers can share it
Early stopping inside folds; learning rate fixednumber of trees is not a search dimension
set_user_attrkeep best iteration and fold std for the refit and the report
gc_after_trial=Truefrees memory between heavy trials

Every API detail (samplers, pruners, distributions, callbacks, ask-and-tell, multi-objective): optuna-api-guide.md.

Mistakes the Linter Catches

CodeMistakeConsequence
OP001pruner configured, objective never reportsno pruning: full cost for every trial
OP002report without should_prune, or should_prune without raising TrialPrunedsame
OP003one name suggested with different types/rangesOptuna raises ValueError (verified on 5.0)
OP004dynamic names in loops (f"units_{i}")search space changes shape between trials
OP005shuffled splitter without seed inside the objectiveeach trial sees different folds
OP006direction does not match the metric (maximize a loss)the study optimises the wrong way
OP007except Exception: return 0the sampler learns from fake values
OP008sampler without seednot reproducible
OP009no storage / study_namecannot resume after a crash
OP010suggest_uniform/suggest_loguniform/suggest_discrete_uniform, optuna.integration.* callbacksdeprecated / moved to optuna-integration
OP011objective reads study.best_*trials depend on each other

Conditional and Multi-Objective Spaces

python
boot = trial.suggest_categorical("bootstrap_type", ["Bayesian", "Bernoulli"])
if boot == "Bayesian":
    params["bagging_temperature"] = trial.suggest_float("bagging_temperature", 0.0, 1.0)
else:
    params["subsample"] = trial.suggest_float("subsample", 0.6, 1.0)

study = optuna.create_study(directions=["maximize", "minimize"])          # PR-AUC vs latency
pareto = study.best_trials

Patterns for model selection inside one study, constrained optimisation and warm-starting with known good configurations (study.enqueue_trial): study-patterns.md.

Refit and Report

  • Boosting: refit on all training data with the best parameters, half the learning rate and about 2 x 1.1 x the mean best iteration (the generated script does this), or early-stop on a final validation split.
  • The best value is the maximum over trials (optimistic): confirm on a holdout or with nested CV.
  • Report trials completed/pruned, best value ± fold std, importances, the budget, and the holdout result.

Output Format

When writing or reviewing a study, deliver:

  1. Study design - model, metric and direction, folds, sampler, pruner, storage, budget.
  2. Code - the generated or corrected script.
  3. Lint result - optuna_lint.py findings fixed.
  4. Run summary - trials complete/pruned, best value ± std, best params, importances.
  5. Refit and validation - final model settings and holdout comparison against the untuned baseline.

Scripts

bash
python scripts/gen_optuna_study.py data.csv --target churned --model lgbm --metric average_precision -o study.py
python scripts/gen_optuna_study.py data.csv --target price --model xgb --metric neg_root_mean_squared_error --trials 100
python scripts/gen_optuna_study.py data.csv --target churned --model catboost --group-col customer_id --pruner hyperband
python scripts/gen_optuna_study.py data.csv --target churned --model hgb --time-col signup_date --patience 20

Models: lgbm, xgb, catboost, hgb, rf, logreg. Metrics: roc_auc, average_precision, neg_log_loss, accuracy, neg_mean_absolute_error, neg_root_mean_squared_error, r2 (direction set automatically). The script builds fixed StratifiedKFold/KFold, StratifiedGroupKFold/GroupKFold (--group-col) or TimeSeriesSplit (--time-col) folds, data-sized ranges, early stopping per fold, per-fold reporting, Median/Hyperband/no pruner, SQLite storage with resume, a stale-study callback (--patience), trials CSV, importances and a joblib refit. Exit codes: 0 written, 2 usage error.

bash
python scripts/optuna_lint.py study.py
python scripts/optuna_lint.py src/*.py --json

Rules OP001-OP011 above. Exit codes: 0 no high findings, 1 high findings, 2 read or syntax error.

Additional Resources

FileContent
optuna-api-guide.mdcreate_study, samplers (TPE, GP, CMA-ES, random, grid, NSGA-II), pruners, suggest_* API, storage and parallel workers, callbacks, ask-and-tell, visualisation
study-patterns.mdCV objectives, boosting with early stopping, conditional spaces, model selection, multi-objective, constraints, enqueue_trial, timeouts, refitting
More in Classical Machine Learning

anomaly-detection-model-builder

Build and evaluate anomaly detection for tabular, sensor and transaction data with few or no labels: define the anomaly type (point, contextual, collective), choose detectors (robust z-score/MAD and IQR rules, Mahalanobis with robust covariance, k-NN distance, LOF, Isolation Forest, one-class SVM, PCA reconstruction / autoencoders, forecasting residuals for time series), combine them by rank, explain each alert by its deviating features, set thresholds from an alert budget or the score distribution, and evaluate with precision@k, PR-AUC and recall on known incidents, or detector agreement when no labels exist. Includes pure-Python detectors (robust z, Mahalanobis, kNN, Isolation Forest, ensemble) and an evaluation script.

catboost-model-builder

Build CatBoost models that use its strengths correctly: raw categorical columns as cat_features (ordered target statistics, no manual encoding), text_features, Pool construction with NaN-safe categories, ordered boosting and has_time for temporal data, symmetric (oblivious) trees, overfitting detector with eval_set and use_best_model, losses for classification, regression (Quantile, Poisson, Tweedie) and ranking (YetiRank), class weights, monotone constraints, feature importance (PredictionValuesChange, LossFunctionChange) and SHAP values, GPU training and model export. Plans cat/text/numeric columns from a CSV and generates a runnable training script, and checks parameter sets for the conflicts CatBoost rejects (iterations vs n_estimators, Bayesian bootstrap with subsample, multiple class-weight options, depth > 16, use_best_model without eval_set).

categorical-encoding-selector

Choose and implement the right encoding for every categorical column given the model family and the data: one-hot (with infrequent-level grouping and handle_unknown), ordinal with an explicit order, cross-fitted target encoding (scikit-learn TargetEncoder, CatBoost ordered statistics, category_encoders inside CV), frequency/count encoding, feature hashing, native categorical support in LightGBM, HistGradientBoosting, XGBoost and CatBoost, prefix levels for codes (zip3, NAICS-2), and embeddings. Measures cardinality, rare-level share, unseen-level rate on a time-ordered holdout and target signal per column, emits the ColumnTransformer, and quantifies target-encoding leakage (in-sample vs out-of-fold vs holdout AUC).

classification-model-selector

Choose the right classification algorithm for tabular, text or mixed data and prove the choice with a fair bake-off: logistic regression (L2, L1, elastic net), linear SVM, naive Bayes, k-NN, RBF SVM, decision trees, random forest, extra trees, HistGradientBoosting, LightGBM, XGBoost, CatBoost, explainable boosting machines (EBM/GAM), points scorecards and MLPs, matched to data size, width, categorical and high-cardinality share, missingness, class imbalance, text, and constraints such as interpretability, monotonic constraints, calibrated probabilities, latency budgets and tiny deployable models. Ranks candidates with starting hyperparameters and cautions, then generates a bake-off script running every candidate on identical CV folds with per-fold metrics, fit time and prediction latency.

clustering-algorithm-selector

Choose the right clustering algorithm and check that clustering makes sense at all: cluster tendency (Hopkins statistic with a correlated-features caveat, nearest-neighbour isolation), then k-means / MiniBatchKMeans, Gaussian mixtures with BIC, agglomerative (Ward, average, complete, single) with dendrograms, DBSCAN, HDBSCAN, OPTICS, spectral clustering, BIRCH, mean shift, k-prototypes and k-modes for mixed or categorical data, and Gower distance, matched to data size, expected shapes, noise, varying density, known k, soft memberships, hierarchy and streaming. Covers preprocessing (scaling, encoding, dimensionality, distance metrics), validation (internal indices, bootstrap stability, external labels, business profiling) and assigning new points.

collaborative-filtering-implementer

Implement collaborative filtering recommenders that beat popularity: item-based kNN with cosine, Jaccard or asymmetric similarity, shrinkage, neighbourhood size, positive-only or mean-centred rating signals and 'because you liked X' explanations; implicit ALS with confidence weighting, BPR pairwise ranking and the closed-form EASE model in plain NumPy, tuned on a temporal validation split by recall@k and NDCG@k and refit on all data; plus user-kNN, explicit matrix factorisation, the implicit/LightFM libraries, hyperparameter ranges, cold-start fallbacks, scaling with sparse matrices and approximate nearest neighbours. Produces top-k lists in the format of the recommendation evaluator.