#!/usr/bin/env python3


Face Datasets Analysis with DBSCAN


# load library

from sklearn.datasets import fetch_lfw_people

import numpy as np

import matplotlib

import matplotlib.pyplot as plt

from sklearn.decomposition import PCA, NMF

from sklearn.preprocessing import MinMaxScaler

from sklearn.cluster import DBSCAN


# matplotlib 설정 

matplotlib.rc('font', family='AppleGothic')

plt.rcParams['axes.unicode_minus'] = False

people = fetch_lfw_people(min_faces_per_person=20, resize=0.7, color=False# min_faces_per_person = 겹치지 않는 최소 얼굴 수, 

image_size = people.images[0].shape


# outlier 있는지 확인 

_, bins = np.histogram(people.target, bins=100)

plt.hist(people.target, bins=bins)

plt.xticks(range(len(people.target_names)), people.target_names, rotation=90, ha='center')

plt.hlines(50, xmin=0, xmax=bins[-1], linestyles='--')# 수평선

plt.show()

각 특성별 히스토그램 

# 특성이 50개

idx = np.zeros(people.target.shape, dtype=np.bool)

for tg in np.unique(people.target):

    idx[np.where(people.target == tg)[0][:50]] = 1


# 데이터 분할

x_people = people.data[idx]

y_people = people.target[idx]


# 전처리

scaler_mms = MinMaxScaler()

x_people_scaled = scaler_mms.fit_transform(x_people)

# pca는 whiten 옵션을 True를 주면 전처리효과가 발생


# 훈련데이터를 PCA주성분 100개로 변환

pca = PCA(n_components=100, random_state=0, whiten=True).fit(x_people)

x_pca = pca.transform(x_people)


# 훈련데이터를 NMF 성분 100개로 변환

nmf = NMF(n_components=100, random_state=0).fit(x_people_scaled)

x_nmf = nmf.transform(x_people_scaled)


# DBSCAN에서 알맞은 min_sample값과 eps구하기

for min_sample in [3, 5, 7, 9]:

    for eps in [0.2, 0.4, 0.9, 1, 3, 5, 7, 9, 11, 13, 15, 17]:

        dbscan = DBSCAN(n_jobs=-1, min_samples=min_sample, eps=eps)

        labels_pca = dbscan.fit_predict(x_pca) # pca를 이용한 성분을 이용한 dbscan을 예측

        labels_nmf = dbscan.fit_predict(x_nmf) # nmf를 이용한 성분을 이용한 dbscan으로 예측

        print('︎=== min_sample:{}, eps:{} ==='.format(min_sample, eps))

        print('')

        print('pca ==> cluster 수: {}'.format(len(np.unique(labels_pca))))

        print('pca ==> cluster 크기: {}\n'.format(np.bincount(labels_pca+1)))


        print('nmf ==> cluster 수: {}'.format(len(np.unique(labels_nmf))))

        print('nmf ==> cluster 크기: {}'.format(np.bincount(labels_nmf+1)))

        print('---------------------------------------')

알맞은 min_sample과 eps를 찾는 과정

### pca : min_sample=3, eps=7일 때 가장 많은 클러스터가 생김

### nmf : min_sample=3, eps=0.9일 때 가장 많은 클러스터가 생김


# 가장 많은 클러스터가 생긴 eps와 n_sample 츨력

dbscan = DBSCAN(min_samples=3, eps=7, n_jobs=-1) # n_jobs = 코어의 갯수

labels_pca = dbscan.fit_predict(x_pca)


dbscan = DBSCAN(min_samples=3, eps=0.9, n_jobs=-1)

labels_nmf = dbscan.fit_predict(x_nmf)


# pca 주성분, DBSCAN 을 이용한 시각화

for cluster in range(labels_pca.max() + 1):

    idx = labels_pca == cluster 

    n_images = np.sum(idx) # idx중 true인 것의 갯수

    fig, axes = plt.subplots(1,n_images, figsize=(n_images*1.5, 4), # figsize=(a,b) axb 그림사이즈

                            subplot_kw={'xticks':(), 'yticks':()})

    

    for image, label, ax in zip(x_people[idx], y_people[idx], axes.ravel()):

        ax.imshow(image.reshape(image_size))

        ax.set_title(people.target_names[label].split()[-1])


plt.gray()

plt.show()

0123456789101112

PCA 주성분을 이용한 DBSCAN 





#!/usr/bin/env python3


face dataset으로 Agglomerative Algorithm 비교

1. face data load

# library import

from sklearn.decomposition import PCA, NMF

from sklearn.preprocessing import StandardScaler, MinMaxScaler

from sklearn.datasets import fetch_lfw_people

from sklearn.model_selection import train_test_split

from sklearn.cluster import DBSCAN, KMeans, AgglomerativeClustering

import numpy as np

import matplotlib.pyplot as plt

import matplotlib


# matplotlib 설정

matplotlib.rc('font', family='AppleGothic'# 한글출력

plt.rcParams['axes.unicode_minus'] = False # 축 -


# dataset

people = fetch_lfw_people(min_faces_per_person=20, resize=0.7, color=False) 

people_image = people.images[0].shape 


# 빈도수 확인

counts = np.bincount(people.target)


for i, (count, name) in enumerate(zip(counts, people.target_names)):

    print('{0:12} {1:3}'.format(count, name), end='\t')

    if i%3 == 0:

        print()


# 특성의수를 50개까지

idx = np.zeros(people.target.shape, dtype=np.bool)

for target in np.unique(people.target):

    idx[np.where(people.target == target)[0][:50]] = 1


# 데이터 분할

x_people = people.data[idx]

y_people = people.target[idx]


# preprocessing

scaler = MinMaxScaler()

x_people_scaled = scaler.fit_transform(x_people) # 전처리 메소드로 원래데이터 적용




2 . PCA와 NMF를 이용하여 DBSCAN


# PCA 주성분 분석

pca = PCA(n_components=100, whiten=True, random_state=0) 

pca.fit_transform(x_people_scaled) # 모델 fitting

x_pca = pca.transform(x_people_scaled) # 적용한 모델을 원래 데이터에 적용


# NMF 분석

nmf = NMF(n_components=100, random_state=0)

nmf.fit_transform(x_people_scaled) # 모델 fitting

x_nmf = nmf.transform(x_people_scaled) # 적용한 모델을 원래 데이터에 적용


# DBSCAN Agglomerative Algorithm

dbscan = DBSCAN() 

labels_pca = dbscan.fit_predict(x_pca) # pca를 군집화

labels_nmf = dbscan.fit_predict(x_nmf) # nmf를 군집화


print('\n레이블 종류: \npca: {}\nnmf: {}'.format(np.unique(labels_pca), np.unique(labels_nmf))) # [-1], 

### PCA, NMF 모두 레이블이 -1뿐임 ==> DBSCAN에 의해 noise point로 처리

### solution:

### 1. eps값을 크게 ==> 데이터 point의 이웃의수 증가

### 2. min_samples 값을 작게 ==> cluster에 모을 point 수를 감소



3. 알맞은 DBSCAN값 찾기

### 알맞은 eps값 확인 (-1, 0)

for eps in [0.5, 0.7, 0.9, 1, 5, 9, 15, 17]: # eps 목록

    dbscan = DBSCAN(min_samples=3, eps=eps)

    labels_pca = dbscan.fit_predict(x_pca)

    labels_nmf = dbscan.fit_predict(x_nmf)

    print('pca ==> n_samples: {}, eps: {:.3f} - {}'.format(3, eps, np.unique(labels_pca)))

    print('nmf ==> n_samples: {}, eps: {:.3f} - {}\n'.format(3, eps, np.unique(labels_nmf)))


# DBSCAN -- pca

dbscan = DBSCAN(min_samples=3, eps=15)

labels_pca = dbscan.fit_predict(x_pca)


# DBSCAN -- NMF

dbscan = DBSCAN(min_samples=3, eps=1)

labels_nmf = dbscan.fit_predict(x_nmf)


print('레이블 종류: \npca: {}\nnmf: {}'.format(np.unique(labels_pca), np.unique(labels_nmf))) # (-1, 0)

pca_count = np.bincount(labels_pca + 1) # bincount는 음수는 표현할 수없으니 1을 더함

nmf_count = np.bincount(labels_nmf + 1)


print('클러스터별 포인트 수:\npca: {}\nnmf: {}'.format(pca_count, nmf_count))


### visualization

noise_pca = x_people[labels_pca == -1] # noise 값만 추출

noise_nmf = x_people[labels_nmf == -1]


fig, axes = plt.subplots(3, 9,

                        subplot_kw={'xticks':(), 'yticks':()},

                        gridspec_kw={'hspace':0.5})


for image, ax in zip(noise_pca, axes.ravel()):

    ax.imshow(image.reshape(people_image))

plt.gray()

plt.show()

face dataset에서 DBSCAN이 noise point로 레이블한 sample(PCA)


fig, axes = plt.subplots(3, 9,

                        subplot_kw={'xticks':(), 'yticks':()},

                        gridspec_kw={'hspace':0.5})


for image, ax in zip(noise_nmf, axes.ravel()):

    ax.imshow(image.reshape(people_image))

plt.gray()

plt.show()

face dataset에서 DBSCAN이 noise point로 레이블한 sample(NMF)


face image에서 무작위로 선택한 이미지와 비교해보면 대부분 얼굴이 일부 가려져 있거나 각도가 이상, 혹은 너무 가까이거나 너무 멀리 있는 경우임


#!/usr/bin/env python3


clustering valuation(군집 모델 평가하기)


agglomerative algorithm군집 알고리즘을 적용하는데 어려운 점은 

algorithm이 잘 작동하는지 평가하거나 여러 algorithm의 출력을 비교하기가 매우 어려움


1. target value로 clustering 평가하기


agglomerative algorithm의 결과를 실제 정답 cluster와 비교하여 평가할 수 있는 지표

1. ARIadjusted rand index : 0과 1사이의 값을 제공(그러나 음수가 될 수 있음)

2. NMInormalized mutual information를 가장 많이 사용


# library import

import matplotlib

import matplotlib.pyplot as plt

import numpy as np

import mglearn

from sklearn.datasets import make_moons

from sklearn.preprocessing import StandardScaler

from sklearn.cluster import KMeans, AgglomerativeClustering, DBSCAN

from sklearn.metrics.cluster import adjusted_rand_score


# matplotlib 설정

matplotlib.rc('font', family='AppleGothic'# 한글출력

plt.rcParams['axes.unicode_minus'] = False # 축 -표시


# dataset

x, y = make_moons(n_samples=200, noise=0.05, random_state=0)


# pre-processing

scaler = StandardScaler().fit(x)

x_scaled = scaler.transform(x)


# model list

algorithms = [KMeans(n_clusters=2),

              AgglomerativeClustering(n_clusters=2),

                      DBSCAN()]

kmean_pred = algorithms[0].fit_predict(x_scaled)

# random cluster

random_state = np.random.RandomState(seed=0)

random_clusters = random_state.randint(low=0, high=2, size=len(x))


### visualization

fig, axes = plt.subplots(2, 2,

                         subplot_kw={'xticks':(), 'yticks':()})



axes[0,0].scatter(x_scaled[:, 0], x_scaled[:, 1], c=random_clusters,

                cmap=mglearn.cm3, s=60, edgecolors='k')

axes[0,0].set_title('random assign - ARI: {:.3f}'.format(

    adjusted_rand_score(y, random_clusters))) # 실제, 모델로 구한 클러스터


axes[0,1].scatter(x_scaled[:, 0], x_scaled[:, 1], c=kmean_pred,

                  cmap=mglearn.cm3, s=60, edgecolors='k')

axes[0,1].set_title('{} - ARI: {:.3f}'.format(algorithms[0].__class__.__name__, #__class__.__name__ ==> 클래스에서 이름 속성

                                              adjusted_rand_score(y, kmean_pred)))


for ax, algorithm in zip(axes.ravel()[2:], algorithms[1:]):

    clusters = algorithm.fit_predict(x_scaled)

    ax.scatter(x_scaled[:, 0], x_scaled[:, 1], c=clusters,

               cmap=mglearn.cm3, s=60, edgecolors='k')

    ax.set_title('{} - ARI: {:.3f}'.format(algorithm.__class__.__name__,

                                           adjusted_rand_score(y, clusters)))

plt.show()

two_moons 데이터셋에 적용한 random assign, KMeans, AgglomerativeClustering, DBSCAN의 ARI Score



2. no target value로 clustering 평가하기

ARI 같은 방법은 반드시 target value가 있어야하지만 실제로는 결과와 비교할 target value가 없음

따라서 ARI나 NMI 같은 지표는 algorithm을 개발할 때 도움이 많이 됨


silhouette coefficient실루엣 계수: 실제로 잘 동작하지 않음, cluster의 밀집 정도를 계산, max = 1

모양이 복잡할때는 잘 들어맞지가 않음


# library import

from sklearn.metrics.cluster import silhouette_score


# datasets

x, y = make_moons(n_samples=300, noise=0.05, random_state=0)


# pre-processing

scaler = StandardScaler().fit(x)

x_scaled = scaler.transform(x)


# random cluster

random_state = np.random.RandomState(seed=0)

random_clusters = random_state.randint(0, 2, size=len(x)) # randint(low, high, size)


# K-Means 모델

kmeans = KMeans(n_clusters=2)

kmeans_pred = kmeans.fit_predict(x_scaled)


### visualization

fig, axes = plt.subplots(2, 2,

                         subplot_kw={'xticks':(), 'yticks':()})

ax = axes.ravel()


# random cluster 그리기

ax[0].scatter(x_scaled[:, 0], x_scaled[:,1], c=random_clusters,

            cmap=mglearn.cm2, edgecolors='k', s=60)

ax[0].set_title('random assign - silhouette score: {:.3f}'.format(silhouette_score(x_scaled, random_clusters))) # silhouette_score(훈련데이터, 예측한 클러스터)


# K-Means 그리기

ax[1].scatter(x_scaled[:, 0], x_scaled[:, 1], c=kmeans_pred,

           cmap=mglearn.cm2, edgecolors='k', s=60)

ax[1].set_title('{} - silhouette score: {:.3f}'.format(kmeans.__class__.__name__,

                                                 silhouette_score(x_scaled, kmeans_pred)))


# AgglomerativeClustering과 DBSCAN 그리기

algorithms = [AgglomerativeClustering(n_clusters=2), DBSCAN()]

for ax, model in zip(ax[2:], algorithms):

    cluster = model.fit_predict(x_scaled)

    ax.scatter(x_scaled[:, 0], x_scaled[:, 1], c=cluster,

               cmap=mglearn.cm2, edgecolors='k', s=60)

    ax.set_title('{} - silhouette score: {:.3f}'.format(model.__class__.__name__,

                                                  silhouette_score(x_scaled, cluster)))

plt.show()

two_moons 데이터셋에 적용한 random_assign, KMeans, AgglomerativeClustering, DBSCAN의 silhouette score


DBSCAN의 결과가 더 보기 좋지만 K-mean의 silhouette score가 더 높음

Agglomerative Model이 매우 안정적이거나 silhouette score가 더 높더라도 cluster에 어떤 유의미한 것이 있는지 알 수가 없음


#!/usr/bin/env python3


DBSCAN



0. 살펴보기

DBSCANdensity-based spatial clustering of application with noise은 클러스터의 갯수를 미리 지정하지 않는 군집 알고리즘

DBSCAN은 병합 군집이나 k-평균보다는 다소 느리지만 비교적 큰 데이터셋에도 적용


데이터의 밀집지역이 한 클러스터를 구성하며 비교적 비어있는 지역을 경계로 다른 클러스터와 구분함


DBSCAN은 특성 공간에서 가까이 있는 데이터가 많아 붐비는 지역의 포인트를 찾음

이런 지역을 밀집 지역dense region이라 함

밀집 지역에 있는 포인트를 핵심 포인트core point라고함


핵심 포인트: min_samples, epsepsilon

한 데이터 포인트에서 eps 거리 안에 데이터가 min_samples 갯수만큼 들어 있으면 이 데이터 포인트를 핵심 포인트로 분류

eps(거리)보다 가까운 핵심 샘플은 동일한 클러스터로 분류


알고리즘

1. 무작위로 데이터 포인트를 선택


2. 그 포인트에서 eps 거리안의 모든 포인트를 찾음

2-1 eps 거리 안에 있는 데이터 포인트 수가 min_samples보다 적다면 어떤 클래스에도 속하지 않는 잡음noise로 레이블

2-2 eps 거리 안에 있는 데이터 포인트 수가 min_samples보다 많으면 핵심 포인트로 레이블하고 새로운 클러스터 레이블할당


3. 2-2의 핵심 포인트의 eps거리안의 모든 이웃을 살핌

3-1 만약 어떤 클러스터에도 아직 할당되지 않았다면 바로 전에 만든 클러스터 레이블을 할당

3-2 만약 핵심 포인트면 그 포인트의 이웃을 차례로 확인


4. eps 거리안에 더이상 핵심 포인트가 없을 때까지 진행


핵심포인트, 경계포인트(핵심 포인트에서 eps거리 안에 있는 포인트), 잡음 포인트가 핵심


DBSCAN을 한 데이터셋에 여러 번 실행하면 핵심 포인트의 군집은 항상 같고, 매번 같은 포인트를 잡음으로 레이블함

그러나 경계포인트는 한 개 이상의 핵심 포인트 샘플의 이웃일 수 있음


# library import

import mglearn

import matplotlib.pyplot as plt

from matplotlib import rc


# matplotlib 설정

rc('font', family='AppleGothic'

plt.rcParams['axes.unicode_minus'] = False


# 예제

mglearn.plots.plot_dbscan()

plt.show()


min_samples와 eps 매개변수를 바꿔가며 DBSCAN으로 계산한 클러스터


이 그래프에서 클러스터에 속한 포인트는 색을 칠하고 잡음 포인트는 하얀색으로 남겨둠

핵심포인트는 크게 표시하고 경계 포인트는 작게 나타냄

eps를 증가시키면(왼쪽 -> 오른쪽) 하나의 클러스터에 더 많은 포인트가 포함

이는 클러스터를 커지게 하지만 여러 클러스터를 하나로 합치게도 함

min_samples를 키우면(위 -> 아래) 핵심 포인트 수가 줄어들며 잡음 포인트가 늘어남




1. make_blobs(인위적 데이터)로 DBSCAN 적용

eps 매개변수가까운 포인트의 범위를 결정하기 때문에 매우 중요

eps를 매우 작게 하면 어떤 포인트도 핵심 포인트가 되지 못하고, 모든 포인트가 잡음 포인트가 될 수 있음

eps를 매우 크게 하면 모든 포인트가 단 하나의 클러스터에 속하게 됨


min_samples 설정은 덜 조밀한 지역에 있는 포인트들이 잡은 포인트가 될 것인지, 아니면 하나의 클러스터가 될 것인지를 결정

min_samples보다 작은 클러스터들은 잡음포인트가 되기 때문에 클러스터의 최소 크기를 결정


DBSCAN은 클러스터의 갯수를 지정할 필요가 없지만 eps의 값은 간접적으로 클러스터의 갯수 제어

적절한 eps 값을 찾기 위해 전처리preprocessing(StandardScaler, MinMaxScaler)로 조정하는 것이 좋음


### DBSCAN은 새로운 테스트 데이터에 대해 예측

### 할 수 없으므로(병합군집, 매니폴드학습과 동일) fit_predict를 사용

# library import

from sklearn.cluster import DBSCAN

from sklearn.datasets import make_blobs


# dataset

x, y = make_blobs(n_samples=12, random_state=0)


# 모델생성 및 클러스터 레이블

dbscan = DBSCAN()

clusters = dbscan.fit_predict(x)


# 클러스터 레이블 출력

print('clusters \n클러스터레이블: {}'.format(clusters))

### 모든 포인트에 잡은 포인트를 의미하는 '-1'이 할당

### eps와 min_samples 기본값이 작은 데이터셋에 적절하지 않음



2. 전처리 후 make_blobs(인위적 데이터)에 DBSCAN적용

# library import

from sklearn.datasets import make_moons

from sklearn.preprocessing import StandardScaler

from sklearn.preprocessing import MinMaxScaler

import numpy as np


# dataset

x, y = make_moons(n_samples=200, noise=0.05, random_state=0)


# MinMaxScaler 메소드로 전처리

scaler_MMS = MinMaxScaler().fit(x)

x_scaled_MMS = scaler_MMS.transform(x) # 전처리 메소드를 훈련데이터에 적용

dbscan = DBSCAN() # 모델생성

clusters_MMS = dbscan.fit_predict(x_scaled_MMS) # 모델 학습

print('np.unique(clusters_MMS)\n예측한 레이블: {}'.format(np.unique(clusters_MMS))) # [0]

### 예측한 레이블이 0으로 전부 하나의 클러스터로 표현

### MinMaxScaler전처리가 적합하지 않음


scaler_ss = StandardScaler().fit(x)

x_scaled_ss = scaler_ss.transform(x) 

dbscan = DBSCAN()

clusters_ss = dbscan.fit_predict(x_scaled_ss)

print('np.unique(clusters_ss)\n예측한 레이블:{}'.format(np.unique(clusters_ss))) # [0 ,1]

### 2차원 데이터셋을 0과 1로 구분했기 때문에 전처리가 잘되었음을 확인


# visualization

df = np.hstack([x_scaled_ss, clusters_ss.reshape(-1, 1)]) # x_scaled_ss 오른쪽에 1열 붙이기


df_ft0 = df[df[:,2]==0, :] # 클러스터 0 추출

df_ft1 = df[df[:,2]==1, :] # 클러스터 1 추출


# matplotlib로 그래프 그리기

plt.scatter(df_ft0[:, 0], df_ft0[:, 1], label='cluster 0', cmap='Pairs'# x, y, label, 색상

plt.scatter(df_ft1[:, 0], df_ft1[:, 1], label='cluster 1', cmap='Pairs')

plt.xlabel('feature 0')

plt.ylabel('feature 1')

plt.legend()

plt.show()

기본값 eps=0.5를 사용해 DBSCAN으로 찾은 클러스터



#!/usr/bin/env python3


병합 군집(agglomerative clustering)


0. 살펴보기


병합 군집agglomerative clustering 알고리즘은 

시작할 때 각 포인트를 하나의 클러스터로 지정하고, 그다음 종료 조건을 만족할 때까지 가장 비슷한 두 클러스터를 합침

종료조건 : 클러스터 갯수, 지정된 갯수의 클러스터가 남을 때까지 비슷한 클러스터를 합침


linkage: 옵션에서 가장 비슷한 클러스터를 측정하는 방법 지정, 이 측정은 항상 두 클러스터 사이에서 이뤄짐


ward : 기본값, 모든 클러스터 내의 분산을 가장 작게 증가시키는 두 클러스터를 합침, 크기가 비교적 비슷한 클러스터가 만들어짐


average : 클러스터 포인트 사이의 평균 거리가 가장 짤븐 두 클러스터를 합침


complete : complete연결(최대 연결)은 클러스터 포인트 사이의 최대 거리가 가장 짧은 두 클러스터를 합침


클러스터에 속한 포인트 수가 많이 다를 때(하나의 클러스터가 다른 것보다 매우 클 때 ) average나 complete가 더 나음


# library import

import mglearn

import matplotlib

import matplotlib.pyplot as plt


# matplotlib 설정

matplotlib.rc('font', family='AppleGothic')

plt.rcParams['axes.unicode_minus'] = False


# 알고리즘 설명 시각화

mglearn.plots.plot_agglomerative_algorithm()

plt.show()

두 인접 클러스터를 반복적으로 합쳐나가는 병합 군집

1. 초기에 각 포인트가 하나의 클러스터

2. 가장 가까운 두 클러스터가 합쳐짐

3. step4까지 이 방식으로 진행

4. step5에서 두 개의 포인트를 가진 클러스중 하나가 3개로 확장

5. step9까지 이 방식으로 진행

6. 3개의 클러스터를 찾는다고 지정하면 알고리즘은 종료




1. make blobs(인위적 데이터)로 병합 군집 분석


알고리즘의 작동 특성상 병합 군집은 새로운 데이터 포인트에 대해서는 예측을 할 수 없기 때문에

predict메소드가 없음(매니폴드 학습과 동일)


# library import

from sklearn.cluster import AgglomerativeClustering

from sklearn.datasets import make_blobs

import numpy as np


# dataset

x, y = make_blobs(random_state=1)


# 모델 생성 및 학습

agg = AgglomerativeClustering(n_clusters=3)

assign = agg.fit_predict(x)


# 배열 x 오른쪽에 열 한개 추가

a = assign.reshape(-1, 1)

x1 = np.hstack([x, a])


# 각 클래스별로 데이터 추출

x_0 = x1[x1[:, 2]==0, :]

x_1 = x1[x1[:, 2]==1, :]

x_2 = x1[x1[:, 2]==2, :]


# 시각화

plt.scatter(x_0[:, 0], x_0[:, 1], cmap=mglearn.cm3)

plt.scatter(x_1[:, 0], x_1[:, 1], cmap=mglearn.cm3)

plt.scatter(x_2[:, 0], x_2[:, 1], cmap=mglearn.cm3)

plt.legend(['cluster 0', 'cluster 1', 'cluster 2'], loc=2)

plt.show()

병합 군집을 사용한 세 개의 클러스터 할당




2. 계층적 군집과 덴드로그램

병합군집은 계층적 군집hierarchical clustering을 만듬. 군집이 반복하여 진행되면 모든 포인트는 하나의 포인트를 가진 클러스터에서 시작하여 마지막 클러스터까지 이동

각 중간단계는 데이터에 대한 (다른 갯수)의 클러스터를 생성


mglearn.plots.plot_agglomerative()

plt.show()

병합 군집으로 생성한 계층적 군집


이 그래프는 계층 군집의 모습을 자세히 나타내지만, 2차원 데이터일 뿐이며 특성이 셋 이상인 데이터셋에는 적용불가

dendrogram은 다차원 데이터셋을 처리가능



3. dendrogram으로 3차원 이상의 데이터 시각화


# library import

from scipy.cluster.hierarchy import dendrogram, ward[각주:1]


# dataset

x, y = make_blobs(random_state=0, n_samples=12)


### 데이터 배열 x에 ward 함수를 적용

### scipy의 ward 함수는 병합 군집을 수행할 때 생성된 거리 정보가 담긴 배열을 반환


linkage_array = ward(x)

dendrogram(linkage_array)

ax = plt.gca() # get current axes

bounds = ax.get_xbound() # x축 데이터(처음과 끝), 즉 최소/최대값을 가진 (1,2)리스트

ax.plot(bounds, [7.25, 7.25], linestyle='--', c='k'# 임의로 라인 생성

ax.plot(bounds, [4, 4], linestyle='--', c='k')

ax.text(bounds[1], 7.25, ' 두 개의 클러스터', va='center', fontdict={'size':15}) # bounds: x축 끝

ax.text(bounds[1], 4, ' 세 개의 클러스터', va='center', fontdict={'size':15})

plt.xlabel('샘플 번호')

plt.ylabel('클러스터 거리')

plt.show()

클러스터의 덴드로그램과 클러스터를 구분하는 점선


덴드로그램에서 데이터 포인트(0 ~ 11)까지는 맨 아래 나타남

이 포인트들을 잎leaf으로 하는 트리가 만들어지며 부모 노드는 두 클러스터가 합쳐질때 추가됨


가지의 길이는 합쳐진 클러스터가 얼마나 멀리 떨어져 있는지를 보여줌

빨강 그룹과 바다색 그룹이 합쳐질때 가지의 길이는 짧아짐 ==> 비교적 짧은 거리의 데이터를 병합

  1. ward : 기본값, 모든 클러스터 내의 분산을 가장 작게 증가시키는 두 클러스터를 합침, 크기가 비교적 비슷한 클러스터가 만들어짐 [본문으로]

#!/usr/bin/env python3


벡터 양자화(vector quantization)로서의 k-평균


0. 살펴보기

PCA : 데이터의 분산이 가장 큰 방향

NMF : 데이터의 극단 또는 일부분에 상응되는 중첩할 수 있는 성분

k-평균 : 클러스터의 중심으로 데이터 포인트를 표현 ==> 각 데이터 포인트가 클러스터 중심, 즉 하나의 성분으로 표현된다고 볼 수 있음

k-평균을 이렇게 각 포인트가 하나의 성분으로 분해되는 관점으로 보는 것을 벡터 양자화vector quantization이라 함


1. PCA, NMF, k-평균에서 추출한 성분을 바탕으로 시각화

# library import

from sklearn.datasets import fetch_lfw_people

from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import matplotlib

import numpy as np

from sklearn.decomposition import NMF

from sklearn.decomposition import PCA

from sklearn.cluster import KMeans


# matplotlib 설정

matplotlib.rc('font', family='AppleGothic')

plt.rcParams['axes.unicode_minus'] = False


# people객체 생성

people = fetch_lfw_people(min_faces_per_person=20, resize=0.7, color=False# 겹치치 않는 최소사람 수, 비율, 흑백

image_shape = people.images[0].shape 


# 특성 50개 추출(True)

idx = np.zeros(people.target.shape, dtype=np.bool) # 3023개의 False생성

for target in np.unique(people.target): 

    idx[np.where(people.target == target)[0][:50]] = 1 


# 데이터 추출

x_people = people.data[idx]

y_people = people.target[idx]


# 데이터 분할

x_train, x_test, y_train, y_test = train_test_split(

    x_people, y_people, # 분할할 데이터

    stratify=y_people, random_state=0) # 그룹, 랜덤상태


# 모델생성 및 학습

nmf = NMF(n_components=100, random_state=0).fit(x_train) 

pca = PCA(n_components=100, random_state=0).fit(x_train)

kmeans = KMeans(n_clusters=100, random_state=0).fit(x_train)


### pca.transform(x_test): 주성분 갯수만큼 차원을 줄인 데이터

### pca.inverse_transform(pca.transform(x_test)): 차원을 줄인 주성분으로 원래 데이터에 매핑

### kmeans.cluster_centers_[kmeans.predict(x_test)] k-평균 중심에서 k-평균으로 구한 라벨을 행 인덱스로 추출하여 재구성

x_reconst_nmf = np.dot(nmf.transform(x_test), nmf.components_) nmf.inverse_transform(nmf.transform(x_test)) 와 동일

x_reconst_pca = pca.inverse_transform(pca.transform(x_test))

x_reconst_kmeans = kmeans.cluster_centers_[kmeans.predict(x_test)]


# visualization

fig, axes = plt.subplots(3, 5,

                         subplot_kw={'xticks':(), 'yticks':()})

for ax, comp_nmf, comp_pca, comp_kmeans in zip(

        axes.T, nmf.components_, pca.components_, kmeans.cluster_centers_): # 행방향 순서로 그림을 채우기 때문에 Transfrom을 

    ax[0].imshow(comp_nmf.reshape(image_shape))

    ax[1].imshow(comp_pca.reshape(image_shape))

    ax[2].imshow(comp_kmeans.reshape(image_shape))


axes[0, 0].set_ylabel('nmf')

axes[1, 0].set_ylabel('pca')

axes[2, 0].set_ylabel('kmeans')

plt.gray()

plt.show()

NMF, PCA, k-평균의 클러스터 중심으로 찾은 성분의 비교

# library import 

fig, axes = plt.subplots(4, 5,

                         subplot_kw={'xticks':(), 'yticks':()}) # subplot 축 없애기

for ax, orig, rec_nmf, rec_pca, rec_kmeans in zip(

        axes.T, x_test, x_reconst_nmf, x_reconst_pca, x_reconst_kmeans):

    ax[0].imshow(orig.reshape(image_shape))

    ax[1].imshow(rec_nmf.reshape(image_shape))

    ax[2].imshow(rec_pca.reshape(image_shape))

    ax[3].imshow(rec_kmeans.reshape(image_shape))


for i, name in zip(range(4), ['원본', 'kmeans', 'pca', 'nmf']):

    axes[i, 0].set_ylabel(name)

plt.gray()

plt.show()

성분(또는 클러스터 중심) 100개를 사용한 NMF, PCA, k-평균의 이미지 재구성


k-평균을 사용한 벡터 양자화는 데이터의 차원보다 더 많은 클러스터를 사용해 데이터를 인코딩할 수 있음



2. 특성의 수보다 많은 클러스터중심을 사용한 k-평균


# library import

from sklearn.datasets import make_moons


# dataset생성

x, y = make_moons(n_samples=200, noise=0.05, random_state=0) # noise: 퍼짐정도


# 모델 생성 및 학습, 예측

kmeans = KMeans(n_clusters=10, random_state=0).fit(x)

y_pred = kmeans.predict(x)


plt.scatter(x[:, 0], x[:, 1], c=y_pred, s=60, cmap='Paired', edgecolors='black', alpha=0.7) # cmap: palette

plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], c=np.unique(kmeans.labels_), # c: group

            s=100, marker='s', linewidths=2, cmap='Paired', edgecolors='black')

plt.xlabel('특성 0')

plt.ylabel('특성 1')

plt.show()

복잡한 형태의 데이터셋을 다루기 위해 많은 클러스터를 사용한 k-평균


10개의 클러스터를 사용했기 때문에 10개로 구분

해당 클러스터에 해당하는 특성을 1, 해당하지않는 특성을 0으로 구분


큰 규모의 데이터셋을 처리할 수 있는 scikit-learn은 MiniBatchKMenas도 제공


k-평균의 단점은 클러스터의 모양을 가정(원)하고 있어서 활용범위가 비교적 제한

찾으려하는 클러스터의 갯수를 지정해야함

#!/usr/bin/env python3


k-평균 군집 알고리즘


0. 살펴보기

데이터셋을 클러스터cluster라는 그룹으로 나누는 작업

다른 클러스터의 데이터 포인트와는 구분되도록 데이터를 나누는 것이 목표


k-평균k-mean 군집은 가장 간단하고 널리 사용됨

이 알고리즘은 데이터의 어떤 영역을 대표하는 클러스터 중심cluster center를 찾음

1. 데이터 포인트를 가장 가까운 클러스터 영역에 할당

2. 클러스터에 할당된 데이터 포인트의 평균으로 클러스터 중심을 다시 지정

3. 클러스터에 할당되는 데이터 포인트에 변화가 없을 때 알고리즘 종료


# library import

import mglearn

import matplotlib

import matplotlib.pyplot as plt


# matplotlib 설정

matplotlib.rc('font', family='AppleGothic'# 한글 출력

plt.rcParams['axes.unicode_minus'] = False #  축 -


mglearn.plots.plot_kmeans_algorithm()

plt.show()

입력 데이터와 k-평균 군집 알고리즘이 세번 진행되기가지의 과정


# 경계선 그리기

mglearn.plots.plot_kmeans_boundaries()

plt.show() 

k-평균 알고리즘으로 찾은 클러스터 중심과 클러스터 경계




1. make_blobs(인위적 데이터셋)에 k-평균 알고리즘을 적용

# library import

from sklearn.datasets import make_blobs

from sklearn.cluster import KMeans

import numpy as np


# 데이터 만들기

x, y = make_blobs(random_state=1)


# 모델 생성 및 학습

kmean = KMeans(n_clusters=3) # 클러스터 수

kmean.fit(x)


print('클러스터 레이블: \n{}'.format(kmean.labels_)) 

3개의 클러스터를 지정했으므로 각 클러스터는 0 ~ 2까지 번호가 붙음


### predict메소드를 통해 새로운 데이터 클러스터 레이블을 예측할 수 있음

### 예측은 각 포인트에 가장 가까운 클러스터 영역으로 할당하는 것이며 기존 모델을 변경하지 않음

### 훈련 세트에 대해 predict 메소드를 실행하면 labels_와 같은 결과를 얻게 됨


print('예측 레이블: \n{}'.format(kmean.predict(x)))


### 클러스터는 각 데이터 포인트가 레이블을 가진다는 면에서 분류와 비슷함

### 그러나 정답을 모르고 있으며 레이블 자체에 의미가 없음

### 알고리즘이 우리에게 주는 정보는 각 레이블에 속한 데이터는 서로 비슷하다는 것





2. make_blobs 데이터로 그래프 그리기

mglearn.discrete_scatter(x[:, 0], x[:, 1], kmean.labels_, markers= 'o') # x, y, group, marker

mglearn.discrete_scatter(

    kmean.cluster_centers_[:, 0], kmean.cluster_centers_[:, 1], y=np.unique(kmean.labels_),# x, y, group

    markers='^', markeredgewidth=2) # marker, 두께

plt.show()

k-평균 알고리즘으로 찾은 세 개의 클러스터 중심과 클러스터 할당




3. 클러스터수를 변경하여 비교

fig, axes = plt.subplots(2, 2, gridspec_kw={'hspace':0.5}) # gridspec_kw: 그래프 수평간격

n_clusters_set = [2, 4, 5, 6] 

for n, ax in zip(n_clusters_set, axes.ravel()):

    kmeans = KMeans(n_clusters=n) # 클러스터 갯수

    kmeans.fit(x)

    mglearn.discrete_scatter(x[:, 0], x[:, 1], y=kmeans.labels_, ax=ax) # x, y, group, ax = plot객체

    mglearn.discrete_scatter(

        kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], y=np.unique(kmeans.labels_), # x, y, group

        markeredgewidth=3, ax=ax, s=15) # 두께, 그림객체, 사이즈

    ax.set_title('n_cluster: {}'.format(n)) # title

    ax.set_xlabel('feature 0')

    ax.set_ylabel('feature 1')

plt.show()

k-평균 알고리즘으로 클러스터를 여러개 사용했을 때의 중심과 클러스터 할당




4. k-평균 알고리즘이 실패하는 경우

데이터셋의 클러스터 갯수를 알고있더라도 k-평균 알고리즘이 항상 이를 구분해낼 수 있는 것은 아님

각 클러스터를 정의하는 것이 중심 하나뿐이므로 클러스터는 둥근 형태로 나타남

이런 이유로 k-평균 알고리즘은 비교적 간단한 형태를 구분할 수 있음

또한 k-평균은 모든 클러스터의 반경이 똑같다고 가정 ==> 클러스터 중심 사이에 정확히 중간에 경계를 그림


4-1 클러스터의 밀도가 다를 때

x1, y1 = make_blobs(n_samples=200, cluster_std=[1, 2.5, 0.5], random_state=170) # 샘플수, 밀도, 랜덤상태

y_pred = KMeans(n_clusters=3, random_state=0).fit_predict(x1) # 모델 생성 및 훈련 후 예측


mglearn.discrete_scatter(x1[:, 0], x1[:, 1], y=y_pred) # x, y, group

plt.legend(['cluster 0', 'cluster 1', 'cluster 2']) # 범례

plt.xlabel('feature 0')

plt.ylabel('feature 1')

plt.show()

클러스터의 밀도가 다를 때 k-평균으로 찾은 클러스터 할당


4-2 클러스터가 원형이 아닐  때

x2, y2 = make_blobs(n_samples=600, random_state=170) # 샘플 갯수, 램덤상태

rng = np.random.RandomState(74).normal(size=(2,2)) # 랜덤상태, (2,2)size로 normal분포의 숫자 무작위추출

x2_reshape = np.dot(x2, rng) # np.dot 행렬곱


kmeans = KMeans(n_clusters=3).fit(x2_reshape) # 모델 생성

y2_pred = kmeans.predict(x2_reshape) # 예측


mglearn.discrete_scatter(x2_reshape[:, 0], x2_reshape[:, 1], y=kmeans.labels_, alpha=0.7) # x, y , group, 투명도

mglearn.discrete_scatter(

    kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], y=np.unique(kmeans.labels_), # x, y, group

    markeredgewidth=3, s=15) # 두께, size

plt.xlabel('feature 0')

plt.ylabel('feature 1')

plt.show()

원형이 아닌 클러스터를 구분하지 못하는 k-평균 알고리즘


4-3 클러스터의 구조가 복합형태를 나타낼 때

from sklearn.datasets import make_moons


x3, y3 = make_moons(n_samples=200, noise=0.05, random_state=0) # 갯수, 퍼짐정도, 랜덤상태

kmeans = KMeans(n_clusters=2).fit(x3) # 모델 객체 생성 및 학습

y3_pred = kmeans.predict(x3) # 모델 적용


plt.scatter(x3[:, 0], x3[:, 1], c=y3_pred, s=60, edgecolors='black', cmap=mglearn.cm2) # x, y, group, 점크기 60%, 테두리색 black, palette

plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], # x, y

            marker='^', s=120, linewidth=2, edgecolors='k', c=[mglearn.cm2(0), mglearn.cm2(1)]) # marker,  점크기 120%, 두께, 테두리색, 색상 

plt.xlabel('feature 0')

plt.ylabel('feature 1')

plt.show()

반달 모양의 클러스터를 구분하지 못하는 k-평균 알고리즘


k-평균 알고리즘이 2개의 반달모양을 구분하지 못하고 원형으로 데이터를 비교






#!/usr/bin/env python3


t-SNE를 이용한 매니폴드 학습


데이터를 산점도로 시각화할 수 있다는 이점 때문에 PCA가 데이터 변환에 가장 먼저 시도해볼 만한 방법이지만,

LFW(Labeled Faces in the Wild) 데이터셋의 산점도에서 본 것처럼 알고리즘의(회전하고 방향을 제거하는) 유용성이 떨어짐

maniford learning매니폴드 학습 알고리즘이라고 하는 시각화 알고리즘들은 훨씬 복잡한 매핑을 만들어 더 나은 시각화를 제공

t-SNE(t-Distributed Stochastic Neighbor Embedding) 알고리즘을 아주 많이 사용함


maniford learnning 알고리즘은 그 모적이 시각화라 3개이상의 특성을 뽑는 경우는 거의 없음

t-SNE를 포함해서 일부 maniford learnning 알고리즘들은 훈련 데이터를 새로운 표현으로 변환시키지만 새로운 데이터에는 변활할 수 없음

즉 테스트세트에는 적용할 수 없고, 단지 훈련데이터에만 변환할 수 있음


따라서 maniford learnning은 탐색적 데이터 분석에 유용하지만 지도학습용으로는 거의 사용하지 않음


t-SNE의 아이디어는 데이터 포인트 사이의 거리를 가장 잘 보존하는 2차원 표현을 찾는 것

먼저 t-SNE는 각 데이터 포인트를 2차원에 무작위로 표현한 후 원본 특성 공간에서 가까운 포인트는 가깝게, 멀리 떨어진 포인트는 멀어지게 만듬

t-SNE는 가까이 있는 포인트에 더 많은 비중을 둠 ==> 이웃 데이터 포인트에 대한 정보를 보존하려함



1. 손글씨 숫자 데이터셋에 t-SNE maniford learnning을 적용

# load library

from sklearn.datasets import load_digits

import matplotlib

import matplotlib.pyplot as plt


# matplotlib 설정

matplotlib.rc('font', family='AppleGothic'# 한글 출력

plt.rcParams['axes.unicode_minus'] = False # 축 - 설정


# data load

digits = load_digits()


# subplot 객체 생성

fig, axes = plt.subplots(2, 5, #  subplot객체(2x5)를 axes에 할당

                         subplot_kw={'xticks':(), 'yticks':()}) # subplot 축 눈금 해제


for ax, img in zip(axes.ravel(), digits.images): # axes.ravel()과 digits.images를 하나씩 할당

    ax.imshow(img)

plt.gray() # 그래프 흑백

plt.show() # 그래프 출력

숫자 데이터셋의 샘플 이미지



2. PCA를 사용하여 데이터를 2차원으로 축소해 시각화

### 처음 두 개의 주성분을 이용해 그래프를 그리고 각 샘플을 해당하는 클래스의 숫자로


from sklearn.decomposition import PCA


# PCA 모델을 생성

pca = PCA(n_components=2) # 주성분 갯수

pca.fit(digits.data) # PCA 적용


# 처음 두 개의 주성분으로 숫자 데이터를 변환

digits_pca = pca.transform(digits.data) # PCA를 데이터에 적용

colors = ['#476A2A', '#7851B8', '#BD3430', '#4A2D4E', '#875525',

               '#A83683', '#4E655E', '#853541', '#3A3120', '#535D8E']


for i in range(len(digits.data)): # digits.data의 길이까지 정수 갯수

    # 숫자 텍스트를 이용해 산점도 그리기

    plt.text(digits_pca[i, 0], digits_pca[i, 1], str(digits.target[i]), # x, y, 그룹; str은 문자로 변환

             color=colors[digits.target[i]], # 산점도 색상

             fontdict={'weight':'bold', 'size':9}) # font 설정

plt.xlim(digits_pca[:, 0].min(), digits_pca[:,1].max()) # 최소, 최대

plt.ylim(digits_pca[:, 1].min(), digits_pca[:,1].max()) # 최소, 최대

plt.xlabel('first principle component'# x 축 이름

plt.ylabel('second principle componet'# y 축 이름

plt.show()

처음 두 개의 주성분을 사용한 숫자 데이터셋의 산점도


각 클래스가 어디 있는지 보기 위해 실제 숫자를 사용해 산점도를 출력

숫자 0, 6, 4는 두 개의 주성분만으로 잘 분리 됨

다른 숫자들은 대부분 많은 부분이 겹쳐 있음


3. 같은 데이터셋에 t-SNE를 적용해 결과를 비교

### TSNE모델에는 transform 메소드가 없고 fit_transform만 있음


# library import

from sklearn.manifold import TSNE


# t-SNE 모델 생성 및 학습

tsne = TSNE(random_state=0)

digits_tsne = tsne.fit_transform(digits.data)


# 시각화

for i in range(len(digits.data)): # 0부터  digits.data까지 정수

    plt.text(digits_tsne[i, 0], digits_tsne[i, 1], str(digits.target[i]), # x, y , 그룹

             color=colors[digits.target[i]], # 색상

             fontdict={'weight': 'bold', 'size':9}) # font

plt.xlim(digits_tsne[:, 0].min(), digits_tsne[:, 0].max()) # 최소, 최대

plt.ylim(digits_tsne[:, 1].min(), digits_tsne[:, 1].max()) # 최소, 최대

plt.xlabel('t-SNE 특성0'# x축 이름

plt.ylabel('t-SNE 특성1'# y축 이름

plt.show() # 그래프 출력

t-SNE로 찾은 두 개의 성분을 사용한 숫자 데이터셋의 산점도


모든 클래스가 확실히 잘 구분 되었음

이 알고리즘은 클래스 레이블 정보를 사용하지 않으므로 완전한 비지도 학습

그럼에도 원본 데이터 공간에서 포인트들이 얼마나 가가이 있는지에 대한 정보로 클래스가 잘 구분되는 2차원 표현을 찾음


t-SNE의 매개변수perplexity, early_exaggeration


perplexity: 값이 크면 더 많은 이웃을 포함하여 작은 그룹은 무시

                       기본값 = 30

                       보통 5~50사이의 값을 가짐


early_exaggeration: 원본 공간의 클러스터들이 얼마나 멀게 2차원에 나타낼지를 정함, 기본값은 4

                                         최소한 1보다 커야하고 값이 클수록 간격이 커짐

#!/usr/bin/env python3


얼굴이미지에 NMF 적용하기


NMF의 핵심 매개변수는 추출할 성분의 갯수

보통 이 값은 특성의 갯수보다 작음(픽셀하나가 두개의 성분으로 나뉘어 표현될 수 있음)


1. NMF를 사용하여 데이터를 재구성하는데 성분의 갯수가 어떤 영향을 주는지 살펴보기

# library import

from sklearn.datasets import fetch_lfw_people

import matplotlib

import matplotlib.pyplot as plt

import numpy as np

import mglearn


# matplotlib 설정

matplotlib.rc('font', family='AppleGothic'# 한글 설정

plt.rcParams['axes.unicode_minus'] = False # 축 -설정


people = fetch_lfw_people(min_faces_per_person=20, resize=0.7, color=False) # 겹치지 않는 최소사람 수, 사이즈비율, 흑백

images_shape = people.images[0].shape # 이미지 한개의 사이즈 저장


counts = np.bincount(people.target) # 갯수 새기

for i, (count, names) in enumerate(zip(counts, people.target_names)): # counts와 people.target_names에서 원소 하나씩 할당하여 인덱스 부여

    print('{0:25} {1:3}'.format(names, count), end='\t'# names에서 25자리, count에서 3자리, 끝에 탭할당

    if i % 3 == 0: # 3의 배수

        print() # 개행


idx = np.zeros(people.target.shape, dtype=np.bool) # False배열 3023개 설정

people_target_unq = np.unique(people.target) # target에서 unique값 할당

for target in people_target_unq: # perploe_target_unq에서 원소 하나씩 target에 할당

    idx[np.where(people.target == target)[0][:50]] = 1 # 전체 리스트 3023개에서 target과 일치하는 것의 수가를 50개 까지 True로 변경

x_people = people.data[idx] # 훈련 데이터

y_people = people.target[idx] # 테스트 데이터


# library import

from sklearn.preprocessing import MinMaxScaler

from sklearn.model_selection import train_test_split


scaler = MinMaxScaler()

x_people_scaled = scaler.fit_transform(x_people) # 훈련 데이터를 적용하여 전처리


x_train, x_test, y_train, y_test = \ 

  train_test_split(x_people_scaled, y_people, # 분할할 데이터

                   stratify=y_people, random_state=0, test_size=0.3) # 그룹화할데이터, 랜덤상태, 테스트 비율


mglearn.plots.plot_nmf_faces(x_train, x_test, images_shape)

plt.gray() # 그림 흑백

plt.show() # 그림 출력

NMF 성분 갯수에 따른 얼굴 이미지의 재구성


변환을 되돌린 결과는 PCA를 사용했을 때와 비슷하지만 품질이 떨어짐

PCA가 재구성 측면에서 최선의 방향을 찾기 때문

NMF는 데이터에 있는 유용한 패턴을 찾는데 활용


2. 비음수 성분 15개로 시각화

# library import , model 생성 , 학습

from sklearn.decomposition import NMF

nmf = NMF(n_components=15, random_state=0)

nmf.fit(x_train)


# 모델 적용

x_train_nmf = nmf.transform(x_train)

x_test_nmf = nmf.transform(x_test)


# 시각화

fig, axes = plt.subplots(3, 5, # axes에 plots객체를 3x5 만큼 할당

                         subplot_kw={'xticks':(), 'yticks':()}) # subplot 축 없애기


for i, (comp, ax) in enumerate(zip(nmf.components_, axes.ravel())): # nmf.components_, axes.ravel()에서 하나씩 comp와 ax에 인덱스부여

    ax.imshow(comp.reshape(images_shape)) # images_shape= (87, 65)

    ax.set_title('성분 {}'.format(i)) # subplot 타이틀

plt.gray() # 그림 흑백

plt.show()

얼굴 데이터셋에서 NMF로 찾은 성분 15개


이 성분들은 모두 양수 값이어서 PCA 성분보다 훨씬 더 얼굴 원형처럼 보임


비음수 성분이 특별히 강하게 나타난 이미지 분석(1)

### argsort : 자료를 정렬한 것의 index


idx = np.argsort(x_train_nmf[:, 3])[::-1] # [start : end : stpe] , step=-1 :내림차순

fig, axes = plt.subplots(2, 5, # subplots객체 (2x5) 를 axes에 할당

                         subplot_kw={'xticks':(), 'yticks':()}) # subplots에 축 없애기


for idx, ax in zip(idx, axes.ravel()): # idx와 aexs.ravel()을 하나씩 idx, ax에 할당

    ax.imshow(x_train[idx].reshape(images_shape))

plt.gray() # 그림 흑백

plt.show() # 그림 출력

성분 3의 계수가 큰 얼굴들 10개


비음수 성분이 특별히 강하게 나타난 이미지 분석(2)

idx = np.argsort(x_train_nmf[:, 10])[::-1] # [start : end : stpe] , step=-1 :내림차순

fig, axes = plt.subplots(2, 5, # subplots객체 (2x5) 를 axes에 할당

                         subplot_kw={'xticks':(), 'yticks':()}) # subplots에 축 없애기


for idx, ax in zip(idx, axes.ravel()): # idx와 aexs.ravel()을 하나씩 idx, ax에 할당

    ax.imshow(x_train[idx].reshape(images_shape)) 

plt.gray() # 그림 흑백

plt.show() # 그림 출력   

성분 10의 계수가 큰 얼굴들 10개


성분 3의 계수가 큰 얼굴은 왼쪽으로 돌아가 있고 성분 10의 계수가 큰 얼굴을 왼쪽으로 돌아가 있음을 확인

이런 패턴으로 데이터를 추출




#!/usr/bin/env python3


NMF(비음수 행렬 분해)


NMF(non-negative matrix factorization)는 유용한 특성을 뽑아내기 위한 또 다른 비지도 학습 알고리즘

NMF에서는 음수가 아닌 성분과 계수 값을 찾음.

즉 주성분과 계수가 모두 0보다 크거나 같아야 함 ==> 이 방식은 음수가 아닌 특성을 가진 데이터에만 적용


음수가 아닌 가중치 합으로 데이터를 분해하는 기능오디오 트랙이나 음악처럼 독립된 소스를 추가하여 만들어진 데이터에 특히 유용. 

이럴 때 NMF는 섞여 있는 데이터에서 원본 성분을 구분할 수 있음

음수로 된 성분이나 계수가 만드는 상쇄 효과를 이해하기 어려운 PCA보다 대체로 NMF의 주성분이 해석하기는 쉬움


인위적 데이터에 NMF 적용하기

NMF로 데이터를 다루려면 주어진 데이터가 양수인지 확인해야함

즉 원점 (0, 0)에서 상대적으로 어디에 놓여 있는지(위치 벡터)가 NMF에서는 중요함

그렇기 대문에 원점 (0, 0)에서 데이터로 가는 방향을 추출한 것으로 음수 미포함 성분을 이해가능


# library import

import mglearn

import matplotlib

import matplotlib.pyplot as plt


# matplotlib 설정

plt.rcParams['axes.unicode_minus'] = False # 축- 설정

matplotlib.rc('font', family='AppleGothic'# 한글출력


# 예제 데이터

mglearn.plots.plot_nmf_illustration()

plt.show() # 그래프 출력

NMF로 찾은 성분이 2개일때(왼쪽)와 1개일 때(오른쪽)


왼쪽은 성분이 둘인 NMF로, 데이터셋의 모든 포인트를 양수로 이뤄진 두 개의 성분으로 표현할 수 있음.

데이터를 완벽하게 재구성할 수 있을 만큼 성분이 아주 많다면(특성의 수가 많다면) 알고리즘은 데이터의 각 특성의 끝에 위치한 포인트를 가리키는 방향(위치벡터)을 선택


하나의 성분만을 사용한다면 NMF는 데이터를 가장 잘 표현할 수 있는 평균으로 향하는 성분을 만듬

PCA와는 반대로 성분 개수를 줄이면 특정 방향이 제거되고 전체성분이 완전 바뀜

NMF에서 성분은 특정 방식으로 정렬되어 있지도 않아서 첫번째, 두번째 같은 것이 없고 모든 성분은 동등하게 취급


NMF는 무작위로 초기화하기 때문에 난수 생성 초기값에 따라 결과가 달라짐 두 개의 성분으로 모든 데이터를 완벽하게 나타낼 수 있는 간단한 데이터에서는 난수가 거의 영향을 주지 않음(성분의 크기나 순서가 바뀔 수는 있음)


인위적인 데이터셋을 사용한 NMF분석

# library import

import mglearn

import matplotlib

import matplotlib.pyplot as plt


# matplotlib 설정

matplotlib.rc('font', family='AppleGothic'# 한글 출력

plt.rcParams['axes.unicode_minus'] = False # 축 - 설정


# data load

S = mglearn.datasets.make_signals() 


# visualization

plt.plot(S) # 라인그래프

plt.xlabel('time'# x 축

plt.ylabel('signal'# y 축

plt.show() # 그래프 출력

원본신호


3개가 섞인 신호만 관찰할 수 있는 상황

합쳐진 신호를 분해해서 원본 신호를 복원


NMF와 PCA를 통한 데이터 복원

# library import

from sklearn.decomposition import NMF 

from sklearn.decomposition import PCA

import numpy as np

A = np.random.RandomState(0).uniform(size=(100, 3)) # 100x3  형태의 균일분포에서 랜덤 데이터 생성

X = np.dot(S, A.T) # S(2000x3)에 A에 전치행렬(3X100)을 행렬 곱


print('X.shape \n측정데이터 형태:{}'.format(X.shape)) # (2000, 3)


nmf = NMF(n_components=3, random_state=0) # 주성분 3개, 랜덤상태

S_ = nmf.fit_transform(X) # nmf적용

print('S_.shape \n복원한 신호 데이터 형태: {}'.format(S_.shape)) # (2000, 3)


pca = PCA(n_components=3) # PCA주성분 3

H = pca.fit_transform(X) # PCA적용


# visualization

models = [X, S, S_, H] 

names = ['측정 신호(처음 3개)', '원본 신호', 'NMF로 복원' ,'PCA로 복원']


fig, axes = plt.subplots(4, # subplot의 객체4개를 axes에 할당

                         gridspec_kw={'hspace':0.5}, # 수평간격 비율

                         subplot_kw={'xticks':(), 'yticks':()}) # subplot의 축 눈금 없애기


for model, name, ax in zip(models, names, axes.ravel()): # models와 names, axes.ravel()에서 하나씩 선택

    ax.set_title(name) # 타이틀 이름

    ax.plot(model[:,:3]) # 모든행, :3 ==> 3개의 열

plt.show() # 그래프 출력

NMF와 PCA를 사용해 복원한 신호


NMF는 원본신호를 잘 복원했지만 PCA는 실패했고 데이터 변동의 대부분을 첫번째 성분을 사용해 나타냄

NMF로 생성한 성분은 순서가 없음을 알 수 있음

NMF 성분의 선수가 원본신호와 같지만 우연의 일치


PCA나 NMF처럼 데이터 포인트를 일정 개수의 성분을 사용해 가중치 합으로 분해하는 알고리즘은 다수 있음

ICA(독립 성분분석), FA(요인 분석), sparse coding(희소 분석)등이 있음 

    










#!/usr/bin/env python3


1. PCA -- eigen face


PCA는 원본 데이터 표현보다 분석하기에 더 적합한 표현을 찾을 수 있으리란 생각에서 출발

이미지는 RGB의 강도가 기록된 픽셀로 구성

PCA를 이용하여 LFW(Labeled Faces in the Wild) 데이터셋의 이미지에서 특성을 추출


# library import

from sklearn.datasets import fetch_lfw_people # fetch_lfw_people library

import matplotlib.pyplot as plt

import matplotlib

import numpy as np


# matplotlib 설정

matplotlib.rc('font', family='AppleGothic'# 한글출력

plt.rcParams['axes.unicode_minus'] = False # 축 - 표시


people = fetch_lfw_people(min_faces_per_person=20, resize=0.7, color=False# 추출된 사진의 서로 다른 얼굴의 수, 사진 비율, 흑백여부

image_shape = people.images[0].shape # people객체 image의 첫번째 원소의 모양

print('people.images의 형태 \n{}'.format(people.images.shape)) # people객체의 image 형태: (3023, 87, 65)

print('class 갯수 \n{}'.format(len(people.target_names))) # people객체의 class 갯수, 62


fig, axes = plt.subplots(2, 5, # figure객체와 2x5의 plot객체를 각각 할당

                         subplot_kw={'xticks': (), 'yticks': ()}) # subplot의 축 설정; x축 눈굼없게, y축 눈금없게


# axes.ravel() : 리스트를 1차원으로

for target, image, ax in zip(people.target, people.images, axes.ravel()): # people.target, people.images, axes.ravel()에서 하나씩 원소 할당

    ax.imshow(image) # imshow로 그림 출력

    ax.set_title(people.target_names[target]) # 각 subplot의 타이틀

plt.gray() # 그림 흑백

plt.show() # 그래프 출력

LFW 데이터셋에 있는 이미지의 샘플



1 -1 people 객체 분석

# 각 target이 나타난 횟수 계산

counts = np.bincount(people.target) # people.target의 빈도 계산


# target별 이름과 횟수 출력

### enumerate() : 각 원소에 인덱스 부여

### print('{0:25}, {1:3}').format(var1, var2):  첫번째 {}에 var1과 25자리까지, 두번째{}에 var2와 3자리를 부여

### print({0!s}.format(var1)): print문에 전달된 형식이 숫자라도 문자로 처리

print('frequency')

for i, (count, name) in enumerate(zip(counts, people.target_names)):

    print('{0:25} {1:3}'.format(name, count), end= '\t'# name에서 25자리, count에서 3자리, 끝은 탭

    if (i + 1) % 3 == 0: # 3의 배수이면, 즉 4번째 원소부터

        print() # 개행처리


# Geoge W Bush와 Colin Powell의 이미지가 많음을 확인


### 데이터셋의 편중을 막기 위해 50개의 이미지만 선택

mask = np.zeros(people.target.shape, dtype=np.bool) # 3023개의 boolean타입 False 생성


people_unique = np.unique(people.target) # 중복된 값 제외

for target in people_unique: # 중복을 제거한 target리스트에서 한개의 원소 선택

# people.target(3023개의 리스트)중 선택된 원소와 같은 것만 출력 ==> [0] 리스트의 원소로 접근 ==> False의 갯수 50개까지 출력

# 이 논리 값을 mask의 인덱스로 사용 후 True로 변환

    mask[np.where(people.target == target)[0][:50]] = 1 

x_people = people.data[mask] # 훈련 데이터 생성

y_people = people.target[mask] # 테스트 데이터 생성


# 전처리 메소드 import

from sklearn.preprocessing import MinMaxScaler


scaler = MinMaxScaler()

x_people_scaled = scaler.fit_transform(x_people) # 전처리 메소드 적용


# 머신 러닝 library import

from sklearn.neighbors import KNeighborsClassifier

from sklearn.model_selection import train_test_split


# 전처리한 데이터를 분할

x_train, x_test, y_train, y_test = \

  train_test_split(x_people_scaled, y_people, # 분할할 데이터

                   stratify=y_people, random_state=0) # 그룹화할 데이터, 랜덤상태


# 머신 러닝 라이브러리 import

knn = KNeighborsClassifier(n_neighbors=1) # 이웃의 수

knn.fit(x_train, y_train) # 모델 학습


print('knn.score(x_test, y_test) \n최근접이웃의 일반화 세트 점수: {:.3f}'.format(

    knn.score(x_test, y_test))) # 0.248

정확도가 24.8%, 무작위로 분류하는 정확도는 1/62 = 1.6%


얼굴의 유사도를 측정하기 위해 원본 픽셀 공간에서 거리를 계산하는 것은 비효율적

각 픽셀의 회색톤 값을 다른 이미지에서 동일한 위치에 있는 픽셀 값과 비교. 이런 방식은 사람이 얼굴 이미지를 인식하는 것과는 많이 다르고, 픽셀을 있는 그대로 비교하는 방식으로는 얼굴의 특징을 잡아내기 어려움


2. PCA whitening 분석


픽셀을 비교할 때 얼굴 위치가 한 픽셀만 오른쪽으로 이동해도 큰 차이를 만들어 완전히 다른얼굴로 인식

주성분으로 변환하여 거리를 계산하여 정확도를 높이는 방법을 사용, PCA whitening 옵션을 사용하여 주성분의 스케일이 같아지도록 조정 ==> whitening 옵션 없이 변환 후 StandardScaler를 적용하는 것과 같음


# library import

import mglearn

mglearn.plots.plot_pca_whitening()

plt.show()

whitening옵션을 사용한 PCA 데이터 변환


데이터가 회전하는 것뿐만 아니라 스케일도 조정되어 그래프가 원 모양으로 바뀜


2-1. PCA  머신 러닝

# library import

from sklearn.decomposition import PCA


# PCA 모델 생성 및 적용

pca = PCA(n_components=100, whiten=True, random_state=0) # 주성분 갯수, whitening option, 랜덤상태

pca.fit(x_train) # PCA 학습

x_train_pca = pca.transform(x_train) # PCA를 데이터에 적용

x_test_pca = pca.transform(x_test)


# PCA를 적용한 데이터 형태

print('x_train_pca.shape \ntrain형태:{}'.format(x_train_pca.shape)) # (1547, 100)

print('x_test_pca.shape \ntest형태:{}'.format(x_test_pca.shape)) # (516, 100)


# 머신 러닝 모델 생성 및 학습

knn = KNeighborsClassifier(n_neighbors=1) # 이웃의 수

knn.fit(x_train_pca, y_train) # 모델 학습

print('테스트 세트 정확도: \n{:.3f}'.format(knn.score(x_test_pca, y_test))) # 0.314


모델의 정확도가 23%에서 31%로 상승


이미지 데이터일 경우엔 계산한 주성분을 쉽게 시각화 가능

주성분이 나타내는 것은 입력 데이터 공간에서의 어떤 방향임을 기억

입력 차원이 87 x 65픽셀의 흑백 이미지이고 따라서 주성분또한 87 x 65


print('pca.components_.shape \n{}'.format(pca.components_.shape)) # (100, 5655)


fig, axes = plt.subplots(3, 5, # subplot 3x5를 axes에 할당

                         subplot_kw={'xticks': (), 'yticks': ()}) # subplot 축 설정


for i, (comp, ax) in enumerate(zip(pca.components_, axes.ravel())): # pca.components_와 axes.ravel()을 하나씩 순서대로 할당한 후 인덱스 부여

    ax.imshow(comp.reshape(image_shape)) # image_shape= (87, 65)

    ax.set_title('pricipal component {}'.format(i+1)) # image title

plt.gray() # 사진 흑백

plt.show() # 사진 출력

얼굴 데이터셋의 pricipal component중 처음 15개


첫 번째 주성분은 얼굴과 배경의 명암차이를 기록한 것으로 추정

두번째 주성분은 오른쪽과 왼쪽 조명의 차이를 담고 있는 것으로 보임

이런 방식이 원본 픽셀 값을 사용하는 것보다 더 의미 있지만, 사람이 얼굴을 인식하는 방식과는 거리가 멈

알고리즘이 데이터를 해석하는 방식은 사람의 방식과는 상당히 다름


PCA 모델을 이해하기 위해 몇개의 주성분을 사용해 원본 데이터를 재구성

몇 개의 주성분으로 데이터를 줄이고 원래 공간으로 되돌릴 수 있음

원래 특성 공간으로 되돌리는 작업은 inverse_transform 메소드를 사용


mglearn.plots.plot_pca_faces(x_train, x_test, image_shape) # 훈련데이터, 테스트데이터, 이미지크기(87x65)

plt.gray() # 그림 흑백

plt.show() # 그림 출력

주성분 갯수에 따른 세 얼굴 이미지의 재구성



'python 머신러닝 -- 비지도학습 > PCA(주성분 분석)' 카테고리의 다른 글

PCA  (0) 2018.03.19

#!/usr/bin/env python3


PCA


PCA주성분 분석은 특성들이 통계적으로 상관관계가 없도록 데이터를 회전시키는 것입니다.

회전한 뒤에 데이터를 설명하는 데 얼마나 중요하냐에 따라 새로운 특성 중 일부만 선택합니다.


다음은 PCA분석의 algorithm입니다.

import mglearn

import matplotlib

import matplotlib.pyplot as plt


matplotlib.rc('font', family='AppleGothic') # 한글출력

plt.rcParams['axes.unicode_minus'] = False # 축 - 표시


mglearn.plots.plot_pca_illustration()

plt.show()

PCA를 이용한 데이터 변환

왼쪽 위 그래프는 원본 데이터 포인트를 색으로 구분해 표시, 이 알고리즘은 먼저 'component1'이라고 쓰여 있는, Variant(분산)이 가장 큰 방향을 찾습니다. 이 방향(벡터)가 가장 많은 정보를 담고 있는 방향이고 ==> 특성들의 상관관계가 가장 큰 방향입니다.


그 다음에 이 algorithm은 첫 번째 방향과 직각인 방향 중에서 가장 많은 정보를 담은 방향을 찾습니다.

2차원에서는 직각 방향은 하나지만 3차원이상부터는 무수히 많은 직각 방향이 존재하고, 이 그래프에서 화살표 머리방향은 의미가 없습니다.


이런 방법으로 찾은 방향이 데이터의 주된 Variant(분산)의 방향이라해서 principal component(주성분)이라합니다.

일반적으로 원본 특성 개수만큼의 주성분 존재합니다.


오른쪽 위 그래프는 주성분1과 2를 각각 x축과 y축에 나란하도록 회전시켰습니다.

회전하기전에 데이터에서 평균을 빼서 중심을 원점에 맞춥니다.


PCA에 의해 회전된 두 축은 독립이므로 변환된 데이터의 correlation matirx(상관관계 행렬)이 대각선 방향(자기자신)을 제외하고는 0이 나옵니다.


PCA는 주성분의 일부만 남기는 차원 축소 용도로 사용할 수 있습니다. 왼쪽 아래 그림은 첫 번재 주성분만 유지하려고하며 2차원 데이터셋이 1차원 데이터셋으로 감소하지만 단순히 원본특성 중 하나만 남기는 것이 아니라, 첫번째 방향의 성분을 유지하도록 데이터를 가공합니다.


마지막으로 데이터에 다시 평균을 더해서 반대로 회전(오른쪽 아래 그림)

이 데이터들은 원래 특성 공간에 놓여 있지만 첫 번째 주성분의 정보만 담고 있음


이 변환은 데이터에서 노이즈를 제거하거나 주성분에서 유지되는 정보를 시각화하는데 사용됩니다.



PCA이 가장 널리 사용되는 분야는 고차원 데이터셋의 시각화영역입니다.

breast cancer와 같은 데이터셋은 특성이 30개나 있어서 30개중 2개를 택하는 경우의 수인 435개의 산점도를 그려야하므로 단순한 시각화가 비효율적입니다.


그러나 'malignant' 'benign' 두 클래스에 대해 각 특성의 히스토그램을 그리면 보다 쉽게 해석 가능

from sklearn.datasets import load_breast_cancer

import numpy as np


cancer = load_breast_cancer()


# 특성이 30개이므로  5X2 3set의 plot 객체 생성합니다.

# set 1

fig, axes = plt.subplots(5, 2)

malignant = cancer.data[cancer.target == 0]

benign = cancer.data[cancer.target == 1]

target_set = np.array([malignant, benign])


for i, ax in zip(np.arange(10), axes.ravel()):

    for t in target_set:

        _, bins = np.histogram(cancer.data[:, i], bins=50) # bins: histogram 간격

        ax.hist(t[:, i], bins=bins, alpha=0.5)

        ax.set_title(cancer.feature_names[i])

        ax.set_yticks(()) 

        ax.set_xlabel('feature size') 

        ax.set_ylabel('frequency')

axes[0, 0].legend(['malignant', 'benign'], loc=(0.85, 1.1), bbox_to_anchor=(0.85, 1.1), fancybox=True, shadow=True)

fig.tight_layout()

plt.show()

특성 1 ~ 10까지의 breast cancer 히스토그램


# set 2

fig, axes = plt.subplots(5, 2) 


for i, ax in zip(np.arange(10, 20), axes.ravel()):

    for t in target_set:

        _, bins = np.histogram(cancer.data[:,i], bins=50) 

        ax.hist(t[:, i], bins=bins, alpha=0.5)

        ax.hist(benign[:, i], bins=bins, alpha=0.5)

        ax.set_title(cancer.feature_names[i])

        ax.set_yticks(())

fig.tight_layout()

plt.show()

특성 11 ~ 20까지의 breast cancer의 히스토그램


# set 3

fig, axes = plt.subplots(5, 2) 


for i, ax in zip(np.arange(20, 30), axes.ravel()):

    for t in target_set:

        _, bins = np.histogram(cancer.data[:,i], bins=50)

        ax.hist(malignant[:, i], bins=bins, alpha=0.5)

        ax.hist(benign[:, i], bins=bins, alpha=0.5)

        ax.set_title(cancer.feature_names[i])

        ax.set_yticks(())

fig.tight_layout()

plt.show()

특성 21 ~ 30까지의 breast cancer의 히스토그램

각 특성에 대해 특정 간격(bin)에 얼마나 많은 데이터 포인트가 나타나는지 횟수를 나타냈습니다.

'smoothness error' 특성은 두 히스토그램이 거의 겹쳐져 불필요한 특성이고

'worst concave point'는 두 히스토그램이 확실히 구분되어 매우 유용한 특성입니다.


그러나 이 그래프는 특성 간의 상호작용이나 상호작용이 클래스와 어떤 관련이 있는지는 알려주지 못합니다.



PCA 주성분분석으로 breast cancer 데이터를 분석해보겠습니다.

PCA를 사용하면 주요 상호작용을 찾아낼 수 있어 데이터 분석에 더 용이합니다.

PCA를 사용하기전 StandardScaler사용(평균=0, 분산=1)하여 전처리를 하겠습니다.

from sklearn.preprocessing import StandardScaler


scaler = StandardScaler()

scaler.fit(cancer.data)

x_scaled = scaler.transform(cancer.data)


# 기본값일 때 PCA는 데이터를 회전과 이동만 시키고 모든 주성분을 유지합니다.

# 데이터의 차원을 줄이려면 PCA객체를 만들 때 얼마나 많은 성분을 유지할지 알려주어야 합니다.

from sklearn.decomposition import PCA


pca = PCA(n_components=2)

pca.fit(x_scaled)

x_pca = pca.transform(x_scaled)


x_scaled_shape =  x_scaled.shape

print('{}'.format(x_scaled_shape))

# (569, 30)


x_pca_shape = x_pca.shape

print('{}'.format(x_pca_shape))

# (569, 2)


targets = np.unique(cancer.target)

markers = ['^', 'o']

for target, marker in zip(targets, markers):

    plt.scatter(x_pca[cancer.target==target][:, 0], x_pca[cancer.target==target][:, 1],

                s=20, alpha=0.7, marker=marker, edgecolors='k')


plt.legend(['malgnent', 'reglgent'], loc=1)

plt.gca().set_aspect('equal') # x축과 y축의 길이를 같게합니다.

plt.xlabel('first component', size=15)

plt.ylabel('second component', size=15)

plt.show()

두 개의 주성분을 사용해 그린 breast cancer 2차원 scatter plot


PCA의 단점은 그래프의 두 축을 해석하기가 쉽지 않다는 것입니다.

주성분은 원본데이터에 있는 어떤 방향에 대응하는 여러 특성이 조합된 형태이며

PCA객체가 학습될 때 components_ 속성에 주성분이 저장됩니다.

주성분의 구성 요소

print('{}'.format(pca.components_.shape))

# (2, 30)

print('{}'.format(pca.components_)) # 주성분 출력

# [[ 0.21890244  0.10372458  0.22753729  0.22099499  0.14258969  0.23928535

#    0.25840048  0.26085376  0.13816696  0.06436335  0.20597878  0.01742803

#    0.21132592  0.20286964  0.01453145  0.17039345  0.15358979  0.1834174

#    0.04249842  0.10256832  0.22799663  0.10446933  0.23663968  0.22487053

#    0.12795256  0.21009588  0.22876753  0.25088597  0.12290456  0.13178394]

#  [-0.23385713 -0.05970609 -0.21518136 -0.23107671  0.18611302  0.15189161

#    0.06016536 -0.0347675   0.19034877  0.36657547 -0.10555215  0.08997968

#   -0.08945723 -0.15229263  0.20443045  0.2327159   0.19720728  0.13032156

#    0.183848    0.28009203 -0.21986638 -0.0454673  -0.19987843 -0.21935186

#    0.17230435  0.14359317  0.09796411 -0.00825724  0.14188335  0.27533947]]

components_의 각 행은 주성분 하나씩을 나타내며 중요도에 따라 정렬되어 있습니다.

열은 원본 데이터의 특성에 대응하는 값입니다.



히트맵으로 그려보면

from mpl_toolkits.axes_grid1 import make_axes_locatable


image = plt.matshow(pca.components_, cmap='viridis')

plt.yticks([0, 1], ['first', 'second'])

plt.xticks(range(len(cancer.feature_names)), cancer.feature_names,

           rotation=90, ha='left')

plt.xlabel('feature', size=15)

plt.ylabel('principal component', size=15)


ax = plt.gca() # GetCurrentAxis

divider = make_axes_locatable(ax)

cax = divider.append_axes('right', size='5%', pad='5%')

plt.colorbar(image, cax=cax)

plt.show()

breast cancer에서 찾은 처음 두개의 주성분 히트맵

첫번째 주성분의 모든 특성은 부호가 같음, 모든 특성 사이에 양의 상관관계가 있습니다.

두번째 주성분은 부호가 섞여있음을 알 수 있습니다.

모든 특성이 섞여 있기 때문에 축이 가지는 의미를 알기가 어려습니다.



참고 자료: 

[1]Introduction to Machine Learning with Python, Sarah Guido

+ Recent posts