基本思想

谱聚类是从图论中演化出来的算法,后来在聚类中得到了广泛的应用。它的主要思想是把所有的数据看做空间中的点,这些点之间可以用边连接起来。距离较远的两个点之间的边权重值较低,而距离较近的两个点之间的边权重值较高,通过对所有数据点组成的图进行切图,让切图后不同的子图间边权重和尽可能的低,而子图内的边权重和尽可能的高,从而达到聚类的目的。
    谱聚类的算法流程可总结如下:
    输入:样本集D=(x1,x2,⋯,xn)D=(x_1,x_2,\cdots,x_n)D=(x1​,x2​,⋯,xn​),相似矩阵的生成方式,降维后的维度k1k_1k1​,聚类方法,聚类后的维度k2k_2k2​
    输出:簇划分C(c1,c2,⋯,ck2)C(c_1,c_2,\cdots,c_{k_2})C(c1​,c2​,⋯,ck2​​)
    (1)根据输入的相似矩阵的生成方式构建样本的相似矩阵SSS
    (2)根据相似矩阵SSS构建邻接矩阵WWW,构建度矩阵DDD
    (3)计算出拉普拉斯矩阵LLL
    (4)构建标准化后的拉普拉斯矩阵D−12LD−12D^{-\frac{1}{2}}LD^{-\frac{1}{2}}D−21​LD−21​
    (5)计算D−12LD−12D^{-\frac{1}{2}}LD^{-\frac{1}{2}}D−21​LD−21​最小的k1k_1k1​个特征值所各自对应的特征向量fff
    (6)将各自对应的特征向量fff组成的矩阵按行标准化,最终组成n×k1n \times k_1n×k1​维的特征矩阵FFF
    (7)对FFF中的每一行作为一个k1k_1k1​维的样本,共nnn个样本,用输入的聚类方法进行聚类,聚类维数为k2k_2k2​
    (8)得到簇划分C(c1,c2,⋯,ck2)C(c_1,c_2,\cdots,c_{k_2})C(c1​,c2​,⋯,ck2​​)
    谱聚类的主要优点有:
    (1)谱聚类只需要数据之间的相似度矩阵,因此对于处理稀疏数据的聚类很有效。传统聚类算法比如K-Means很难做到这一点
    (2)由于使用了降维,因此在处理高维数据聚类时的复杂度比传统聚类算法好
    谱聚类的主要缺点有:
    (1)如果最终聚类的维度非常高,则由于降维的幅度不够,谱聚类的运行速度和最后的聚类效果可能都不好
    (2)聚类效果依赖于相似矩阵,不同的相似矩阵得到的最终聚类效果可能很不同
API学习

sklearn.cluster.spectral_clustering(affinity, *, n_clusters=8, n_components=None, eigen_solver=None, random_state=None, n_init=10, eigen_tol=0.0, assign_labels='kmeans', verbose=False
)
参数 类型 解释
affinity str or callable, default=’rbf’ 构建权重矩阵的方法,默认为径向基函数,还可选用’nearest_neighbors’/‘precomputed’/‘precomputed_nearest_neighbors’
n_clusters int, default=None 聚类数
n_components int, default=n_clusters 用于谱嵌入的特征向量数
eigen_solver {None, ‘arpack’, ‘lobpcg’, or ‘amg’} 使用的特征值分解方法,None表示使用’arpack’,使用’amg’需要pyamg包
random_state int, RandomState instance, default=None 初始化特征向量分解和K-Means聚类的随机数生成器
n_init int, default=10 迭代次数,当assign_labels为’kmeans’时使用
eigen_tol float, default=0.0 使用’arpack’时拉普拉斯矩阵特征分解的终止条件
assign_labels {‘kmeans’, ‘discretize’}, default=‘kmeans’ 拉普拉斯嵌入后使用的分类策略,'kmeans’策略对初始化更敏感
verbose bool, default=False 输出信息是否详细

代码示例

>>> from sklearn.cluster import SpectralClustering
>>> sc = SpectralClustering(3, affinity='precomputed', n_init=100,
...                         assign_labels='discretize')
>>> sc.fit_predict(adjacency_matrix)

作品学习

import numpy as np
import matplotlib.pyplot as pltfrom sklearn.feature_extraction import image
from sklearn.cluster import spectral_clusteringl = 100
x, y = np.indices((l, l))center1 = (28, 24)
center2 = (40, 50)
center3 = (67, 58)
center4 = (24, 70)radius1, radius2, radius3, radius4 = 16, 14, 15, 14circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1 ** 2
circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2 ** 2
circle3 = (x - center3[0]) ** 2 + (y - center3[1]) ** 2 < radius3 ** 2
circle4 = (x - center4[0]) ** 2 + (y - center4[1]) ** 2 < radius4 ** 2# #############################################################################
# 4 circles
img = circle1 + circle2 + circle3 + circle4# We use a mask that limits to the foreground: the problem that we are
# interested in here is not separating the objects from the background,
# but separating them one from the other.
mask = img.astype(bool)img = img.astype(float)
img += 1 + 0.2 * np.random.randn(*img.shape)# Convert the image into a graph with the value of the gradient on the
# edges.
graph = image.img_to_graph(img, mask=mask)# Take a decreasing function of the gradient: we take it weakly
# dependent from the gradient the segmentation is close to a voronoi
graph.data = np.exp(-graph.data / graph.data.std())# Force the solver to be arpack, since amg is numerically
# unstable on this example
labels = spectral_clustering(graph, n_clusters=4, eigen_solver="arpack")
label_im = np.full(mask.shape, -1.0)
label_im[mask] = labelsplt.matshow(img)
plt.matshow(label_im)# #############################################################################
# 2 circles
img = circle1 + circle2
mask = img.astype(bool)
img = img.astype(float)img += 1 + 0.2 * np.random.randn(*img.shape)graph = image.img_to_graph(img, mask=mask)
graph.data = np.exp(-graph.data / graph.data.std())labels = spectral_clustering(graph, n_clusters=2, eigen_solver="arpack")
label_im = np.full(mask.shape, -1.0)
label_im[mask] = labelsplt.matshow(img)
plt.matshow(label_im)plt.show()

运行结果:




参考文献

[1] Normalized cuts and image segmentation, 2000 Jianbo Shi, Jitendra Malik

[2] A Tutorial on Spectral Clustering, 2007 Ulrike von Luxburg

[3] Multiclass spectral clustering, 2003 Stella X. Yu, Jianbo Shi

[4] Toward the Optimal Preconditioned Eigensolver: Locally Optimal Block Preconditioned Conjugate Gradient Method, 2001. A. V. Knyazev SIAM Journal on Scientific Computing 23, no. 2, pp. 517-541.

sklearn学习之Spectral Clustering相关推荐

  1. 【论文阅读和实现】On Spectral Clustering: Analysis and an algorithm【Python实现】

    On Spectral Clustering: Analysis and an algorithm https://github.com/Sean16SYSU/MachineLearningImple ...

  2. python谱聚类算法_谱聚类Spectral clustering(SC)

    在之前的文章里,介绍了比较传统的K-Means聚类.Affinity Propagation(AP)聚类.比K-Means更快的Mini Batch K-Means聚类以及混合高斯模型Gaussian ...

  3. 谱聚类(Spectral Clustering)算法介绍

    一. 前言 本来想写关于聚类系列算法的介绍,但是聚类系列的其它几个算法原理比较简单,网上有大量的教程可以查阅.这里主要是介绍一下谱聚类算法,做一个学习笔记,同时也希望对想要了解该算法的朋友有一个帮助. ...

  4. 谱聚类 Spectral Clustering

    谱聚类 Spectral Clustering sklearn.cluster.SpectralClustering 谱聚类(Spectral Clustering, SC), 是一种基于图论的聚类方 ...

  5. 聚类算法K-Means, K-Medoids, GMM, Spectral clustering,Ncut .

    聚类算法是ML中一个重要分支,一般采用unsupervised learning进行学习,本文根据常见聚类算法分类讲解K-Means, K-Medoids, GMM, Spectral cluster ...

  6. Spectral clustering 谱聚类讲解及实现

    简述 https://github.com/Sean16SYSU/MachineLearningImplement 这篇是在网上看了wiki之后写出来的代码. 附上一篇看过论文之后根据论文实现的版本: ...

  7. 聚类算法K-Means, K-Medoids, GMM, Spectral clustering,Ncut

    聚类算法是ML中一个重要分支,一般采用unsupervised learning进行学习,本文根据常见聚类算法分类讲解K-Means, K-Medoids, GMM, Spectral cluster ...

  8. python谱聚类算法_谱聚类(spectral clustering)原理总结

    谱聚类(spectral clustering)是广泛使用的聚类算法,比起传统的K-Means算法,谱聚类对数据分布的适应性更强,聚类效果也很优秀,同时聚类的计算量也小很多,更加难能可贵的是实现起来也 ...

  9. 谱聚类(spectral clustering)原理总结

    谱聚类(spectral clustering)是广泛使用的聚类算法,比起传统的K-Means算法,谱聚类对数据分布的适应性更强,聚类效果也很优秀,同时聚类的计算量也小很多,更加难能可贵的是实现起来也 ...

最新文章

  1. sql server 与 mysql在自定以数据类型的区别
  2. 零基础自学python教程-零基础5个月快速学会Python的秘诀
  3. boost::EccentricityProperty用法的测试程序
  4. 如何结合SharePoint Designer 2010 与Visio 2010 创建工作流?
  5. 产品经理没有做过成功的产品,该何去何从?
  6. 跨server传输数据注意事项
  7. 中国农用喷洒机行业市场供需与战略研究报告
  8. ASP.NET登录状态保持 并 设置IE cookie
  9. qq android qav,33 BK.QQAVManager 音视频管理
  10. matlab 求导数
  11. js实现页面视频全屏播放
  12. 数学游戏“数三角形”的可编程图论模型
  13. APNG替代GIF制作完美动效
  14. Unity拼图小游戏
  15. python怎么设置随机数种子_Pytorch在dataloader类中设置shuffle的随机数种子方式
  16. 传奇世界手游活人最多服务器,传奇世界手游那些不为人知小技巧分享
  17. 转置矩阵,逆矩阵和倒转置矩阵
  18. EasyCVR平台接入大华EVS存储服务器,需要注意哪些事项?
  19. 统计一组名字中每个姓出现的次数
  20. toft 测试用例rat_TD-LTE终端测试规范——通信功能和性能分册(上).pdf

热门文章

  1. ROT13加密和解密
  2. matlab设计椭圆低通滤波器,【 MATLAB 】ellip 函数介绍(椭圆滤波器设计)
  3. curl打开网页403或者302的解决方法(详细总结)
  4. arcgis10之给图层添加注记
  5. java 法定节假日_【java】如何获得每年的法定节假日期以及全年的日历
  6. 国内外主流云计算开发平台对比图 云开发平台
  7. 基于STM32单片机智能手环脉搏心率检测计步器原理图PCB
  8. C# NPOI 导出Excel 日期格式
  9. vins estimator ProjectionFactor (Td) factor
  10. 安徽省大数据与人工智能竞赛经验分享-4【从赛题角度看人员分工】