์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- LDA
- PCA
- ์ค๋ฒ์ํ๋ง
- ์๋ํด๋ผ์ฐ๋
- datascience
- ํ์ด์ฌ
- Lambda
- ์ฃผ์ฑ๋ถ๋ถ์
- ๋น ๋ฐ์ดํฐ
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- ADP
- ๋ฐ์ดํฐ๋ถ์
- ๋ ๋ฆฝํ๋ณธ
- DBSCAN
- ๊ตฐ์งํ
- t-test
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- ํ ์คํธ๋ถ์
- numpy
- pandas
- dataframe
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- Python
- ์ธ๋์ํ๋ง
- opencv
- iloc
- ํฌ๋กค๋ง
- ๋์ํ๋ณธ
- ADsP
- ๋ฐ์ดํฐ๋ถ๊ท ํ
Data Science LAB
[python] GMM(Gaussian Mixture Model) ๋ณธ๋ฌธ
[python] GMM(Gaussian Mixture Model)
ใ ใ ใ ใ 2022. 3. 3. 13:46GMM
GMM ๊ตฐ์งํ๋ ๊ตฐ์งํ๋ฅผ ์ ์ฉํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ๊ฐ ์ฌ๋ฌ ๊ฐ์ ๊ฐ์ฐ์์ ๋ถํฌ๋ฅผ ๊ฐ์ง ๋ฐ์ดํฐ ์งํฉ๋ค์ด ์์ฌ์ ์์ฑ๋ ๊ฒ์ด๋ผ๋ ๊ฐ์ ํ์ ๊ตฐ์งํ๋ฅผ ์ํํ๋ ๋ฐฉ์์ด๋ค. ๊ฐ์ฐ์์ ๋ถํฌ๋ ์ ๊ท ๋ถํฌ(Normal distribution)๋ผ๊ณ ๋ ํ๋ฉฐ, ์ข์ฐ ๋์นญํ์ ์ข ํํ์ด๋ค. GMM์ ๋ฐ์ดํฐ๋ฅผ ์ฌ๋ฌ๊ฐ์ ์ ๊ท ๋ถํฌ๊ฐ ์์ธ ๊ฒ์ผ๋ก ๊ฐ์ฃผํ์ฌ ์์ธ ๋ฐ์ดํฐ ๋ถํฌ์์ ๊ฐ๋ณ ์ ํ์ ์ ๊ท ๋ถํฌ๋ฅผ ์ถ์ถํ๋ค.
์ ์ฒด ๋ฐ์ดํฐ ์ ์ ์๋ก ๋ค๋ฅธ ์ ๊ท ๋ถํฌ ํํ๋ฅผ ๊ฐ์ง ์ฌ๋ฌ ๊ฐ์ง ํ๋ฅ ๋ถํฌ ๊ณก์ ์ผ๋ก ๊ตฌ์ฑ๋ ์ ์์ผ๋ฉฐ, ์ด๋ ๊ฒ ์๋ก ๋ค๋ฅธ ์ ๊ท ๋ถํฌ์ ๊ธฐ๋ฐํ์ฌ ๊ตฐ์งํ๋ฅผ ์ํํ๋ ๊ฒ์ด GMM ๊ตฐ์งํ ๋ฐฉ์์ด๋ค.
GMM์ ์ด์ฉํ iris ๋ฐ์ดํฐ์ ๊ตฐ์งํ
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
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']
iris_df = pd.DataFrame(data = iris.data,columns=feature_names)
iris_df['target'] = iris.target
iris_df.head()
from sklearn.mixture import GaussianMixture
gmm = GaussianMixture(n_components=3, random_state = 0).fit(iris.data)
gmm_cluster_labels = gmm.predict(iris.data)
#๊ตฐ์งํ ๊ฒฐ๊ณผ๋ฅผ iris_df์ ์ ์ฅ
iris_df['gmm_cluster'] = gmm_cluster_labels
iris_df['target'] = iris.target
#target๊ฐ์ ๋ฐ๋ผ gmm_cluster๊ฐ ์ด๋ป๊ฒ ๋งคํ๋๋์ง ํ์ธ
gmm_result = iris_df.groupby('target')['gmm_cluster'].value_counts()
print(gmm_result)
GMM์์ ๊ฐ์ฅ ์ค์ํ ํ๋ผ๋ฏธํฐ๋ n_components์ด๋ค. gausian mixture ๋ชจ๋ธใน์ ์ด ๊ฐ์์ด๋ฉฐ, KMeans ๊ตฐ์ง ๊ฐ์์ ๊ฐ์ด ์ค์ํ ์ญํ ์ ํ๋ค. n_components๋ฅผ 3์ผ๋ก ์ค์ ํ์ฌ GaussianMixture๋ก ๊ตฐ์งํ๋ฅผ ์ํํ์๋ค.
Target 0 ์ ๊ตฐ์ง 0์ผ๋ก, Target 2๋ ๊ตฐ์ง 1๋ก ์ ๋งคํ ๋์์ง๋ง, target 1์ ๋ฐ์ดํฐ ์ค 5๊ฐ๊ฐ ๊ตฐ์ง 2๋ก ์๋ชป ๋งคํ๋์๋ค.
iris ๋ฐ์ดํฐ KMeans ์ ์ฉ(n_clusters=3)
kmeans = KMeans(n_clusters = 3, init='k-means++',max_iter=300,random_state=0).fit(iris.data)
kmeans_cluster_labels = kmeans.predict(iris.data)
iris_df['kmeans_cluster'] = kmeans_cluster_labels
iris_result = iris_df.groupby(['target'])['kmeans_cluster'].value_counts()
print(iris_result)
GMM๋ณด๋ค KMeans์ ์ค์ฐจ๊ฐ ๋ ํฌ๋ค๋ ๊ฒ์ ํ์ธํ ์ ์์๋ค.
KMeans VS GMM
KMeans๋ ๊ฐ๋ณ ๊ตฐ์ง์ ์ค์ฌ์์ ์ํ์ ๋ฒ์๋ก ๋ฐ์ดํฐ๋ฅผ ๊ตฐ์งํํ๊ธฐ์ ์ ๋ฆฌ
GMM๋ ํ์ํ์ ๋ฐ์ดํฐ์ ์ ์ ๋ฆฌ
def visualize_cluster_plot(clusterobj, dataframe, label_name, iscenter=True):
if iscenter :
centers = clusterobj.cluster_centers_
unique_labels = np.unique(dataframe[label_name].values)
markers=['o', 's', '^', 'x', '*']
isNoise=False
for label in unique_labels:
label_cluster = dataframe[dataframe[label_name]==label]
if label == -1:
cluster_legend = 'Noise'
isNoise=True
else :
cluster_legend = 'Cluster '+str(label)
plt.scatter(x=label_cluster['ftr1'], y=label_cluster['ftr2'], s=70,\
edgecolor='k', marker=markers[label], label=cluster_legend)
if iscenter:
center_x_y = centers[label]
plt.scatter(x=center_x_y[0], y=center_x_y[1], s=250, 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)
if isNoise:
legend_loc='upper center'
else: legend_loc='upper right'
plt.legend(loc=legend_loc)
plt.show()
ํด๋ฌ์คํฐ ๊ฒฐ๊ณผ๋ฅผ ๋ด์ DataFrame๊ณผ ์ฌ์ดํท๋ฐ์ Cluster ๊ฐ์ฒด๋ฑ์ ์ธ์๋ก ๋ฐ์ ํด๋ฌ์คํฐ๋ง ๊ฒฐ๊ณผ๋ฅผ ์๊ฐํํ๋ ํจ์ ์์ฑ
ํ์ํ์ ๋ฐ์ดํฐ์ ์์ฑ
from sklearn.datasets import make_blobs
X,y = make_blobs(n_samples=300,n_features=2,centers=3,cluster_std = 0.5,random_state=0)
#๊ธธ๊ฒ ๋์ด๋ ํ์ํ์ ๋ฐ์ดํฐ์
์ ์์ฑํ๊ธฐ ์ํด ๋ณํ
transformation = [[0.60834549,-0.63667349],[-0.40887718,0.85253229]]
X_aniso = np.dot(X,transformation)
#feature๋ฐ์ดํฐ ์
๊ณผ make_blob์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐ์ดํฐํ๋ ์์ผ๋ก ์ ์ฅ
clusterDF = pd.DataFrame(data=X_aniso,columns=['ftr1','ftr2'])
clusterDF['target'] = y
#์์ฑ๋ ๋ฐ์ดํฐ์
์ target๋ณ๋ก ๋ค๋ฅธ ๋ง์ปค๋ก ํ์ํด ์๊ฐํ
visualize_cluster_plot(None,clusterDF,'target',iscenter=False)
KMeans ์ ์ฉ
kmeans = KMeans(3,random_state=0)
kmeans_label = kmeans.fit_predict(X_aniso)
clusterDF['kmeans_label'] = kmeans_label
visualize_cluster_plot(kmeans,clusterDF,'kmeans_label',iscenter=True)
๊ตฐ์ง 0๊ณผ 2๊ฐ ์ ๋ถ๋ฅ๋์ง ์์๋ค.
GMM ์ ์ฉ
#3๊ฐ์ n_components๊ธฐ๋ฐ GMM์ X_aniso ๋ฐ์ดํฐ์
์ ์ ์ฉ
gmm = GaussianMixture(n_components=3,random_state=0)
gmm_label = gmm.fit(X_aniso).predict(X_aniso)
clusterDF['gmm_label'] = gmm_label
#GaussianMixture๋ cluster_centers์์ฑ์ด ์์ด iscluster๋ฅผ False๋ก ์ค์
visualize_cluster_plot(gmm,clusterDF,'gmm_label',iscenter=False)
๊ตฐ์ง์ด ์ ๋ถ๋ฅ๋์๋ค.
KMeans์ GMM๋น๊ต
print("----- KMeans Clustering -----")
print(clusterDF.groupby('target')['kmeans_label'].value_counts())
print('\n------ Gaussian Mixture Clustering -----')
print(clusterDF.groupby('target')['gmm_label'].value_counts())
GMM์ KMeans๋ณด๋ค ์ ์ฐํ๊ฒ ๋ค์ํ ๋ฐ์ดํฐ์ ์ ์ ์ ์ฉ๋์ง๋ง ๊ตฐ์งํ๋ฅผ ์ํ ์ํ์๊ฐ์ด ์ค๋๊ฑธ๋ฆฐ๋ค.
'๐ Machine Learning > Clustering' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] DBSCAN (0) | 2022.03.04 |
---|---|
[Python] ํ๊ท ์ด๋ (0) | 2022.03.02 |
[Python] ๊ตฐ์ง ํ๊ฐ(์ค๋ฃจ์ฃ ๊ณ์) (0) | 2022.03.01 |
[Python] KMeans Clustering(K-ํ๊ท ๊ตฐ์งํ) (0) | 2022.02.28 |