์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- datascience
- ๊ตฐ์งํ
- ์ค๋ฒ์ํ๋ง
- pandas
- Python
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- ADsP
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- ํฌ๋กค๋ง
- PCA
- ํ์ด์ฌ
- ํ ์คํธ๋ถ์
- iloc
- ์๋ํด๋ผ์ฐ๋
- Lambda
- ์ฃผ์ฑ๋ถ๋ถ์
- DBSCAN
- ์ธ๋์ํ๋ง
- ๋ ๋ฆฝํ๋ณธ
- opencv
- t-test
- ADP
- ๋ฐ์ดํฐ๋ถ์
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- ๋์ํ๋ณธ
- ๋น ๋ฐ์ดํฐ
- LDA
- numpy
Data Science LAB
[Python] DBSCAN ๋ณธ๋ฌธ
DBSCAN
DBSCAN์ ๋ฐ๋ ๊ธฐ๋ฐ์ ๊ตฐ์งํ ๋ํ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. ๊ฐ๋จํ๊ณ ์ง๊ด์ ์ธ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋ฐ์ดํฐ์ ๋ถํฌ๊ฐ ๊ธฐํํ์ ์ผ๋ก ๋ณต์กํ ๊ฒฝ์ฐ์๋ ํจ๊ณผ์ ์ผ๋ก ๊ตฐ์งํํ ์ ์๋ค.
์์ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ์ํ์ ๋ฐ์ดํฐ ๋ถํฌ๋ฅผ ๋๋ ๊ฒฝ์ฐ, KMeans ๋ GMM์ ๊ตฐ์งํ๋ฅผ ์ ์ํํ์ง ๋ชปํ๋ค.
- ์ ์ค๋ก ์ฃผ๋ณ ์์ญ(epsilon) : ๊ฐ๋ณ ๋ฐ์ดํฐ๋ฅผ ์ค์ฌ์ผ๋ก ์ ์ค๋ก ๋ฐ๊ฒฝ์ ๊ฐ์ง๋ ์ํ์ ์์ญ
- ์ต์ ๋ฐ์ดํฐ ๊ฐ์(min points) : ๊ฐ๋ณ ๋ฐ์ดํฐ์ ์ฃผ๋ณ ์์ญ์ ํฌํจ๋๋ ํ ๋ฐ์ดํฐ์ ๊ฐ์
์ ์ค๋ก ์ฃผ๋ณ ์์ญ ๋ด์ ํฌํจ๋๋ ์ต์ ๋ฐ์ดํฐ ๊ฐ์๋ฅผ ์ถฉ์กฑ์ํค๋๊ฐ์ ๋ฐ๋ผ ๋ฐ์ดํฐ ํฌ์ธํธ๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํ๋ค.
- ํต์ฌ ํฌ์ธํธ(Core Point) : ์ฃผ๋ณ ์์ญ ๋ด์ ์ต์ ๋ฐ์ดํฐ ๊ฐ์ ์ด์์ ํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ ๊ฒฝ์ฐ, ํด๋น ๋ฐ์ดํฐ๋ฅผ ์๋ฏธ
- ์ด์ ํฌ์ธํธ(Neighbor Point) : ์ฃผ๋ณ ์์ญ ๋ด์ ์์นํ ํ ๋ฐ์ดํฐ
- ๊ฒฝ๊ณ ํฌ์ธํธ(Border Point) : ์ฃผ๋ณ ์์ญ ๋ด์ ์ต์ ๋ฐ์ดํฐ ๊ฐ์ ์ด์์ ์ด์ ํฌ์ธํธ๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์ง๋ง, ํต์ฌ ํฌ์ธํธ๋ฅผ ์ด์ ํฌ์ธํธ๋ก ๊ฐ์ง๊ณ ์๋ ๋ฐ์ดํฐ
- ์ก์ ํฌ์ธํธ(Noise Point) : ์ต์ ๋ฐ์ดํฐ ๊ฐ์ ์ด์์ ์ด์ ํฌ์ธํธ๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์ผ๋ฉฐ, ํต์ฌ ํฌ์ธํธ๋ ์ด์ ํฌ์ธํธ๋ก ๊ฐ์ง๊ณ ์์ง ์์ ๋ฐ์ดํฐ
1. epsilon๊ณผ min points ์ค์
2. ๋ฐ์ดํฐ๋ก๋ถํฐ Core Points์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์์ ์ ์ ํ
3. ๋ฐ๋ - ๋๋ฌ ๊ฐ๋ฅํ ์ ์ ๋ฝ์ Core Points์ Border Points ๊ตฌ๋ถ, ์ด์ ์ํ์ง ์์ ์ ๋ค์ Noise Points๋ก ๊ตฌ๋ถ
4. epsilon ๋ฐ๊ฒฝ ์์ Core Points ์ฐ๊ฒฐ
5. ์ฐ๊ฒฐ๋ ์ ๋ค์ ํ๋์ ๊ตฐ์ง์ผ๋ก ํ์ฑ
6. ๋ชจ๋ ์ ๋ค์ ๊ตฐ์ง์ ํ ๋นํด์ผํจ(์ฌ๋ฌ ๊ตฐ์ง์ ๊ฑธ์ณ์์ผ๋ฉด, ๋จผ์ ํ ๋น๋ ๊ตฐ์ง์ผ๋ก ํ ๋น)
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()
DBSCAN ์ ์ฉ
from sklearn.cluster import DBSCAN
dbscan = DBSCAN(eps=0.6,min_samples =8, metric='euclidean')
dbscan_labels = dbscan.fit_predict(iris.data)
iris_df['dbscan_cluster'] = dbscan_labels
iris_df['target'] = iris.target
iris_result = iris_df.groupby(['target'])['dbscan_cluster'].value_counts()
print(iris_result)
-1๋ก ํํ๋ ๊ตฐ์ง ๋ ์ด๋ธ์ Noise Point๋ฅผ ์๋ฏธํ๋ค. 3๊ฐ์ ํ๊ฒ์ด์ง๋ง, ์ค์ ๊ตฐ์ง์ 0,1 ๋๊ฐ์ง๋ก ์ด๋ฃจ์ด์ง ๊ฒ์ ํ์ธํ ์์๋ค. (๊ผญ ์์ข์ ๊ฒ์ ์๋!)
PCA๋ฅผ ์ ์ฉํ์ฌ 2๊ฐ์ ํผ์ฒ๋ก ์์ถ
#๊ตฐ์ง ์์ฑ ๋ฐ ์๊ฐํํ๋ ํจ์ ์์ฑ
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()
from sklearn.decomposition import PCA
pca = PCA(n_components=2,random_state = 0)
pca_transformed = pca.fit_transform(iris.data)
iris_df['ftr1'] = pca_transformed[:,0]
iris_df['ftr2'] = pca_transformed[:,1]
visualize_cluster_plot(dbscan,iris_df,'dbscan_cluster',iscenter = False)
โญ ํ์๋ ์ก์(Noise)๋ฅผ ์๋ฏธํ๋ค.
Noise ๋ฐ์ดํฐ๊ฐ ๋ง์ด ๋ณด์ด๋ ๊ฒ์ํ์ธํ ์ ์๋ค.
eps๋ฅผ ํฌ๊ฒ ํ๋ฉด ๋ฐ๊ฒฝ์ด ์ปค์ ธ ํฌํจํ๋ ๋ฐ์ดํฐ๊ฐ ๋ง์์ ธ ๋ ธ์ด์ฆ ๋ฐ์ดํฐ๊ฐ ๊ฐ์ํ๋ค.
min_samples๋ฅผ ํฌ๊ฒ ํ๋ฉด ์ฃผ์ด์ง ๋ฐ๊ฒฝ ๋ด์ ๋ ๋ง์ ๋ฐ์ดํฐ๋ฅผ ํฌํจ์์ผ์ผ ํ๋ฏ๋ก ๋ ธ์ด์ฆ ๊ฐ์๊ฐ ์ปค์ง๊ฒ ๋๋ค.
eps ์ฆ๊ฐ(0.6 => 0.8)
dbscan = DBSCAN(eps=0.8,min_samples = 8, metric='euclidean')
dbscan_labels = dbscan.fit_predict(iris.data)
iris_df['dbscan_cluster'] = dbscan_labels
iris_df['target'] = iris.target
iris_result = iris_df.groupby(['target'])['dbscan_cluster'].value_counts()
print(iris_result)
visualize_cluster_plot(dbscan,iris_df,'dbscan_cluster',iscenter=False)
๋ ธ์ด์ฆ๊ฐ 3๊ฐ๋ก ๊ฐ์ํ ๊ฒ์ ๋ณผ ์ ์๋ค.
min_samples ์ฆ๊ฐ(8 => 16)
dbscan = DBSCAN(eps=0.6,min_samples=16,metric='euclidean')
iris_result = iris_df.groupby(['target'])['dbscan_cluster'].value_counts()
print(iris_result)
visualize_cluster_plot(dbscan,iris_df,'dbscan_cluster',iscenter=False)
๋ ธ์ด์ฆ ๊ฐ์๋ ๋๊ฐ๋ค.
make_circles๋ฅผ ์ด์ฉํด ๋ฐ์ดํฐ ์ ์์ฑ
KMeans VS GMM VS DBSCAN
from sklearn.datasets import make_circles
X,y = make_circles(n_samples=1000, shuffle=True, noise=0.05, random_state =0,factor=0.5)
clusterDF = pd.DataFrame(data=X,columns=['ftr1','ftr2'])
clusterDF['target'] = y
visualize_cluster_plot(None, clusterDF, 'target', iscenter=False)
KMEANS
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=2,max_iter=1000,random_state=0)
kmeans_labels = kmeans.fit_predict(X)
clusterDF['kmeans_cluster'] = kmeans_labels
visualize_cluster_plot(kmeans,clusterDF,'kmeans_cluster',iscenter=True)
๊ตฐ์งํ๊ฐ ์ ์ด๋ฃจ์ด์ง์ง ์์๋ค.
GMM
from sklearn.mixture import GaussianMixture
gmm = GaussianMixture(n_components=2,random_state = 0)
gmm_label = gmm.fit_predict(X)
clusterDF['gmm_cluster'] = gmm_label
visualize_cluster_plot(gmm,clusterDF,'gmm_cluster',iscenter=False)
๋ง์ฐฌ๊ฐ์ง๋ก ๊ตฐ์งํ๊ฐ ์ ์ด๋ฃจ์ด ์ง์ง ์์๋ค.
DBSCAN
from sklearn.cluster import DBSCAN
dbscan = DBSCAN(eps=0.2,min_samples=10, metric='euclidean')
dbscan_labels = dbscan.fit_predict(X)
clusterDF['dbscan_cluster'] = dbscan_labels
visualize_cluster_plot(dbscan,clusterDF,'dbscan_cluster',iscenter=False)
์ ํํ๊ฒ ๊ตฐ์งํ๊ฐ ๋์๋ค.
DBSCAN์ ์ํ์ ๋ฐ์ดํฐ์ ์์ ๊ตฐ์งํ๊ฐ ์ ์ด๋ฃจ์ด์ง๋ ๊ฒ์ ํ์ธ
'๐ Machine Learning > Clustering' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python] GMM(Gaussian Mixture Model) (0) | 2022.03.03 |
---|---|
[Python] ํ๊ท ์ด๋ (0) | 2022.03.02 |
[Python] ๊ตฐ์ง ํ๊ฐ(์ค๋ฃจ์ฃ ๊ณ์) (0) | 2022.03.01 |
[Python] KMeans Clustering(K-ํ๊ท ๊ตฐ์งํ) (0) | 2022.02.28 |