pythonintermediate
Optuna Hyperparameter Optimization
Automate hyperparameter search with Optuna using Bayesian optimization and pruning.
pythonPress ⌘/Ctrl + Shift + C to copy
import optuna
from sklearn.datasets import load_iris
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import cross_val_score
import warnings
warnings.filterwarnings('ignore')
X, y = load_iris(return_X_y=True)
def objective(trial: optuna.Trial) -> float:
params = {
'n_estimators': trial.suggest_int('n_estimators', 50, 500),
'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3, log=True),
'max_depth': trial.suggest_int('max_depth', 2, 8),
'subsample': trial.suggest_float('subsample', 0.5, 1.0),
}
model = GradientBoostingClassifier(**params, random_state=42)
return cross_val_score(model, X, y, cv=5, scoring='accuracy').mean()
study = optuna.create_study(direction='maximize', sampler=optuna.samplers.TPESampler())
study.optimize(objective, n_trials=30, n_jobs=-1)
print(f'Best accuracy: {study.best_value:.4f}')
print(f'Best params: {study.best_params}')Use Cases
- AutoML
- model tuning
- Bayesian optimization
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonadvanced
Weights & Biases Hyperparameter Sweeps
Run automated hyperparameter search with W&B Sweeps using Bayesian optimization.
Best for: hyperparameter tuning
#wandb#hyperparameter
typescriptadvanced
Semantic Caching Layer for LLM Calls
Cache LLM responses by semantic similarity of prompts to reduce API costs and improve latency.
Best for: Reducing LLM API costs for repeated queries
#caching#embeddings
typescriptintermediate
Batch Embeddings Processing
Generate embeddings for large document sets in batches with rate limiting and progress tracking.
Best for: Indexing large document collections for search
#embeddings#batch-processing
typescriptadvanced
AI Model Router for Cost Optimization
Route prompts to different LLM models based on complexity to optimize cost and response quality.
Best for: Reducing AI API costs for production apps
#routing#optimization