< Support Vector Machine >
# Support Vector Machine (SVM)은 지도 학습 알고리즘 중 하나로, 데이터를 분류하기 위한 최적의 결정 경계(decision boundary)를 찾는 것을 목표로 한다.
아래의 3개 의 선 모두, 분류하는 선이 모두 맞다. 그러면 어떤것이 더 정확할까?
분류선에 가장 가까운 데이터들을, 가장 큰 마진(margin)으로 설정하는 선으로 결정하자.
분류선을 Maximum Margin Classifer 라고 한다.
SVM은 다른 머신러닝 알고리즘과 비교해서 무엇이 특별한가?
사과인지 오렌지인지 분석하는 문제
일반적인 사과와 오렌지들은, 클래서파이어에서 멀리 분포한다.
정상적이지 않은 것들, 즉 구분하기 힘든 부분에 있는 것들은 클래서파이어 근처에 있게 되며,
이 데이터들이 레이블링 되어 있으므로, Margin을 최대화 하여 분류하기 때문에, 특이한 것들까지 잘 분류하는 문제에 SVM 이 최고다.
< 예제를 통하여 코딩해 보자 >
# 이전 예제문에서 사용했었던 동일한 데이터로 구매의사 분석을 SVM을 통해서 해보자
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
df = pd.read_csv('../data/Social_Network_Ads.csv')
# X,y 분류
y = df['Purchased']
y
0 0
1 0
2 0
3 0
4 0
..
395 1
396 1
397 1
398 0
399 1
Name: Purchased, Length: 400, dtype: int64
X = df.iloc [ : , [2,3]]
X
# 문자열 데이터는 없으므로 인코딩은 제외하고 피쳐 스케일링 진행
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]])
# train, test용 데이터 분리
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)
# sklearn의 SVC import
from sklearn.svm import SVC
# 구글에 python sklearn svc 검색 식 커널 디폴트는 rbf 인것 확인
# 파라미터안에 커널 종류에 따라 다른 데이터값이 도출되므로 실제 작업할때는 설명서를 참고해서 여러 커널을 넣어보는것이 좋다
# 다른 커널을 사용하고 싶다면 SVC(kernel='linear') 이런식으로 파라미터 안에 커널 작성
# default 커널인 rbf로 깡통 인공지능 생성
classifier = SVC()
# 학습 .fit
classifier.fit(X_train, y_train)
# 예측 실행 후 예측 결과 변수로 메모리에 업로드 .predict
y_pred = classifier.predict(X_test)
y_pred
array([0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1,
1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0,
1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1,
0, 0, 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)
# 분류 문제이므로 컨퓨전 메트릭스 사용
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
cm
array([[49, 9],
[ 3, 39]], dtype=int64)
# 정확도 확인
accuracy_score(y_test, y_pred)
0.88