학습 목표
- max_depth 파라미터 값을 튜닝할 수 있습니다.
핵심 키워드
- sample
- gini 계수
- max_depth
학습하기
학습 목표
- max_depth 파라미터 값을 튜닝할 수 있습니다.
핵심 키워드
- sample
- gini 계수
- max_depth
학습하기
학습내용
feature_names = X_train.columns.tolist()
from sklearn.tree import plot_tree
plt.figure(figsize=(15, 15))
tree = plot_tree(model, feature_names=feature_names, fontsize=10, filled=True)
만약 plot_tree 기능을 사용할 수 없다면 라이브러리를 업데이트 해야 합니다.
plt.figure으로 사이즈를 지정하고, filled를 True로 설정하면 색상이 생깁니다.
Insulin이 가장 상위 조건으로 나뉘고, gini 계수가 0이면 tree 그리기를 멈춥니다.
샘플의 개수가 밑으로 갈수록 줄어듭니다.
max_depth는 트리의 깊이를 나타냅니다.
결과 :
from sklearn.tree import DecisionTreeClassifier
model = DecisionTreeClassifier(max_depth=11, random_state=42)
model
최적의 max_depth 값 찾기
from sklearn.metrics import accuracy_score
for max_depth in range(3, 12):
model = DecisionTreeClassifier(max_depth=max_depth, random_state=42)
y_predict = model.fit(X_train, y_train).predict(X_test)
score = accuracy_score(y_test, y_predict) * 100
print(max_depth, score)
https://colab.research.google.com