Python实现DBSCAN聚类算法并样例测试
作者:午夜的行人 时间:2022-04-22 22:25:48
什么是聚类算法
聚类是一种机器学习技术,它涉及到数据点的分组。给定一组数据点,我们可以使用聚类算法将每个数据点划分为一个特定的组。理论上,同一组中的数据点应该具有相似的属性和/或特征,而不同组中的数据点应该具有高度不同的属性和/或特征。聚类是一种无监督学习的方法,是许多领域中常用的统计数据分析技术。
常用的算法包括K-MEANS、高斯混合模型(Gaussian Mixed Model,GMM)、自组织映射神经网络(Self-Organizing Map,SOM)
重点给大家介绍Python实现DBSCAN聚类算法并通过简单样例测试。
发现高密度的核心样品并从中膨胀团簇。
Python代码如下:
# -*- coding: utf-8 -*-
"""
Demo of DBSCAN clustering algorithm
Finds core samples of high density and expands clusters from them.
"""
print(__doc__)
# 引入相关包
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
import matplotlib.pyplot as plt
# 初始化样本数据
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)
# 计算DBSCAN
db = DBSCAN(eps=0.3, min_samples=10).fit(X)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_
# 聚类的结果
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
n_noise_ = list(labels).count(-1)
print('Estimated number of clusters: %d' % n_clusters_)
print('Estimated number of noise points: %d' % n_noise_)
print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels))
print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels))
print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels))
print("Adjusted Rand Index: %0.3f"
% metrics.adjusted_rand_score(labels_true, labels))
print("Adjusted Mutual Information: %0.3f"
% metrics.adjusted_mutual_info_score(labels_true, labels,
average_method='arithmetic'))
print("Silhouette Coefficient: %0.3f"
% metrics.silhouette_score(X, labels))
# 绘出结果
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:
col = [0, 0, 0, 1]
class_member_mask = (labels == k)
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()
测试结果如下:
最终结果绘图:
具体数据:
来源:https://www.cnblogs.com/wydxry/p/11029691.html
标签:Python,DBSCAN,聚类算法
0
投稿
猜你喜欢
MySql数据库捕获sql语句异常的方法
2011-08-05 18:17:28
浅谈Python traceback的优雅处理
2023-08-06 06:57:42
使用Python读写及压缩和解压缩文件的示例
2023-08-30 17:42:52
将数据从MySQL迁移到 Oracle的注意事项
2008-12-03 15:41:00
CentOS下php使用127.0.0.1不能连接mysql的解决方法
2023-11-15 08:25:52
PHP rsa加密解密算法原理解析
2023-08-18 17:23:38
全方位清理浮动
2009-06-16 14:51:00
Python实现随机生成图片验证码详解
2023-02-26 12:20:20
简介Python中用于处理字符串的center()方法
2021-04-15 20:47:17
python 爬虫如何实现百度翻译
2023-02-20 18:33:51
详解Python如何利用turtle绘制中国结
2021-02-10 13:52:29
Date对象格式化方法
2009-11-16 13:17:00
Python 如何解决稀疏矩阵运算
2022-02-13 21:59:01
怎样用cmd命令行运行Python文件
2023-07-15 00:25:11
python数组处理之最值与下标问题
2023-06-01 12:13:27
python使用re模块爬取豆瓣Top250电影
2023-10-11 17:28:38
pytorch cnn 识别手写的字实现自建图片数据
2023-04-18 02:39:22
SQL列名无效 sql查询列名 sql返回列名
2009-09-03 13:19:00
python 公共方法汇总解析
2023-11-23 03:03:13
使用python处理一万份word表格简历操作
2021-09-15 17:36:36