RMSEを最適化する場合のコード例

# optunaをインストール
!pip install optuna

# ライブラリをインポート
import optuna
import numpy as np
from sklearn.model_selection import train_test_split
from catboost import CatBoostRegressor
from sklearn.metrics import mean_squared_error, mean_squared_log_error

# X(説明変数)とy(目的変数)は事前に準備しとく
train_X, val_X, train_y, val_y = train_test_split(X, y, test_size=0.2, random_state=42)

def objective(trial):
    params = {
        "iterations": trial.suggest_int("iterations", 50, 500),
        "depth": trial.suggest_int("depth", 2, 10),
        "learning_rate": trial.suggest_float("learning_rate", 0.01, 0.3, log=True),
        "loss_function": "RMSE",
        "random_seed": 42,  # 乱数のシードを固定
        "verbose": 0  # トレーニング中の出力メッセージを非表示
    }

    model = CatBoostRegressor(**params)
    model.fit(train_X, train_y, eval_set=(val_X, val_y))
    y_pred = model.predict(val_X)

    rmse = np.sqrt(mean_squared_error(val_y, y_pred)) # RMSE
    return rmse


study = optuna.create_study(direction="minimize")
study.optimize(objective, n_trials=100)  # n_trials を増やすとより精度の高いチューニングが可能になります

print("Number of finished trials:", len(study.trials))
print("Best trial:", study.best_trial.params)
print("Best value:", study.best_value)

▼出力例

~
Number of finished trials: 100
Best trial: {'iterations': 229, 'depth': 3, 'learning_rate': 0.13115993620497746}
Best value: 301371.1093541424

おすすめの記事