728x90

[SAS]

DATA iris;INFILE '/home/joinos0/sas_class/iris.csv' DLM=',' FIRSTOBS=2;
LENGTH Species $15;
INPUT sepal_length sepal_width petal_length petal_width species $;
PROC PRINT;
RUN;

DATA iris1;SET iris;
IF _N_ <= 10;
PROC CLUSTER DATA=iris1 METHOD=SINGLE;                     
PROC TREE HORIZONTAL SPACES=2;
RUN;

 

[R]

head(iris)
dist(iris[1:10,1:4])                 # 편의상 7개 관측치 간의 거리를 구한다...
round(dist(iris[1:10,1:4]),digits=4) # 소수7자리 -> 소수 4 자리
dist01 <- round(dist(iris[1:10,1:4]),digits=4);dist01 # 거리를 객체 dis01 저장 
model_hc <- hclust(dist01, method="ave")
plot(model_hc)
plot(model_hc,hang=-1)

[Python]

from sklearn.datasets import load_iris
iris = load_iris()
# 계층적 군집 model
from scipy.cluster.hierarchy import linkage, dendrogram
clusters = linkage(y=iris['data'], method='complete', metric='euclidean')
clusters
clusters.shape # (149, 4)
import matplotlib.pyplot as plt
plt.figure( figsize = (25, 10) )
dendrogram(clusters, leaf_rotation=90, leaf_font_size=12,)
# leaf_rotation=90 : 글자 각도
# leaf_font_size=20 : 글자 사이즈
plt.show() 

'SAS, R, Python 일반 > 28. 군집분석' 카테고리의 다른 글

(S)제28강(01)_군집분석이란?  (0) 2021.12.28
(S)제28강(00)_군집분석 목차  (0) 2021.12.28

+ Recent posts