-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path기계학습기초_4주차과제.py
35 lines (26 loc) · 1.03 KB
/
기계학습기초_4주차과제.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
"""백민하201704045.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1RUdNBwZ1A8GLPdlDI5CQmupQdPqlfJ7i
"""
import matplotlib.pyplot as plt
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
fst_grp = [1, 2, 3, 4, 5]
snd_grp = [6, 7, 8, 9, 10]
num_grp = fst_grp + snd_grp
num_data = np.column_stack(([num_grp]))
num_target = np.concatenate((np.ones(5), np.zeros(5)))
kn3s = []
kn5s = []
for i in range(0, 1000):
trn_ipt, tst_ipt, trn_tgt, tst_tgt = train_test_split(num_data, num_target, test_size = 0.4, stratify=num_target)
kn3 = KNeighborsClassifier(3)
kn3.fit(trn_ipt, trn_tgt)
kn3s.append(kn3.score(tst_ipt, tst_tgt))
kn5 = KNeighborsClassifier(5)
kn5.fit(trn_ipt, trn_tgt)
kn5s.append(kn5.score(tst_ipt, tst_tgt))
print(f'k=3일때 평균: {np.mean(kn3s)} / 표준편차: {np.std(kn3s)}\nk=5일때 평균: {np.mean(kn5s)} / 표준편차: {np.std(kn5s)}')