
- 교차검증
- 하이퍼파라미터

from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint, uniform
#하이퍼 파라미터 범위 지정
dists = {
'simpleimputer__strategy': ['mean', 'median'],
'randomforestclassifier__n_estimators': randint(20, 1000),
'randomforestclassifier__max_depth': [20, 30, 50, 100, None],
'randomforestclassifier__class_weight': ['balanced', None],
'randomforestclassifier__max_features': ['auto', 'sqrt', 'log2', None],
'randomforestclassifier__min_samples_leaf' : randint(1, 15)
}
#RandomizedSearchCV
clf = RandomizedSearchCV(
pipe,
param_distributions=dists,
n_iter=50, #시행 횟수
cv=3, #교차검증 횟수
scoring='f1', #f1
verbose=1,
n_jobs=-1
)
#실행
clf.fit(x_train, y_train);
print('최적 하이퍼파라미터: ', clf.best_params_)
print('F1: ', clf.best_score_)
#최적 하이퍼 파라미터로 파이프 만들기
pipe = clf.best_estimator_
이 파트는 하이퍼파라미터 범위조정 때문에 골치아팠다. 범위를 너무 넓게 하면 너무 오래걸리고 너무 좁게 하면 안좋게 나온다. 잘 조절해보면서 나만의 최적의 범위를 찾아야할듯하다.
힘내자!!