K-means

This notebook illustrates the clustering of a graph by k-means. This clustering involves the embedding of the graph in a space of low dimension.

[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 KMeans, BiKMeans, modularity, bimodularity
from sknetwork.linalg import normalize
from sknetwork.embedding import GSVD
from sknetwork.utils import 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]:
kmeans = KMeans(n_clusters = 2, embedding_method=GSVD(3))
labels = kmeans.fit_transform(adjacency)
[6]:
unique_labels, counts = np.unique(labels, return_counts=True)
print(unique_labels, counts)
[0 1] [20 14]
[7]:
image = svg_graph(adjacency, position, labels=labels)
[8]:
SVG(image)
[8]:
../../_images/tutorials_clustering_kmeans_11_0.svg

Metrics

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

Aggregate graph

[10]:
adjacency_aggregate = kmeans.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_kmeans_18_0.svg

Soft clustering

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

Digraphs

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

Clustering

[18]:
kmeans = KMeans(3, GSVD(3))
labels = kmeans.fit_transform(adjacency)
[19]:
image = svg_digraph(adjacency, position, names=names, labels=labels)
[20]:
SVG(image)
[20]:
../../_images/tutorials_clustering_kmeans_28_0.svg

Metrics

[21]:
modularity(adjacency, labels)
[21]:
0.21639999999999993

Aggregate graph

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

Soft clustering

[26]:
scores = kmeans.membership_[:,1].toarray().ravel()
[27]:
image = svg_digraph(adjacency, position, scores=scores)
[28]:
SVG(image)
[28]:
../../_images/tutorials_clustering_kmeans_39_0.svg

Bigraphs

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

Clustering

[30]:
bikmeans = BiKMeans(3, GSVD(3), co_cluster=True)
bikmeans.fit(biadjacency)
labels_row = bikmeans.labels_row_
labels_col = bikmeans.labels_col_
[31]:
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
[32]:
SVG(image)
[32]:
../../_images/tutorials_clustering_kmeans_45_0.svg

Metrics

[33]:
bimodularity(biadjacency, labels_row, labels_col)
[33]:
0.4943310657596373

Aggregate graph

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

Soft clustering

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