자주 접하는 머신러닝 학습법의 한 종류

- 분류, 회귀 모두 가능한 지도 학습 모델 중 하나

 

트리 구조에 기반하여 결정을 진행함

- True, False 

 

하나의 루트 노드, 여러 개의 내부 노드 및 리프 노드를 포함

 

 

소스 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from sklearn import tree
from sklearn.datasets import load_iris
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.tree import export_graphviz
import pydotplus
from IPython.display import Image
 
iris = load_iris()
 
= iris.data[:, [23]]
= iris.target
 
# 자동으로 데이터셋을 분리해주는 함수
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
 
# 데이터 표준화 작업
sc = StandardScaler()
sc.fit(X_train)
 
# 표준화된 데이터셋
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
 
iris_tree = tree.DecisionTreeClassifier(criterion='entropy', max_depth=3, random_state=0)
iris_tree.fit(X_train, y_train)
 
y_pred_tr = iris_tree.predict(X_test)
print('Accuracy: %.2f' % accuracy_score(y_test, y_pred_tr))
 
dot_data = export_graphviz(iris_tree, out_file=None, feature_names=['petal length''petal width'],
                          class_names=iris.target_names, filled=True, rounded=True, special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())
cs

 

 

참고

 

 

 

단단한 머신러닝

간결한 설명과 최소한의 수학적 지식을 통해 체계적으로 정리한 머신러닝 입문서! 『단단한 머신러닝』은 인공지능 분야의 명예의 전당이라는 AAAI의 펠로우로 선정된 저자가 머신러닝을 처음

books.google.co.kr

 

Python - sklearn, jupyter로 Decision Tree 학습하기

02DecisionTree_practice In [2]: R을 공부하며 Decision Tree를 정리했었는데, 파이썬에서 비슷한 내용을 정리해보고자 한다. 소스코드는 scikit-learn의 공식 튜토리얼 문서자료와 [Python Machine Learning]을..

yamalab.tistory.com

 

'인공지능' 카테고리의 다른 글

K-최근접 이웃(K-Nearest Neighbor, K-NN)  (0) 2021.01.25

+ Recent posts