KCenters

This notebook illustrates graph clustering with the KCenters algorithm.

[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 get_modularity
from sknetwork.clustering import KCenters
from sknetwork.visualization import svg_graph, svg_bigraph

Graphs

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
[5]:
kcenters = KCenters(n_clusters=2)
kcenters.fit(adjacency)
labels = kcenters.fit_predict(adjacency)
[6]:
# cluster centers
kcenters.centers_
[6]:
array([ 1, 30])
[7]:
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
[0 1] [16 18]
[8]:
image = svg_graph(adjacency, position, labels=labels)
SVG(image)
[8]:
../../_images/tutorials_clustering_kcenters_10_0.svg
[9]:
# metric
get_modularity(adjacency, labels)
[9]:
np.float64(0.37146614069690986)

Directed graphs

[10]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
[11]:
# clustering
kcenters = KCenters(n_clusters=3, directed=True)
labels = kcenters.fit_predict(adjacency)
[12]:
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
[0 1 2] [4 7 3]
[13]:
# cluster centers
names[kcenters.centers_]
[13]:
array(['Michel Angelo', 'Paul Cezanne', 'Vincent van Gogh'], dtype='<U21')
[14]:
image = svg_graph(adjacency, position, names=names, labels=labels)
SVG(image)
[14]:
../../_images/tutorials_clustering_kcenters_17_0.svg
[15]:
get_modularity(adjacency, labels)
[15]:
np.float64(0.30200000000000005)

Bipartite graphs

[16]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[17]:
# clustering
kcenters = KCenters(n_clusters=3, center_position="both")
kcenters.fit(biadjacency)
labels_row = kcenters.labels_row_
labels_col = kcenters.labels_col_
[18]:
# cluster centers
centers_row = kcenters.centers_row_
centers_col = kcenters.centers_col_

print("centers row:", names_row[centers_row])
print("centers col:", names_col[centers_col])
centers row: ['The Dark Knight Rises']
centers col: ['Johnny Depp' 'Ralph Fiennes']
[19]:
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)
[19]:
../../_images/tutorials_clustering_kcenters_23_0.svg
[20]:
# metric
get_modularity(biadjacency, labels_row, labels_col)
[20]:
np.float64(0.43320713899405094)