KCenters

此笔记本演示了使用 KCenters 算法进行图聚类。

[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

[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([19, 15])
[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]:
0.37146614069690986

有向图

[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] [5 5 4]
[13]:
# cluster centers
names[kcenters.centers_]
[13]:
array(['Pablo Picasso', 'Edouard Manet', 'Leonardo da Vinci'],
      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]:
0.32480000000000003

二部图

[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: ["Pirates of the Caribbean: At World's End" 'La La Land']
centers col: ['Marion Cotillard']
[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]:
0.3861546782044349