< K-Nearest Neighbor >
# K-Nearest Neighbors (K-NN)는 지도 학습 알고리즘 중 하나로, 학습 데이터셋 내에서 새로운 데이터 포인트와 가장 가까운 k개의 이웃을 찾아 해당 이웃들의 라벨 또는 값을 기반으로 예측을 수행
# Logistic Regression과 같은 분류형
< KNN 알고리즘 >
내 주위에 몇개의 이웃을 확인해 볼것인가를 결정한다. => K
새로운 데이터가 발생 시, Euclidean distance 에 의해서, 가장 가까운 K 개의 이웃을 택한다.
K 개의 이웃의 카테고리를 확인한다.
카테고리의 숫자가 많은 쪽으로, 새로운 데이터의 카테고리를 정해버린다.
< 예제문을 통해서 코딩해 보자>
import pandas as pd
# Logistic Regression 분류때 사용하였던 데이터를 가지고 연습해보자
df = pd.read_csv('../data/Social_Network_Ads.csv')
# NaN은 이전에 없던것이 확인된 데이터여서 스킵하고, 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
train_test_split(X, y , test_size=0.25, random_state=1)
[array([[-1.6862843 , -0.60910054],
[ 0.79705706, 0.53612887],
[-0.63563988, -1.04957339],
[ 0.89257019, -1.16703281],
[-0.25358736, -0.57973568],
[ 0.12846516, 0.77104772],
[-0.44461362, 1.27025028],
[ 1.08359645, 0.47739916],
[ 0.70154394, 0.27184516],
[ 0.60603081, -0.9027491 ],
[ 0.31949142, -0.31545197],
[ 0.41500455, -0.49164111],
[-0.25358736, 0.09565602],
[ 0.31949142, 0.06629116],
[-1.01769239, 0.77104772],
[-0.73115301, 1.35834485],
[ 0.31949142, -0.52100597],
[ 0.31949142, -0.52100597],
[-1.6862843 , 0.35993973],
[ 0.79705706, -0.84401939],
[-1.01769239, -1.13766796],
[-0.25358736, 0.03692631],
[ 1.08359645, -1.22576253],
[-0.25358736, 0.62422344],
[-0.0625611 , 0.15438573],
[-0.54012675, 1.38770971],
[-0.15807423, 0.15438573],
[-1.6862843 , 0.47739916],
[ 0.03295203, -0.57973568],
[ 1.08359645, -0.99084367],
[ 0.22397829, -0.13926283],
[-0.0625611 , 0.21311545],
[ 2.13424088, -0.69719511],
[ 0.98808332, 2.0043717 ],
[ 0.22397829, 0.03692631],
[-0.15807423, -0.19799255],
[-1.11320552, 0.33057487],
[-0.44461362, -1.13766796],
[ 0.31949142, -0.55037082],
[ 0.79705706, 0.12502088],
[-1.87731056, 0.35993973],
[-0.0625611 , -1.07893824],
[-0.25358736, -0.49164111],
[-0.92217926, -0.31545197],
[-0.0625611 , 0.03692631],
[-1.87731056, -0.75592482],
[ 0.03295203, -0.25672226],
[-0.25358736, -0.13926283],
[-0.63563988, -0.10989798],
[ 0.89257019, -1.3138571 ],
[ 0.41500455, 1.00596657],
[-1.78179743, -1.49004624],
[-1.59077117, 0.06629116],
[-0.92217926, -1.1083031 ],
[ 1.37013584, 2.35674998],
[ 1.46564897, 1.00596657],
[-1.20871865, 0.30121002],
[-1.11320552, 0.06629116],
[-1.11320552, -1.60750566],
[ 0.22397829, 2.12183112],
[-0.25358736, -0.93211396],
[ 1.84770149, 1.53453399],
[ 0.31949142, 0.50676401],
[-1.30423178, 0.56549373],
[-0.0625611 , -0.52100597],
[-0.25358736, 1.123426 ],
[-0.54012675, 0.88850715],
[-0.25358736, 2.26865541],
[-1.78179743, -1.28449224],
[ 0.79705706, 0.35993973],
[ 0.89257019, 1.03533143],
[ 0.70154394, -0.72655996],
[ 0.03295203, -0.55037082],
[-0.0625611 , 0.68295315],
[-0.82666613, -0.78528968],
[ 0.31949142, -1.16703281],
[ 0.22397829, -0.28608712],
[-1.01769239, 0.53612887],
[-1.01769239, 0.59485858],
[ 0.12846516, 0.03692631],
[-1.39974491, -1.22576253],
[ 1.27462271, 2.23929055],
[-1.39974491, -0.19799255],
[-0.0625611 , 0.30121002],
[ 0.22397829, -0.66783025],
[ 0.98808332, -1.19639767],
[-0.25358736, -0.31545197],
[-1.11320552, -1.16703281],
[ 0.12846516, 1.06469629],
[-0.92217926, 0.41866944],
[ 2.03872775, 0.18375059],
[ 0.22397829, -0.37418169],
[-0.25358736, 0.80041258],
[-1.11320552, 0.47739916],
[ 0.31949142, -0.19799255],
[-0.82666613, -0.66783025],
[-1.11320552, -1.54877595],
[ 1.94321462, -1.37258681],
[-0.73115301, -0.2273574 ],
[-1.20871865, -1.07893824],
[ 2.03872775, -1.19639767],
[-0.92217926, -0.96147882],
[ 0.12846516, 0.21311545],
[ 0.89257019, 1.09406114],
[ 1.37013584, 2.0043717 ],
[-0.82666613, -0.78528968],
[-1.01769239, 1.97500684],
[-1.30423178, -0.4329114 ],
[ 0.22397829, 0.06629116],
[-1.59077117, -1.5781408 ],
[ 1.84770149, -1.28449224],
[-0.73115301, 1.91627713],
[ 0.98808332, 1.44643942],
[ 0.22397829, -0.37418169],
[-0.25358736, 0.15438573],
[-0.92217926, -0.75592482],
[-0.0625611 , 0.12502088],
[-0.25358736, -1.25512738],
[-0.0625611 , 0.27184516],
[-0.54012675, 1.47580428],
[-0.82666613, 0.15438573],
[-0.73115301, -1.54877595],
[-1.20871865, 0.27184516],
[ 0.22397829, 1.09406114],
[-1.30423178, -1.49004624],
[ 0.31949142, 0.06629116],
[ 0.03295203, -0.25672226],
[-0.15807423, 2.18056084],
[ 0.41500455, -0.16862769],
[ 0.98808332, -1.07893824],
[ 0.98808332, -1.16703281],
[-1.39974491, -1.46068138],
[ 1.65667523, 1.76945285],
[-0.82666613, 2.29802026],
[ 0.41500455, -0.13926283],
[-1.01769239, -0.46227625],
[-0.0625611 , -0.4329114 ],
[-1.20871865, -1.40195167],
[ 2.03872775, 2.15119598],
[ 0.41500455, -0.46227625],
[-1.01769239, -1.54877595],
[ 0.89257019, -0.60910054],
[-0.92217926, -0.31545197],
[-0.44461362, -0.02180341],
[-0.63563988, -0.05116826],
[-0.25358736, -1.37258681],
[-0.25358736, -1.40195167],
[ 1.84770149, -1.07893824],
[-0.73115301, 1.09406114],
[ 0.79705706, -1.37258681],
[ 0.31949142, -0.28608712],
[-0.44461362, -0.84401939],
[-0.44461362, -0.28608712],
[ 0.89257019, -0.66783025],
[-0.92217926, -0.4329114 ],
[ 0.70154394, -1.1083031 ],
[-0.25358736, 0.15438573],
[ 2.03872775, -0.81465453],
[ 0.89257019, -0.57973568],
[ 0.89257019, -0.55037082],
[-0.25358736, 0.27184516],
[ 0.12846516, -0.81465453],
[ 0.22397829, 0.2424803 ],
[-1.39974491, -0.63846539],
[ 0.12846516, 0.15438573],
[-0.25358736, -0.37418169],
[ 1.65667523, -0.9027491 ],
[-0.63563988, 0.03692631],
[-0.92217926, 1.56389885],
[ 0.41500455, 0.30121002],
[ 0.22397829, -0.31545197],
[ 0.98808332, 0.12502088],
[-1.59077117, 0.53612887],
[ 1.46564897, 0.06629116],
[-1.01769239, -0.34481683],
[-0.25358736, -0.66783025],
[-0.92217926, 0.56549373],
[-1.11320552, 0.30121002],
[-0.54012675, 1.38770971],
[ 0.41500455, 0.15438573],
[-0.44461362, 2.32738512],
[ 0.31949142, 0.30121002],
[-0.63563988, -0.34481683],
[ 0.12846516, 1.88691227],
[-1.01769239, -1.46068138],
[-0.82666613, 0.38930459],
[-1.78179743, 0.00756145],
[-1.78179743, 0.4480343 ],
[-0.82666613, -0.25672226],
[ 0.12846516, 1.88691227],
[-0.63563988, 0.18375059],
[ 0.31949142, 0.06629116],
[-1.11320552, 0.30121002],
[ 0.22397829, -0.25672226],
[-0.25358736, -1.3138571 ],
[ 0.98808332, 0.77104772],
[ 0.41500455, 2.32738512],
[-0.92217926, 0.27184516],
[ 1.27462271, 1.88691227],
[-0.44461362, -1.22576253],
[ 0.41500455, -0.46227625],
[ 0.70154394, -1.40195167],
[-0.0625611 , -0.37418169],
[-0.34910049, 1.24088543],
[-0.25358736, -0.9027491 ],
[-1.49525804, -1.25512738],
[-0.25358736, 0.06629116],
[ 0.12846516, -0.31545197],
[-1.6862843 , 0.12502088],
[ 0.79705706, -1.1083031 ],
[-1.11320552, -0.78528968],
[-0.82666613, 0.38930459],
[ 1.65667523, 1.62262856],
[-0.0625611 , 2.23929055],
[ 0.70154394, 1.7988177 ],
[-0.44461362, -0.78528968],
[ 0.31949142, -0.72655996],
[ 0.41500455, 0.27184516],
[-0.0625611 , 1.97500684],
[-1.30423178, 0.41866944],
[-0.54012675, -1.51941109],
[ 1.75218836, 1.00596657],
[ 1.08359645, -0.9027491 ],
[-0.92217926, 0.4480343 ],
[ 1.46564897, 2.15119598],
[-0.73115301, 0.56549373],
[ 1.84770149, -0.28608712],
[ 0.22397829, 0.15438573],
[ 0.31949142, 0.27184516],
[-1.6862843 , 0.35993973],
[-1.49525804, -0.4329114 ],
[-0.25358736, 0.53612887],
[ 1.5611621 , 0.00756145],
[-0.63563988, -1.60750566],
[ 1.17910958, -0.99084367],
[ 0.41500455, -0.13926283],
[-0.34910049, -0.78528968],
[ 0.41500455, 1.123426 ],
[ 1.5611621 , -1.28449224],
[-1.78179743, -1.43131652],
[-0.15807423, -0.57973568],
[-0.0625611 , 0.00756145],
[-0.15807423, 1.65199342],
[ 0.89257019, -1.46068138],
[-0.73115301, 0.27184516],
[ 2.03872775, 0.38930459],
[-0.82666613, 0.30121002],
[ 0.51051768, 1.24088543],
[ 0.03295203, 0.30121002],
[ 1.94321462, 2.18056084],
[-0.15807423, 1.62262856],
[ 1.08359645, -1.22576253],
[-1.87731056, -0.52100597],
[-0.73115301, -1.60750566],
[ 2.03872775, 0.53612887],
[-1.01769239, -0.37418169],
[ 0.03295203, 0.03692631],
[-0.63563988, 0.56549373],
[ 0.89257019, -1.04957339],
[-0.63563988, 0.12502088],
[-0.0625611 , 0.06629116],
[ 0.22397829, -0.37418169],
[ 2.03872775, 1.76945285],
[ 1.08359645, -0.13926283],
[ 0.98808332, 0.59485858],
[ 0.79705706, -1.40195167],
[ 1.5611621 , 1.00596657],
[-0.25358736, -1.46068138],
[ 1.08359645, 2.09246627],
[ 0.98808332, -0.84401939],
[-0.25358736, -0.4329114 ],
[-0.15807423, -0.28608712],
[-0.54012675, 2.35674998],
[-1.87731056, -0.05116826],
[-1.30423178, -0.4329114 ],
[ 0.41500455, 0.59485858],
[ 0.03295203, -0.31545197],
[ 2.13424088, 1.123426 ],
[-1.49525804, -0.19799255],
[-1.30423178, -1.1083031 ],
[ 0.79705706, -0.31545197],
[ 0.98808332, 1.88691227],
[ 0.31949142, 0.06629116],
[ 1.17910958, -0.75592482],
[ 0.03295203, 0.03692631],
[-1.30423178, -1.37258681],
[-0.25358736, -0.25672226],
[-0.0625611 , 0.30121002],
[-1.30423178, -1.25512738],
[-1.11320552, 0.41866944],
[-0.34910049, -1.3138571 ],
[-0.15807423, -0.46227625],
[-1.59077117, -0.05116826],
[ 0.31949142, 0.03692631],
[ 2.13424088, -0.81465453],
[ 1.37013584, 0.59485858],
[-1.6862843 , -1.37258681],
[ 1.27462271, -1.37258681],
[ 0.79705706, 0.27184516],
[-0.73115301, -0.60910054]]),
array([[-0.15807423, -1.07893824],
[ 0.12846516, -0.25672226],
[-0.15807423, 1.41707457],
[ 0.12846516, 1.53453399],
[-1.11320552, 1.41707457],
[ 0.03295203, -0.13926283],
[-1.6862843 , -0.99084367],
[ 1.08359645, 0.56549373],
[-0.63563988, -1.51941109],
[ 0.98808332, 2.09246627],
[-0.34910049, 0.06629116],
[ 0.12846516, 0.09565602],
[-0.25358736, 0.06629116],
[ 0.98808332, 1.7988177 ],
[ 1.46564897, 0.35993973],
[ 1.75218836, 1.85754742],
[ 2.13424088, 0.38930459],
[-1.01769239, -0.34481683],
[-0.92217926, 0.50676401],
[ 2.13424088, 0.94723686],
[ 0.22397829, 0.15438573],
[ 1.17910958, 0.53612887],
[ 0.60603081, 2.03373655],
[ 0.89257019, -0.78528968],
[ 0.70154394, -1.28449224],
[-1.11320552, -1.60750566],
[ 1.94321462, -0.66783025],
[ 1.08359645, 0.12502088],
[ 1.46564897, -1.04957339],
[ 1.37013584, 1.29961514],
[ 0.12846516, -0.81465453],
[-1.78179743, 0.18375059],
[-1.87731056, 0.47739916],
[ 1.84770149, 0.12502088],
[-1.01769239, 0.41866944],
[-0.73115301, 0.30121002],
[-1.49525804, -1.51941109],
[-0.54012675, 0.47739916],
[ 1.17910958, -1.46068138],
[-1.78179743, -1.3138571 ],
[ 0.89257019, 2.18056084],
[ 1.94321462, 0.917872 ],
[-0.34910049, 1.32898 ],
[-1.39974491, -0.10989798],
[ 1.75218836, -0.28608712],
[-0.63563988, 1.41707457],
[ 0.98808332, -1.02020853],
[ 0.89257019, 1.27025028],
[ 0.12846516, 0.27184516],
[ 1.37013584, -0.93211396],
[-1.30423178, -0.34481683],
[-0.0625611 , -0.49164111],
[ 0.41500455, 0.30121002],
[ 0.79705706, -1.22576253],
[ 0.41500455, 0.09565602],
[-0.0625611 , -0.2273574 ],
[ 2.13424088, -0.81465453],
[-0.15807423, -0.52100597],
[ 1.94321462, 0.74168287],
[ 0.51051768, 1.74008799],
[-1.01769239, 0.56549373],
[-1.39974491, 0.35993973],
[ 0.03295203, 1.24088543],
[-0.25358736, -0.57973568],
[-0.15807423, 0.85914229],
[-0.0625611 , 2.18056084],
[-1.11320552, -1.02020853],
[ 0.41500455, 0.00756145],
[ 0.51051768, 1.85754742],
[ 0.03295203, -0.57973568],
[ 0.79705706, 0.77104772],
[-0.25358736, -0.75592482],
[ 0.03295203, 1.27025028],
[ 0.12846516, 0.03692631],
[-1.11320552, -0.52100597],
[ 1.5611621 , 1.123426 ],
[-0.44461362, -0.55037082],
[-1.11320552, -1.5781408 ],
[-0.73115301, 0.50676401],
[-0.25358736, -0.28608712],
[-0.82666613, -1.22576253],
[ 0.70154394, -1.40195167],
[ 0.79705706, 1.38770971],
[-0.54012675, -1.51941109],
[-1.49525804, 0.33057487],
[-1.20871865, 0.50676401],
[ 0.98808332, -1.07893824],
[-0.25358736, -0.34481683],
[ 0.89257019, -1.37258681],
[-1.11320552, -1.1083031 ],
[-0.54012675, 1.91627713],
[ 2.13424088, -1.04957339],
[ 1.37013584, -1.43131652],
[ 0.03295203, -0.4329114 ],
[-1.20871865, 0.59485858],
[ 1.94321462, -0.93211396],
[ 1.08359645, 0.53612887],
[-0.0625611 , 0.2424803 ],
[-0.25358736, 0.21311545],
[-0.34910049, -0.78528968]]),
82 0
367 1
179 0
27 1
89 0
..
255 1
72 0
396 1
235 1
37 0
Name: Purchased, Length: 300, dtype: int64,
398 0
125 0
328 1
339 1
172 0
..
300 1
277 1
289 1
260 0
173 0
Name: Purchased, Length: 100, dtype: int64]
# 변수로 지정하여 메모리에 업로드
X_train, X_test, y_train, y_test = train_test_split(X, y , test_size=0.25, random_state=1)
# 로지스틱 리그레이션 과 K-NN 중 이번엔 K-NN classifier
# regressor는 수치 예측 => 데이터를 확인하고 목적에 따라 수치형인지 분리형인지 구분 후 import 주의
from sklearn.neighbors import KNeighborsClassifier
# 깡통 인공지능 생성
# () 파라미터안에 n_neighbors 를 적지 않으면 디폴트 5로 동작됨
# n_neighbors 파라미터는 K-NN 알고리즘에서 사용할 이웃의 수를 지정하는 값
# n_neighbors 값은 일반적으로 홀수로 설정하는 것이 좋은데, 이웃의 수가 짝수일 경우 동점이 나올 가능성이 있어 분류 결과가 명확하지 않을 수 있기 때문
classifier = KNeighborsClassifier(n_neighbors= 7)
# 학습 .fit
classifier.fit(X_train, y_train)
# 에측 실행 .predict
classifier.predict(X_test)
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)
# 예측값이므로 pred로 저장
y_pred = classifier.predict(X_test)
# 수치형은 confusion score , 분류형은 매트릭스
from sklearn.metrics import confusion_matrix, accuracy_score
confusion_matrix(y_test, y_pred)
array([[49, 9],
[ 3, 39]], dtype=int64)
cm = confusion_matrix(y_test, y_pred)
# 직접 정확도를 계산해보자. 제대로 맞춘 데이터의 개수를 더하고, 전체데이터 계수로 나누면 된다.
(49 + 39) / cm.sum()
0.88
ㄴ 88%의 정확도
# 명령어로 확인
accuracy_score(y_test, y_pred)
0.88