데이터 뢄석 이둠/λ¨Έμ‹ λŸ¬λ‹

[IRIS 데이터 뢄석] 2. Python Decision Tree ( μ˜μ‚¬ κ²°μ • λ‚˜λ¬΄ )

silversu 2021. 8. 20. 11:19

λŸ¬λ‹μŠ€ν‘Όμ¦ˆ μˆ˜μ—… 정리 

 

 

< 이전 κΈ€ > 

https://silvercoding.tistory.com/64

 

[IRIS 데이터 뢄석] 1. Python KNN λΆ„λ₯˜

λŸ¬λ‹μŠ€ν‘Όμ¦ˆ μˆ˜μ—… 정리 < 이전 κΈ€ > https://silvercoding.tistory.com/63?category=967543 https://silvercoding.tistory.com/62 [boston 데이터 뢄석] 1. μ°¨μ›μΆ•μ†Œ (PCA) 파이썬 예제 λŸ¬λ‹μŠ€ν‘Όμ¦ˆ μˆ˜μ—… 정리  λΌ..

silvercoding.tistory.com

 

 

 

 


 λ°μ΄ν„° 뢈러였기 

이전 κΈ€κ³Ό λ™μΌν•œ Iris Flower Dataset 을 μ΄μš©ν•˜μ—¬ μ‹€μŠ΅μ„ μ§„ν–‰ν•œλ‹€. 

< Iris Flower Dataset >

https://www.kaggle.com/arshid/iris-flower-dataset

 

Iris Flower Dataset

Iris flower data set used for multi-class classification.

www.kaggle.com

 

import pandas as pd
import os
os.chdir('../data')  # 데이터셋이 μžˆλŠ” 본인 폴더 경둜
iris = pd.read_csv("IRIS.csv")
iris.head()

iris['species'].value_counts()

각 μ’…λ₯˜λ§ˆλ‹€ 50개의 데이터가 μ‘΄μž¬ν•œλ‹€. 

 

 

 

 

 


 

μ˜μ‚¬κ²°μ •λ‚˜λ¬΄ μ‚¬μš© 

train & Test 데이터셋 뢄리 

iris['id'] = range(len(iris))

μš°μ„  데이터λ₯Ό κ΅¬λΆ„ν•˜κΈ° μœ„ν•΄ μˆœμ„œλŒ€λ‘œ 값을 λ„£μ–΄μ€€ id μ»¬λŸΌμ„ μƒμ„±ν•œλ‹€. 

iris = iris[['id','sepal_length','sepal_width','petal_length','petal_width','species']]

id 컬럼이 κ°€μž₯ μ•žμ— μ˜€λ„λ‘ μ •λ ¬ν•΄μ€€λ‹€. 

train = iris.sample(100,replace=False,random_state=7).reset_index().drop(['index'],axis=1)

랜덀으둜 100개의 μƒ˜ν”Œμ„ μΆ”μΆœν•˜μ—¬ train 에 μ €μž₯ν•΄ μ€€λ‹€. 

test = iris.loc[ ~iris['id'].isin(train['id']) ]
test = test.reset_index().drop(['index'],axis=1)

train의 id값이 μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” iris 데이터듀을 test에 λ„£μ–΄μ€€λ‹€. 

 

 

 

μ˜μ‚¬κ²°μ •λ‚˜λ¬΄ ν•™μŠ΅ 

DecisionTreeClassifier(min_samples_split = n)

---> νŠΉμ§• : 해석이 쉽고 λΉ λ₯΄λ‹€. 

---> min_samples_split : μ˜μ‚¬κ²°μ •λ‚˜λ¬΄μ—μ„œ μ΅œμ’… λ…Έλ“œμ˜ μ΅œμ†Œ μƒ˜ν”Œ 수 

 

from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier(min_samples_split = 10)

min_samples_split 을 10으둜 μ„€μ •ν•΄μ£Όμ–΄ μ΅œμ’… λ…Έλ“œμ˜ μƒ˜ν”Œμˆ˜κ°€ 10미만이 λ˜μ§€ μ•Šλ„λ‘ μ‘°μ •ν•œλ‹€. 

dt.fit(train[['sepal_length','sepal_width','petal_length','petal_width']],train['species'])

생성해 놓은 dt 객체둜 ν•™μŠ΅μ„ μ‹œμΌœμ€€λ‹€. 

predictions = dt.predict(test[['sepal_length','sepal_width','petal_length','petal_width']])

μ˜ˆμΈ‘κ°’μ„ prediction에 λ„£μ–΄μ€€λ‹€. 

test['pred'] = predictions

μ˜ˆμΈ‘κ°’ prediction을 test의 pred μ»¬λŸΌμ— μ €μž₯ν•œλ‹€. 

test.head()

(pd.Series(predictions)==test['species']).mean()

μ˜ˆμΈ‘κ°’κ³Ό 정닡을 λΉ„κ΅ν•˜μ—¬ 정확도λ₯Ό κ΅¬ν•΄λ³΄λ‹ˆ 0.98이 λ‚˜μ™”λ‹€. 

 

 

 


μœ„μ˜ 정확도 μΈ‘μ • 방법을 μ‚¬μš©ν•˜λ©΄ μ‹ λ’°μ„±μ΄ ν•˜λ½ν•  수 μžˆλ‹€. train, test λ°μ΄ν„°λ₯Ό μ–΄λ–»κ²Œ λ‚˜λˆ„λŠ”μ§€μ— 따라 κ²°κ³Όκ°€ 크게 λ‹¬λΌμ§ˆ μˆ˜λ„ 있기 λ•Œλ¬Έμ΄λ‹€. λ”°λΌμ„œ cross validation을 μ΄μš©ν•˜μ—¬ 정확도λ₯Ό ꡬ해볼 수 μžˆλ‹€. 


from sklearn.model_selection import cross_val_score
import numpy as np
dt = DecisionTreeClassifier(min_samples_split = 10)
scores = cross_val_score(dt, iris[['sepal_length','sepal_width','petal_length','petal_width']], iris['species'], cv=5, scoring="accuracy")
np.mean(scores)

 

이번 μ˜ˆμ‹œμ²˜λŸΌ 데이터 μˆ˜κ°€ 적을 κ²½μš°μ—λŠ” μœ„μ™€ 같이 전체 λ°μ΄ν„°λ‘œ cross validation을 μˆ˜ν–‰ν•˜λŠ” 것이 신뒰성이 λ†’λ‹€. 5 fold cross validation을 μˆ˜ν–‰ν•œ κ²°κ³Ό , 정확도가 μ•½ 0.97이 λ‚˜μ˜¨ 것을 λ³Ό 수 μžˆλ‹€. 

 

 

 


μ˜μ‚¬κ²°μ •λ‚˜λ¬΄ μ‹œκ°ν™”

from sklearn import tree
import matplotlib.pyplot as plt
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 16,10
a=tree.plot_tree(dt,feature_names = ['sepal_length','sepal_width','petal_length','petal_width'],impurity=False, max_depth=2, fontsize=10, proportion=True)
plt.show(a)

max_depthλ₯Ό μ΄μš©ν•˜μ—¬ 깊이λ₯Ό μ‘°μ ˆν•  수 μžˆλ‹€. 2개 μ΄ν›„λ‘œλŠ” (...) 으둜 μƒλž΅λœ 것을 λ³Ό 수 μžˆλ‹€. μœ„μ™€ 같이 μ˜μ‚¬κ²°μ • λ‚˜λ¬΄λ₯Ό μ‚¬μš©ν•˜κ³ , μ‹œκ°ν™” 해보면 해석을 쉽고 κ°„νŽΈν•˜κ²Œ ν•΄λ‚Ό 수 μžˆλ‹€.