더 나은 군집화를 위한 특성 변환

Python으로 배우는 Unsupervised Learning

Benjamin Wilson

Director of Research at lateral.io

피에몬테 와인 데이터셋

  • 3가지 적포도주 품종(Barolo, Grignolino, Barbera)에서 178개 샘플

  • 특성은 알코올 함량 등 화학 조성을 측정합니다

  • "색 강도" 같은 시각적 속성도 포함됩니다

1 Source: https://archive.ics.uci.edu/ml/datasets/Wine
Python으로 배우는 Unsupervised Learning

와인 군집화하기

from sklearn.cluster import KMeans
model = KMeans(n_clusters=3)
labels = model.fit_predict(samples)
Python으로 배우는 Unsupervised Learning

클러스터 vs. 품종

df = pd.DataFrame({'labels': labels, 
                       'varieties': varieties})
ct = pd.crosstab(df['labels'], df['varieties'])

print(ct)
varieties  Barbera  Barolo  Grignolino
labels                                
0               29      13          20
1                0      46           1
2               19       0          50
Python으로 배우는 Unsupervised Learning

특성 분산

  • 와인 특성들의 분산이 매우 다릅니다!

  • 특성의 분산은 값의 퍼짐을 측정합니다

feature     variance
alcohol         0.65
malic_acid      1.24
...
od280           0.50
proline     99166.71

od280 변수 vs malic_acid 변수 산점도

Python으로 배우는 Unsupervised Learning

특성 분산

  • 와인 특성들의 분산이 매우 다릅니다!

  • 특성의 분산은 값의 퍼짐을 측정합니다

feature     variance
alcohol         0.65
malic_acid      1.24
...
od280           0.50
proline     99166.71

od280 변수 vs 관측치 번호 산점도

Python으로 배우는 Unsupervised Learning

StandardScaler

  • kmeans에서: 특성 분산 = 특성 영향력

  • StandardScaler는 각 특성을 평균 0, 분산 1로 변환합니다

  • 이를 "표준화"했다고 합니다

표준화된 od280 vs 표준화된 proline 산점도

Python으로 배우는 Unsupervised Learning

sklearn StandardScaler

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
scaler.fit(samples) StandardScaler(copy=True, with_mean=True, with_std=True)
samples_scaled = scaler.transform(samples)
Python으로 배우는 Unsupervised Learning

유사한 메서드

  • StandardScalerKMeans는 유사한 메서드를 가집니다

  • StandardScaler에는 fit() / transform()을 사용합니다

  • KMeans에는 fit() / predict()를 사용합니다

Python으로 배우는 Unsupervised Learning

StandardScaler 후 KMeans

  • 두 단계 필요: StandardScalerKMeans

  • 여러 단계를 묶으려면 sklearn 파이프라인 사용

  • 데이터는 앞 단계에서 다음 단계로 흐릅니다

Python으로 배우는 Unsupervised Learning

파이프라인으로 여러 단계 결합

from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
scaler = StandardScaler()
kmeans = KMeans(n_clusters=3)

from sklearn.pipeline import make_pipeline
pipeline = make_pipeline(scaler, kmeans)
pipeline.fit(samples)
Pipeline(steps=...)
labels = pipeline.predict(samples)
Python으로 배우는 Unsupervised Learning

특성 표준화로 군집화 개선

특성 표준화 적용 시:

varieties  Barbera  Barolo  Grignolino
labels                                
0                0      59           3
1               48       0           3
2                0       0          65

특성 표준화 없이: 매우 나쁨

varieties  Barbera  Barolo  Grignolino
labels                                
0               29      13          20
1                0      46           1
2               19       0          50
Python으로 배우는 Unsupervised Learning

sklearn 전처리 단계

  • StandardScaler는 "전처리" 단계입니다

  • MaxAbsScaler, Normalizer도 예시입니다

Python으로 배우는 Unsupervised Learning

연습해 봅시다!

Python으로 배우는 Unsupervised Learning

Preparing Video For Download...