๋ชฉํ
- Model Selection(๋ชจ๋ธ ์ ํ)์ ์ํ Cross Validation(๊ต์ฐจ๊ฒ์ฆ) ๋ฐฉ๋ฒ ์ดํด ๋ฐ ํ์ฉ
- Hyperparameter๋ฅผ ์ต์ ํํ์ฌ ๋ชจ๋ธ์ ์ฑ๋ฅ ํฅ์
Cross-Validation(๊ต์ฐจ๊ฒ์ฆ)
- Hold-Out ๊ต์ฐจ๊ฒ์ฆ : train/validate/test set์ผ๋ก ๋๋ ํ์ต์ ์งํ
- train set์ ํฌ๊ธฐ๊ฐ ์์ ๋๋ val set์ ๋ฐ๋ก ๋ถ๋ฆฌํ๋ ๊ฒ์ด ๋ถ๋ด์ด ๋ ์ ์์ต๋๋ค.
- ์ต์ง๋ก val set์ ๋ฐ๋ก ์ถ์ถํด๋ ์์ธก ์ฑ๋ฅ์ ๋ํ ์ถ์ ์ด ๋ถ์ ํํ ํ๋ฅ ์ด ๋์ต๋๋ค.
- K-fold ๊ต์ฐจ๊ฒ์ฆ : ๋ฐ์ดํฐ๋ฅผ k๊ฐ๋ก ๋ฑ๋ถํ๊ณ k๊ฐ์ ์งํฉ์์ k-1๊ฐ๋ train set, 1๊ฐ๋ val set์ผ๋ก ์ฌ์ฉํ์ฌ k๋ฒ ํ์ตํ๋ ๋ฐฉ๋ฒ
- ์์ Hold-Out ๋ฐฉ๋ฒ์ ๋จ์ ์ ๊ทน๋ณตํ ์ ์์ต๋๋ค.
- ์ด๋ค ํ์ต ๋ชจ๋ธ์ ์ฌ์ฉํด์ผํ ์ง, ์ด๋ค ํ์ดํผํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํด์ผํ ์ง ๊ฒฐ์ ํ๋๋ฐ ๋์์ ์ค๋๋ค.
์ฃผ์์ฌํญ : ์๊ณ์ด๋ฐ์ดํฐ(time series)์๋ ๊ต์ฐจ๊ฒ์ฆ์ด ์ ํฉํ์ง ์์ต๋๋ค.
๊ต์ฐจ๊ฒ์ฆ์ ํตํ score ํ์ธ(from sklearn.model_selection import cross_val_score) ์์
from sklearn.model_selection import cross_val_score
# 3-fold ๊ต์ฐจ๊ฒ์ฆ์ ์ํํฉ๋๋ค.
k = 3
scores = cross_val_score(pipe, X_train, y_train, cv=k,
scoring='f1')
Hyperparameter tuning
- ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ์ ๋ง๋ค ๋ ์ค์ํ ์ด์๋ Optimization(์ต์ ํ)์ Generalization(์ผ๋ฐํ)
- Optimization(์ต์ ํ) : ํ๋ จ ๋ฐ์ดํฐ๋ก ๋ ์ข์ ์ฑ๋ฅ์ ์ป๊ธฐ ์ํด ๋ชจ๋ธ์ ์กฐ์
- Generalization(์ผ๋ฐํ) : ํ์ต๋ ๋ชจ๋ธ์ด ์ฒ์ ๋ณธ ๋ฐ์ดํฐ์์ ์ผ๋ง๋ ์ข์ ์ฑ๋ฅ์ ๋ด๋์ง
- ๊ณผ์์ ํฉ(underfitting) : ๋ชจ๋ธ์ ๋ณต์ก๋๋ฅผ ๋์ด๋ ๊ณผ์ ์์ train, val set์ ์์ค์ด ํจ๊ป ๊ฐ์ํ๋ ์์ (๋ชจ๋ธ์ด ๋ ํ์ต ๊ฐ๋ฅ)
- ๊ณผ์ ํฉ(overfitting) : train set์ ์์ค์ ๊ณ์ ๊ฐ์ํ๋๋ฐ val set์ ์์ค์ ์ฆ๊ฐํ๋ ์์
- ์ด์์ ์ธ ๋ชจ๋ธ์ ๊ณผ์์ ํฉ๊ณผ ๊ณผ์ ํฉ ์ฌ์ด์ ์กด์ฌ
๊ฒ์ฆ๊ณก์ ๊ทธ๋ฆฌ๊ธฐ
- ๊ฒ์ฆ๊ณก์ : train/val set์ ๋ํด y์ถ : score(์์์์ MAE), x์ถ : ํ์ดํผํ๋ผ๋ฏธํฐ(์์์์ max_depth)๋ก ๊ทธ๋ฆฐ ๊ทธ๋ํํ๋ จ๊ณก์ (learning curve)์ ๊ฒฝ์ฐ๋ x์ถ : ํ๋ จ๋ฐ์ดํฐ ์(# of traing samples)์ ๋ํ ๊ฒ์ ๋๋ค. ํผ๋ ์ฃผ์!
์์(from sklearn.model_selection import validation_curve) , max_depth์ ๋ํด์๋ง ๋ณด๊ธฐ๋ก ํ๊ฒ ์ต๋๋ค.
- ์ค์ ๋ก๋ ํ๋์ ํ์ดํผํ๋ผ๋ฏธํฐ๋ง ๊ฐ์ง๊ณ ๊ฒ์ฆ๊ณก์ ์ ๊ทธ๋ฆฌ๋ ๊ฒ์ด ์์ฃผ ์ ์ฉํ์ง ์์ต๋๋ค.
import matplotlib.pyplot as plt
from category_encoders import OrdinalEncoder
from sklearn.model_selection import validation_curve
from sklearn.tree import DecisionTreeRegressor
pipe = make_pipeline(
OrdinalEncoder(),
SimpleImputer(),
DecisionTreeRegressor()
)
depth = range(1, 30, 2)
ts, vs = validation_curve(
pipe, X_train, y_train
, param_name='decisiontreeregressor__max_depth'
, param_range=depth, scoring='neg_mean_absolute_error'
, cv=3
, n_jobs=-1
)
train_scores_mean = np.mean(-ts, axis=1)
validation_scores_mean = np.mean(-vs, axis=1)
fig, ax = plt.subplots()
# ํ๋ จ์ธํธ ๊ฒ์ฆ๊ณก์
ax.plot(depth, train_scores_mean, label='training error')
# ๊ฒ์ฆ์ธํธ ๊ฒ์ฆ๊ณก์
ax.plot(depth, validation_scores_mean, label='validation error')
# ์ด์์ ์ธ max_depth
ax.vlines(5,0, train_scores_mean.max(), color='blue')
# ๊ทธ๋ํ ์
ํ
ax.set(title='Validation Curve'
, xlabel='Model Complexity(max_depth)', ylabel='MAE')
ax.legend()
fig.dpi = 100
- ๋๋ต์ ์ผ๋ก max_depth=5 ์ ๋์ผ ๋๊ฐ ๊ณผ์ ํฉ์ ๋ง์ผ๋ฉด์ ๋์์ ์ผ๋ฐํ ์ฑ๋ฅ๋ ์ ์งํ ์ ์๊ฒ ์ต๋๋ค.
Randomized Search CV
- ํ์ดํผํ๋ผ๋ฏธํฐ์ ๋ํด ์ผ์ ๋ฒ์ฃผ๋ฅผ ์ค์ ํ๊ณ ๋ฌด์์๋ก ์ถ์ถํ์ฌ ๋์จ ์กฐํฉ์ผ๋ก ๋ชจ๋ธ์ ํ์ต์์ผ ์ต์ ์ ๋ชจ๋ธ์ ์ฐพ๋ ๋ฐฉ๋ฒ
- ํ์ดํผํ๋ผ๋ฏธํฐ : ๋ชจ๋ธ ํ๋ จ ์ค์ ํ์ต์ด ๋์ง ์๋ ํ๋ผ๋ฏธํฐ ์ฆ, ์ฌ๋์ด ์ง์ ์กฐ์ ์ ํด์ฃผ์ด์ผํ๋ ํ๋ผ๋ฏธํฐ
- ์ด ๋ ํ์ดํผํ๋ผ๋ฏธํฐ ์กฐํฉ์ ์ฐพ์์ฃผ๋ ์ข์ ํด์ด ์์ต๋๋ค.
-GridSearchCV : ๊ฒ์ฆํ๊ณ ์ถ์ ํ์ดํผํ๋ผ๋ฏธํฐ๋ค์ ์์น๋ฅผ ์ ํด์ฃผ๊ณ ๊ทธ ์กฐํฉ์ ๋ชจ๋ ๊ฒ์ฆ
-RandomizedSearchCV : ๊ฒ์ฆํ๊ณ ์ถ์ ํ์ดํผํ๋ผ๋ฏธํฐ๋ค์ ๊ฐ ๋ฒ์๋ฅผ ์ง์ ํด์ฃผ๊ณ ๋ฌด์์๋ก ๊ทธ ๊ฐ์์ ์ถ์ถํด ๊ทธ ์กฐํฉ์ ๊ฒ์ฆ
๋๋คํฌ๋ ์คํธ ์ ์ฉ ์์
from scipy.stats import randint, uniform
pipe = make_pipeline(
TargetEncoder(),
SimpleImputer(),
RandomForestRegressor(random_state=2)
)
dists = {
'targetencoder__smoothing': [2.,20.,50.,60.,100.,500.,1000.], # int๋ก ๋ฃ์ผ๋ฉด error(bug)
'targetencoder__min_samples_leaf': randint(1, 10),
'simpleimputer__strategy': ['mean', 'median'],
'randomforestregressor__n_estimators': randint(50, 500),
'randomforestregressor__max_depth': [5, 10, 15, 20, None],
'randomforestregressor__max_features': uniform(0, 1) # max_features
}
clf = RandomizedSearchCV(
pipe,
param_distributions=dists,
n_iter=50, # ์์ ํ์ดํผํ๋ผ๋ฏธํฐ ์กฐ๊ฑด์์ ์กฐํฉ์ 50๊ฐ๋ฅผ ์ถ์ถํด์ ํ์ตํฉ๋๋ค.
cv=3, # 50 x 3 = 150 ์ ํ์ต ๊ณผ์ ์ ์คํํ๊ฒ ๋ฉ๋๋ค.
scoring='neg_mean_absolute_error', # MAE์ '-'๊ฐ ๋ถ์ ๊ฐ์ ์ฐ๋ ์ด์ ๋, ๋ค๋ฅธ score์๋ ๋ค๋ฅด๊ฒ MAE, MSE, RMSE ๋ฑ์ ์ซ์๊ฐ ๋ฎ์์๋ก ์ข์ ๊ฒ์ด๋ค. ๊ทธ๋ฌ๋ฏ๋ก -๋ฅผ ๋ถ์ฌ์ค์ ์ด ๊ธฐ์ค์ ๋ง์ถฐ์ค ๊ฒ!
verbose=1,
n_jobs=-1
)
clf.fit(X_train, y_train);
์ต์ ์ ํ์ดํผํ๋ผ๋ฏธํฐ ๋ฐ ์ต๊ณ ์ ์
clf.best_params_ # ์ต์ ์ ํ์ดํผํ๋ผ๋ฏธํฐ
-clf.best_score_ # ์ต๊ณ ์ ์(์์์์ ์ ์๋ฅผ 'neg_mean_absolute_error'๋ก ์ง์ ํ์๊ธฐ ๋๋ฌธ์ '-'๋ฅผ ๋ถ์ฌ์ค๋๋ค.
# MAE, MSE, RMSE ๋ฑ์ ๋ค๋ฅธ score์ ๋ค๋ฅด๊ฒ ๋์์๋ก ์์ข๊ธฐ ๋๋ฌธ์ -๋ฅผ ๋ถ์ฌ์ฃผ์ด ๊ณ์ฐํ๊ฒ ๋ฉ๋๋ค.
ํ์ดํผํ๋ผ๋ฏธํฐ ์กฐํฉ์ ๋ฐ๋ผ, ์ํํ cv์ ๋ํ ์ ๋ณด
pd.DataFrame(clf.cv_results_).sort_values(by='rank_test_score').T
# rank_test_score: ํ
์คํธ ์์
# mean_score_time: ์์ธก์ ๊ฑธ๋ฆฌ๋ ์๊ฐ
- ํ์ดํผํ๋ผ๋ฏธํฐ์ ์กฐํฉ์ด 50๊ฐ์ด๊ธฐ ๋๋ฌธ์ columns ์๊ฐ 50๊ฐ์ธ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
๋ง๋ค์ด์ง ๋ชจ๋ธ ์ค ๊ฐ์ฅ ์ฑ๋ฅ์ด ์ข์ ๋ชจ๋ธ์ pipe์ ์ง์
pipe = clf.best_estimator_
best_estimator_๊ณผ refit parameter
- best_estimator_
- Estimator that was chosen by the search, i.e. estimator which gave highest score (or smallest loss if specified) on the left out data. Not available if
refit=False
. ... Seerefit
parameter for more information ... - refit parameter
- refit : boolean, string, or callable, default=True
Refit an estimator using the best found parameters on the whole dataset.
์ฆ, best_estimator_๋ CV๊ฐ ๋๋ ํ ์ฐพ์ best parameter๋ฅผ ์ฌ์ฉํด ๋ชจ๋ train set์ ๊ฐ์ง๊ณ ๋ค์ ํ์ต(refit)ํ ์ํ์ ๋๋ค.
- Hold-Out ๊ต์ฐจ๊ฒ์ฆ์ ์ํํ ๊ฒฝ์ฐ์๋, ์ถํ train + val set์ ์ต์ ํ๋ ํ์ดํผํ๋ผ๋ฏธํฐ๋ก ์ต์ข ๋ชจ๋ธ์ refitํด์ผ ํฉ๋๋ค.
์ ํํ๊ท, ๋๋คํฌ๋ ์คํธ ๋ชจ๋ธ๋ค์ ํ๋ ์ถ์ฒ ํ์ดํผํ๋ผ๋ฏธํฐ
Random Forest
- class_weight (๋ถ๊ท ํ(imbalanced) ํด๋์ค์ธ ๊ฒฝ์ฐ)
- max_depth (๋๋ฌด ๊น์ด์ง๋ฉด ๊ณผ์ ํฉ)
- n_estimators (์ ์๊ฒฝ์ฐ ๊ณผ์์ ํฉ, ๋์๊ฒฝ์ฐ ๊ธด ํ์ต์๊ฐ)
- min_samples_leaf (๊ณผ์ ํฉ์ผ๊ฒฝ์ฐ ๋์)
- max_features (์ค์ผ ์๋ก ๋ค์ํ ํธ๋ฆฌ์์ฑ)
Logistic Regression
- C (Inverse of regularization strength)
- class_weight (๋ถ๊ท ํ ํด๋์ค์ธ ๊ฒฝ์ฐ)
- penalty
Ridge / Lasso Regression
- alpha
'๐ฟ Data > ๋ถํธ์บ ํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TIL]33.Choose your ML problems (0) | 2022.01.01 |
---|---|
[TIL]32.Section2 Sprint2 Chall(Sprint2 ํค์๋ ์ค์ฌ ์ ๋ฆฌ) (0) | 2021.12.31 |
[TIL]30.Evaluation Metrics for Classification(Precision, Recall, f1score, threshold, ROC curve, AUC) (0) | 2021.12.29 |
[TIL]29.RandomForest(๋๋คํฌ๋ ์คํธ) (0) | 2021.12.27 |
[TIL]28.Decision Tree(์์ฌ๊ฒฐ์ ๋๋ฌด) (0) | 2021.12.26 |