spectral_clustering

spectral_clustering#

graph_tool.spectral.spectral_clustering(g, k, weight=None, b=None)[git][source]#

Partition the vertices with spectral clustering.

Parameters:
gGraph

Graph to be used (the edge directions are ignored).

kint

Number of groups.

weightEdgePropertyMap (optional, default: None)

Edge weights.

bVertexPropertyMap (optional, default: None)

If provided, the labels will be stored in this property map.

Returns:
bVertexPropertyMap

The group of every vertex, from 0 to \(k-1\).

See also

spectral_embedding

Spectral embedding of the vertices

algebraic_connectivity

Algebraic connectivity and Fiedler vector

graph_tool.inference.NormCutState

Normalized cut partition

graph_tool.inference.minimize_blockmodel_dl

SBM 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, see seed_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 by minimize_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")
<...>
../_images/spectral_clustering-polbooks.png

Partition of the network of political books into three groups obtained with spectral clustering, with the groups shown in blue, red, and green.#