์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- dataframe
- ๋ฐ์ดํฐ๋ถ์
- ์๋ํด๋ผ์ฐ๋
- PCA
- ์ค๋ฒ์ํ๋ง
- ํ์ด์ฌ
- numpy
- iloc
- ADP
- ๊ตฐ์งํ
- t-test
- ์ฃผ์ฑ๋ถ๋ถ์
- pandas
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- ํฌ๋กค๋ง
- ๋ ๋ฆฝํ๋ณธ
- ๋์ํ๋ณธ
- ๋น ๋ฐ์ดํฐ
- datascience
- Lambda
- ์ธ๋์ํ๋ง
- ADsP
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- Python
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- ํ ์คํธ๋ถ์
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- DBSCAN
- LDA
- opencv
Data Science LAB
[Python] ๊ตฐ์ง ํ๊ฐ(์ค๋ฃจ์ฃ ๊ณ์) ๋ณธ๋ฌธ
[Python] ๊ตฐ์ง ํ๊ฐ(์ค๋ฃจ์ฃ ๊ณ์)
ใ ใ ใ ใ 2022. 3. 1. 21:10Clustering Evaluation
iris ๋ฐ์ดํฐ์ ์๋ ๊ฒฐ๊ณผ๊ฐ์ ํ์ข ์ ์๋ฏธํ๋ ํ๊น ๋ ์ด๋ธ์ด ์์ด ๊ตฐ์งํ๊ฐ ์ผ๋ง๋ ์ ์ด๋ฃจ์ด์ ธ ์๋ ์ง ํ๋จํ ์ ์์๋ค. ํ์ง๋ง ๋๋ถ๋ถ์ ๊ตฐ์งํ ๋ฐ์ดํฐ์ ์๋ ํ๊น ๋ ์ด๋ธ์ด ์กด์ฌํ์ง ์๊ธฐ ๋๋ฌธ์, ๋ฐ์ดํฐ ๋ด์ ์จ์ด ์๋ ๋ณ๋์ ๊ทธ๋ฃน์ ์ฐพ์ ์๋ฏธ๋ฅผ ๋ถ์ฌํ๊ณ ๋, ๋์ผํ ๋ถ๋ฅ ๊ฐ์ ์ํ๋๋ผ๋ ๊ทธ ์์์ ๋ ์ธ๋ถํ๋ ๊ตฐ์งํ๋ฅผ ์ถ๊ตฌํ๊ฑฐ๋, ์๋ก ๋ค๋ฅธ ๋ถ๋ฅ๊ฐ์ ๋ฐ์ดํฐ๋ ๋ ๋์ ๊ตฐ์งํ ๋ ๋ฒจํ ๋ฑ์ ์์ญ์ ๊ฐ์ง๊ณ ์๋ค. ๋น์ง๋ํ์ต์ ํน์ฑ ์ ์ ํํ๊ฒ ์ฑ๋ฅ์ ํ๊ฐํ๊ธฐ๋ ์ด๋ ต์ง๋ง, ๊ตฐ์งํ์ ์ฑ๋ฅ์ ํ๊ฐํ๋ ๋ํ์ ์ธ ๋ฐฉ๋ฒ์ผ๋ก ์ค๋ฃจ์ฃ ๋ถ์์ ์ฌ์ฉํ๋ค.
Silhouette analysis
์ค๋ฃจ์ฃ ๋ถ์์ด๋ ๊ฐ ๊ตฐ์ง ๊ฐ์ ๊ฑฐ๋ฆฌ๊ฐ ์ผ๋ง๋ ํจ์ก์ ์ผ๋ก ๋ถ๋ฆฌ๋์ด ์๋ ์ง๋ฅผ ๋ํ๋ธ๋ค. ํจ์จ์ ์ผ๋ก ๋ถ๋ฆฌ๋์ด ์๋ ๊ฒ์ ๋ค๋ฅธ ๊ตฐ์ง๊ณผ๋ ๊ฑฐ๋ฆฌ๊ฐ ๋ฉ๊ณ , ๋์ผ ๊ตฐ์ง ๋ด์์๋ ์๋ก ๊ฐ๊น๊ฒ ์ ๋ญ์ณ ์๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
์ค๋ฃจ์ฃ ๋ถ์์ ์ค๋ฃจ์ฃ ๊ณ์(silhouette coefficient)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ค. ์ค๋ฃจ์ฃ ๊ณ์๋ ๊ฐ๋ณ ๋ฐ์ดํฐ๊ฐ ๊ฐ์ ๊ตฐ์ง ๋ด์ ๋ฐ์ดํฐ์๋ ์ผ๋ง๋ ๊ฐ๊น๊ฒ ๊ตฐ์งํ ๋์ด ์๊ณ , ๋ค๋ฅธ ๊ตฐ์ง์ ๋ฐ์ดํฐ์๋ ์ผ๋ง๋ ๋ฉ๋ฆฌ ๋ถ๋ฆฌ ๋์ด ์๋ ์ง๋ฅผ ๋ํ๋ด๋ ์งํ์ด๋ค.
a(i) : ํด๋น ๋ฐ์ดํฐ ํฌ์ธํธ์ ๊ฐ์ ๊ตฐ์ง ๋ด์ ์๋ ๋ค๋ฅธ ๋ฐ์ดํฐ ํฌ์ธํธ์์ ๊ฑฐ๋ฆฌ๋ฅผ ํ๊ท ํ ๊ฐ
b(i) : ํด๋น ๋ฐ์ดํฐ ํฌ์ธํธ๊ฐ ์ํ์ง ์์ ๊ตฐ์ง ์ค ๊ฐ์ฅ ๊ฐ๊น์ด ๊ตฐ์ง๊ณผ์ ํ๊ท ๊ฑฐ๋ฆฌ
๋ ๊ตฐ์ง๊ฐ์ ๊ฑฐ๋ฆฌ๊ฐ ์ผ๋ง๋ ๋จ์ด์ ธ ์๋ ๊ฐ์ ๊ฐ์ b(i)-a(i) ์ด๋ฉฐ, ์ด ๊ฐ์ ์ ๊ทํ ํ๊ธฐ ์ํด MAX(a(i),b(i))๊ฐ์ผ๋ก ๋๋๋ค.
๋ฐ๋ผ์ i๋ฒ์งธ ๋ฐ์ดํฐ ํฌ์ธํธ์ ์ค๋ฃจ์ฃ ๊ณ์๊ฐ s(i)๋ ์์ ๊ฐ์ด ์ ์ํ๋ค.
์ค๋ฃจ์ฃ ๊ณ์๋ -1~1์ฌ์ด์ ๊ฐ์ ๊ฐ์ง๋ฉฐ, 1์ ๊ฐ๊น์ธ ์๋ก ๊ทผ์ฒ์ ๊ตฐ์ง๊ณผ ๋ ๋ฉ๋ฆฌ ๋จ์ด์ ธ ์๋ ๊ฒ์ ์๋ฏธ,
0์ ๊ฐ๊น์ธ์๋ก ๊ทผ์ฒ์ ๊ตฐ์ง๊ณผ ๊ฐ๊น์์ง๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
-๊ฐ์ ์์ ๋ค๋ฅธ ๊ตฐ์ง์ ๋ฐ์ดํฐ ํฌ์ธํธ๊ฐ ํ ๋น๋์์ ์๋ฏธํ๋ค.
์ข์ ๊ตฐ์งํ ์กฐ๊ฑด
- ์ ์ฒด ์ค๋ฃจ์ฃ ๊ณ์์ ํ๊ท ๊ฐ, ์ฆ ์ฌ์ดํท๋ฐ์ silhouette_score()๊ฐ์ด 0~1์ฌ์ด์ด๋ฉฐ, 1์ ๊ฐ๊น์ธ ์๋ก ์ข๋ค.
- ์ ์ฒด ์ค๋ฃจ์ฃ ๊ณ์์ ํ๊ท ๊ฐ๊ณผ ๊ฐ๋ณ ๊ตฐ์ง์ ํ๊ท ๊ฐ์ ํธ์ฐจ๊ฐ ํฌ์ง ์์์ผ ํ๋ค. ์ฆ, ๊ฐ๋ณ ๊ตฐ์ง์ ์ค๋ฃจ์ฃ ๊ณ์ ํ๊ท ๊ฐ์ด ์ ์ฒด ์ค๋ฃจ์ฃ ๊ณ์์ ํ๊ท ๊ฐ์์ ํฌ๊ฒ ๋ฒ์ด๋์ง ์์์ผ ํ๋ค.
iris ๋ฐ์ดํฐ ์ ์ ์ด์ฉํ ๊ตฐ์ง ํ๊ฐ
sklearn.metircs ๋ชจ๋์ silhouette_samples()์ silhouette_score()์ด์ฉ
from sklearn.preprocessing import scale
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_samples, silhouette_score
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline
iris = load_iris()
feature_names = ['sepal_length','sepal_width','petal_length','petal_width']
irisdf = pd.DataFrame(data = iris.data,columns = feature_names)
kmeans = KMeans(n_clusters=3,init='k-means++',max_iter=300,random_state=0).fit(irisdf)
irisdf['cluster'] = kmeans.labels_
#iris์ ๋ชจ๋ ๊ฐ๋ณ ๋ฐ์ดํฐ์ ์ค๋ฃจ์ฃ ๊ณ์ ๊ฐ์ ๊ตฌํจ
score_samples = silhouette_samples(iris.data,irisdf['cluster'])
print('์ค๋ฃจ์ฃ return ๊ฐ์ shape : ',score_samples.shape)
#irisdf์ ์ค๋ฃจ์ฃ ๊ณ์ ์นผ๋ผ ์ถ๊ฐ
irisdf['silhouette'] = score_samples
#๋ชจ๋ ๋ฐ์ดํฐ์ ํ๊ท ์ค๋ฃจ์ฃ ๊ณ์ ๊ฐ
average_score = silhouette_score(iris.data,irisdf['cluster'])
print('๋ถ๊ฝ ๋ฐ์ดํฐ ์
Silhoutte Anaylsis Score : {:.3f}'.format(average_score))
irisdf.head()
ํ๊ท ์ค๋ฃจ์ฃ ๊ณ์ ๊ฐ์ 0.553์ด์ง๋ง, 1๋ฒ ๊ตฐ์ง์ ์ค๋ฃจ์ฃ ๊ณ์ ๊ฐ์ 0.8์ด์์ผ๋ก ๋๊ฒ ๋ํ๋๋ค.
irisdf.groupby('cluster')['silhouette'].mean()
๊ตฐ์ง ์ปฌ๋ผ๋ณ๋ก group by ํ์ฌ ์ค๋ฃจ์ฃ ๊ณ์์ ํ๊ท ๊ฐ์ ์ถ๋ ฅํด ๋ณด๋ 1๋ฒ ๊ตฐ์ง์ 0.79์ด์ง๋ง 0๋ฒ ๊ตฐ์ง๊ณผ 2๋ฒ ๊ตฐ์ง์ 0.4๋ก ์๋์ ์ผ๋ก ํ๊ท ๊ฐ์ด 1๋ฒ์ ๋นํ์ฌ ๋ฎ์ ๊ฒ์ ํ์ธํ ์ ์์๋ค.
๊ตฐ์ง๋ณ ํ๊ท ์ค๋ฃจ์ฃ ๊ณ์์ ์๊ฐํ๋ฅผ ํตํ ๊ตฐ์ง ๊ฐ์ ์ต์ ํ ๋ฐฉ๋ฒ
์ ์ฒด ๋ฐ์ดํฐ์ ํ๊ท ์ค๋ฃจ์ฃ ๊ณ์ ๊ฐ์ด ๋๋ค๊ณ ํด์ ๋ฐ๋์ ์ต์ ๊ตฐ์ง ๊ฐ์๋ก ๊ตฐ์งํ๊ฐ ์ ๋๋ค๊ณ ๋ณผ ์๋ ์๋ค.
๊ฐ๋ณ ๊ตฐ์ง๋ณ๋ก ์ ๋นํ ๋ถ๋ฆฌ๋ ๊ฑฐ๋ฆฌ๋ฅผ ์ ์งํ๋ฉด์ ๊ตฐ์ง ๋ด์ ๋ฐ์ดํฐ๊ฐ ์๋ก ๋ญ์ณ ์๋ ๊ฒฝ์ฐ์ KMeans์ ์ ์ ํ ๊ตฐ์ง ๊ฐ์๊ฐ ์ค์ ๋์๋ค๊ณ ํ๋จํ ์ ์๋ค.
์ค๋ฃจ์ฃ ๊ณ์ ์๊ฐํ ํจ์
def visualize_silhouette(cluster_lists, X_features):
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_samples, silhouette_score
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import math
# ์
๋ ฅ๊ฐ์ผ๋ก ํด๋ฌ์คํฐ๋ง ๊ฐฏ์๋ค์ ๋ฆฌ์คํธ๋ก ๋ฐ์์, ๊ฐ ๊ฐฏ์๋ณ๋ก ํด๋ฌ์คํฐ๋ง์ ์ ์ฉํ๊ณ ์ค๋ฃจ์ฃ ๊ฐ์๋ฅผ ๊ตฌํจ
n_cols = len(cluster_lists)
# plt.subplots()์ผ๋ก ๋ฆฌ์คํธ์ ๊ธฐ์ฌ๋ ํด๋ฌ์คํฐ๋ง ์๋งํผ์ sub figures๋ฅผ ๊ฐ์ง๋ axs ์์ฑ
fig, axs = plt.subplots(figsize=(4*n_cols, 4), nrows=1, ncols=n_cols)
# ๋ฆฌ์คํธ์ ๊ธฐ์ฌ๋ ํด๋ฌ์คํฐ๋ง ๊ฐฏ์๋ค์ ์ฐจ๋ก๋ก iteration ์ํํ๋ฉด์ ์ค๋ฃจ์ฃ ๊ฐ์ ์๊ฐํ
for ind, n_cluster in enumerate(cluster_lists):
# KMeans ํด๋ฌ์คํฐ๋ง ์ํํ๊ณ , ์ค๋ฃจ์ฃ ์ค์ฝ์ด์ ๊ฐ๋ณ ๋ฐ์ดํฐ์ ์ค๋ฃจ์ฃ ๊ฐ ๊ณ์ฐ.
clusterer = KMeans(n_clusters = n_cluster, max_iter=500, random_state=0)
cluster_labels = clusterer.fit_predict(X_features)
sil_avg = silhouette_score(X_features, cluster_labels)
sil_values = silhouette_samples(X_features, cluster_labels)
y_lower = 10
axs[ind].set_title('Number of Cluster : '+ str(n_cluster)+'\n' \
'Silhouette Score :' + str(round(sil_avg,3)) )
axs[ind].set_xlabel("The silhouette coefficient values")
axs[ind].set_ylabel("Cluster label")
axs[ind].set_xlim([-0.1, 1])
axs[ind].set_ylim([0, len(X_features) + (n_cluster + 1) * 10])
axs[ind].set_yticks([]) # Clear the yaxis labels / ticks
axs[ind].set_xticks([0, 0.2, 0.4, 0.6, 0.8, 1])
# ํด๋ฌ์คํฐ๋ง ๊ฐฏ์๋ณ๋ก fill_betweenx( )ํํ์ ๋ง๋ ๊ทธ๋ํ ํํ.
for i in range(n_cluster):
ith_cluster_sil_values = sil_values[cluster_labels==i]
ith_cluster_sil_values.sort()
size_cluster_i = ith_cluster_sil_values.shape[0]
y_upper = y_lower + size_cluster_i
color = cm.nipy_spectral(float(i) / n_cluster)
axs[ind].fill_betweenx(np.arange(y_lower, y_upper), 0, ith_cluster_sil_values, \
facecolor=color, edgecolor=color, alpha=0.7)
axs[ind].text(-0.05, y_lower + 0.5 * size_cluster_i, str(i))
y_lower = y_upper + 10
axs[ind].axvline(x=sil_avg, color="red", linestyle="--")
# make_blobs ์ ํตํด clustering ์ ์ํ 4๊ฐ์ ํด๋ฌ์คํฐ ์ค์ฌ์ 500๊ฐ 2์ฐจ์ ๋ฐ์ดํฐ ์
์์ฑ
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=500, n_features=2, centers=4, cluster_std=1, \
center_box=(-10.0, 10.0), shuffle=True, random_state=1)
# cluster ๊ฐ์๋ฅผ 2๊ฐ, 3๊ฐ, 4๊ฐ, 5๊ฐ ์ผ๋์ ํด๋ฌ์คํฐ๋ณ ์ค๋ฃจ์ฃ ๊ณ์ ํ๊ท ๊ฐ์ ์๊ฐํ
visualize_silhouette([ 2, 3, 4, 5], X)
๊ตฐ์ง์ด ๊ฐ๊ฐ 2,3,4,5๊ฐ์ผ ๋์ ์ค๋ฃจ์ฃ ๊ณ์๋ฅผ ์๊ฐํ ํด๋ณธ ๊ฒฐ๊ณผ 4๊ฐ๋ก ๊ตฐ์งํํ์์ ๋ ๊ฐ์ฅ ์ด์์ ์ด๋ผ๊ณ ํ ์ ์๋ค.
1) ๊ตฐ์ง์ด 2๊ฐ์ผ ๋
1๋ฒ ๊ตฐ์ง์ 0๋ฒ ๊ตฐ์ง๊ณผ ๋ฉ๋ฆฌ ๋จ์ด์ ธ ์๊ณ , ๋ด๋ถ ๋ฐ์ดํฐ๋ผ๋ฆฌ๋ ์ ๋ญ์ณ ์์ง๋ง, 0๋ฒ ๊ตฐ์ง์ ๋ด๋ถ ๋ฐ์ดํฐ๋ผ๋ฆฌ๋ ๋ง์ด ๋จ์ด์ ธ ์๋ค.
2) ๊ตฐ์ง์ด 3๊ฐ์ผ ๋
1๋ฒ ๊ตฐ์ง๊ณผ 2๋ฒ ๊ตฐ์ง์ ์ค๋ฃจ์ฃ ๊ณ์๊ฐ ํ๊ท ๊ฐ ์ด์์ด๋ฉฐ ๊ตฐ์ง์ ๋ด๋ถ ๋ฐ์ดํฐ๋ค์ด ์ ๋ญ์ณ์ ธ ์์ง๋ง,
0๋ฒ ๊ตฐ์ง์ ์ค๋ฃจ์ฃ ๊ณ์๋ ํ๊ท ์ดํ์ด๋ฉฐ ๋ฉ๋ฆฌ ํผ์ ธ ์๋ค.
3) ๊ตฐ์ง์ด 4๊ฐ์ผ ๋
1๋ฒ ๊ตฐ์ง์ ๋ชจ๋ ๊ฐ์ ํ๊ท ์ด์์ด๋ฉฐ, ๋๋จธ์ง ๊ตฐ์ง๋ค์ ๋ฐ์ดํฐ๋ค๋ ํ๊ท ์ด์์ธ ๊ฒฝ์ฐ๊ฐ ๋ง์ ๊ฐ์ฅ ์ด์์ ์ธ ๊ตฐ์งํ ๊ฐ์๋ก ํ๋จํ ์ ์๋ค.
iris ๋ฐ์ดํฐ์ ๊ตฐ์งํ ๊ฒฐ๊ณผ ์๊ฐํ
from sklearn.datasets import load_iris
iris=load_iris()
visualize_silhouette([ 2, 3, 4,5 ], iris.data)
iris ๋ฐ์ดํฐ์ ๊ตฐ์งํ ๊ฒฐ๊ณผ 2๊ฐ์ ๊ตฐ์ง์ผ๋ก ๋๋๋ ๊ฒ์ด ๊ฐ์ฅ ๋ฐ๋์งํด ๋ณด์ธ๋ค.
##์ฐธ๊ณ
https://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_silhouette_analysis.html
Selecting the number of clusters with silhouette analysis on KMeans clustering
Silhouette analysis can be used to study the separation distance between the resulting clusters. The silhouette plot displays a measure of how close each point in one cluster is to points in the ne...
scikit-learn.org
'๐ Machine Learning > Clustering' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] DBSCAN (0) | 2022.03.04 |
---|---|
[python] GMM(Gaussian Mixture Model) (0) | 2022.03.03 |
[Python] ํ๊ท ์ด๋ (0) | 2022.03.02 |
[Python] KMeans Clustering(K-ํ๊ท ๊ตฐ์งํ) (0) | 2022.02.28 |