harmonic_function#
- graph_tool.inference.harmonic_function(g, labels, weight=None, max_iter=30)[git][source]#
Predict the labels of the unlabeled vertices with the harmonic function.
- Parameters:
- g
Graph Undirected graph.
- labels
VertexPropertyMap Integer labels of the vertices, with negative values for the unlabeled ones.
- weight
EdgePropertyMap(optional, default:None) Edge weights.
- max_iter
int(optional, default:30) Number of iterations.
- g
- Returns:
- pred
VertexPropertyMap The label of every vertex (the given ones for the labeled vertices).
- pred
See also
local_and_global_consistencyLocal and global consistency method
label_propagationLabel propagation heuristic
Notes
The method of Zhu, Ghahramani, and Lafferty [zhu-semi-supervised-2003] finds, for every class, the harmonic function that is one at the vertices with this label, zero at the vertices with other labels, and equal to the weighted average of its neighbors at the unlabeled vertices (the absorption probability of a random walk); every unlabeled vertex gets the class with the largest value. It is approximated by
max_iteriterations of \(F \leftarrow PF + B\), starting from \(F = 0\), where \(F_{vc}\) is the score of class \(c\) at vertex \(v\), \(P = D^{-1}A\) is the transition matrix of the random walk, with the rows of the labeled vertices set to zero, \(A\) the weighted adjacency matrix (with self-loops counted twice), \(D\) the diagonal matrix of the weighted degrees (set to one for isolated vertices), and \(B_{vc} = 1\) if vertex \(v\) has the label \(c\), and zero otherwise. Every vertex is then given the class \(\operatorname{arg\,max}_c F_{vc}\), with ties broken in favor of the class that first appears among the vertices, in the order of their indices. This takes time \(O(E)\) per iteration and class.References
[zhu-semi-supervised-2003]Xiaojin Zhu, Zoubin Ghahramani and John Lafferty, “Semi-supervised learning using Gaussian fields and harmonic functions”, Proceedings of the 20th International Conference on Machine Learning, 912-919 (2003), https://aaai.org/papers/icml03-118-semi-supervised-learning-using-gaussian-fields-and-harmonic-functions/
Examples
>>> g = gt.collection.data["karate"] >>> labels = g.new_vp("int", val=-1) >>> labels[0] = 0 >>> labels[33] = 1 >>> pred = gt.harmonic_function(g, labels) >>> print(pred.a) [0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1]
The predicted classes are shown in the figure below, which is obtained with
>>> colors = np.array(["#9bbedc", "#f28c8e"]) >>> fill = g.new_vp("string", vals=colors[pred.a]) >>> seed = g.new_vp("bool", vals=labels.a >= 0) >>> shape = g.new_vp("string", vals=np.where(seed.a, "square", "circle")) >>> gt.graph_draw(g, pos=g.vp.pos, vertex_fill_color=fill, ... vertex_shape=shape, vertex_halo=seed, ... vertex_halo_color="#555555", vertex_halo_size=1.4, ... output="harmonic-function-karate.pdf") <...>
Classes of the vertices of the karate club network predicted with the harmonic function from the labels of vertices 0 and 33 (squares with a dark halo). The vertex colors indicate the classes.#