ランダムフォレストのパラメーターチューニング方法
諸々のインポート
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.svm import SVC
ランダムフォレスト
rfc=RandomForestClassifier(random_state=42)
param_grid = {
'n_estimators': [200, 500],
'max_features': ['auto', 'sqrt', 'log2'],
'max_depth' : [4,5,6,7,8],
'criterion' :['gini', 'entropy']
}
チューニング(めちゃくちゃ時間かかる)
CV_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5) #cvは分割数(2-5で指定)
CV_rfc.fit(X_train, y_train)
最適パラメーターの表示
CV_rfc.best_params_
この最適パラメーターで出た値を設定してランダムフォレスト
rfc1=RandomForestClassifier(random_state=42, max_features='auto', n_estimators= 200, max_depth=8, criterion='gini')
学習
rfc1.fit(X_train, y_train)
提出データ作成
test_pred=rfc1.predict(X_test)
#サブミット用ファイルの出力
sub_df = pd.DataFrame({"datetime":test["datetime"].values,"count":test_pred})
sub_df.to_csv("output/submission.csv", index = False)