2017-01-26 12 views
3

私はPythonを使用していますので、scikitでネストされたクロスバリデーションを使用したいと思います。私は非常に良いexampleを発見した:Scikitにアクセスする方法ネストされたクロスバリデーションスコアを学ぶ

NUM_TRIALS = 30 
non_nested_scores = np.zeros(NUM_TRIALS) 
nested_scores = np.zeros(NUM_TRIALS) 
# Choose cross-validation techniques for the inner and outer loops, 
# independently of the dataset. 
# E.g "LabelKFold", "LeaveOneOut", "LeaveOneLabelOut", etc. 
inner_cv = KFold(n_splits=4, shuffle=True, random_state=i) 
outer_cv = KFold(n_splits=4, shuffle=True, random_state=i) 

# Non_nested parameter search and scoring 
clf = GridSearchCV(estimator=svr, param_grid=p_grid, cv=inner_cv) 
clf.fit(X_iris, y_iris) 
non_nested_scores[i] = clf.best_score_ 

# Nested CV with parameter optimization 
nested_score = cross_val_score(clf, X=X_iris, y=y_iris, cv=outer_cv) 
nested_scores[i] = nested_score.mean() 

どのようにネストされたクロスバリデーションからパラメータの最良のセットだけでなく(それに対応するスコア)パラメータのすべての設定にアクセスすることができますか?

答えて

3

cross_val_scoreから個別のパラメータと最適なパラメータにアクセスすることはできません。どのようなcross_val_scoreを内部で実行するかは、付属の見積もりツールを複製してからfitscoreメソッドを呼び出すことで、個々の見積もりでX,yとなります。

あなたが使用することができ、各分割でのparamsにアクセスする場合:

#put below code inside your NUM_TRIALS for loop 
cv_iter = 0 
temp_nested_scores_train = np.zeros(4) 
temp_nested_scores_test = np.zeros(4) 
for train, test in outer_cv.split(X_iris): 
    clf.fit(X_iris[train], y_iris[train]) 
    temp_nested_scores_train[cv_iter] = clf.best_score_ 
    temp_nested_scores_test[cv_iter] = clf.score(X_iris[test], y_iris[test]) 
    #You can access grid search's params here 
nested_scores_train[i] = temp_nested_scores_train.mean() 
nested_scores_test[i] = temp_nested_scores_test.mean() 
関連する問題