1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Gridsearchサンプルコード

Posted at

引用
https://watlab-blog.com/2020/02/20/grid-search-decisiontree/#GridSearchCV-2

from sklearn import datasets
from sklearn.model_selection import GridSearchCV
from sklearn import tree
from sklearn.ensemble import RandomForestRegressor
import pandas as pd

# データを用意する
X = df['GDP'].values.reshape(148,1)
y = df['乳児死亡率']

test = pd.read_csv("./data/infant_mortality_test.csv")
testX = test[["GDP"]]
testy = test['乳児死亡率']

# グリッドサーチ用のパラメータを辞書型で設定
param = {
    'n_estimators':[1,10,100],
    'max_leaf_nodes':[20,25,30],
    'max_depth':[1, 2, 3, 4, 5],
    'min_samples_leaf':[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'min_samples_split':[2, 3, 4, 5]}

# GridSearchCV
'''学習器、パラメータリスト,cv'''
RFR = GridSearchCV(RandomForestRegressor(),   # グリッドサーチで決定木を定義
                   param, cv=5)
RFR.fit(X, y)                           # フィッティング

best_rfr.fit(X,y)
best_rfr.score(testX, testy)

# スコアとパラメータの組み合わせ
scores = RFR.cv_results_['mean_test_score']
params = RFR.cv_results_['params']

# 結果の確認
best_rfr = RFR.best_estimator_
print('最良条件:\n', best_rfr)
print('訓練スコア:\n', best_rfr.score(X, y))
print('テストスコア:\n', best_rfr.score(testX, testy))
for i in range(len(scores)):
    print(scores[i], params[i])
# データフレームに変換してサマリをcsvに保存
df_summary = pd.DataFrame(params)                       # 辞書型のパラメータをデータフレームに変換
df_summary['Score'] = pd.Series(scores)                 # スコアをデータフレームに追加
print(df_summary)
df_summary.to_csv('summary.csv', encoding='shift_jis')  # csvファイルに結果一覧を保存
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?