python 密度聚类 使用_使用python+sklearn实现分层聚类:结构化与非结构化

注意:单击此处https://urlify.cn/3auQBf下载完整的示例代码,或通过Binder在浏览器中运行此示例

本示例构建瑞士卷数据集,并在该数据集上运行分层聚类(Hierarchical clustering)。 有关更多信息,请参见分层聚类。 第一步,在不对结构进行连通限制的情况下仅基于距离来进行分层聚类;而在第二步中,聚类仅限于k最近邻图:它是具有结构优先级的分层聚类。 在没有连通性约束的情况下学习到的某些聚类是不遵循瑞士卷结构的,并延伸到集合管(manifolds)的不同折叠处(folds)。相反,当面对相反的连通性约束时,从瑞士卷数据集中可以形成一个很好的聚类分割。

fe14657f0a0940dcbf3fead55fac63df.pngc64a364e5edb0c706a63c9edca22a22e.png

输出:
Compute unstructured hierarchical clustering...Elapsed time: 0.05sNumber of points: 1500Compute structured hierarchical clustering...Elapsed time: 0.10sNumber of points: 1500
# 作者 : Vincent Michel, 2010#           Alexandre Gramfort, 2010#           Gael Varoquaux, 2010# 许可证: BSD 3 clauseprint(__doc__)import time as timeimport numpy as npimport matplotlib.pyplot as pltimport mpl_toolkits.mplot3d.axes3d as p3from sklearn.cluster import AgglomerativeClusteringfrom sklearn.datasets import make_swiss_roll# ############################################################################## 生成数据(瑞士卷数据集)n_samples = 1500noise = 0.05X, _ = make_swiss_roll(n_samples, noise)# 使其更薄X[:, 1] *= .5# ############################################################################## 计算聚类print("Compute unstructured hierarchical clustering...")st = time.time()ward = AgglomerativeClustering(n_clusters=6, linkage='ward').fit(X)elapsed_time = time.time() - stlabel = ward.labels_print("Elapsed time: %.2fs" % elapsed_time)print("Number of points: %i" % label.size)# ############################################################################## 绘制结果fig = plt.figure()ax = p3.Axes3D(fig)ax.view_init(7, -80)for l in np.unique(label):    ax.scatter(X[label == l, 0], X[label == l, 1], X[label == l, 2],               color=plt.cm.jet(np.float(l) / np.max(label + 1)),               s=20, edgecolor='k')plt.title('Without connectivity constraints (time %.2fs)' % elapsed_time)# ############################################################################## 定义数据的结构A。10个最近邻from sklearn.neighbors import kneighbors_graphconnectivity = kneighbors_graph(X, n_neighbors=10, include_self=False)# ############################################################################## 计算聚类print("Compute structured hierarchical clustering...")st = time.time()ward = AgglomerativeClustering(n_clusters=6, connectivity=connectivity,                               linkage='ward').fit(X)elapsed_time = time.time() - stlabel = ward.labels_print("Elapsed time: %.2fs" % elapsed_time)print("Number of points: %i" % label.size)# ############################################################################## 绘制结果fig = plt.figure()ax = p3.Axes3D(fig)ax.view_init(7, -80)for l in np.unique(label):    ax.scatter(X[label == l, 0], X[label == l, 1], X[label == l, 2],               color=plt.cm.jet(float(l) / np.max(label + 1)),               s=20, edgecolor='k')plt.title('With connectivity constraints (time %.2fs)' % elapsed_time)plt.show()
脚本的总运行时间:(0分钟0.646秒) 估计的内存使用量:25 MB 8dd014774385129bbfd10d4c0d5ffe9c.png 下载Python源代码: plot_ward_structured_vs_unstructured.py 下载Jupyter notebook源代码: plot_ward_structured_vs_unstructured.ipynb 由Sphinx-Gallery生成的画廊

文壹由“伴编辑器”提供技术支持

☆☆☆为方便大家查阅,小编已将scikit-learn学习路线专栏 文章统一整理到公众号底部菜单栏,同步更新中,关注公众号,点击左下方“系列文章”,如图:

efe2d5abb5fe6a04100873111efec1f5.png

欢迎大家和我一起沿着scikit-learn文档这条路线,一起巩固机器学习算法基础。(添加微信:mthler备注:sklearn学习,一起进【sklearn机器学习进步群】开启打怪升级的学习之旅。)

ac4d8001635fe7ea66e8f73187e035fc.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值