harmonic_function#

harmonic_function(G, max_iter=30, label_name='label')[source]#

Node classification by Harmonic function

Function for computing Harmonic function algorithm by Zhu et al.

Parameters:
GNetworkX Graph
max_iterint

maximum number of iterations allowed

label_namestring

name of target labels to predict

Returns:
predictedlist

List of length len(G) with the predicted labels for each node.

Raises:
NetworkXError

If no nodes in G have attribute label_name.

References

Zhu, X., Ghahramani, Z., & Lafferty, J. (2003, August). Semi-supervised learning using gaussian fields and harmonic functions. In ICML (Vol. 3, pp. 912-919).

Examples

>>> from networkx.algorithms import node_classification
>>> G = nx.path_graph(4)
>>> G.nodes[0]["label"] = "A"
>>> G.nodes[3]["label"] = "B"
>>> G.nodes(data=True)
NodeDataView({0: {'label': 'A'}, 1: {}, 2: {}, 3: {'label': 'B'}})
>>> G.edges()
EdgeView([(0, 1), (1, 2), (2, 3)])
>>> predicted = node_classification.harmonic_function(G)
>>> predicted
['A', 'A', 'B', 'B']