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, BiPropagationClustering, 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

Clustering

[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)
[8]:
SVG(image)
[8]:
../../_images/tutorials_clustering_propagation_11_0.svg

Metrics

[9]:
modularity(adjacency, labels)
[9]:
0.35231755424063116

Aggregate graph

[10]:
adjacency_aggregate = propagation.adjacency_
[11]:
average = normalize(membership_matrix(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
[12]:
image = svg_graph(adjacency_aggregate, position_aggregate, counts, labels=labels_unique,
                  display_node_weight=True, node_weights=counts, scale=0.5)
[13]:
SVG(image)
[13]:
../../_images/tutorials_clustering_propagation_18_0.svg

Soft clustering

[14]:
scores = propagation.membership_[:,1].toarray().ravel()
[15]:
image = svg_graph(adjacency, position, scores=scores)
[16]:
SVG(image)
[16]:
../../_images/tutorials_clustering_propagation_22_0.svg

Digraphs

[17]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position

Clustering

[18]:
propagation = PropagationClustering()
labels = propagation.fit_transform(adjacency)
[19]:
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
[0 1] [10  4]
[20]:
image = svg_digraph(adjacency, position, names, labels)
[21]:
SVG(image)
[21]:
../../_images/tutorials_clustering_propagation_29_0.svg

Metrics

[22]:
modularity(adjacency, labels)
[22]:
0.256

Aggregate graph

[23]:
adjacency_aggregate = propagation.adjacency_
[24]:
average = normalize(membership_matrix(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
[25]:
image = svg_digraph(adjacency_aggregate, position_aggregate, counts, labels=labels_unique,
                    display_node_weight=True, node_weights=counts, scale=0.5)
[26]:
SVG(image)
[26]:
../../_images/tutorials_clustering_propagation_36_0.svg

Soft clustering

[27]:
scores = propagation.membership_[:,0].toarray().ravel()
[28]:
image = svg_graph(adjacency, position, scores=scores)
[29]:
SVG(image)
[29]:
../../_images/tutorials_clustering_propagation_40_0.svg

Bigraphs

[30]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col

Clustering

[31]:
bipropagation = BiPropagationClustering()
bipropagation.fit(biadjacency)
labels_row = bipropagation.labels_row_
labels_col = bipropagation.labels_col_
[32]:
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
[33]:
SVG(image)
[33]:
../../_images/tutorials_clustering_propagation_46_0.svg

Metrics

[34]:
bimodularity(biadjacency, labels_row, labels_col)
[34]:
0.41496598639455773

Aggregate graph

[35]:
biadjacency_aggregate = bipropagation.biadjacency_
[36]:
labels_unique_row, counts_row = np.unique(labels_row, return_counts=True)
labels_unique_col, counts_col = np.unique(labels_col, return_counts=True)
[37]:
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,
                    scale=0.5)
[38]:
SVG(image)
[38]:
../../_images/tutorials_clustering_propagation_53_0.svg

Soft clustering

[39]:
scores_row = bipropagation.membership_row_[:,1].toarray().ravel()
scores_col = bipropagation.membership_col_[:,1].toarray().ravel()
[40]:
image = svg_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col)
[41]:
SVG(image)
[41]:
../../_images/tutorials_clustering_propagation_57_0.svg