Skip to content

Commit

Permalink
Merge pull request #49 from SDSTony/master
Browse files Browse the repository at this point in the history
edit and add imgs for ch6 to ch12
  • Loading branch information
SDSTony authored May 9, 2021
2 parents c30abd0 + 33e669b commit fd03f3b
Show file tree
Hide file tree
Showing 55 changed files with 222 additions and 164 deletions.
Binary file added course-website/imgs/ch06-img01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img02.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img03.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img04.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img05.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img06.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img07.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img08.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img09.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img11.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img12.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img13.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img14.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img15.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch06-img16.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch07-img01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch07-img02.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch07-img03.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch07-img04.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch07-img05.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch07-img06.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch07-img07.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch07-img08.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch07-img09.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added course-website/imgs/ch07-img10.jpg
Binary file added course-website/imgs/ch07-img11.jpg
Binary file added course-website/imgs/ch08-img01.jpg
Binary file added course-website/imgs/ch08-img02.jpg
Binary file added course-website/imgs/ch08-img03.jpg
Binary file added course-website/imgs/ch08-img04.jpg
Binary file added course-website/imgs/ch08-img05.jpg
Binary file added course-website/imgs/ch08-img06.jpg
Binary file added course-website/imgs/ch09-img01.jpg
Binary file added course-website/imgs/ch09-img02.jpg
Binary file added course-website/imgs/ch09-img03.jpg
Binary file added course-website/imgs/ch09-img04.jpg
Binary file added course-website/imgs/ch09-img05.jpg
Binary file added course-website/imgs/ch09-img06.jpg
Binary file added course-website/imgs/ch09-img07.jpg
Binary file added course-website/imgs/ch10-img01.jpg
Binary file added course-website/imgs/ch10-img02.jpg
Binary file added course-website/imgs/ch12-img01.jpg
Binary file added course-website/imgs/ch12-img02.jpg
Binary file added course-website/imgs/ch12-img03.jpg
Binary file added course-website/imgs/ch12-img04.jpg
Binary file added course-website/imgs/ch12-img05.jpg
Binary file added course-website/imgs/ch12-img06.jpg
Binary file added course-website/imgs/ch12-img07.jpg
139 changes: 78 additions & 61 deletions course-website/lectures/06-scikit-learn.md

Large diffs are not rendered by default.

79 changes: 48 additions & 31 deletions course-website/lectures/07-rf-lgb.md

Large diffs are not rendered by default.

40 changes: 23 additions & 17 deletions course-website/lectures/08-cv-stacking.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,39 @@

구축한 모델의 성능 평가시 사용할 수 있는 주요 방법은 4가지가 있습니다. 각 방법에 대한 설명은 아래와 같습니다.

방법 | 설명 |
---------|----------|
Hold-out Validation | 학습데이터의 일부를 검증셋으로 분류 후 나머지만 학습에 사용하고 검증셋은 모델 성능 검증에 사용 |
N-Fold CV | 학습데이터를 N개의 폴드로 나눈 후 각각의 폴드에 대해 한 폴드를 검증셋으로 나머지는 학습셋으로 사용해 총 N번 학습하고 N개의 검증셋을 모아 모델 성능 검증에 사용 |
Stratified N-Fold CV | N-Fold CV에서 각각의 폴드에 종속변수의 분포가 동일하게 폴드를 나누는 방식, 분류학습에서 종속변수의 범주의 분포가 균일하지 않을 때 사용 |
Leave-One-Out (LOO) CV | 샘플의 개수를 N으로 사용한 N-Fold CV. 샘플의 개수가 적을 때 (50 미만) 사용 |
- 표 8.X
방법 | 설명
---------|----------
Hold-out Validation | 학습데이터의 일부를 검증셋으로 분류 후 나머지만 학습에 사용하고 검증셋은 모델 성능 검증에 사용
N-Fold CV | 학습데이터를 N개의 폴드로 나눈 후 각각의 폴드에 대해 한 폴드를 검증셋으로 나머지는 학습셋으로 사용해 총 N번 학습하고 N개의 검증셋을 모아 모델 성능 검증에 사용
Stratified N-Fold CV | N-Fold CV에서 각각의 폴드에 종속변수의 분포가 동일하게 폴드를 나누는 방식, 분류학습에서 종속변수의 범주의 분포가 균일하지 않을 때 사용
Leave-One-Out (LOO) CV | 샘플의 개수를 N으로 사용한 N-Fold CV. 샘플의 개수가 적을 때 (50 미만) 사용
- 표 8-1 모델 검증 방법

Hold-out Validation은 데이터가 아주 많은 경우에는 충분합니다. 하지만 데이터과학 대회에서는 조금이라도 성능을 높이기 위해 N-Fold CV를 사용해 모든 학습데이터를 학습과 검증에 사용하는 것을 선호합니다.

Hold-out Validation을 도식화한 그림과 코드 예제는 아래와 같습니다.

- 그림
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch08-img01.jpg?raw=true)
- 그림 8-1 Hold-out Validation([출처](https://docs.google.com/presentation/d/160j7GQEoMPFkKn0mjculI0TpkDRJIsNkggUuwlG_ouY/edit?usp=sharing))

- 그림
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch08-img02.jpg?raw=true)
- 그림 8-2 Hold-out Validation 예제 코드([출처](https://docs.google.com/presentation/d/160j7GQEoMPFkKn0mjculI0TpkDRJIsNkggUuwlG_ouY/edit?usp=sharing))

Scikit-learn에서 `train_test_split()`함수를 통해 적용 가능하며 해당 함수는 나누고자 하는 독립변수와 종속변수, 그리고 검증셋의 크기를 나타내는 `test_size`와 재현성을 위한 `random_state`를 인자로 받습니다.

N-Fold CV를 도식화한 그림과 코드 예제는 아래와 같습니다. N-Fold CV는 K-Fold CV라고도 불립니다.

- 그림
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch08-img03.jpg?raw=true)
- 그림 8-3 N-Fold CV([출처](https://docs.google.com/presentation/d/160j7GQEoMPFkKn0mjculI0TpkDRJIsNkggUuwlG_ouY/edit?usp=sharing))

- 그림
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch08-img04.jpg?raw=true)
- 그림 8-4 N-Fold CV 예제 코드([출처](https://docs.google.com/presentation/d/160j7GQEoMPFkKn0mjculI0TpkDRJIsNkggUuwlG_ouY/edit?usp=sharing))

Scikit-learn에서 `KFold()`함수를 통해 적용 가능합니다. `KFold()`는 나누고자 하는 폴드의 개수인 `n_splits`를 인자로 명시해주어야 합니다. `shuffle`인자는 데이터 분리 시 섞을지 여부를 결정하며 `random_state`는 데이터를 섞을 시 재현성을 고정시키는 인자입니다. KFold 객체를 생성한 뒤 for문을 활용해 KFold 객체가 생성하는 인덱스를 활용해 학습데이터셋과 검증데이터셋으로 구분하여 모델을 학습하고 검증합니다.

KFold를 활용해 각각의 검증셋에 대해 예측한 값을 cross-validation prediction 또는 out-of-fold prediction이라고 하는데 해당 값을 stacking시 활용하게 됩니다. 다음 절에서 관련 내용을 자세히 살펴보겠습니다.

이번 실습에서는 lightgbm, logistic regression, 그리고 random forest 각각에 대해 cross-validation을 적용한 파일이 3개가 있습니다. 실습을 통해 cross-validaion 개념을 익혀보시길 바랍니다.
이번 실습에서는 [lightgbm](08-lightgbm-cv.ipynb), [logistic regression](08-lr-cv.ipynb), 그리고 [random forest](08-rf-cv.ipynb) 각각에 대해 cross-validation을 적용한 파일이 3개가 있습니다. 실습을 통해 cross-validaion 개념을 익혀보시길 바랍니다.

## 8.2 Stacking

Expand All @@ -42,15 +46,17 @@ Stacking은 1992년에 Los Alamos National Laboratory의 David H. Wolpert가 Sta

데이터가 방대한 경우 Hold-out Validation과 함께 사용 가능하며 이런 경우 Blending이라고도 표현합니다. Blending 방식은 Netflix Grand Prize 대회에서 우승자들이 사용한 방법이기도 합니다.

- 그림
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch08-img05.jpg?raw=true)
- 그림 8-5 Stacking([출처](https://docs.google.com/presentation/d/160j7GQEoMPFkKn0mjculI0TpkDRJIsNkggUuwlG_ouY/edit?usp=sharing))

위 그림은 stacking을 도식화한 그림입니다. 위 예시에서는 5-Fold CV를 통해 학습데이터셋에 대한 CV Prediction을 산출합니다. 만약 random forest, lightgbm, 그리고 logistic regression으로 5-Fold CV를 3번 진행한다면 각 모델 별로 하나씩 CV Prediction이 산출되어 총 3개의 CV Prediction값을 갖게 됩니다. 이렇게 얻은 3개의 CV Prediction을 독립변수로 사용해서 다음 단계인 Stage 1 Ensemble에서 모델을 학습합니다. Stage 1 단계에서도 N-Fold CV를 활용해 CV Prediction을 얻게 됩니다.
그림 8-5은 stacking을 도식화한 그림입니다. 위 예시에서는 5-Fold CV를 통해 학습데이터셋에 대한 CV Prediction을 산출합니다. 만약 random forest, lightgbm, 그리고 logistic regression으로 5-Fold CV를 3번 진행한다면 각 모델 별로 하나씩 CV Prediction이 산출되어 총 3개의 CV Prediction값을 갖게 됩니다. 이렇게 얻은 3개의 CV Prediction을 독립변수로 사용해서 다음 단계인 Stage 1 Ensemble에서 모델을 학습합니다. Stage 1 단계에서도 N-Fold CV를 활용해 CV Prediction을 얻게 됩니다.

시험데이터셋에 대해서는 초기에 사용한 random forest, lightgbm, 그리고 logistic regression을 활용해 얻은 예측값을 Stage 1 Ensemble에서 학습한 모델의 입력값으로 넣어 최종 예측을 하게 됩니다.

- 그림
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch08-img06.jpg?raw=true)
- 그림 8-6 Stacking 활용 예시([출처](https://docs.google.com/presentation/d/160j7GQEoMPFkKn0mjculI0TpkDRJIsNkggUuwlG_ouY/edit?usp=sharing))

위 그림은 stacking을 적용한 예시입니다. Single model을 생성하는 단계에서 총 64개의 모델을 구축합니다. 이 때 모델은 GBM, NN, FM, LR등 다양하게 사용한 것을 볼 수 있습니다. 각각의 single model에 예측한 총 64개의 예측값을 Stage 1 Ensemble의 입력값으로 활용합니다. Stage 1에서는 총 15개의 모델이 구축됐으며 마찬가지로 15개의 예측값이 생성됩니다. 15개의 예측값을 Stage 2의 입력값으로 주어 2개의 모델을 구축합니다. Stage 3에서는 이전 단계에서 활용한 모든 모델의 예측값을 입력값으로 주어 최종 모델 1개를 학습합니다.
그림 8-6은 stacking의 활용 예시입니다. Single model을 생성하는 단계에서 총 64개의 모델을 구축합니다. 이 때 모델은 GBM, NN, FM, LR등 다양하게 사용한 것을 볼 수 있습니다. 각각의 single model에 예측한 총 64개의 예측값을 Stage 1 Ensemble의 입력값으로 활용합니다. Stage 1에서는 총 15개의 모델이 구축됐으며 마찬가지로 15개의 예측값이 생성됩니다. 15개의 예측값을 Stage 2의 입력값으로 주어 2개의 모델을 구축합니다. Stage 3에서는 이전 단계에서 활용한 모든 모델의 예측값을 입력값으로 주어 최종 모델 1개를 학습합니다.

위와 같이 계속해서 stage를 쌓아갈 수 있습니다. 하지만 성능 향상에 가장 많은 도움이 되는 것은 Stage 1 Ensemble입니다. Stage가 쌓여갈 수록 성능은 점점 더 적게 향상됩니다.

Expand All @@ -64,7 +70,7 @@ Stacking은 1992년에 Los Alamos National Laboratory의 David H. Wolpert가 Sta
동계 스포츠인 쇼트트랙에서 결승선에 들어올 때 앞다리를 내밀어 조금이나마 기록을 향상시키는데, stacking도 이와 같다고 볼 수 있습니다. 성능을 극단적으로 향상시키기 위해 사용되기 보다는 마지막 단계에서 최종 순위를 다툴 때 사용하는 방법입니다.
```

Stacking 실습 파일은 총 2개가 있으며 하나는 lightgbm을 활용한 예제이고 하나는 logistic regression을 활용한 예제입니다. 8.1에서 실습한 3개의 N-Fold CV 실습 파일에서 생성한 예측값을 불러와서 stacking을 진행하도록 설계돼 있습니다. 실습을 통해 stacking 개념을 익혀보시길 바랍니다.
Stacking 실습 파일은 총 2개가 있으며 하나는 [lightgbm](08-stacking-lgb.ipynb) 활용한 예제이고 하나는 [logistic regression](08-stacking-lr.ipynb) 활용한 예제입니다. 8.1절에서 실습한 3개의 N-Fold CV 실습 파일에서 생성한 예측값을 불러와서 stacking을 진행하도록 설계돼 있습니다. 실습을 통해 stacking 개념을 익혀보시길 바랍니다.

## 8.3 참고자료

Expand Down
39 changes: 26 additions & 13 deletions course-website/lectures/09-tuning.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,67 @@

이번 장에서는 모델 튜닝 또는 하이퍼파라미터 최적화를 진행할 때 사용가능한 라이브러리에 대해 알아보겠습니다.

- 그림 9-X
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch09-img01.jpg?raw=true)
- 그림 9-1 Hold-out Validation([출처](https://docs.google.com/presentation/d/1Yy3s4nbwJPiZwSa73WRpWGQkhgnnEwfffhwJUYs2Ozk/edit?usp=sharing))

위 그림은 모델 튜닝 싸이클을 나타내고 있습니다. 먼저 초기 파라미터를 설정 후 모델을 학습하고 검증합니다. 검증 결과에 따라 파라미터를 변경하고 다시 학습 및 검증을 해서 최선의 결과에 도달할 때 까지 반복을 합니다. 검증을 할 때는 8장에서 확인했듯이 Hold-out Validation 또는 N-Fold Cross-Validation을 사용합니다.
그림 9-1은 모델 튜닝 싸이클을 나타내고 있습니다. 먼저 초기 파라미터를 설정 후 모델을 학습하고 검증합니다. 검증 결과에 따라 파라미터를 변경하고 다시 학습 및 검증을 해서 최선의 결과에 도달할 때 까지 반복을 합니다. 검증을 할 때는 [8장](08-cv-stacking.md)에서 확인했듯이 Hold-out Validation 또는 N-Fold Cross-Validation을 사용합니다.

최선의 결과를 찾기 위해선 검증결과를 확인하고 파라미터를 변경하는 과정을 반복해야 합니다. 선형회귀 알고리즘 처럼 파라미터 개수가 적고 이진값으로 인자를 받는 경우 직접 파라미터를 변경해볼 수 있습니다.
최선의 결과를 찾기 위해선 검증결과를 확인하고 파라미터를 변경하는 과정을 반복해야 합니다. 선형회귀 알고리즘 처럼 파라미터 개수가 적거나 이진값으로 인자를 입력받는 경우 직접 파라미터를 변경하면서 모델 검증을 할 수 있습니다.

- 그림 9-X Scikit-learn 선형 회귀 알고리즘 파라미터
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch09-img02.jpg?raw=true)
- 그림 9-2 Scikit-learn 선형 회귀 알고리즘 파라미터([출처](https://docs.google.com/presentation/d/1Yy3s4nbwJPiZwSa73WRpWGQkhgnnEwfffhwJUYs2Ozk/edit?usp=sharing))

하지만 lightgbm 처럼 파라미터가 많고 정수형 또는 실수형을 인자로 받게되면 직접 파라미터를 변경하면서 검증하기가 어렵습니다. 이런 경우 하이퍼파라미터 최적화를 용이하게 해주는 도구 활용이 필요합니다.

- 그림 9-X LightGBM 알고리즘 파라미터
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch09-img03.jpg?raw=true)
- 그림 9-3 LightGBM 알고리즘 파라미터([출처](https://docs.google.com/presentation/d/1Yy3s4nbwJPiZwSa73WRpWGQkhgnnEwfffhwJUYs2Ozk/edit?usp=sharing))

Scikit-learn에서 대표적으로 제공하는 하이퍼 파리미터 최적화 툴로 GridSearchCV와 RandomizedSearchCV가 있습니다. GridSearch는 검색하고자 하는 피처 값들의 모든 조합을 사용하여 모델을 학습/겁증 후 최고의 모델을 선택합니다. 아래 에시 코드에서 처럼 `param_grid`에 탐색하고자 하는 조합을 모두 입력을 해서 검증을 실시합니다.

- 그림
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch09-img04.jpg?raw=true)
- 그림 9-4 GridSearchCV 사용 예제([출처](https://docs.google.com/presentation/d/1Yy3s4nbwJPiZwSa73WRpWGQkhgnnEwfffhwJUYs2Ozk/edit?usp=sharing))

RandomizedSearch는 검색하고자하는 피쳐 값들의 조합 중 N개의 조합을 임의로 선택해서 모델을 학습/검증 후 최고의 모델을 선택합니다. 이 때 피쳐 값은 범위 또는 분포로도 지정할 수 있습니다. 아래 그림처럼 `l1_ratio``alpha`를 각각 균등분포와 로그균등분포를 적용하면 해당 분포로 부터 임의의 값을 추출해서 모델을 학습하고 검증을 합니다.

- 그림
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch09-img05.jpg?raw=true)
- 그림 9-5 RandomizedSearchCV 사용 예제([출처](https://docs.google.com/presentation/d/1Yy3s4nbwJPiZwSa73WRpWGQkhgnnEwfffhwJUYs2Ozk/edit?usp=sharing))

Scikit-learn 공식 튜토리얼 페이지에 추가 설명을 확인할 수 있습니다.
[Scikit-learn 공식 튜토리얼 페이지](https://scikit-learn.org/stable/modules/grid_search.html)에서 추가 설명을 확인할 수 있습니다.

앞서 살펴본 GridSearch나 RandomizedSearch는 가능한 모든 조합을 사용하거나 그 중 일부를 랜덤하게 사용합니다. 다음 절에서 살펴볼 Hyperopt와 Optuna는 베이지안 방식을 활용해 사용하고자 하는 하이퍼파라미터 조합을 탐색합니다.

## 9.1 Hyperopt

Hyperopt 라이브러리는 가장 널리 사용되는 모델 튜닝 라이브러리 중 하나입니다. 2013년에 Harvard에 James Bergstra와 MIT의 Dan Yamins가 Hyperopt를 [ICML](http://proceedings.mlr.press/v28/bergstra13.pdf)학회와 [SciPy](https://conference.scipy.org/proceedings/scipy2013/pdfs/bergstra_hyperopt.pdf)에서 발표를 했습니다. Hyperopt는 Tree-structured Parzen Estimator(TPE) 알고리즘을 사용해 베이지안 최적화를 수행합니다. 출시 당시에는 scipy 라이브러리를 활용한 최적화 작업을 주로 사용했었는데 Hyperopt가 scipy에서 제공하는 `scipy.optimize.minimize()` API와 사용 방법이 유사해서 많은 관심을 끌었습니다.
Hyperopt 라이브러리는 가장 널리 사용되는 모델 튜닝 라이브러리 중 하나입니다. 2013년에 Harvard에 James Bergstra와 MIT의 Dan Yamins가 Hyperopt를 [ICML](http://proceedings.mlr.press/v28/bergstra13.pdf)학회와 [SciPy](https://conference.scipy.org/proceedings/scipy2013/pdfs/bergstra_hyperopt.pdf)에서 Hyperopt에 대한 발표를 했습니다. Hyperopt는 Tree-structured Parzen Estimator(TPE) 알고리즘을 사용해 베이지안 최적화를 수행합니다. Hyperopt가 출시 되기 전에는 scipy 라이브러리를 활용한 최적화 작업을 주로 사용했었는데 Hyperopt가 scipy에서 제공하는 `scipy.optimize.minimize()` API와 사용 방법이 유사해서 많은 관심을 끌었습니다.

실습 파일에서 Hyperopt를 적용해 lightGBM의 하이퍼파라미터를 최적화해보겠습니다.
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch09-img06.jpg?raw=true)
- 그림 9-6 Hyperopt 로고([출처](https://docs.google.com/presentation/d/1Yy3s4nbwJPiZwSa73WRpWGQkhgnnEwfffhwJUYs2Ozk/edit?usp=sharing))

[실습 파일](09-lightgbm-hyperopt.ipynb)에서 Hyperopt를 적용해 lightGBM의 하이퍼파라미터를 최적화해보겠습니다.

```{tip}
데이터가 많을 경우 하이퍼파라미터 최적화 시에 N-Fold CV를 사용하면 시간이 오래 걸립니다. Hold-out Validation이나 전체 데이터셋에서 샘플링을 한 데이터를 사용해서 최적화 하면 탐색 시간을 줄일 수 있습니다.
데이터가 많을 경우 하이퍼파라미터 최적화에 N-Fold CV를 사용하면 시간이 오래 걸립니다. Hold-out Validation이나 전체 데이터셋에서 샘플링을 한 데이터를 사용해서 최적화 하면 탐색 시간을 줄일 수 있습니다.
```

```{tip}
모델 튜닝은 마지막 단계에서 성능을 끌어올릴 때 사용하시길 바랍니다. 무작정 좋은 하이퍼파라미터를 찾을려고 하는 것 보다 일반적으로 적합한 피쳐를 생성하는 것이 점수 상승에 효과적입니다.
모델 튜닝은 마지막 단계에서 성능을 끌어올릴 때 사용하시길 바랍니다. 무작정 좋은 하이퍼파라미터를 찾을려고 하는것 보다는 적합한 피쳐를 생성하는 것이 일반적으로 점수 상승에 효과적입니다.
```

## 9.2 Optuna

Optuna는 2019년 일본의 Preferred Networks사의 연구진들이 [KDD](https://arxiv.org/pdf/1907.10902.pdf)에서 발표를 한 라이브러리입니다. 최근에 각광을 받고 있는 모델 튜닝 라이브러리입니다. Optuna는 기존 하이퍼파라미터 최적화 라이브러리들의 장점들을 결합했으며 단순한 API를 제공합니다.

Optuna의 `integration`모듈 내에 있는 알고리즘들은 일반적으로 탐색 범위로 사용하는 하이퍼파라미터들을 사용해 최적화를 실시합니다. 그래서 사용자가 직접 범위를 명시하지 않아도 내부적으로 모델 튜닝을 실시합니다. 다만 세부적인 사항은 사용자가 지정할 수 없다는 단점이 존재합니다.
![](https://github.com/kaggler-tv/dku-kaggle-class/blob/master/course-website/imgs/ch09-img07.jpg?raw=true)
- 그림 9-7 Optuna 로고([출처](https://docs.google.com/presentation/d/1Yy3s4nbwJPiZwSa73WRpWGQkhgnnEwfffhwJUYs2Ozk/edit?usp=sharing))

Optuna의 `integration`모듈 내에 있는 알고리즘은 내재된 탐색 범위로 하이퍼파라미터 최적화를 실시합니다. 그래서 사용자가 직접 범위를 명시하지 않아도 내부적으로 모델 튜닝을 실시합니다. 다만 세부적인 사항은 사용자가 지정할 수 없다는 단점이 존재합니다.

```{tip}
처음 모델 튜닝을 실시해서 적정한 탐색 범위 설정이 어렵다면 optuna의 integration모듈 사용을 추천드립니다. 적절한 범위를 직접 명시해 빠른 시간내 탐색을 마칠려면 hyperopt 또는 optuna에서 제공하는 다른 API를 활용해 직접 범위를 설정해서 모델 튜닝을 하시길 바랍니다.
```

[실습 파일](09-lightgbm-optuna.ipynb)에서 Optuna를 적용해 lightGBM의 하이퍼파라미터를 최적화해보겠습니다.

## 9.3 참고자료

- [Scikit-learn Hyperparameter Tuning 튜토리얼](https://scikit-learn.org/stable/modules/grid_search.html)
Expand Down
Loading

0 comments on commit fd03f3b

Please sign in to comment.