-
Tree based modelAI부트캠프 2021. 3. 3. 22:01
1. Decision Trees
특성들을 기준으로 샘플을 분류해 나가는 나무 형태의 기계학습 모델
- 분류와 회귀 모두 적용 가능
- 각 분할 지점에서 하나의 특성을 이용하여 sample 들을 분할
비용함수
- gini impurity
- entropy
두 class가 혼합된 집단에서 두 class의 비율이 비슷하면 impurity와 entropy 낮음
각 분할 후 자식노드의 불순도가 가장 낮아지는 feature를 해당 지점에서 분할에 사용하여 model을 생성함
from sklearn.tree import DecisionTreeClassifier clf = DecisionTreeClassifier(random_state=2, criterion='entropy') #모델을 학습 clf.fit(X_train, y_train) #예측값 y_pred = clf.predict(X_train)
과적합을 해결하기 위해 hyperparameter를 조정
* min_samples_split : 분할에 사용할 수 있는 최소 샘플 수,
* min_samples_leaf : 자식노드에 최소한 포함되는 샘플 수,
* max_depth : decision tree의 depth 최대값
clf = DecisionTreeClassifier(random_state=2, max_depth=3, min_samples_leaf=10, criterion='entropy') clf.fit(X_train, y_train)
#decision tree 시각화 import graphviz from sklearn.tree import export_graphviz #export_graphviz : Export a decision tree in DOT format. dot_data = export_graphviz(clf, max_depth=3, feature_names=X_train.columns, class_names=y_train.target.unique(), filled=True, proportion=True) display(graphviz.Source(dot_data))
2. Random forest model
decision tree model을 weak leaner로 갖는 ensemble 모델
ensemble (앙상블 방법) :
여러 머신러닝 학습모델(기본모델, weak leaner)을 만들어 그 모델들의 예측결과를 수합하여 결론을 내는 방법
-회귀 : 기본모델 결과들을 평균한 것으로 예측
-분류 : 기본모델 결과들 중 가장 많은 모델들이 예측한 범주로 예측
Bagging(Bootstrap Aggregation) :
Bootstrap sampling - 원본데이터에서, 원본데이터와 크기가 같은 샘플을 복원추출로 샘플링하는 방법
- 여기서 추출되지 않은 데이터들은 out-of-bag 샘플이라고 하며, 이것을 이용해 모델을 검증할 수 있음
(model.oob_score_ 함수 사용)
random forest 모델은 특성 중 일부분을 무작위로 선택하여 weak model에서 예측할때 사용
Bootstrap sampling과 weak model을 만들어 결과를 예측하는 일련의 과정을 bagging이라고 함
* random forest 모델의 무작위성 (sampling, 특성선택) 때문에 decision tree에 비해 과적합을 줄일 수 있다.
random forest 모델에서 특성이 중요할수록 불순도를 크게 감소시킴
'AI부트캠프' 카테고리의 다른 글
Model Selection (0) 2021.03.03 Evaluation metrics for classification (0) 2021.03.03 Linear model (0) 2021.03.03 [Method] Linear regression (선형회귀) (0) 2021.01.28 [Concept] Section review (0) 2021.01.27