umap_layout

Contents

umap_layout#

graph_tool.draw.umap_layout(g, dist=None, pos=None, dim=2, min_dist=0.1, spread=1.0, epochs=500, negative_sample_rate=5, learning_rate=1.0)[git][source]#

Calculate a layout of the graph with the UMAP algorithm.

Parameters:
gGraph

Graph to be used (the edge directions are ignored), typically a \(k\)-nearest-neighbor graph (see generate_knn()).

distEdgePropertyMap (optional, default: None)

Edge lengths (distances between the data points). If not given, every edge has unit length.

posVertexPropertyMap (optional, default: None)

Initial positions. If not given, a scaled spectral embedding is used.

dimint (optional, default: 2)

Number of dimensions.

min_distfloat (optional, default: 0.1)

Smallest distance between embedded points.

spreadfloat (optional, default: 1.)

Scale of the embedded points.

epochsint (optional, default: 500)

Number of optimization epochs.

negative_sample_rateint (optional, default: 5)

Number of repulsive samples per attractive one.

learning_ratefloat (optional, default: 1.)

Initial learning rate.

Returns:
posVertexPropertyMap

The vertex positions.

See also

stress_layout

Layout by stress majorization

mds_layout

Layout by classical multidimensional scaling

sfdp_layout

Multilevel force-directed layout

graph_tool.generation.generate_knn

K-nearest-neighbor graph

Notes

The UMAP algorithm [mcinnes-umap-2018] assigns to every edge \((i, j)\) the membership strength \(w_{i\to j} = e^{-\max(0, d_{ij}-\rho_i)/\sigma_i}\), where \(\rho_i\) is the length of the shortest edge of \(i\) and \(\sigma_i\) is chosen such that \(\sum_j w_{i\to j} = \log_2 k_i\), symmetrized as \(w_{ij} = w_{i\to j} + w_{j\to i} - w_{i\to j}w_{j\to i}\). (Here \(k_i\) is the degree of \(i\), parallel edges add up, self-loops are ignored, and \(\sigma_i\) is found by bisection.) The positions minimize the cross-entropy

\[C = \sum_{i<j}\left[w_{ij}\ln\frac{w_{ij}}{v_{ij}} + (1 - w_{ij}) \ln\frac{1 - w_{ij}}{1 - v_{ij}}\right], \qquad v_{ij} = \frac{1}{1 + a|\boldsymbol x_i - \boldsymbol x_j|^{2b}},\]

where \(a\) and \(b\) are obtained by a least-squares fit of \(v(r) = 1/(1 + ar^{2b})\) to the function that is one for \(r < r_0\) and \(e^{-(r - r_0)/s}\) otherwise, with \(r_0\) given by min_dist and \(s\) by spread, on 300 points in \([0, 3s]\). The minimization is done by stochastic gradient descent. In epoch \(n = 1, \dots, N\) (where \(N\) is epochs), with the learning rate \(\eta_n = \eta_0(1 - (n - 1)/N)\), where \(\eta_0\) is learning_rate, every edge \((i, j)\) of strength \(w_{ij}\) is sampled once every \(\max w/w_{ij}\) epochs, and moves its endpoints by

\[\boldsymbol x_i \leftarrow \boldsymbol x_i + \eta_n\boldsymbol g, \quad \boldsymbol x_j \leftarrow \boldsymbol x_j - \eta_n\boldsymbol g, \qquad \boldsymbol g = -\frac{2abr^{2(b-1)}}{1 + ar^{2b}} (\boldsymbol x_i - \boldsymbol x_j),\]

with \(r = |\boldsymbol x_i - \boldsymbol x_j|\), after which, on average negative_sample_rate times per sample, a uniformly chosen vertex \(k\ne i\) repels \(i\) alone with

\[\boldsymbol x_i \leftarrow \boldsymbol x_i + \eta_n\frac{2b}{(0.001 + r_{ik}^2)(1 + ar_{ik}^{2b})}(\boldsymbol x_i - \boldsymbol x_k),\]

where every component of the moves is clipped to \([-4, 4]\) before it is multiplied by \(\eta_n\). If pos is not given, the initial positions are given by spectral_embedding(), rescaled so that the largest absolute coordinate is 10 (or, if it fails, with every coordinate drawn uniformly in \([-10, 10]\)), plus Gaussian noise of standard deviation \(10^{-4}\), drawn with NumPy’s random number generator. Every epoch takes time \(O(E)\). The edges of every epoch are processed in parallel, with no synchronization between the threads within an epoch, as in the reference implementation with parallel=True, so that the result is not deterministic when more than one thread is used. This is the layout of the reference implementation of UMAP, applied to the given graph.

References

[mcinnes-umap-2018]

Leland McInnes, John Healy and James Melville, “UMAP: Uniform manifold approximation and projection for dimension reduction”, arXiv: 1802.03426

Parallel implementation.

If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.

Examples

>>> g = gt.collection.data["football"]
>>> pos = gt.umap_layout(g)
>>> gt.graph_draw(g, pos=pos, vertex_fill_color=g.vp.value,
...               output="football-umap.pdf")
<...>
../_images/football-umap.png

UMAP layout of the network of American college football games, with the vertex colors given by the conferences.#