의사결정트리를 만드는 과정¶
의사결정트리의 가장 기본적인 역할은 범주형 데이터를 예측하는 것
In [16]:
# 엔트로피가 가장 많이 줄어드는 방법으로 데이터를 나눈다
# => 타겟변수에 영향을 미치는 변수 순서
In [17]:
import matplotlib.pyplot as plt
import random
from sklearn import datasets, tree
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.metrics import precision_score, recall_score, f1_score
In [18]:
# 데이터 읽어오기
In [19]:
digits=datasets.load_digits()
In [ ]:
# 이미지를 표시함
In [28]:
for label, img in zip(digits.target[:10], digits.images[:10]):
plt.subplot(2,5,label+1)
plt.imshow(img,cmap=plt.cm.gray_r, interpolation='nearest')
plt.title('Digit:{0}'.format(label))
plt.show()
In [29]:
digits
Out[29]:
{'data': array([[ 0., 0., 5., ..., 0., 0., 0.],
[ 0., 0., 0., ..., 10., 0., 0.],
[ 0., 0., 0., ..., 16., 9., 0.],
...,
[ 0., 0., 1., ..., 6., 0., 0.],
[ 0., 0., 2., ..., 12., 0., 0.],
[ 0., 0., 10., ..., 12., 1., 0.]]),
'target': array([0, 1, 2, ..., 8, 9, 8]),
'frame': None,
'feature_names': ['pixel_0_0',
'pixel_0_1',
'pixel_0_2',
'pixel_0_3',
'pixel_0_4',
'pixel_0_5',
'pixel_0_6',
'pixel_0_7',
'pixel_1_0',
'pixel_1_1',
'pixel_1_2',
'pixel_1_3',
'pixel_1_4',
'pixel_1_5',
'pixel_1_6',
'pixel_1_7',
'pixel_2_0',
'pixel_2_1',
'pixel_2_2',
'pixel_2_3',
'pixel_2_4',
'pixel_2_5',
'pixel_2_6',
'pixel_2_7',
'pixel_3_0',
'pixel_3_1',
'pixel_3_2',
'pixel_3_3',
'pixel_3_4',
'pixel_3_5',
'pixel_3_6',
'pixel_3_7',
'pixel_4_0',
'pixel_4_1',
'pixel_4_2',
'pixel_4_3',
'pixel_4_4',
'pixel_4_5',
'pixel_4_6',
'pixel_4_7',
'pixel_5_0',
'pixel_5_1',
'pixel_5_2',
'pixel_5_3',
'pixel_5_4',
'pixel_5_5',
'pixel_5_6',
'pixel_5_7',
'pixel_6_0',
'pixel_6_1',
'pixel_6_2',
'pixel_6_3',
'pixel_6_4',
'pixel_6_5',
'pixel_6_6',
'pixel_6_7',
'pixel_7_0',
'pixel_7_1',
'pixel_7_2',
'pixel_7_3',
'pixel_7_4',
'pixel_7_5',
'pixel_7_6',
'pixel_7_7'],
'target_names': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
'images': array([[[ 0., 0., 5., ..., 1., 0., 0.],
[ 0., 0., 13., ..., 15., 5., 0.],
[ 0., 3., 15., ..., 11., 8., 0.],
...,
[ 0., 4., 11., ..., 12., 7., 0.],
[ 0., 2., 14., ..., 12., 0., 0.],
[ 0., 0., 6., ..., 0., 0., 0.]],
[[ 0., 0., 0., ..., 5., 0., 0.],
[ 0., 0., 0., ..., 9., 0., 0.],
[ 0., 0., 3., ..., 6., 0., 0.],
...,
[ 0., 0., 1., ..., 6., 0., 0.],
[ 0., 0., 1., ..., 6., 0., 0.],
[ 0., 0., 0., ..., 10., 0., 0.]],
[[ 0., 0., 0., ..., 12., 0., 0.],
[ 0., 0., 3., ..., 14., 0., 0.],
[ 0., 0., 8., ..., 16., 0., 0.],
...,
[ 0., 9., 16., ..., 0., 0., 0.],
[ 0., 3., 13., ..., 11., 5., 0.],
[ 0., 0., 0., ..., 16., 9., 0.]],
...,
[[ 0., 0., 1., ..., 1., 0., 0.],
[ 0., 0., 13., ..., 2., 1., 0.],
[ 0., 0., 16., ..., 16., 5., 0.],
...,
[ 0., 0., 16., ..., 15., 0., 0.],
[ 0., 0., 15., ..., 16., 0., 0.],
[ 0., 0., 2., ..., 6., 0., 0.]],
[[ 0., 0., 2., ..., 0., 0., 0.],
[ 0., 0., 14., ..., 15., 1., 0.],
[ 0., 4., 16., ..., 16., 7., 0.],
...,
[ 0., 0., 0., ..., 16., 2., 0.],
[ 0., 0., 4., ..., 16., 2., 0.],
[ 0., 0., 5., ..., 12., 0., 0.]],
[[ 0., 0., 10., ..., 1., 0., 0.],
[ 0., 2., 16., ..., 1., 0., 0.],
[ 0., 0., 15., ..., 15., 0., 0.],
...,
[ 0., 4., 16., ..., 16., 6., 0.],
[ 0., 8., 16., ..., 16., 8., 0.],
[ 0., 1., 8., ..., 12., 1., 0.]]]),
'DESCR': ".. _digits_dataset:\n\nOptical recognition of handwritten digits dataset\n--------------------------------------------------\n\n**Data Set Characteristics:**\n\n :Number of Instances: 1797\n :Number of Attributes: 64\n :Attribute Information: 8x8 image of integer pixels in the range 0..16.\n :Missing Attribute Values: None\n :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)\n :Date: July; 1998\n\nThis is a copy of the test set of the UCI ML hand-written digits datasets\nhttps://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits\n\nThe data set contains images of hand-written digits: 10 classes where\neach class refers to a digit.\n\nPreprocessing programs made available by NIST were used to extract\nnormalized bitmaps of handwritten digits from a preprinted form. From a\ntotal of 43 people, 30 contributed to the training set and different 13\nto the test set. 32x32 bitmaps are divided into nonoverlapping blocks of\n4x4 and the number of on pixels are counted in each block. This generates\nan input matrix of 8x8 where each element is an integer in the range\n0..16. This reduces dimensionality and gives invariance to small\ndistortions.\n\nFor info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G.\nT. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C.\nL. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469,\n1994.\n\n.. topic:: References\n\n - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their\n Applications to Handwritten Digit Recognition, MSc Thesis, Institute of\n Graduate Studies in Science and Engineering, Bogazici University.\n - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.\n - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.\n Linear dimensionalityreduction using relevance weighted LDA. School of\n Electrical and Electronic Engineering Nanyang Technological University.\n 2005.\n - Claudio Gentile. A New Approximate Maximal Margin Classification\n Algorithm. NIPS. 2000.\n"}
In [32]:
label, img, img.shape
Out[32]:
(9,
array([[ 0., 0., 11., 12., 0., 0., 0., 0.],
[ 0., 2., 16., 16., 16., 13., 0., 0.],
[ 0., 3., 16., 12., 10., 14., 0., 0.],
[ 0., 1., 16., 1., 12., 15., 0., 0.],
[ 0., 0., 13., 16., 9., 15., 2., 0.],
[ 0., 0., 0., 3., 0., 9., 11., 0.],
[ 0., 0., 0., 0., 9., 15., 4., 0.],
[ 0., 0., 9., 12., 13., 3., 0., 0.]]),
(8, 8))
In [44]:
images=digits.images
labels=digits.target
# 차원을 하나 줄인다.
images = images.reshape(images.shape[0],-1)
# 결정트리를 생성한다
n_samples=len(images)
train_size=int(n_samples *2/3)
classifier=tree.DecisionTreeClassifier(max_depth=3)
classifier.fit(images[:train_size],labels[:train_size])
# 결정트리의 성능을 확인한다.
expected =labels[train_size:]
predicted = classifier.predict(images[train_size:])
# 실제값과 예측값을 비교
print('Accuracy:\n',
accuracy_score(expected, predicted))
# 혼동행렬 (대각선에 숫자가 모여있을수록 좋은 모델)
print('Confusion matrix:\n',
confusion_matrix(expected, predicted))
Accuracy:
0.44908180300500833
Confusion matrix:
[[57 0 1 0 1 0 0 0 0 0]
[ 0 0 25 16 6 0 0 15 0 0]
[ 1 0 49 6 0 1 0 3 0 0]
[ 0 0 10 46 0 0 0 5 0 1]
[ 4 0 27 0 30 0 0 1 0 0]
[ 0 0 2 29 20 5 0 3 0 0]
[ 0 0 55 5 0 1 0 0 0 0]
[ 0 0 0 0 6 0 0 55 0 0]
[ 0 0 33 6 2 1 0 13 0 0]
[ 2 0 1 21 7 0 0 0 0 27]]
In [40]:
img, img.shape
Out[40]:
(array([[ 0., 0., 11., 12., 0., 0., 0., 0.],
[ 0., 2., 16., 16., 16., 13., 0., 0.],
[ 0., 3., 16., 12., 10., 14., 0., 0.],
[ 0., 1., 16., 1., 12., 15., 0., 0.],
[ 0., 0., 13., 16., 9., 15., 2., 0.],
[ 0., 0., 0., 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