参考资料:python机器学习库sklearn——DBSCAN密度聚类, Python实现DBScan
import numpy as np
from sklearn.cluster import DBSCAN
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs
from sklearn.preprocessing import StandardScaler # #############################################################################
# 产生样本数据
centers = [[1, 1], [-1, -1], [1, -1]] # 生成聚类中心点
X, labels_true = make_blobs(n_samples=750, centers=centers, cluster_std=0.4,random_state=0) # 生成样本数据集 X = StandardScaler().fit_transform(X) # StandardScaler作用:去均值和方差归一化。且是针对每一个特征维度来做的,而不是针对样本。 # 参数设置
aa = []
for i in range(X.shape[0]-1):
for j in range(i+1,X.shape[0]):
aa.append(np.power(X[i]-X[j], 2).sum())
plt.hist(aa, bins=10, density=1, edgecolor ='k', facecolor='g', alpha=0.75) # 调参#############################################################################
t0 = time.time()
optimum_parameter = [0,0,0]
for r in np.linspace(0.1, 0.3, 5):
for min_samples in range(5,12):
db = DBSCAN(eps=r, min_samples=min_samples).fit(X)
score = metrics.silhouette_score(X, db.labels_)
print('(%0.2f, %d) 轮廓系数: %0.3f'%(r, min_samples, score))
if score > optimum_parameter[2]: optimum_parameter=[r, min_samples, score]
print('最佳参数为:eps=%0.2f, min_samples=%d, 轮廓系数=%0.3f'%(optimum_parameter[0], optimum_parameter[1], optimum_parameter[2]))
print('调参耗时:', time.time()-t0) # #############################################################################
# 调用密度聚类 DBSCAN
db = DBSCAN(eps=0.3, min_samples=9).fit(X)
# print(db.labels_) # db.labels_为所有样本的聚类索引,没有聚类索引为-1
# print(db.core_sample_indices_) # 所有核心样本的索引
core_samples_mask = np.zeros_like(db.labels_, dtype=bool) # 设置一个样本个数长度的全false向量
core_samples_mask[db.core_sample_indices_] = True #将核心样本部分设置为true
labels = db.labels_ # 获取聚类个数。(聚类结果中-1表示没有聚类为离散点)
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) # 模型评估
print('估计的聚类个数为: %d' % n_clusters_)
print("同质性: %0.3f" % metrics.homogeneity_score(labels_true, labels)) # 每个群集只包含单个类的成员。
print("完整性: %0.3f" % metrics.completeness_score(labels_true, labels)) # 给定类的所有成员都分配给同一个群集。
print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels)) # 同质性和完整性的调和平均
print("调整兰德指数: %0.3f" % metrics.adjusted_rand_score(labels_true, labels))
print("调整互信息: %0.3f" % metrics.adjusted_mutual_info_score(labels_true, labels))
print("轮廓系数: %0.3f" % metrics.silhouette_score(X, labels)) # #############################################################################
# Plot result
import matplotlib.pyplot as plt # 使用黑色标注离散点
unique_labels = set(labels)
colors = [plt.cm.Spectral(each) for each in np.linspace(0, 1, len(unique_labels))]
for k, col in zip(unique_labels, colors):
if k == -1: # 聚类结果为-1的样本为离散点
# 使用黑色绘制离散点
col = [0, 0, 0, 1] class_member_mask = (labels == k) # 将所有属于该聚类的样本位置置为true xy = X[class_member_mask & core_samples_mask] # 将所有属于该类的核心样本取出,使用大图标绘制
plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),markeredgecolor='k', markersize=14) xy = X[class_member_mask & ~core_samples_mask] # 将所有属于该类的非核心样本取出,使用小图标绘制
plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),markeredgecolor='k', markersize=6) plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()