spectral_embedding#
- graph_tool.spectral.spectral_embedding(g, dim=2, weight=None, norm=True, pos=None, matrix='laplacian', scaled=True, max_dim=50)[git][source]#
Return a spectral embedding of the vertices, given by the eigenvectors of the Laplacian (Laplacian eigenmaps) or of the adjacency matrix.
- Parameters:
- g
Graph Graph to be used. For
matrix == "laplacian", the edge directions are ignored.- dim
intor"auto"(optional, default:2) Number of dimensions. If
dim == "auto", it is chosen from the eigenvalues (or singular values) withdim_select()(only formatrix == "adjacency").- weight
EdgePropertyMap(optional, default:None) Edge weights.
- norm
bool(optional, default:True) If
True, the normalized Laplacian is used (only formatrix == "laplacian").- pos
VertexPropertyMap(optional, default:None) If provided, the coordinates will be stored in this vector-valued property map (for directed graphs and
matrix == "adjacency", the out-embedding).- matrix
str(optional, default:"laplacian") Matrix to be decomposed:
"laplacian"or"adjacency".- scaled
bool(optional, default:True) If
True, the eigenvectors of the adjacency matrix are scaled by the square roots of the magnitudes of the eigenvalues (or singular values).- max_dim
int(optional, default:50) Number of eigenvalues (or singular values) from which the dimension is chosen if
dim == "auto", for graphs too large for a dense decomposition (for smaller graphs, all of them are used).
- g
- Returns:
- pos
VertexPropertyMap The coordinates of every vertex.
- pos_in
VertexPropertyMap The in-embedding, only returned for directed graphs if
matrix == "adjacency", in which caseposis the out-embedding.
- pos
See also
dim_selectSelection of the embedding dimension
spectral_clusteringSpectral clustering
laplacianLaplacian matrix
graph_tool.draw.spectral_layoutSpectral layout
Notes
With
matrix == "laplacian", the coordinates are the entries of the eigenvectors of the Laplacian with the smallest eigenvalues, skipping the trivial one [belkin-laplacian-2003] (for disconnected graphs, the first eigenvectors are the indicators of the components). More precisely, if \(u_1, u_2, \dots\) are the unit eigenvectors of \(L = D - A\), defined as inalgebraic_connectivity()(with the edge directions and self-loops ignored), sorted by increasing eigenvalue, the coordinates of vertex \(i\) are \((u_{2,i}, \dots, u_{d+1,i})\), where \(d\) isdim. Ifnorm == True, the eigenvectors \(x_1, x_2, \dots\) of the generalized eigenvalue problem \(Lx = \lambda Dx\) of [belkin-laplacian-2003] are used instead, with the normalization \(x^TDx = 1\); they are obtained as \(x = D^{-1/2}u\) from the unit eigenvectors \(u\) of the normalized Laplacian \(I - D^{-1/2}AD^{-1/2}\) (as in scikit-learn’sspectral_embedding), with the entries of the vertices of degree zero left unchanged.With
matrix == "adjacency", the adjacency spectral embedding [sussman-consistent-2012] is computed: for undirected graphs, the coordinates are given by \(X = U|\Lambda|^{1/2}\), where \(\Lambda\) are thedimeigenvalues of the adjacency matrix with the largest magnitudes, and \(U\) the corresponding eigenvectors. For directed graphs, the truncated singular value decomposition \(A^T \approx USV^T\) is used instead (where the rows of \(A^T\) correspond to the sources of the edges), and two embeddings are returned, the out-embedding \(X = US^{1/2}\) and the in-embedding \(Y = VS^{1/2}\), so that \(XY^T\) approximates \(A^T\). Ifdim == "auto", the number of dimensions is chosen with the method of Zhu and Ghodsi [zhu-automatic-2006] (seedim_select()) from the magnitudes of the eigenvalues (or the singular values).The signs of the eigenvectors are arbitrary. The embedding can be used as a layout with
graph_draw().References
[belkin-laplacian-2003] (1,2)Mikhail Belkin and Partha Niyogi, “Laplacian eigenmaps for dimensionality reduction and data representation”, Neural Computation 15, 1373-1396 (2003), DOI: 10.1162/089976603321780317 [sci-hub, @tor]
[sussman-consistent-2012]Daniel L. Sussman, Minh Tang, Donniell E. Fishkind and Carey E. Priebe, “A consistent adjacency spectral embedding for stochastic blockmodel graphs”, Journal of the American Statistical Association 107, 1119-1128 (2012), DOI: 10.1080/01621459.2012.699795 [sci-hub, @tor]
[zhu-automatic-2006]Mu Zhu and Ali Ghodsi, “Automatic dimensionality selection from the scree plot via the use of profile likelihood”, Computational Statistics & Data Analysis 51, 918-930 (2006), DOI: 10.1016/j.csda.2005.09.010 [sci-hub, @tor]
Examples
>>> g = gt.collection.data["football"] >>> pos = gt.spectral_embedding(g) >>> print(pos[0].a.shape) (2,) >>> pos = gt.spectral_embedding(g, dim="auto", matrix="adjacency") >>> print(pos[0].a.shape) (11,)
For the network of political books, the two-dimensional Laplacian embedding can be used as a layout, with the vertices colored according to the political leaning of the books (liberal, neutral, or conservative):
>>> g = gt.collection.data["polbooks"] >>> pos = gt.spectral_embedding(g) >>> colors = {"l": "#9bbedc", "n": "#cccccc", "c": "#f28c8e"} >>> vcolor = g.new_vp("string", vals=[colors[x] for x in g.vp.value]) >>> gt.graph_draw(g, pos, vertex_fill_color=vcolor, ... output="spectral_embedding-polbooks.pdf") <...>
Two-dimensional Laplacian embedding of the network of political books, used as a layout. The liberal books are shown in blue, the neutral ones in gray, and the conservative ones in red.#