본문 바로가기
knowledge/statistics

군집분석 : hierarchical clustering in python

by 앙냥냥냥 2020. 6. 9.

Hierarchical Clustering

hierarchical clustering, 계층적 군집분석은 요소를 단계별로 계속해서 군집화하며 그리는(혹은 거꾸로 해체하며 그리는) 덴드로그램을 통해 데이터를 클러스터링 하는 기법이다.

 

#    1   2   3   4   5   6   7   8   9   10
x = [10, 12, 5,  10, 19, 28, 30, 1,  6,  7]
y = [12, 23, 13, 17, 16, 8,  16, 30, 20, 16]

 

산점도와 dendrogram을 그리기위해 라이브러리 import

 

from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt

 

 

산점도

A scatter plot (also called a scatterplot, scatter graph, scatter chart, scattergram, or scatter diagram) is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data. If the points are coded (color/shape/size), one additional variable can be displayed. The data are displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis.

간단하게 말해서, 좌표상에 데이터를 나열하고 데이터 간의 관계나 경향성을 파악하는 것.

 

    plt.scatter(x,y)

Dendrogram

A dendrogram is a diagram representing a tree. This diagrammatic representation is frequently used in different contexts:

  • in hierarchical clustering, it illustrates the arrangement of the clusters produced by the corresponding analyses.
  • in computational biology, it shows the clustering of genes or samples, sometimes in the margins of heatmaps.
  • in phylogenetics, it displays the evolutionary relationships among various biological taxa. In this case, the dendrogram is also called a phylogenetic tree.

 

dendrogram(
    linkage([(x[i], y[i]) for i in range(len(x))]), 
    labels=range(1, len(x)+1)
)

 

1. Nearest Point Algorithm

두 군집 데이터 중 가장 가까운 데이터 간의 거리를 계산하여 군집 간의 거리로 정의한다. linkage 설정 시에 method='single'을 변수로 넘겨주면 된다.(or default 값이 single이라 아무 인자도 넘겨주지 않을 시 single로 linkage 된다.)

 

dendrogram(
    linkage([(x[i], y[i]) for i in range(len(x))], method='single'), 
    labels=range(1, len(x)+1)
)

 

 

 

 

2. Farthest Point Algorithm

두 군집 데이터 중 가장 먼 데이터간의 거리를 계산하여 군집 간의 거리로 정의한다. method ='complete'로 인자를 넘겨주면 설정된다

 

dendrogram(
    linkage([(x[i], y[i]) for i in range(len(x))], method='complete'), 
    labels=range(1, len(x)+1)
)

 

 

 

3. UPGMA algorithm

두 군집 데이터의 평균값간의 거리를 계산하여 군집 간의 거리로 정의한다. method=average로 인자를 넘겨주면 설정된다

 

dendrogram(
    linkage([(x[i], y[i]) for i in range(len(x))], method='average'), 
    labels=range(1, len(x)+1)
)

 

4. WPGMA algorithm

상위 군집을 구성하는 하위 군집들과 다른 상위 군집에 대한 거리의 평균값으로 두 군집 간의 거리를 정의한다.
method= 'weighted'로 인자 넘겨주면 설정된다.

 

dendrogram(
    linkage([(x[i], y[i]) for i in range(len(x))], method='weighted'), 
    labels=range(1, len(x)+1)
)

 

5. UPGMC algorithm

군집을 이루는 모든 값의 평균값의 거리로 두 군집간의 거리를 정의한다. method = 'centroid'로 넘겨주면 설정된다.

 

dendrogram(
    linkage([(x[i], y[i]) for i in range(len(x))], method='centroid'), 
    labels=range(1, len(x)+1)
)

 

6. WPGMC algorithm

재귀적으로 군집을 구성하는 군집들의 중심점 평균으로 군집 중심점 값을 구하고, 중심점 값의 산술평균으로 두 군집 간의 거리를 정의한다. method = 'median' 로 넘겨주면 설정된다.

 

dendrogram(
    linkage([(x[i], y[i]) for i in range(len(x))], method='median'), 
    labels=range(1, len(x)+1)
)

 

7. incremental algorithm

클러스터 내의 분산을 최소화하기 위한 방법으로, 가중 거리를 계산하면서 군집 결합도를 빼주는 방식으로 두 군집 간의 거리를 정의한다. 군집 중심 사이의 가중 제곱 거리를 최소화하는 방식으로 계산한다.

 

dendrogram(
    linkage([(x[i], y[i]) for i in range(len(x))], method='ward'), 
    labels=range(1, len(x)+1)
)

 

 

Refenrence