Propagation

This notebook illustrates the clustering of a graph by label propagation.

[1]:
from IPython.display import SVG
[2]:
import numpy as np
[3]:
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.clustering import PropagationClustering, modularity, bimodularity
from sknetwork.linalg import normalize
from sknetwork.utils import bipartite2undirected, membership_matrix
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
[5]:
propagation = PropagationClustering()
labels = propagation.fit_transform(adjacency)
[6]:
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
[0 1] [19 15]
[7]:
image = svg_graph(adjacency, position, labels=labels)
SVG(image)
[7]:
../../_images/tutorials_clustering_propagation_9_0.svg
[8]:
# metric
modularity(adjacency, labels)
[8]:
0.35231755424063116
[9]:
# aggregate graph
adjacency_aggregate = propagation.aggregate_
[10]:
average = normalize(membership_matrix(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
[11]:
image = svg_graph(adjacency_aggregate, position_aggregate, counts, labels=labels_unique,
                  display_node_weight=True, node_weights=counts)
SVG(image)
[11]:
../../_images/tutorials_clustering_propagation_13_0.svg
[12]:
# soft clustering
scores = propagation.membership_[:,1].toarray().ravel()
[13]:
image = svg_graph(adjacency, position, scores=scores)
SVG(image)
[13]:
../../_images/tutorials_clustering_propagation_15_0.svg

Directed graphs

[14]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
[15]:
propagation = PropagationClustering()
labels = propagation.fit_transform(adjacency)
[16]:
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
[0 1] [10  4]
[17]:
image = svg_digraph(adjacency, position, names=names, labels=labels)
SVG(image)
[17]:
../../_images/tutorials_clustering_propagation_20_0.svg
[18]:
modularity(adjacency, labels)
[18]:
0.256
[19]:
# aggregate graph
adjacency_aggregate = propagation.aggregate_
[20]:
average = normalize(membership_matrix(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
[21]:
image = svg_digraph(adjacency_aggregate, position_aggregate, counts, labels=labels_unique,
                    display_node_weight=True, node_weights=counts)
SVG(image)
[21]:
../../_images/tutorials_clustering_propagation_24_0.svg
[22]:
# soft clustering
scores = propagation.membership_[:,0].toarray().ravel()
[23]:
image = svg_graph(adjacency, position, scores=scores)
SVG(image)
[23]:
../../_images/tutorials_clustering_propagation_26_0.svg

Bipartite graphs

[24]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[25]:
propagation = PropagationClustering()
propagation.fit(biadjacency)
labels_row = propagation.labels_row_
labels_col = propagation.labels_col_
[26]:
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)
[26]:
../../_images/tutorials_clustering_propagation_30_0.svg
[27]:
bimodularity(biadjacency, labels_row, labels_col)
[27]:
0.41496598639455773
[28]:
# aggregate graph
biadjacency_aggregate = propagation.aggregate_
[29]:
labels_unique_row, counts_row = np.unique(labels_row, return_counts=True)
labels_unique_col, counts_col = np.unique(labels_col, return_counts=True)
[30]:
image = svg_bigraph(biadjacency_aggregate, counts_row, counts_col, labels_unique_row, labels_unique_col,
                    display_node_weight=True, node_weights_row=counts_row, node_weights_col=counts_col)
SVG(image)
[30]:
../../_images/tutorials_clustering_propagation_34_0.svg
[31]:
# soft clustering
scores_row = propagation.membership_row_[:,1].toarray().ravel()
scores_col = propagation.membership_col_[:,1].toarray().ravel()
[32]:
image = svg_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col)
SVG(image)
[32]:
../../_images/tutorials_clustering_propagation_36_0.svg