# 마켓과 머신러닝¶

## 생선 분류 문제¶

### 도미 데이터 준비하기¶

In [0]:
bream_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]
bream_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]

In [2]:
import matplotlib.pyplot as plt

plt.scatter(bream_length, bream_weight)
plt.xlabel('length')
plt.ylabel('weight')
plt.show()


### 빙어 데이터 준비하기¶

In [0]:
smelt_length = [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]
smelt_weight = [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 [4]:
plt.scatter(bream_length, bream_weight)
plt.scatter(smelt_length, smelt_weight)
plt.xlabel('length')
plt.ylabel('weight')
plt.show()


## 첫 번째 머신러닝 프로그램¶

In [0]:
length = bream_length+smelt_length
weight = bream_weight+smelt_weight

In [6]:
fish_data = [[l, w] for l, w in zip(length, weight)]

print(fish_data)

[[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]]

In [7]:
fish_target = [1]*35 + [0]*14
print(fish_target)

[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 [0]:
from sklearn.neighbors import KNeighborsClassifier

In [0]:
kn = KNeighborsClassifier()

In [0]:
kn.fit(fish_data, fish_target)

In [11]:
kn.score(fish_data, fish_target)

Out[11]:
1.0

### k-최근접 이웃 알고리즘¶

In [12]:
plt.scatter(bream_length, bream_weight)
plt.scatter(smelt_length, smelt_weight)
plt.scatter(30, 600, marker='^')
plt.xlabel('length')
plt.ylabel('weight')
plt.show()

In [13]:
kn.predict([[30, 600]])

Out[13]:
array([1])
In [14]:
print(kn._fit_X)

[[  25.4  242. ]
[  26.3  290. ]
[  26.5  340. ]
[  29.   363. ]
[  29.   430. ]
[  29.7  450. ]
[  29.7  500. ]
[  30.   390. ]
[  30.   450. ]
[  30.7  500. ]
[  31.   475. ]
[  31.   500. ]
[  31.5  500. ]
[  32.   340. ]
[  32.   600. ]
[  32.   600. ]
[  33.   700. ]
[  33.   700. ]
[  33.5  610. ]
[  33.5  650. ]
[  34.   575. ]
[  34.   685. ]
[  34.5  620. ]
[  35.   680. ]
[  35.   700. ]
[  35.   725. ]
[  35.   720. ]
[  36.   714. ]
[  36.   850. ]
[  37.  1000. ]
[  38.5  920. ]
[  38.5  955. ]
[  39.5  925. ]
[  41.   975. ]
[  41.   950. ]
[   9.8    6.7]
[  10.5    7.5]
[  10.6    7. ]
[  11.     9.7]
[  11.2    9.8]
[  11.3    8.7]
[  11.8   10. ]
[  11.8    9.9]
[  12.     9.8]
[  12.2   12.2]
[  12.4   13.4]
[  13.    12.2]
[  14.3   19.7]
[  15.    19.9]]

In [15]:
print(kn._y)

[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 [0]:
kn49 = KNeighborsClassifier(n_neighbors=49)

In [22]:
kn49.fit(fish_data, fish_target)
kn49.score(fish_data, fish_target)

Out[22]:
0.7142857142857143
In [18]:
print(35/49)

0.7142857142857143


### 확인 문제¶

In [34]:
kn = KNeighborsClassifier()
kn.fit(fish_data, fish_target)

for n in range(5, 50):
# 최근접 이웃 개수 설정
kn.n_neighbors = n
# 점수 계산
score = kn.score(fish_data, fish_target)
# 100% 정확도에 미치지 못하는 이웃 개수 출력
if score < 1:
print(n, score)
break

18 0.9795918367346939