MACHINE LEARNING/Machine Learning Library

ML(머신러닝) : Decision Tree 개념 정리 (sklearn.svm 의 SVC 인공지능 생성)

신강희 2024. 4. 23. 17:39
728x90

< Decision Tree >

# Decision Tree(의사결정트리)는 지도 학습 알고리즘 중 하나로, 데이터를 특정 기준을 기반으로 여러 노드로 분할하여 결정을 내리는 트리 구조를 사용

 

계속하여 이것인지 저것인지 결정한다.

 

 

 

< 예제문을 통해 코딩해 보자 >

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

 

# 이전 게시글에서 사용한 데이터와 동일한 데이터로 실습

df = pd.read_csv('../data/Social_Network_Ads.csv')

 

# X,y 분류

y = df['Purchased']

X = df.loc[ : , 'Age' : 'EstimatedSalary' ]

 

# 피쳐 스케일링

from sklearn.preprocessing import StandardScaler

 

scaler_X = StandardScaler()

 

X = scaler_X.fit_transform(X)

X

array([[-1.78179743, -1.49004624],
       [-0.25358736, -1.46068138],
       [-1.11320552, -0.78528968],
       [-1.01769239, -0.37418169],
       [-1.78179743,  0.18375059],
       [-1.01769239, -0.34481683],
       [-1.01769239,  0.41866944],
       [-0.54012675,  2.35674998],
       [-1.20871865, -1.07893824],
       [-0.25358736, -0.13926283],
       [-1.11320552,  0.30121002],
       [-1.11320552, -0.52100597],
       [-1.6862843 ,  0.47739916],
       [-0.54012675, -1.51941109],
       [-1.87731056,  0.35993973],
       [-0.82666613,  0.30121002],
       [ 0.89257019, -1.3138571 ],
       [ 0.70154394, -1.28449224],
       [ 0.79705706, -1.22576253],
       [ 0.98808332, -1.19639767],
       [ 0.70154394, -1.40195167],
       [ 0.89257019, -0.60910054],
       [ 0.98808332, -0.84401939],
       [ 0.70154394, -1.40195167],
       [ 0.79705706, -1.37258681],
       [ 0.89257019, -1.46068138],
       [ 1.08359645, -1.22576253],
       [ 0.89257019, -1.16703281],
       [-0.82666613, -0.78528968],
       [-0.63563988, -1.51941109],
       [-0.63563988,  0.12502088],
       [-1.01769239,  1.97500684],
       [-1.59077117, -1.5781408 ],
       [-0.92217926, -0.75592482],
       [-1.01769239,  0.59485858],
       [-0.25358736, -1.25512738],
       [-0.44461362, -1.22576253],
       [-0.73115301, -0.60910054],
       [-1.11320552,  0.06629116],
       [-1.01769239, -1.13766796],
       [-1.01769239, -1.54877595],
       [-0.44461362, -0.55037082],
       [-0.25358736,  1.123426  ],
       [-0.73115301, -1.60750566],
       [-0.92217926,  0.41866944],
       [-1.39974491, -1.46068138],
       [-1.20871865,  0.27184516],
       [-1.01769239, -0.46227625],
       [-0.73115301,  1.91627713],
       [-0.63563988,  0.56549373],
       [-1.30423178, -1.1083031 ],
       [-1.87731056, -0.75592482],
       [-0.82666613,  0.38930459],
       [-0.25358736, -1.37258681],
       [-1.01769239, -0.34481683],
       [-1.30423178, -0.4329114 ],
       [-1.39974491, -0.63846539],
       [-0.92217926,  0.27184516],
       [-1.49525804, -1.51941109],
       [-0.54012675,  1.38770971],
       [-1.01769239, -1.46068138],
       [-1.20871865,  0.50676401],
       [-1.39974491, -0.10989798],
       [-0.54012675,  1.47580428],
       [ 2.03872775,  0.38930459],
       [-1.30423178, -0.34481683],
       [-1.30423178, -1.49004624],
       [-1.39974491,  0.35993973],
       [-1.49525804, -0.19799255],
       [-0.63563988, -0.05116826],
       [-1.20871865,  0.30121002],
       [-1.30423178, -1.25512738],
       [-1.6862843 , -1.37258681],
       [-0.44461362,  1.27025028],
       [-0.54012675, -1.51941109],
       [-0.34910049,  1.24088543],
       [-1.87731056, -0.52100597],
       [-1.49525804, -1.25512738],
       [-0.92217926,  0.50676401],
       [-1.11320552, -1.54877595],
       [-0.73115301,  0.30121002],
       [ 0.12846516, -0.81465453],
       [-1.6862843 , -0.60910054],
       [-0.25358736,  0.53612887],
       [-0.73115301, -0.2273574 ],
       [-0.63563988,  1.41707457],
       [-1.30423178, -0.4329114 ],
       [-0.92217926,  0.4480343 ],
       [-1.11320552,  0.33057487],
       [-0.25358736, -0.57973568],
       [-1.49525804,  0.33057487],
       [-0.73115301,  1.35834485],
       [-1.11320552, -1.60750566],
       [-0.82666613, -1.22576253],
       [-0.82666613,  0.38930459],
       [-0.25358736, -0.75592482],
       [-0.25358736, -1.3138571 ],
       [-0.92217926,  1.56389885],
       [-0.25358736,  0.09565602],
       [-0.92217926, -0.96147882],
       [-1.01769239,  0.53612887],
       [-0.92217926, -0.31545197],
       [-0.54012675,  0.47739916],
       [-0.44461362,  2.32738512],
       [-1.78179743, -1.43131652],
       [-1.59077117,  0.06629116],
       [-1.11320552, -1.02020853],
       [-1.01769239,  0.56549373],
       [-1.11320552,  0.47739916],
       [ 0.03295203,  0.30121002],
       [ 0.12846516,  0.03692631],
       [-0.0625611 ,  0.03692631],
       [ 0.03295203, -0.25672226],
       [-0.0625611 , -0.4329114 ],
       [ 0.41500455,  0.30121002],
       [ 0.22397829, -0.37418169],
       [-0.25358736,  0.15438573],
       [-0.15807423, -0.52100597],
       [ 0.22397829, -0.31545197],
       [ 0.31949142, -0.31545197],
       [-0.15807423,  0.15438573],
       [-0.0625611 ,  0.06629116],
       [ 0.22397829,  0.15438573],
       [-0.25358736, -0.49164111],
       [ 0.31949142, -0.55037082],
       [ 0.12846516, -0.25672226],
       [ 0.41500455, -0.13926283],
       [-1.11320552, -1.1083031 ],
       [-0.73115301, -1.54877595],
       [-1.11320552,  0.41866944],
       [-0.63563988, -0.34481683],
       [-0.44461362, -1.13766796],
       [-0.73115301,  0.50676401],
       [-1.59077117, -0.05116826],
       [-0.92217926, -0.4329114 ],
       [-1.39974491, -0.19799255],
       [-1.6862843 ,  0.35993973],
       [-0.73115301,  1.09406114],
       [-0.92217926, -0.31545197],
       [-1.78179743, -1.3138571 ],
       [-1.78179743,  0.4480343 ],
       [-1.87731056, -0.05116826],
       [-0.25358736, -0.31545197],
       [-0.73115301,  0.56549373],
       [-0.34910049, -1.3138571 ],
       [-1.30423178,  0.56549373],
       [-1.01769239,  0.77104772],
       [ 0.31949142, -1.16703281],
       [-0.82666613, -0.25672226],
       [-1.6862843 ,  0.12502088],
       [-1.11320552, -1.60750566],
       [ 0.31949142, -0.72655996],
       [-0.63563988,  0.18375059],
       [-0.15807423, -0.57973568],
       [ 0.22397829, -0.66783025],
       [-0.63563988, -1.60750566],
       [ 0.79705706, -0.31545197],
       [-0.82666613,  0.15438573],
       [-1.11320552, -1.16703281],
       [-0.54012675,  1.91627713],
       [-0.54012675,  0.88850715],
       [-1.20871865,  0.59485858],
       [-0.0625611 , -1.07893824],
       [-0.25358736, -0.93211396],
       [-0.44461362, -0.02180341],
       [-1.87731056,  0.47739916],
       [-1.49525804, -0.4329114 ],
       [-0.25358736,  0.03692631],
       [-0.82666613,  2.29802026],
       [-0.82666613, -0.66783025],
       [-1.59077117,  0.53612887],
       [-0.34910049,  1.32898   ],
       [-1.11320552,  1.41707457],
       [-0.34910049, -0.78528968],
       [-0.34910049,  0.06629116],
       [-1.39974491, -1.22576253],
       [-0.25358736, -0.66783025],
       [-1.20871865, -1.40195167],
       [-1.30423178, -1.37258681],
       [-0.63563988, -1.04957339],
       [-1.11320552, -1.5781408 ],
       [-0.63563988,  0.03692631],
       [-0.54012675,  1.38770971],
       [-0.44461362, -0.78528968],
       [-0.44461362, -0.28608712],
       [-0.63563988, -0.10989798],
       [-1.6862843 ,  0.35993973],
       [-0.44461362, -0.84401939],
       [-0.25358736,  0.06629116],
       [-0.92217926, -1.1083031 ],
       [-1.30423178,  0.41866944],
       [-1.78179743, -1.28449224],
       [-0.82666613, -0.78528968],
       [-1.78179743,  0.00756145],
       [-0.92217926,  0.56549373],
       [-0.34910049, -0.78528968],
       [-0.73115301,  0.27184516],
       [-1.6862843 , -0.99084367],
       [-1.11320552,  0.30121002],
       [-0.25358736, -1.40195167],
       [-0.25358736, -0.9027491 ],
       [ 1.08359645,  0.12502088],
       [ 0.12846516,  1.88691227],
       [ 0.31949142,  0.03692631],
       [ 1.94321462,  0.917872  ],
       [ 0.89257019, -0.66783025],
       [ 1.65667523,  1.76945285],
       [ 1.37013584,  1.29961514],
       [ 0.22397829,  2.12183112],
       [ 0.79705706, -1.40195167],
       [ 0.98808332,  0.77104772],
       [ 1.37013584,  2.35674998],
       [ 2.03872775, -0.81465453],
       [-0.25358736, -0.34481683],
       [ 0.89257019, -0.78528968],
       [ 2.13424088,  1.123426  ],
       [ 1.08359645, -0.13926283],
       [ 0.22397829,  0.2424803 ],
       [ 0.79705706,  0.77104772],
       [ 2.03872775,  2.15119598],
       [ 0.31949142,  0.30121002],
       [-0.25358736,  0.62422344],
       [-0.0625611 ,  2.18056084],
       [ 2.13424088,  0.94723686],
       [-0.25358736, -0.28608712],
       [-0.0625611 , -0.49164111],
       [-0.15807423,  1.65199342],
       [ 1.75218836,  1.85754742],
       [ 0.22397829,  0.06629116],
       [ 0.41500455,  0.30121002],
       [-0.25358736,  2.26865541],
       [ 0.12846516, -0.81465453],
       [ 0.22397829,  1.09406114],
       [ 1.08359645,  0.47739916],
       [ 0.03295203,  1.24088543],
       [ 0.79705706,  0.27184516],
       [ 0.22397829, -0.37418169],
       [-0.0625611 ,  0.30121002],
       [ 0.79705706,  0.35993973],
       [ 1.46564897,  2.15119598],
       [ 0.41500455,  2.32738512],
       [ 0.03295203, -0.31545197],
       [ 1.17910958,  0.53612887],
       [ 1.75218836,  1.00596657],
       [ 0.31949142,  0.06629116],
       [ 1.27462271,  2.23929055],
       [-0.25358736, -0.57973568],
       [ 1.84770149,  1.53453399],
       [ 0.31949142, -0.52100597],
       [-0.25358736,  0.80041258],
       [ 0.60603081, -0.9027491 ],
       [-0.0625611 , -0.52100597],
       [ 0.98808332,  1.88691227],
       [-0.0625611 ,  2.23929055],
       [ 1.17910958, -0.75592482],
       [ 1.37013584,  0.59485858],
       [ 0.31949142,  0.06629116],
       [ 0.22397829, -0.37418169],
       [ 1.94321462,  0.74168287],
       [ 0.70154394,  1.7988177 ],
       [-0.25358736,  0.21311545],
       [-0.15807423,  2.18056084],
       [ 1.65667523,  1.62262856],
       [-0.25358736,  0.06629116],
       [ 0.98808332,  0.59485858],
       [ 0.41500455,  1.123426  ],
       [ 0.22397829,  0.15438573],
       [-0.0625611 ,  0.12502088],
       [ 0.89257019,  2.18056084],
       [ 0.22397829, -0.25672226],
       [ 0.51051768,  1.85754742],
       [ 2.03872775,  0.18375059],
       [ 2.13424088, -0.81465453],
       [ 0.12846516,  1.06469629],
       [ 1.84770149, -1.28449224],
       [ 1.84770149,  0.12502088],
       [ 0.03295203,  0.03692631],
       [ 1.08359645,  0.53612887],
       [ 1.37013584, -0.93211396],
       [ 1.17910958, -0.99084367],
       [ 2.03872775,  0.53612887],
       [-0.25358736, -0.25672226],
       [-0.0625611 ,  0.00756145],
       [ 1.37013584, -1.43131652],
       [ 0.98808332,  2.09246627],
       [-0.0625611 ,  0.68295315],
       [-0.0625611 , -0.2273574 ],
       [ 0.98808332,  2.0043717 ],
       [ 0.31949142,  0.27184516],
       [-0.0625611 ,  0.2424803 ],
       [ 0.12846516,  1.88691227],
       [ 1.08359645,  0.56549373],
       [ 1.65667523, -0.9027491 ],
       [-0.0625611 ,  0.21311545],
       [-0.25358736, -0.37418169],
       [-0.15807423, -0.19799255],
       [ 0.41500455,  0.09565602],
       [ 0.51051768,  1.24088543],
       [ 0.70154394,  0.27184516],
       [ 0.79705706,  1.38770971],
       [ 1.94321462, -0.93211396],
       [ 0.98808332,  0.12502088],
       [-0.0625611 ,  1.97500684],
       [-0.0625611 ,  0.27184516],
       [ 0.22397829, -0.28608712],
       [ 0.41500455, -0.46227625],
       [ 1.27462271,  1.88691227],
       [ 0.89257019,  1.27025028],
       [-0.15807423,  1.62262856],
       [ 0.03295203, -0.57973568],
       [ 0.41500455,  0.00756145],
       [ 0.12846516,  0.77104772],
       [ 0.03295203, -0.57973568],
       [ 1.08359645,  2.09246627],
       [ 0.12846516,  0.27184516],
       [ 0.12846516,  0.15438573],
       [ 1.5611621 ,  1.00596657],
       [-0.25358736, -0.4329114 ],
       [ 0.70154394, -1.1083031 ],
       [-0.15807423, -0.28608712],
       [ 1.37013584,  2.0043717 ],
       [ 1.46564897,  0.35993973],
       [ 0.31949142, -0.52100597],
       [ 0.98808332, -1.16703281],
       [ 0.98808332,  1.7988177 ],
       [ 0.31949142, -0.28608712],
       [ 0.31949142,  0.06629116],
       [ 0.41500455,  0.15438573],
       [-0.15807423,  1.41707457],
       [ 0.89257019,  1.09406114],
       [ 0.03295203, -0.55037082],
       [ 0.98808332,  1.44643942],
       [ 0.41500455, -0.13926283],
       [ 0.22397829, -0.13926283],
       [ 1.84770149, -0.28608712],
       [-0.15807423, -0.46227625],
       [ 1.94321462,  2.18056084],
       [-0.25358736,  0.27184516],
       [ 0.03295203, -0.4329114 ],
       [ 0.12846516,  1.53453399],
       [ 1.46564897,  1.00596657],
       [-0.25358736,  0.15438573],
       [ 0.03295203, -0.13926283],
       [ 0.89257019, -0.55037082],
       [ 0.89257019,  1.03533143],
       [ 0.31949142, -0.19799255],
       [ 1.46564897,  0.06629116],
       [ 1.5611621 ,  1.123426  ],
       [ 0.12846516,  0.21311545],
       [ 0.03295203, -0.25672226],
       [ 0.03295203,  1.27025028],
       [-0.0625611 ,  0.15438573],
       [ 0.41500455,  0.59485858],
       [-0.0625611 , -0.37418169],
       [-0.15807423,  0.85914229],
       [ 2.13424088, -1.04957339],
       [ 1.5611621 ,  0.00756145],
       [ 0.31949142,  0.06629116],
       [ 0.22397829,  0.03692631],
       [ 0.41500455, -0.46227625],
       [ 0.51051768,  1.74008799],
       [ 1.46564897, -1.04957339],
       [ 0.89257019, -0.57973568],
       [ 0.41500455,  0.27184516],
       [ 0.41500455,  1.00596657],
       [ 2.03872775, -1.19639767],
       [ 1.94321462, -0.66783025],
       [ 0.79705706,  0.53612887],
       [ 0.03295203,  0.03692631],
       [ 1.5611621 , -1.28449224],
       [ 2.13424088, -0.69719511],
       [ 2.13424088,  0.38930459],
       [ 0.12846516,  0.09565602],
       [ 2.03872775,  1.76945285],
       [-0.0625611 ,  0.30121002],
       [ 0.79705706, -1.1083031 ],
       [ 0.79705706,  0.12502088],
       [ 0.41500455, -0.49164111],
       [ 0.31949142,  0.50676401],
       [ 1.94321462, -1.37258681],
       [ 0.41500455, -0.16862769],
       [ 0.98808332, -1.07893824],
       [ 0.60603081,  2.03373655],
       [ 1.08359645, -1.22576253],
       [ 1.84770149, -1.07893824],
       [ 1.75218836, -0.28608712],
       [ 1.08359645, -0.9027491 ],
       [ 0.12846516,  0.03692631],
       [ 0.89257019, -1.04957339],
       [ 0.98808332, -1.02020853],
       [ 0.98808332, -1.07893824],
       [ 0.89257019, -1.37258681],
       [ 0.70154394, -0.72655996],
       [ 2.13424088, -0.81465453],
       [ 0.12846516, -0.31545197],
       [ 0.79705706, -0.84401939],
       [ 1.27462271, -1.37258681],
       [ 1.17910958, -1.46068138],
       [-0.15807423, -1.07893824],
       [ 1.08359645, -0.99084367]])

 

# 트레이닝, 테스트 용 데이터 분리

from sklearn.model_selection import train_test_split

 

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)

 

# Decision Tree용 인공지능 생성

from sklearn.tree import DecisionTreeClassifier

 

classifier = DecisionTreeClassifier(random_state= 1)

 

# 학습 .fit()

classifier.fit(X_train, y_train)

 

# 학습된 인공지능으로 에측하여 변수로 메모리에 업로드 .predict()

y_pred = classifier.predict(X_test)

y_pred

array([0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1,
       1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0,
       1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1,
       0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0,
       1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0], dtype=int64)

 

# Confusion_matrix로 결과를 확인해 보자

from sklearn.metrics import confusion_matrix, accuracy_score

 

confusion_matrix(y_test, y_pred)

array([[50,  8],
       [ 8, 34]], dtype=int64)

 

accuracy_score(y_test, y_pred)

0.84

 

다음 게시글로 계속

반응형