11 聚類算法 - 密度聚類 - DBSCAN徒像、MDCA
需求: 使用scikit的相關(guān)API創(chuàng)建模擬數(shù)據(jù)触徐,然后使用DBSCAN密度聚類算法進(jìn)行數(shù)據(jù)聚類操作则北,并比較DBSCAN算法在不同參數(shù)情況下的密度聚類效果群发。
相關(guān)API:https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html
常規(guī)操作:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import sklearn.datasets as ds
import matplotlib.colors
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
## 設(shè)置屬性防止中文亂碼及攔截異常信息
mpl.rcParams['font.sans-serif'] = [u'SimHei']
mpl.rcParams['axes.unicode_minus'] = False
1川背、創(chuàng)建模擬數(shù)據(jù)
N = 1000
centers = [[1, 2], [-1, -1], [1, -1], [-1, 1]]
data1, y1 = ds.make_blobs(N, n_features=2,
centers=centers, cluster_std=(1,0.75, 0.5,0.25), random_state=0)
data1 = StandardScaler().fit_transform(data1)
params1 = ((0.15, 5), (0.2, 10), (0.2, 15), (0.3, 5), (0.3, 10), (0.3, 15))
t = np.arange(0, 2 * np.pi, 0.1)
data2_1 = np.vstack((np.cos(t), np.sin(t))).T
data2_2 = np.vstack((2*np.cos(t), 2*np.sin(t))).T
data2_3 = np.vstack((3*np.cos(t), 3*np.sin(t))).T
data2 = np.vstack((data2_1, data2_2, data2_3))
y2 = np.vstack(([0] * len(data2_1), [1] * len(data2_2), [2] * len(data2_3)))
params2 = ((0.5, 3), (0.5, 5), (0.5, 10), (1., 3), (1., 10), (1., 20))
datasets = [(data1, y1,params1), (data2, y2,params2)]
def expandBorder(a, b):
d = (b - a) * 0.1
return a-d, b+d
2砂轻、畫(huà)圖
colors = ['r', 'g', 'b', 'y', 'c', 'k']
cm = mpl.colors.ListedColormap(colors)
for i,(X, y, params) in enumerate(datasets):
x1_min, x2_min = np.min(X, axis=0)
x1_max, x2_max = np.max(X, axis=0)
x1_min, x1_max = expandBorder(x1_min, x1_max)
x2_min, x2_max = expandBorder(x2_min, x2_max)
plt.figure(figsize=(12, 8), facecolor='w')
plt.suptitle(u'DBSCAN聚類-數(shù)據(jù)%d' % (i+1), fontsize=20)
plt.subplots_adjust(top=0.9,hspace=0.35)
for j,param in enumerate(params):
eps, min_samples = param
model = DBSCAN(eps=eps, min_samples=min_samples)
#eps 半徑猫十,控制鄰域的大小蚓再,值越大只损,越能容忍噪聲點(diǎn)一姿,
#值越小,相比形成的簇就越多
#min_samples 原理中所說(shuō)的M跃惫,控制哪個(gè)是核心點(diǎn)叮叹,
#值越小,越可以容忍噪聲點(diǎn)爆存,越大蛉顽,就更容易把有效點(diǎn)劃分成噪聲點(diǎn)
model.fit(X)
y_hat = model.labels_
unique_y_hat = np.unique(y_hat)
n_clusters = len(unique_y_hat) - (1 if -1 in y_hat else 0)
print ("類別:",unique_y_hat,";聚類簇?cái)?shù)目:",n_clusters)
core_samples_mask = np.zeros_like(y_hat, dtype=bool)
core_samples_mask[model.core_sample_indices_] = True
## 開(kāi)始畫(huà)圖
plt.subplot(3,3,j+1)
for k, col in zip(unique_y_hat, colors):
if k == -1:
col = 'k'
class_member_mask = (y_hat == k)
xy = X[class_member_mask & core_samples_mask]
plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
xy = X[class_member_mask & ~core_samples_mask]
plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=6)
plt.xlim((x1_min, x1_max))
plt.ylim((x2_min, x2_max))
plt.grid(True)
plt.title('$\epsilon$ = %.1f m = %d先较,聚類簇?cái)?shù)目:%d' % (eps, min_samples,
n_clusters), fontsize=16)
## 原始數(shù)據(jù)顯示
plt.subplot(3,3,7)
plt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=cm, edgecolors='none')
plt.xlim((x1_min, x1_max))
plt.ylim((x2_min, x2_max))
plt.title('原始數(shù)據(jù)携冤,聚類簇?cái)?shù)目:%d' % len(np.unique(y)))
plt.grid(True)
plt.show()
下章開(kāi)始講圖形聚類 - 譜聚類
13 聚類算法 - 譜聚類