중첩 된 교차 유효성 검사와 함께 GridSearch를 사용하여 RandomForest를 최적화했습니다. 그 후, 최상의 매개 변수를 사용하면 샘플 밖의 데이터에 대한 예측을하기 전에 전체 데이터 집합을 교육해야한다는 것을 알고 있습니다.중첩 된 교차 유효성 검사에서 얻은 표 검색에서 best_score를 사용하여 수행 할 작업은 무엇입니까?
모델을 두 번 맞추어야합니까? 중첩 된 교차 유효성 검사를 통해 정확도 추정치를 찾은 다음 샘플 외부 데이터를 사용하여 정확도 추정치를 찾는 방법
내 코드를 확인하십시오 :
#Load data
for name in ["AWA"]:
for el in ['Fp1']:
X=sio.loadmat('/home/TrainVal/{}_{}.mat'.format(name, el))['x']
s_y=sio.loadmat('/home/TrainVal/{}_{}.mat'.format(name, el))['y']
y=np.ravel(s_y)
print(name, el, x.shape, y.shape)
print("")
#Pipeline
clf = Pipeline([('rcl', RobustScaler()),
('clf', RandomForestClassifier())])
#Optimization
#Outer loop
sss_outer = StratifiedShuffleSplit(n_splits=2, test_size=0.1, random_state=1)
#Inner loop
sss_inner = StratifiedShuffleSplit(n_splits=2, test_size=0.1, random_state=1)
# Use a full grid over all parameters
param_grid = {'clf__n_estimators': [10, 12, 15],
'clf__max_features': [3, 5, 10],
}
# Run grid search
grid_search = GridSearchCV(clf, param_grid=param_grid, cv=sss_inner, n_jobs=-1)
#FIRST FIT!!!!!
grid_search.fit(X, y)
scores=cross_val_score(grid_search, X, y, cv=sss_outer)
#Show best parameter in inner loop
print(grid_search.best_params_)
#Show Accuracy average of all the outer loops
print(scores.mean())
#SECOND FIT!!!
y_score = grid_search.fit(X, y).score(out-of-sample, y)
print(y_score)
단순히'grid_search.score()'또는'grid_search.predict()'를 호출해도 똑같은 효과가 있습니다. 내부적으로'best_estimator_'에 자동으로 접근 할 것이기 때문입니다. –