spectral_clustering#
- graph_tool.spectral.spectral_clustering(g, k, weight=None, b=None)[git][source]#
Partition the vertices with spectral clustering.
- Parameters:
- g
Graph Graph to be used (the edge directions are ignored).
- k
int Number of groups.
- weight
EdgePropertyMap(optional, default:None) Edge weights.
- b
VertexPropertyMap(optional, default:None) If provided, the labels will be stored in this property map.
- g
- Returns:
- b
VertexPropertyMap The group of every vertex, from 0 to \(k-1\).
- b
See also
spectral_embeddingSpectral embedding of the vertices
algebraic_connectivityAlgebraic connectivity and Fiedler vector
graph_tool.inference.NormCutStateNormalized cut partition
graph_tool.inference.minimize_blockmodel_dlSBM inference
Notes
The algorithm of Ng, Jordan, and Weiss [ng-spectral-2002] is used: the \(k\) eigenvectors of the normalized Laplacian \(I - D^{-1/2}AD^{-1/2}\) (defined as in
algebraic_connectivity(), with the edge directions and self-loops ignored) with the smallest eigenvalues are computed and placed as the columns of a \(V\times k\) matrix, whose rows are normalized to unit length and clustered with \(k\)-means, i.e. by locally minimizing the sum of the squared Euclidean distances between the rows and the centers of their groups, initialized with \(k\)-means++ [arthur-kmeans-2007] (using the random number generator of graph-tool, seeseed_rng()) and iterated until the assignments no longer change (at most 300 times), in time \(O(Vk^2)\) per iteration after the eigenvectors are obtained. The number of groups must be given, and no criterion is optimized for the graph itself; principled inference of the groups and of their number is offered byminimize_blockmodel_dl().References
[ng-spectral-2002]Andrew Y. Ng, Michael I. Jordan and Yair Weiss, “On spectral clustering: Analysis and an algorithm”, Advances in Neural Information Processing Systems 14, 849-856 (2002).
[arthur-kmeans-2007]David Arthur and Sergei Vassilvitskii, “k-means++: The advantages of careful seeding”, Proceedings of the Eighteenth Annual ACM-SIAM Symposium on Discrete Algorithms, 1027-1035 (2007).
Examples
>>> g = gt.collection.data["football"] >>> b = gt.spectral_clustering(g, 12) >>> print(gt.modularity(g, b)) 0.5964794...
For the network of political books, the groups found with \(k=3\) can be shown in a drawing:
>>> g = gt.collection.data["polbooks"] >>> b = gt.spectral_clustering(g, 3) >>> colors = ["#9bbedc", "#f28c8e", "#a6d7a4"] >>> vcolor = g.new_vp("string", vals=[colors[r] for r in b.a]) >>> gt.graph_draw(g, g.vp.pos, vertex_fill_color=vcolor, ... output="spectral_clustering-polbooks.pdf") <...>
Partition of the network of political books into three groups obtained with spectral clustering, with the groups shown in blue, red, and green.#