메이플의 개발 스토리
[ML] 2-1 훈련 세트와 테스트 세트 본문
In [ ]:
fish_length = [25.4, 26.3, 26.5, 29.0, 29.0, 29.7, 29.7, 30.0, 30.0, 30.7, 31.0, 31.0,
31.5, 32.0, 32.0, 32.0, 33.0, 33.0, 33.5, 33.5, 34.0, 34.0, 34.5, 35.0,
35.0, 35.0, 35.0, 36.0, 36.0, 37.0, 38.5, 38.5, 39.5, 41.0, 41.0, 9.8,
10.5, 10.6, 11.0, 11.2, 11.3, 11.8, 11.8, 12.0, 12.2, 12.4, 13.0, 14.3, 15.0]
fish_weight = [242.0, 290.0, 340.0, 363.0, 430.0, 450.0, 500.0, 390.0, 450.0, 500.0, 475.0, 500.0,
500.0, 340.0, 600.0, 600.0, 700.0, 700.0, 610.0, 650.0, 575.0, 685.0, 620.0, 680.0,
700.0, 725.0, 720.0, 714.0, 850.0, 1000.0, 920.0, 955.0, 925.0, 975.0, 950.0, 6.7,
7.5, 7.0, 9.7, 9.8, 8.7, 10.0, 9.9, 9.8, 12.2, 13.4, 12.2, 19.7, 19.9]
In [ ]:
fish_data = [[l, w] for l, w in zip(fish_length, fish_weight)]
fish_target = [1]*35 + [0]*14
In [ ]:
print(fish_data)
print(fish_target)
[[25.4, 242.0], [26.3, 290.0], [26.5, 340.0], [29.0, 363.0], [29.0, 430.0], [29.7, 450.0], [29.7, 500.0], [30.0, 390.0], [30.0, 450.0], [30.7, 500.0], [31.0, 475.0], [31.0, 500.0], [31.5, 500.0], [32.0, 340.0], [32.0, 600.0], [32.0, 600.0], [33.0, 700.0], [33.0, 700.0], [33.5, 610.0], [33.5, 650.0], [34.0, 575.0], [34.0, 685.0], [34.5, 620.0], [35.0, 680.0], [35.0, 700.0], [35.0, 725.0], [35.0, 720.0], [36.0, 714.0], [36.0, 850.0], [37.0, 1000.0], [38.5, 920.0], [38.5, 955.0], [39.5, 925.0], [41.0, 975.0], [41.0, 950.0], [9.8, 6.7], [10.5, 7.5], [10.6, 7.0], [11.0, 9.7], [11.2, 9.8], [11.3, 8.7], [11.8, 10.0], [11.8, 9.9], [12.0, 9.8], [12.2, 12.2], [12.4, 13.4], [13.0, 12.2], [14.3, 19.7], [15.0, 19.9]] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
In [ ]:
In [ ]:
from sklearn.neighbors import KNeighborsClassifier
kn = KNeighborsClassifier()
In [ ]:
print(fish_data[4])
print(fish_data[0:5])
print(fish_data[:5])
print(fish_data[44:])
[29.0, 430.0] [[25.4, 242.0], [26.3, 290.0], [26.5, 340.0], [29.0, 363.0], [29.0, 430.0]] [[25.4, 242.0], [26.3, 290.0], [26.5, 340.0], [29.0, 363.0], [29.0, 430.0]] [[12.2, 12.2], [12.4, 13.4], [13.0, 12.2], [14.3, 19.7], [15.0, 19.9]]
In [ ]:
train_input = fish_data[:35]
train_target = fish_target[:35]
test_input = fish_data[35:]
test_target = fish_target[35:]
In [ ]:
kn = kn.fit(train_input, train_target)
kn.score(test_input, test_target)
Out[ ]:
0.0
In [ ]:
import numpy as np
In [ ]:
fish_data = [[25.4, 242.0], [26.3, 290.0], [14.3, 19.7], [15.0, 19.9]]
fish_target = [1, 1, 0, 0]
input_arr = np.array(fish_data)
target_arr = np.array(fish_target)
In [ ]:
print(fish_data)
[[25.4, 242.0], [26.3, 290.0], [14.3, 19.7], [15.0, 19.9]]
In [ ]:
print(input_arr.shape)
(4, 2)
In [ ]:
np.random.seed(42)
index = np.arange(49)
np.random.shuffle(index)
In [ ]:
print(index)
Out[ ]:
array([13, 45, 47, 44, 17, 27, 26, 25, 31, 19, 12, 4, 34, 8, 3, 6, 40, 41, 46, 15, 9, 16, 24, 33, 30, 0, 43, 32, 5, 29, 11, 36, 1, 21, 2, 37, 35, 23, 39, 10, 22, 18, 48, 20, 7, 42, 14, 28, 38])
In [ ]:
print(input_arr[[1, 3]])
Out[ ]:
array([[ 26.3, 290. ], [ 29. , 363. ]])
In [ ]:
train_input = input_arr[index[:35]]
train_target = target_arr[index[:35]]
In [ ]:
print(input_arr[13], train_input[0])
[ 32. 340.] [ 32. 340.]
In [ ]:
test_input = input_arr[index[35:]]
test_target = target_arr[index[35:]]
In [ ]:
import matplotlib.pyplot as plt
plt.scatter(train_input[:,0], train_input[:,1])
plt.scatter(test_input[:,0], test_input[:,1])
plt.xlabel('length')
plt.ylabel('weight')
plt.show()
In [ ]:
kn = kn.fit(train_input, train_target)
In [ ]:
kn.score(test_input, test_target)
Out[ ]:
1.0
In [ ]:
kn.predict(test_input)
Out[ ]:
array([0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0])
In [ ]:
새 섹션¶
In [ ]:
test_target
Out[ ]:
array([0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0])
In [ ]:
'ML DL' 카테고리의 다른 글
[ML] 3-1 K-최근접 이웃 회귀 (0) | 2022.01.08 |
---|---|
[ML] 2-2 데이터 전처리 (0) | 2022.01.08 |
[ML] 1-3 마켓과 머신러닝 (0) | 2022.01.08 |
[머신러닝] 데이터 전처리 - 표준점수 (0) | 2021.12.19 |
[머신러닝] 훈련 세트와 테스트 세트 (0) | 2021.12.19 |
Comments