비지도 학습

Python으로 배우는 Unsupervised Learning

Benjamin Wilson

Director of Research at lateral.io

비지도 학습

  • 비지도 학습은 데이터에서 패턴을 찾음
  • 예: 구매로 고객을 군집화
  • 구매 패턴으로 데이터 압축(차원 축소)
Python으로 배우는 Unsupervised Learning

지도 학습 vs 비지도 학습

  • 지도 학습은 예측 작업을 위한 패턴을 찾음
  • 예: 종양을 양성/암성으로 분류(레이블)
  • 비지도 학습은 데이터의 패턴을 찾음
  • 단, 특정 예측 작업 없이 수행
Python으로 배우는 Unsupervised Learning

아이리스 데이터셋

  • 다양한 아이리스 측정값
  • 세 종의 아이리스:
    • setosa
    • versicolor
    • virginica
  • 꽃잎 길이/너비, 꽃받침 길이/너비(데이터셋의 특성)

아이리스

1 https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_iris.html
Python으로 배우는 Unsupervised Learning

배열, 특성, 샘플

  • 2D NumPy 배열
  • 열은 측정값(특성)
  • 행은 아이리스 개체(샘플)
Python으로 배우는 Unsupervised Learning

아이리스 데이터는 4차원

  • 아이리스 샘플은 4차원 공간의 점
  • 차원 = 특성(feature) 수
  • 차원이 높아 시각화 불가!
  • …하지만 비지도 학습으로 통찰 확보
Python으로 배우는 Unsupervised Learning

k-평균 군집화

  • 샘플의 군집을 찾음
  • 군집 개수는 지정해야 함
  • sklearn(scikit-learn)에 구현됨
Python으로 배우는 Unsupervised Learning
print(samples)
[[ 5.   3.3  1.4  0.2]
 [ 5.   3.5  1.3  0.3]
 ...
 [ 7.2  3.2  6.   1.8]]
from sklearn.cluster import KMeans

model = KMeans(n_clusters=3)
model.fit(samples)
KMeans(n_clusters=3)
labels = model.predict(samples)

print(labels)
[0 0 1 1 0 1 2 1 0 1 ...]
Python으로 배우는 Unsupervised Learning

새 샘플의 군집 레이블

  • 새 샘플을 기존 군집에 할당 가능
  • k-평균은 각 군집의 평균(센트로이드) 저장
  • 새 샘플에 가장 가까운 센트로이드 찾음
Python으로 배우는 Unsupervised Learning

새 샘플의 군집 레이블

print(new_samples)
[[ 5.7  4.4  1.5  0.4]
 [ 6.5  3.   5.5  1.8]
 [ 5.8  2.7  5.1  1.9]]
new_labels = model.predict(new_samples)

print(new_labels)
[0 2 1]
Python으로 배우는 Unsupervised Learning

산점도

  • 꽃받침 길이 vs 꽃잎 길이 산점도
  • 각 점은 아이리스 샘플
  • 군집 레이블로 색상 표시
  • PyPlot(matplotlib.pyplot)

산점도

Python으로 배우는 Unsupervised Learning

산점도

import matplotlib.pyplot as plt

xs = samples[:,0] ys = samples[:,2]
plt.scatter(xs, ys, c=labels)
plt.show()
Python으로 배우는 Unsupervised Learning

Ayo berlatih!

Python으로 배우는 Unsupervised Learning

Preparing Video For Download...