local_and_global_consistency#
- graph_tool.inference.local_and_global_consistency(g, labels, alpha=0.99, weight=None, max_iter=30)[git][source]#
Predict the labels of the unlabeled vertices with the local and global consistency method.
- Parameters:
- g
Graph Undirected graph.
- labels
VertexPropertyMap Integer labels of the vertices, with negative values for the unlabeled ones.
- alpha
float(optional, default:0.99) Fraction of the score that each vertex takes from its neighbors.
- weight
EdgePropertyMap(optional, default:None) Edge weights.
- max_iter
int(optional, default:30) Number of iterations.
- g
- Returns:
- pred
VertexPropertyMap The predicted label of every vertex (also for the labeled ones, which may change).
- pred
See also
harmonic_functionLabel prediction with the harmonic function
label_propagationLabel propagation heuristic
Notes
The method of Zhou et al. [zhou-learning-2004] iterates \(F \leftarrow \alpha SF + (1 - \alpha)B\)
max_itertimes, starting from \(F = 0\), where \(F_{vc}\) is the score of class \(c\) at vertex \(v\), \(\alpha\) isalpha, \(S = D^{-1/2} A D^{-1/2}\) is the symmetrically normalized adjacency matrix, with \(A\) the weighted adjacency matrix (with self-loops counted twice) and \(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. The time is \(O(E)\) per iteration and class.References
[zhou-learning-2004]Dengyong Zhou, Olivier Bousquet, Thomas N. Lal, Jason Weston and Bernhard Schölkopf, “Learning with local and global consistency”, Advances in Neural Information Processing Systems 16, 321-328 (2004)
Examples
>>> g = gt.collection.data["karate"] >>> labels = g.new_vp("int", val=-1) >>> labels[0] = 0 >>> labels[33] = 1 >>> pred = gt.local_and_global_consistency(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="local-and-global-consistency-karate.pdf") <...>
Classes of the vertices of the karate club network predicted with the local and global consistency method from the labels of vertices 0 and 33 (squares with a dark halo). The vertex colors indicate the classes.#