from sklearn.model_selection import RandomizedSearchCV from scipy.stats import uniform, randint # チューニングするハイパーパラメータの範囲を定義 param_dist = { ‘n_estimators’: randint(100, 1000), ‘max_depth’: randint(3, 10), ‘learning_rate’: uniform(0.01, 0.3), ‘subsample’: uniform(0.6, 0.4), ‘colsample_bytree’: uniform(0.6, 0.4), } # ランダムサーチを行う random_search = RandomizedSearchCV(estimator=xgb_model, param_distributions=param_dist, n_iter=100, cv=3, scoring=’accuracy’, verbose=2, random_state=42, n_jobs=-1) random_search.fit(X_train, y_train_encoded) # 最適なモデルを取得 best_model = random_search.best_estimator_ # 最適なモデルで予測を行う y_pred_encoded = best_model.predict(X_test) # モデルの評価 accuracy = accuracy_score(y_test_encoded, y_pred_encoded) precision = precision_score(y_test_encoded, y_pred_encoded, average=’weighted’) recall = recall_score(y_test_encoded, y_pred_encoded, average=’weighted’) f1 = f1_score(y_test_encoded, y_pred_encoded, average=’weighted’) roc_auc = roc_auc_score(y_test_encoded, best_model.predict_proba(X_test), multi_class=’ovr’) print(“Best Parameters:”, random_search.best_params_) print(“Accuracy:”, accuracy) print(“Precision:”, precision) print(“Recall:”, recall) print(“F1 Score:”, f1) print(“ROC AUC Score:”, roc_auc) print(accuracy,precision,recall,f1,roc_auc) いつ終わるのこれ?何組?

このランダムサーチの処理は、n_iter=100で指定された数のイテレーションを行います。各イテレーションでは、指定されたハイパーパラメータの範囲内でランダムな値を選択してモデルをトレーニングし、交差検証を行います。そして、最適なハイパーパラメータを持つモデルを見つけるために、全体として最大で100回のトレーニングが行われます。

しかし、具体的な所要時間は、ハイパーパラメータの探索空間、データのサイズ、コンピュータのスペックなどに依存します。データやモデルの複雑さが高い場合は、処理が長くかかる可能性があります。

このランダムサーチの進捗状況を見るには、verbose=2と指定されているので、各イテレーションごとの詳細な出力が表示されるはずです。もしくは、実行中のプロセスを監視するためにモニタリングツールを使用することもできます。

未分類

Posted by ぼっち