ランダムフォレストで回帰学習をかける際、optunaでハイパーパラメータを最適化しようと以下のように実行していますが、実行するたびに毎回違う値が出てしまいます。
randomstate、randomseedも固定しているのになぜだかわかりません。
python
1import pandas as pd 2import numpy as np 3import matplotlib.pyplot as plt 4%matplotlib inline 5import warnings 6warnings.filterwarnings('ignore') 7from sklearn.ensemble import RandomForestRegressor as RFR 8 9-----データ読み込み 10df = pd.read_csv('20200519Dataset.csv') 11 12x = df.iloc[:, :17] # 説明変数 13y= df.iloc[:, 18] # 目的変数 14 15-----全データでモデル構築(ランダムフォレスト回帰) 16 17-----データのオートスケーリング 18autoscaled_x_all = (x - x.mean()) / x.std() 19autoscaled_y_all = (y - y.mean()) / y.std() 20 21-----optunaによるハイパーパラメータ最適化 22 23import optuna 24 25def objective(trial): 26-----hyper param 27 max_depth = trial.suggest_int('max_depth', 3, 10) 28 n_estimators = trial.suggest_int('n_estimators', 20, 100) 29 random_state=[0] 30 31 modelbest = RFR(max_depth=max_depth,n_estimators=n_estimators,max_features='sqrt',n_jobs=-1,verbose=True) 32 33 34 modelbest.fit(autoscaled_x_all, autoscaled_y_all) 35 36 37 score = -1 * modelbest.score(autoscaled_x_all, autoscaled_y_all) 38 39 40study = optuna.create_study(sampler=optuna.samplers.RandomSampler(seed=0)) 41 42study.optimize(func=objective, # 実行する関数 43 n_trials=30, # 試行回数 44 timeout=None, # 与えられた秒数後に学習を中止。 45 n_jobs=-1 # 並列実行するjob数 46 ) 47 48#最適化したハイパーパラメータの確認 実行する旅毎回違う結果が出る 49print('best_param:{}'.format(study.best_params)) 50

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/01 06:16
2020/07/01 07:55