6. 비지도 학습¶
6-1. 클러스터 알고리즘¶
- 클러스터링: 비슷한 샘플끼리 그룹으로 모으기
데이터 가져오기
In [7]:
import numpy as np
import matplotlib.pyplot as plt
fruits = np.load("fruits_300.npy")
print(fruits.shape)
(300, 100, 100)
이미지 출력하기
In [10]:
# 흑백 이미지 행렬값
print(fruits[0, 0, :])
# 흑백 이미지 출력
plt.imshow(fruits[0], cmap="gray")
# 컴퓨터는 밝은색(픽셀 값이 큼)에 주의를 기울이므로 값을 반전해줌
plt.show()
## 다른 이미지
fig, axs = plt.subplots(1, 2)
axs[0].imshow(fruits[100], cmap="gray_r")
axs[1].imshow(fruits[200], cmap="gray_r")
plt.show()
[ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 3 2 1 2 1 1 1 1 2 1 3 2 1 3 1 4 1 2 5 5 5 19 148 192 117 28 1 1 2 1 4 1 1 3 1 1 1 1 1 2 2 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]
픽셀 값 분석하기
In [11]:
# 분석을 위해 1차원 배열로 변경하기
apple = fruits[0:100].reshape(-1, 100*100)
pineapple = fruits[100:200].reshape(-1, 100*100)
banana = fruits[200:300].reshape(-1, 100*100)
# 픽셀의 평균값 계산하기(샘플별)
print(apple.mean(axis=1))
# 픽셀의 평균값 시각화하기
plt.hist(np.mean(apple, axis=1), alpha=0.8)
plt.hist(np.mean(pineapple, axis=1), alpha=0.8)
plt.hist(np.mean(banana, axis=1), alpha=0.8)
plt.legend(["apple", "pineapple", "banana"])
[ 88.3346 97.9249 87.3709 98.3703 92.8705 82.6439 94.4244 95.5999 90.681 81.6226 87.0578 95.0745 93.8416 87.017 97.5078 87.2019 88.9827 100.9158 92.7823 100.9184 104.9854 88.674 99.5643 97.2495 94.1179 92.1935 95.1671 93.3322 102.8967 94.6695 90.5285 89.0744 97.7641 97.2938 100.7564 90.5236 100.2542 85.8452 96.4615 97.1492 90.711 102.3193 87.1629 89.8751 86.7327 86.3991 95.2865 89.1709 96.8163 91.6604 96.1065 99.6829 94.9718 87.4812 89.2596 89.5268 93.799 97.3983 87.151 97.825 103.22 94.4239 83.6657 83.5159 102.8453 87.0379 91.2742 100.4848 93.8388 90.8568 97.4616 97.5022 82.446 87.1789 96.9206 90.3135 90.565 97.6538 98.0919 93.6252 87.3867 84.7073 89.1135 86.7646 88.7301 86.643 96.7323 97.2604 81.9424 87.1687 97.2066 83.4712 95.9781 91.8096 98.4086 100.7823 101.556 100.7027 91.6098 88.8976]
Out[11]:
<matplotlib.legend.Legend at 0x207d9246a70>
In [12]:
# 픽셀별 평균값 비교하기
fix, axs = plt.subplots(1, 3, figsize=(20, 5))
axs[0].bar(range(10000), np.mean(apple, axis=0))
axs[1].bar(range(10000), np.mean(pineapple, axis=0))
axs[2].bar(range(10000), np.mean(banana, axis=0))
plt.show()
In [13]:
# 픽셀을 평균 낸 이미지
apple_mean = np.mean(apple, axis=0).reshape(100, 100)
pineapple_mean = np.mean(pineapple, axis=0).reshape(100, 100)
banana_mean = np.mean(banana, axis=0).reshape(100, 100)
fig, axs = plt.subplots(1, 3, figsize=(20, 5))
axs[0].imshow(apple_mean, cmap="gray_r")
axs[1].imshow(pineapple_mean, cmap="gray_r")
axs[2].imshow(banana_mean, cmap="gray_r")
plt.show()
평균값에 가까운 사진 고르기
In [14]:
abs_diff = np.abs(fruits - apple_mean)
abs_mean = np.mean(abs_diff, axis=(1, 2))
# apple_mean에 가장 가까운 과일 고르기
apple_index = np.argsort(abs_mean)[:100]
fig, axs = plt.subplots(10, 10, figsize=(10, 10))
for i in range(10):
for j in range(10):
axs[i, j].imshow(fruits[apple_index[i*10+j]], cmap="gray_r")
axs[i, j].axis("off")
plt.show()
In [15]:
abs_diff = np.abs(fruits - banana_mean)
abs_mean = np.mean(abs_diff, axis=(1, 2))
# apple_mean에 가장 가까운 과일 고르기
banana_index = np.argsort(abs_mean)[:100]
fig, axs = plt.subplots(10, 10, figsize=(10, 10))
for i in range(10):
for j in range(10):
axs[i, j].imshow(fruits[banana_index[i*10+j]], cmap="gray_r")
axs[i, j].axis("off")
plt.show()