一.DBSCAN算法
(1)简介
一种基于密度的聚类算法:
• 聚类的时候不需要预先指定簇的个数
• 最终的簇的个数不定
(2)数据点分类
• 核心点:在半径Eps内含有超过MinPts数目的点
• 边界点:在半径Eps内点的数量小于MinPts,但是落在核心点的邻域内
• 噪音点:既不是核心点也不是边界点的点
(3)DBSCAN算法流程
1.将所有点标记为核心点、边界点或噪声点;
2.删除噪声点;
3.为距离在Eps之内的所有核心点之间赋予一条边;
4.每组连通的核心点形成一个簇;
5.将每个边界点指派到一个与之关联的核心点的簇中(哪一个核心点的半径范围之内)。
举例:有如下13个样本点,使用DBSCAN进行聚类
二.DBSCAN的应用实例
(1)数据介绍
现有大学校园网的日志数据,290条大学生的校园网使用情况数据,数据包括用户ID,设备的MAC地址,IP地址,开始上网时间,停止上网时间,上网时长,校园网套餐等。利用已有数据,分析学生上网的模式。
实验目的:通过DBSCAN聚类,分析学生上网时间和上网时长的模式。
技术路线:sklearn.cluster.DBSCAN
数据实例:
(2)实验过程
• 使用算法: DBSCAN聚类算法
• 实现过程:
上网时间聚类,创建DBSCAN算法实例,并进行训练,获得标签:
import numpy as np import sklearn.cluster as skc from sklearn import metrics import matplotlib.pyplot as plt mac2id=dict() onlinetimes=[] f=open(r'学生月上网时间分布-TestData.txt',encoding='utf-8') for line in f: #读取每条数据中的mac地址,开始上网时间,上网时长 mac=line.split(',')[2] onlinetime=int(line.split(',')[6]) starttime=int(line.split(',')[4].split(' ')[1].split(':')[0]) #mac2id是一个字典:key是mac地址,value是对应mac地址的上网时长以及开始上网时间 if mac not in mac2id: mac2id[mac]=len(onlinetimes) onlinetimes.append((starttime,onlinetime)) else: onlinetimes[mac2id[mac]]=[(starttime,onlinetime)] real_X=np.array(onlinetimes).reshape((-1,2)) #调用DBSCAN方法训练,labels为每个数据的簇标签 X=real_X[:,0:1] db=skc.DBSCAN(eps=0.01,min_samples=20).fit(X) labels = db.labels_ #打印数据被记上的标签,计算标签为-1,即噪声数据的比例 print('Labels:') print(labels) raito=len(labels[labels[:] == -1]) / len(labels) print('Noise raito:',format(raito, '.2%')) #计算簇的个数并打印,评价聚类效果 n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) print('Estimated number of clusters: %d' % n_clusters_) print("Silhouette Coefficient: %0.3f"% metrics.silhouette_score(X, labels)) #打印各簇标号以及各簇内数据 for i in range(n_clusters_): print('Cluster ',i,':') print(list(X[labels == i].flatten())) plt.hist(X,24) plt.show()
结果显示如下:
Labels: [ 0 -1 0 1 -1 1 0 1 2 -1 1 0 1 1 3 -1 -1 3 -1 1 1 -1 1 3 4 -1 1 1 2 0 2 2 -1 0 1 0 0 0 1 3 -1 0 1 1 0 0 2 -1 1 3 1 -1 3 -1 3 0 1 1 2 3 3 -1 -1 -1 0 1 2 1 -1 3 1 1 2 3 0 1 -1 2 0 0 3 2 0 1 -1 1 3 -1 4 2 -1 -1 0 -1 3 -1 0 2 1 -1 -1 2 1 1 2 0 2 1 1 3 3 0 1 2 0 1 0 -1 1 1 3 -1 2 1 3 1 1 1 2 -1 5 -1 1 3 -1 0 1 0 0 1 -1 -1 -1 2 2 0 1 1 3 0 0 0 1 4 4 -1 -1 -1 -1 4 -1 4 4 -1 4 -1 1 2 2 3 0 1 0 -1 1 0 0 1 -1 -1 0 2 1 0 2 -1 1 1 -1 -1 0 1 1 -1 3 1 1 -1 1 1 0 0 -1 0 -1 0 0 2 -1 1 -1 1 0 -1 2 1 3 1 1 -1 1 0 0 -1 0 0 3 2 0 0 5 -1 3 2 -1 5 4 4 4 -1 5 5 -1 4 0 4 4 4 5 4 4 5 5 0 5 4 -1 4 5 5 5 1 5 5 0 5 4 4 -1 4 4 5 4 0 5 4 -1 0 5 5 5 -1 4 5 5 5 5 4 4] #每个数据被划分的簇的分类 Noise raito: 22.15% #噪声数据的比例 Estimated number of clusters: 6 #簇的个数 Silhouette Coefficient: 0.710 #聚类效果评价指标 Cluster 0 : [22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22] Cluster 1 : [23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23] Cluster 2 : [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20] Cluster 3 : [21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21] Cluster 4 : [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8] Cluster 5 : [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]
(3)改进(对数变换)