python 21 lines · 1 tab

Bayesian optimization with Optuna for efficient model tuning

1 tab
import optuna
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.model_selection import cross_val_score

def objective(trial):
    model = HistGradientBoostingClassifier(
        learning_rate=trial.suggest_float('learning_rate', 0.01, 0.2, log=True),
        max_depth=trial.suggest_int('max_depth', 3, 12),
        max_leaf_nodes=trial.suggest_int('max_leaf_nodes', 15, 63),
        min_samples_leaf=trial.suggest_int('min_samples_leaf', 10, 100),
        l2_regularization=trial.suggest_float('l2_regularization', 1e-6, 1.0, log=True),
        random_state=42,
    )
    scores = cross_val_score(model, X_train, y_train, cv=5, scoring='roc_auc', n_jobs=-1)
    return scores.mean()

study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=50)

print(study.best_trial.params)
print(study.best_value)
1 file · python Explain with highlit

When the search space is wide, Optuna gives me better signal per compute dollar than brute-force sweeps. It is easy to define conditional search spaces, prune bad trials early, and track the best trial artifacts. I especially like it for gradient boosting and neural network tuning.


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
ruby
# BAD: N+1 query problem
@users = User.all
@users.each do |user|
  puts user.posts.count  # Fires query for each user!
end

ActiveRecord query optimization and N+1 prevention

ruby rails activerecord
by Sarah Mitchell 3 tabs
sql
-- EXPLAIN ANALYZE (actual execution statistics)
EXPLAIN ANALYZE
SELECT u.username, COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.created_at >= '2024-01-01'

Advanced query optimization techniques

database optimization query-performance
by Maria Garcia 2 tabs
kotlin
package com.example.myapp.utils

import android.os.Build
import android.os.StrictMode
import android.os.Trace
import timber.log.Timber

Performance optimization and profiling

kotlin android performance
by Alex Chen 2 tabs
javascript
const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true',
  openAnalyzer: true,
});

/** @type {import('next').NextConfig} */

Next.js bundle analyzer for targeted performance work

nextjs performance tooling
by codesnips 4 tabs
rust
use std::borrow::Cow;

fn ensure_prefix(input: &str) -> Cow<str> {
    if input.starts_with("https://") {
        Cow::Borrowed(input)
    } else {

Cow for clone-on-write to avoid unnecessary allocations

rust optimization strings
by Marcus Chen 1 tab

Share this code

Here's the card — post it anywhere.

Bayesian optimization with Optuna for efficient model tuning — share card
Link copied