
๊ตฐ์งํ๋ ๋น์ง๋ ํ์ต์ ์ํ๋ค.

๊ตฐ์งํ
- ๋น์ทํ ์ํ์ ํ๋์ ํด๋ฌ์คํฐ๋ก ๋ชจ์ผ๋ ๊ฒ
- ์ฐจ์ ์ถ์๋ฅผ ์ด์ฉํ์ฌ ๋ถ์์ ์ํ ์ถฉ๋ถํ ์ ๋ณด๋ฅผ ๊ฐ์ง ์ ์์
- ์ด์์น ํ์ง ๊ฐ๋ฅ !

์ํํธ ๊ตฐ์ง์ ๊ฐ์ฒด๊ฐ ์ด๋ ๊ตฐ์ง์ ์ํ ์ง๋ฅผย ๊ฐ์ค์น(weight)๋ ํ๋ฅ (probability)๋ก์ ๊ฐ๋ฅ์ฑ ์ ๋๋ฅผ ๋ํ๋ด์ฃผ๋ ๊ธฐ๋ฒ

- ๊ตฐ์งํ์์ ๊ฐ์ฅ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ ์๊ณ ๋ฆฌ์ฆ
- ๊ตฐ์ง ์ค์ฌ์ (centroid)์ด๋ผ๋ ํน์ ํ ์์์ ์ง์ ์ ์ ํํด ํด๋น ์ค์ฌ์ ๊ฐ์ฅ ๊ฐ๊น์ด ํฌ์ธํธ๋ค์ ์ ํํ๋ ๊ตฐ์งํ ๊ธฐ๋ฒ
- ์ ํ๋ ํฌ์ธํธ์ ํ๊ท ์ง์ ์ผ๋ก ์ด๋ํ๊ณ ์ด๋๋ ์ค์ฌ์ ์์ ๋ค์ ๊ฐ๊น์ด ํฌ์ธํธ ์ ํ, ๋ค์ ์ค์ฌ์ ์ ํ๊ท ์ง์ ์ผ๋ก ์ด๋ํ๋ ํ๋ก์ธ์ค ๋ฐ๋ณต ์ํ
- ๊ฐ cluster ๋ด ์ ์ฌ๋ ๋์ด๊ณ ์ธ ์ ์ฌ๋๋ ๋ฎ์ถ๋ ๊ฒ์ ๊ฐ์ ์ผ๋ก ๊ฐ cluster ๊ฑฐ๋ฆฌ ์ฐจ์ด์ ๋ถ์ฐ์ ์ต๋ํํ๋ ๊ฒ์ด ๋ชฉ์
(1) ๋ช ๊ฐ์ ๋ฉ์ด๋ฆฌ๋ก clusteringํ ์ง ์ ํ๋ค

(2) 1์์ ์ ํ ๊ฐ์๋งํผ ์ค์ฌ์ ์ ์ ํ๋ค(์์ ์ด ์ํ๋ ์๋ฌด ๊ฐ์ผ๋ก ์ค์ฌ์ ์ ์ ํ๊ธฐ)

(3) ๊ฐ ์ ์ ๋ํด ๊ฐ์ฅ ๊ฐ๊น์ด centroid๋ฅผ ์ ํ๋ค

(4) ์ด์ ๊ฐ ๋งคํ๋ ์ ๋ค์ ๋ฐํ์ผ๋ก ํ์ฌ centroid๋ฅผ ์ด๋ํ๋ค

(5) 5. 3-4์ ๊ณผ์ ์ ๋์ด์ ์๋ก ๋งคํ๋์ง ์์ ๋๊น์ง ๋ฐ๋ณตํ๋ค

(6) ์๋ก ๋งคํ๋ ์ ๋ค์ ๋ฐํ์ผ๋ก ๋ค์ centroid๋ฅผ ์ด๋ํ๋ค

(7) ์์ ๊ณผ์ ์ ๋์ด์ ์ด๋์ด ์์ ๋๊น์ง ๋ฐ๋ณต

๊ฝ๋ฐ์นจ, ๊ฝ์์ ๊ธธ์ด์ ๋ฐ๋ผ ๊ฐ ๋ฐ์ดํฐ์ ๊ตฐ์งํ๊ฐ ์ด๋ป๊ฒ ๊ฒฐ์ ๋๋ ์ง ํ์ธ, ์ด๋ฅผ ๋ถ๋ฅ๊ฐ๊ณผ ๋น๊ต
kmeans = KMeans(n_clusters=3, init='k-means++', max_iter=300,random_state=0)
kmeans.fit(irisDF)
irisDF['target'] = iris.target
irisDF['cluster']=kmeans.labels_
iris_result = irisDF.groupby(['target','cluster'])['sepal_length'].count()
print(iris_result)
๊ตฐ์งํ ์๊ฐํ
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca_transformed = pca.fit_transform(iris.data)
irisDF['pca_x'] = pca_transformed[:,0]
irisDF['pca_y'] = pca_transformed[:,1]
irisDF.head(3)
# ๊ตฐ์ง ๊ฐ์ด 0, 1, 2์ธ ๊ฒฝ์ฐ๋ง๋ค ๋ณ๋์ ์ธ๋ฑ์ค๋ก ์ถ์ถ
marker0_ind = irisDF[irisDF['cluster']==0].index
marker1_ind = irisDF[irisDF['cluster']==1].index
marker2_ind = irisDF[irisDF['cluster']==2].index
# ๊ตฐ์ง ๊ฐ 0, 1, 2์ ํด๋นํ๋ ์ธ๋ฑ์ค๋ก ๊ฐ ๊ตฐ์ง ๋ ๋ฒจ์ pca_x, pca_y ๊ฐ ์ถ์ถ. o, s, ^ ๋ก ๋ง์ปค ํ์
plt.scatter(x=irisDF.loc[marker0_ind, 'pca_x'], y=irisDF.loc[marker0_ind, 'pca_y'], marker='o')
plt.scatter(x=irisDF.loc[marker1_ind, 'pca_x'], y=irisDF.loc[marker1_ind, 'pca_y'], marker='s')
plt.scatter(x=irisDF.loc[marker2_ind, 'pca_x'], y=irisDF.loc[marker2_ind, 'pca_y'], marker='^')
plt.xlabel('PCA 1')
plt.ylabel('PCA 2')
plt.title('3 Clusters Visualization by 2 PCA Components')
plt.show()

๊ตฐ์งํ์ฉ ๋ฐ์ดํฐ ์์ฑ๊ธฐ
make_blobs()
๊ฐ๋ณ ๊ตฐ์ง์ ์ค์ฌ์ ๊ณผ ํ์คํธ์ฐจ ์ ์ด ๊ธฐ๋ฅ ์ถ๊ฐ > ํผ์ฒ ๋ฐ์ดํฐ ์ธํธ์ ํ๊น ๋ฐ์ดํฐ ์ธํธ๊ฐ ํํ๋ก ๋ฐํ
make_classification()
๋ ธ์ด์ฆ๋ฅผ ํฌํจํ ๋ฐ์ดํฐ๋ฅผ ๋ง๋๋ ๋ฐ ์ ์ฉ
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
%matplotlib inline
X, y = make_blobs(n_samples=200, n_features=2, centers=3, cluster_std=0.8, random_state=0)
print(X.shape, y.shape)
# y target ๊ฐ์ ๋ถํฌ๋ฅผ ํ์ธ
unique, counts = np.unique(y, return_counts=True)
print(unique,counts)
ํผ์ฒ ๋ฐ์ดํฐ ์ธํธ๊ฐ ์ด๋ ํ ๊ตฐ์งํ ๋ถํฌ๋ฅผ ๊ฐ์ง๊ณ ๋ง๋ค์ด์ก๋์ง ํ์ธ
target_list = np.unique(y)
# ๊ฐ ํ๊น๋ณ ์ฐ์ ๋์ ๋ง์ปค ๊ฐ.
markers=['o', 's', '^', 'P', 'D', 'H', 'x']
# 3๊ฐ์ ๊ตฐ์ง ์์ญ์ผ๋ก ๊ตฌ๋ถํ ๋ฐ์ดํฐ ์ธํธ๋ฅผ ์์ฑํ์ผ๋ฏ๋ก target_list๋ [0, 1, 2]
# target==0, target==1, target==2 ๋ก scatter plot์ marker๋ณ๋ก ์์ฑ.
for target in target_list:
target_cluster = clusterDF[clusterDF['target']==target]
plt.scatter(x=target_cluster['ftr1'], y=target_cluster['ftr2'], edgecolor='k',
marker=markers[target] )
plt.show()
KMeans ๊ตฐ์งํ๋ฅผ ์ํํ ๋ค์ ๊ตฐ์ง๋ณ๋ก ์๊ฐํ
# KMeans ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ X ๋ฐ์ดํฐ๋ฅผ K-Means ํด๋ฌ์คํฐ๋ง ์ํ
kmeans = KMeans(n_clusters=3, init='k-means++', max_iter=200, random_state=0)
cluster_labels = kmeans.fit_predict(X)
clusterDF['kmeans_label'] = cluster_labels
#cluster_centers_ ๋ ๊ฐ๋ณ ํด๋ฌ์คํฐ์ ์ค์ฌ ์์น ์ขํ ์๊ฐํ๋ฅผ ์ํด ์ถ์ถ
centers = kmeans.cluster_centers_
unique_labels = np.unique(cluster_labels)
markers=['o', 's', '^', 'P','D','H','x']
# ๊ตฐ์ง๋ label ์ ํ๋ณ๋ก iteration ํ๋ฉด์ marker ๋ณ๋ก scatter plot ์ํ.
for label in unique_labels:
label_cluster = clusterDF[clusterDF['kmeans_label']==label]
center_x_y = centers[label]
plt.scatter(x=label_cluster['ftr1'], y=label_cluster['ftr2'], edgecolor='k',
marker=markers[label] )
# ๊ตฐ์ง๋ณ ์ค์ฌ ์์น ์ขํ ์๊ฐํ
plt.scatter(x=center_x_y[0], y=center_x_y[1], s=200, color='white',
alpha=0.9, edgecolor='k', marker=markers[label])
plt.scatter(x=center_x_y[0], y=center_x_y[1], s=70, color='k', edgecolor='k',
marker='$%d$' % label)
plt.show()
cluster_std๊ฐ ์์ ์๋ก ๊ตฐ์ง ์ค์ฌ์ ๋ฐ์ดํฐ๊ฐ ๋ชจ์ฌ ์์ผ๋ฉฐ, ํด์๋ก ๋ฐ์ดํฐ๊ฐ ํผ์ ธ์์

๋๋ถ๋ถ ๊ตฐ์งํ ๋ฐ์ดํฐ ์ธํธ๋ ํ๊น๋ ์ด๋ธ์ ๊ฐ์ง์ง ์์ง ์๊ณ ๊ตฐ์งํ๋ ๋ถ๋ฅ์ ์ ์ฌํด ๋ณด์ผ ์ ์์ผ๋ ์ฑ๊ฒฉ์ด ๋ค๋ฆ. ์๋ก ๋ค๋ฅธ ๋ถ๋ฅ ๊ฐ์ ๋ฐ์ดํฐ๋ ๋ ๋์ ๊ตฐ์งํ ๋ ๋ฒจํ ๋ฑ์ ์์ญ์ ๊ฐ์ง๊ณ ์์.
๊ตฐ์งํ ์ฑ๋ฅ์ ํ๊ฐํ๋ ๋ํ์ ์ธ ๋ฐฉ๋ฒ์ผ๋ก ์ค๋ฃจ์ฃ ๋ถ์์ ์ด์ฉ
์ค๋ฃจ์ฃ ๋ถ์์ ๊ฐ ๊ตฐ์ง ๊ฐ์ ๊ฑฐ๋ฆฌ๊ฐ ์ผ๋ง๋ ํจ์จ์ ์ผ๋ก ๋ถ๋ฆฌ๋ผ ์๋์ง๋ฅผ ๋ํ๋
์ค๋ฃจ์ฃ ๋ถ์์ ์ค๋ฃจ์ฃ ๊ณ์๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ํ
ํด๋น ๋ฐ์ดํฐ๊ฐ ๊ฐ์ ๊ตฐ์ง ๋ด์ ๋ฐ์ดํฐ์ ์ผ๋ง๋ ๊ฐ๊น๊ฒ ๊ตฐ์งํ๋ผ ์๊ณ , ๋ค๋ฅธ ๊ตฐ์ง์ ์๋ ๋ฐ์ดํฐ์๋ ์ผ๋ง๋ ๋ฉ๋ฆฌ ๋ถ๋ฆฌ๋ผ ์๋์ง๋ฅผ ๋ํ๋ด๋ ์งํ

์ค๋ฃจ์ฃ ๋ถ์ ๋ฉ์๋
.silhouette_samples
๊ฐ ๋ฐ์ดํฐ ํฌ์ธํธ์ ์ค๋ฃจ์ฃ ๊ณ์๋ฅผ ๊ณ์ฐ ํด ๋ฐํ
.silhouette_score
์ ์ฒด ๋ฐ์ดํฐ ์ค๋ฃจ์ฃ ๊ณ์ ๊ฐ์ ํ๊ท ํด ๋ฐํ
+ ๊ตฐ์ง๋ณ ํ๊ท ์ค๋ฃจ์ฃ ๊ณ์ ๊ฐ =.groupby('cluster)['silhouette_coeff'].mean()
์ข์ ๊ตฐ์งํ ๋ง์กฑ ๊ธฐ์ค
from sklearn.preprocessing import scale
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
# ์ค๋ฃจ์ฃ ๋ถ์ metric ๊ฐ์ ๊ตฌํ๊ธฐ ์ํ API ์ถ๊ฐ
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('silhouette_samples( ) return ๊ฐ์ shape' , score_samples.shape)
# irisDF์ ์ค๋ฃจ์ฃ ๊ณ์ ์ปฌ๋ผ ์ถ๊ฐ
irisDF['silhouette_coeff'] = score_samples
# ๋ชจ๋ ๋ฐ์ดํฐ์ ํ๊ท ์ค๋ฃจ์ฃ ๊ณ์๊ฐ์ ๊ตฌํจ.
average_score = silhouette_score(iris.data, irisDF['cluster'])
print('๋ถ๊ฝ ๋ฐ์ดํฐ์
Silhouette Analysis Score:{0:.3f}'.format(average_score))
irisDF.head(3)

ํ๊ท ์ค๋ฃจ์ฃ ๊ณ์๊ฐ์ ๋ฎ์ ๋ฐ๋ฉด์ 1๋ฒ ๊ตฐ์ง์ ์ค๋ฃจ์ฃ ๊ณ์๋ ๋์ ๊ฐ์ ๋ํ๋
ํน์ ๊ตฐ์ง ๋ด์ ์ค๋ฃจ์ฃ ๊ณ์๊ฐ๋ง ๋๋ฌด ๋๊ณ , ๋ค๋ฅธ ๊ตฐ์ง์ ๋ด๋ถ ๋ฐ์ดํฐ ๋ผ๋ฆฌ ๋๋ฌด ๊ฑฐ๋ฆฌ๊ฐ ๋จ์ด์ ธ ์์ด ์ค๋ฃจ์ฃ ๊ณ์ ๊ฐ์ด ๋ฎ์์ ธ๋ ํ๊ท ์ ์ผ๋ก ๋์ ๊ฐ์ ๊ฐ์ง ์ ์์

1๋ฒ ๊ตฐ์ง์ ๋ชจ๋ ๋ฐ์ดํฐ๋ ํ๊ท ์ค๋ฃจ์ฃ ๊ณ์ ๊ฐ ์ด์์ด์ง๋ง, 2๋ฒ ๊ตฐ์ง์ ๊ฒฝ์ฐ๋ ํ๊ท ๋ณด๋ค ์ ์ ๋ฐ์ดํฐ ๊ฐ์ด ๋งค์ฐ ๋ง์

0๋ฒ์ ๊ฒฝ์ฐ ๋ชจ๋ ํ๊ท ๋ณด๋ค ๋ฎ์. 0๋ฒ์ ๋ด๋ถ ๋ฐ์ดํฐ ๊ฐ ๊ฑฐ๋ฆฌ๋ ๋ฉ์ง๋ง 2๋ฒ ๊ตฐ์ง๊ณผ๋ ๊ฐ๊น๊ฒ ์์น

4๊ฐ์ธ ๊ฒฝ์ฐ๊ฐ ๊ฐ์ฅ ์ด์์ ์ธ ๊ตฐ์งํ ๊ฐ์๋ก ํ๋จ ๊ฐ๋ฅ
โถ๏ธ ์ค๋ฃจ์ฃ ๊ณ์๋ฅผ ํตํ k-ํ๊ท ๊ตฐ์ง ํ๊ฐ ๋ฐฉ๋ฒ์ ์ง๊ด์ ์ผ๋ก ์ดํดํ๊ธฐ ์ฝ์ง๋ง, ๊ฐ ๋ฐ์ดํฐ ๋ณ๋ก ๋ค๋ฅธ ๋ฐ์ดํฐ์์ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ๊ณ์ฐํด์ผ ํ๋ฏ๋ก ๋ฐ์ดํฐ์์ด ๋์ด๋๋ฉด์ ์ํ์๊ฐ์ด ํฌ๊ฒ ๋์ด๋จ
ํ๊ท ์ด๋์ k-ํ๊ท ๊ณผ ์ ์ฌํ๊ฒ ์ค์ฌ์ ๊ตฐ์ง์ ์ค์ฌ์ผ๋ก ์ง์์ ์ผ๋ก ์์ง์ด๋ฉด์ ๊ตฐ์งํ๋ฅผ ์ํ. k-ํ๊ท ์ด ์ค์ฌ์ ์์๋ ๋ฐ์ดํฐ์ ํ๊ท ๊ฑฐ๋ฆฌ ์ค์ฌ์ผ๋ก ์ด๋ํ๋ ๋ฐ ๋ฐํด, ํ๊ท ์ด๋์ ์ค์ฌ์ ๋ฐ์ดํฐ๊ฐ ๋ชจ์ฌ์๋ ๋ฐ๋๊ฐ ๊ฐ์ฅ ๋์ ๊ณณ์ผ๋ก ์ด๋์ํด
ํ๋ฅ ๋ฐ๋ํจ์๊ฐ ํผํฌ์ธ ์ ์ ๊ตฐ์ง ์ค์ฌ์ ์ผ๋ก ์ ์ >> KDE๋ฅผ ์ด์ฉํด ํ๋ฅ ๋ฐ๋ ํจ์๋ฅผ ์ฐพ๋๋ค.

KDE - kernel ํจ์๋ฅผ ํตํด ์ด๋ค ๋ณ์์ ํ๋ฅ ๋ฐ๋ ํจ์๋ฅผ ์ถ์ ํ๋ ๋ํ์ ์ธ ๋ฐฉ์. ๊ด์ธก ๋ฐ์ดํฐ ๊ฐ๊ฐ์ ์ปค๋ ํจ์๋ฅผ ์ ์ฉํ๊ฐ์ ์ ๋ถ ๋ํ ๋ค ๋ฐ์ดํฐ ๊ฑด์๋ก ๋๋ ํ๋ฅ ๋ฐ๋ ํจ์๋ฅผ ์ถ์
=> KDE๋ ๊ฐ๋ณ ๊ด์ธก ๋ฐํฐ์ ์ปค๋ ํจ์๋ฅผ ์ ์ฉํ ๋ค, ์ด ์ ์ฉ ๊ฐ์ ๋ชจ๋ ๋ํ ๋ค ๊ฐ๋ณ ๊ด์ธก ๋ฐ์ดํฐ ๊ฑด์๋ก ๋๋ ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ์ถ์

K๋ ์ปค๋ ํจ์, x๋ ํ๋ฅ ๋ณ์ ๊ฐ, xi๋ ๊ด์ธก๊ฐ, h๋ ๋์ญํญ
KDE ํํ๋ฅผ ๋ถ๋๋ฌ์ด ํํ๋ก ํํํ ํ๋๋ฐ ์ ์ฉ

์์ h๊ฐ์ ์ข๊ณ ๋พฐ์กฑํ KDE๋ฅผ ๊ฐ์ง๊ฒ ๋๋ฉฐ, ๊ณผ์ ํฉ ๋๊ธฐ ์ฌ์. ํฐ h๊ฐ์ ๊ณผ๋ํ๊ฒ ํํํ๋ KDE๋ก ์ธํด ์ง๋์น๊ฒ ๋จ์ํ๋ ๋ฐฉ์์ผ๋ก ํ๋ฅ ๋ฐ๋ํจ์๋ฅผ ์ถ์ ํ๋ฉฐ ๊ณผ์์ ํฉ ๋๊ธฐ ์ฌ์
MeanShift ํด๋์ค์ ๊ฐ์ฅ ์ค์ํ ์ด๊ธฐํ ํ๋ผ๋ฏธํฐ bandwidth
meanshift= MeanShift(bandwidth=1)
cluster_labels = meanshift.fit_predict(X)
print('cluster labels ์ ํ:', np.unique(cluster_labels))
[Output]
cluster labels ์ ํ: [0 1 2]
bandwidth๊ฐ ํฐ ์ํฅ์ ๋ฏธ์นจ์ ํ์ธํ ์ ์์. ์ฌ์ดํท๋ฐ์ ์ต์ ํ๋ bandwidth ๊ฐ์ ์ฐพ๊ธฐ ์ํด estimate_bandwidth() ํจ์๋ฅผ ์ ๊ณต
from sklearn.cluster import estimate_bandwidth
bandwidth = estimate_bandwidth(X)
print('bandwidth ๊ฐ:', round(bandwidth,3))

ํ๊ท ์ด๋์ ์๊ณ ๋ฆฌ์ฆ ์ํ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆฌ๊ณ ๋ฌด์๋ณด๋ค๋ bandwidth ํฌ๊ธฐ์ ๋ฐ๋ฅธ ๊ตฐ์งํ ์ํฅ๋๊ฐ ๋งค์ฐ ํผ. ๋ถ์ ์ ๋ฌด ๊ธฐ๋ฐ์ ๋ฐ์ดํฐ ์ธํธ ๋ณด๋ค๋ ์ปดํจํฐ ๋น์ ์์ญ์์ ๋ ๋ง์ด ์ฌ์ฉ๋จ.
GMM ๊ตฐ์งํ๋ ๊ตฐ์งํ๋ฅผ ์ ์ฉํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ๊ฐ ์ฌ๋ฌ ๊ฐ์ ๊ฐ์ฐ์์ ๋ถํฌ๋ฅผ ๊ฐ์ง ๋ฐ์ดํฐ ์งํฉ๋ค์ด ์์ฌ์ ์์ฑ๋ ๊ฒ์ด๋ผ๋ ๊ฐ์ ํ์ ๊ตฐ์งํ๋ฅผ ์ํ (๊ฐ ๋ฐ์ดํฐ ๊ตฐ์ง๋ค์ด ๊ฐ์ฐ์์ ๋ถํฌ ์ด๋ฃธ)
์๋ก ๋ค๋ฅธ ์ ๊ท ๋ถํฌ์ ๊ธฐ๋ฐํด ๊ตฐ์งํ๋ฅผ ์ํ

GMM์ ๊ฐ๋ณ ์ ๊ท ๋ถํฌ์ ํ๊ท ๊ณผ ๋ถ์ฐ/ ๊ฐ ๋ฐ์ดํฐ๊ฐ ์ด๋ค ์ ๊ท ๋ถํฌ์ ํด๋น๋๋ ์ง์ ํ๋ฅ ์ ์ถ์ ํด ๋ฐํ
n_componentsfrom sklearn.mixture import GaussianMixture
gmm = GaussianMixture(n_components=3, random_state=0).fit(iris.data)
gmm_cluster_labels = gmm.predict(iris.data)
# ํด๋ฌ์คํฐ๋ง ๊ฒฐ๊ณผ๋ฅผ irisDF ์ 'gmm_cluster' ์ปฌ๋ผ๋ช
์ผ๋ก ์ ์ฅ
irisDF['gmm_cluster'] = gmm_cluster_labels
irisDF['target'] = iris.target
# target ๊ฐ์ ๋ฐ๋ผ์ gmm_cluster ๊ฐ์ด ์ด๋ป๊ฒ ๋งคํ๋์๋์ง ํ์ธ.
iris_result = irisDF.groupby(['target'])['gmm_cluster'].value_counts()
print(iris_result)
make_blobs()์ transformation(ํ๋ ฌ ๋ด์ ์ฐ์ฐ)์ ์ด์ฉํด ๋ญ์น ๋ฐ์ดํฐ๋ฅผ ํ์ํ์ผ๋ก ๋ณํ
from sklearn.datasets import make_blobs
# make_blobs() ๋ก 300๊ฐ์ ๋ฐ์ดํฐ ์
, 3๊ฐ์ cluster ์
, cluster_std=0.5 ์ ๋ง๋ฌ.
X, y = make_blobs(n_samples=300, n_features=2, centers=3, cluster_std=0.5, random_state=0)
# ๊ธธ๊ฒ ๋์ด๋ ํ์ํ์ ๋ฐ์ดํฐ ์
์ ์์ฑํ๊ธฐ ์ํด ๋ณํํจ.
transformation = [[0.60834549, -0.63667341], [-0.40887718, 0.85253229]]
X_aniso = np.dot(X, transformation)
# feature ๋ฐ์ดํฐ ์
๊ณผ make_blobs( ) ์ y ๊ฒฐ๊ณผ ๊ฐ์ DataFrame์ผ๋ก ์ ์ฅ
clusterDF = pd.DataFrame(data=X_aniso, columns=['ftr1', 'ftr2'])
clusterDF['target'] = y
# ์์ฑ๋ ๋ฐ์ดํฐ ์
์ target ๋ณ๋ก ๋ค๋ฅธ marker ๋ก ํ์ํ์ฌ ์๊ฐํ ํจ.
visualize_cluster_plot(None, clusterDF, 'target', iscenter=False)

KMeans์ผ๋ก ๊ตฐ์งํ๋ฅผ ์ํํ ๊ฒฝ์ฐ, ์ํ ์์น๋ก ๊ฐ๋ณ ๊ตฐ์งํ๊ฐ ๋๋ฉด์ ์ํ๋ ๋ฐฉํฅ์ผ๋ก ๊ตฌ์ฑ๋์ง ์์

๋ฐ์ดํฐ๊ฐ ๋ถํฌํ ๋ฐฉํฅ์ ๋ฐ๋ผ ์ ํํ๊ฒ ๊ตฐ์งํ ๋์์ ํ์ธ ๊ฐ๋ฅ
์
์ค๋ก ์ฃผ๋ณ ์์ญ (epsilon) : ๊ฐ๋ณ ๋ฐ์ดํฐ๋ฅผ ์ค์ฌ์ผ๋ก ์
์ค๋ก ๋ฐ๊ฒฝ์ ๊ฐ์ง๋ ์ํ์ ์์ญ
์ต์ ๋ฐ์ดํฐ ๊ฐ์ (min points) : ๊ฐ๋ณ ๋ฐ์ดํฐ์ ์
์ค๋ก ์ฃผ๋ณ ์์ญ์ ํฌํจ๋๋ ํ ๋ฐ์ดํฐ์ ๊ฐ์
ํต์ฌ ํฌ์ธํธ(Core Point): ์ฃผ๋ณ ์์ญ ๋ด์ ์ต์ ๋ฐ์ดํฐ ๊ฐ์ ์ด์์ ํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ ๊ฒฝ์ฐ
์ด์ ํฌ์ธํธ (Neighbor Point): ์ฃผ๋ณ ์์ญ ๋ด์ ์์นํ ํ ๋ฐ์ดํฐ
๊ฒฝ๊ณ ํฌ์ธํธ (Border Point): ์ฃผ๋ณ ์์ญ ๋ด์ ์ต์ ๋ฐ์ดํฐ ๊ฐ์ ์ด์์ ์ด์ ํฌ์ธํธ๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์ง๋ง ํต์ฌ ํฌ์ธํธ๋ฅผ ์ด์ ํฌ์ธํธ๋ก ๊ฐ์ง๊ณ ์๋ ๋ฐ์ดํฐ
์ก์ ํฌ์ธํธ (Noise Point): ์ต์ ๋ฐ์ดํฐ ๊ฐ์ ์ด์์ ์ด์ ํฌ์ธํธ๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์ผ๋ฉฐ, ํต์ฌ ํฌ์ธํธ๋ ์ด์ ํฌ์ธํธ๋ก ๊ฐ์ง๊ณ ์์ง ์๋ ๋ฐ์ดํฐ


from sklearn.cluster import DBSCAN
dbscan = DBSCAN(eps=0.6, min_samples=8, metric='euclidean')
dbscan_labels = dbscan.fit_predict(iris.data)
irisDF['dbscan_cluster'] = dbscan_labels
irisDF['target'] = iris.target
iris_result = irisDF.groupby(['target'])['dbscan_cluster'].value_counts()
print(iris_result)
from sklearn.decomposition import PCA
# 2์ฐจ์์ผ๋ก ์๊ฐํํ๊ธฐ ์ํด PCA n_componets=2๋ก ํผ์ฒ ๋ฐ์ดํฐ ์ธํธ ๋ณํ
pca = PCA(n_components=2, random_state=0)
pca_transformed = pca.fit_transform(iris.data)
# visualize_cluster_2d( ) ํจ์๋ ftr1, ftr2 ์ปฌ๋ผ์ ์ขํ์ ํํํ๋ฏ๋ก PCA ๋ณํ๊ฐ์ ํด๋น ์ปฌ๋ผ์ผ๋ก ์์ฑ
irisDF['ftr1'] = pca_transformed[:,0]
irisDF['ftr2'] = pca_transformed[:,1]
visualize_cluster_plot(dbscan, irisDF, 'dbscan_cluster', iscenter=False)
from sklearn.cluster import DBSCAN
dbscan = DBSCAN(eps=0.8, min_samples=8, metric='euclidean')
dbscan_labels = dbscan.fit_predict(iris.data)
irisDF['dbscan_cluster'] = dbscan_labels
irisDF['target'] = iris.target
iris_result = irisDF.groupby(['target'])['dbscan_cluster'].value_counts()
print(iris_result)
visualize_cluster_plot(dbscan, irisDF, 'dbscan_cluster', iscenter=False)

dbscan = DBSCAN(eps=0.6, min_samples=16, metric='euclidean')
dbscan_labels = dbscan.fit_predict(iris.data)
irisDF['dbscan_cluster'] = dbscan_labels
irisDF['target'] = iris.target
iris_result = irisDF.groupby(['target'])['dbscan_cluster'].value_counts()
print(iris_result)
visualize_cluster_plot(dbscan, irisDF, 'dbscan_cluster', iscenter=False)



