传播

此笔记本展示了使用标签传播对图进行聚类。

[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, get_modularity
from sknetwork.linalg import normalize
from sknetwork.utils import get_membership
from sknetwork.visualization import visualize_graph, visualize_bigraph

[4]:
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
[5]:
propagation = PropagationClustering()
labels = propagation.fit_predict(adjacency)
[6]:
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
[0 1] [19 15]
[7]:
image = visualize_graph(adjacency, position, labels=labels)
SVG(image)
[7]:
../../_images/tutorials_clustering_propagation_9_0.svg
[8]:
# metric
get_modularity(adjacency, labels)
[8]:
0.35231755424063116
[9]:
# aggregate graph
adjacency_aggregate = propagation.aggregate_
[10]:
average = normalize(get_membership(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
[11]:
image = visualize_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]:
# probability distribution over clusters
probs = propagation.predict_proba()
[13]:
# scores for cluster 1
scores = probs[:,1]
[14]:
image = visualize_graph(adjacency, position, scores=scores)
SVG(image)
[14]:
../../_images/tutorials_clustering_propagation_16_0.svg

有向图

[15]:
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
[16]:
propagation = PropagationClustering()
labels = propagation.fit_predict(adjacency)
[17]:
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
[0 1] [10  4]
[18]:
image = visualize_graph(adjacency, position, names=names, labels=labels)
SVG(image)
[18]:
../../_images/tutorials_clustering_propagation_21_0.svg
[19]:
get_modularity(adjacency, labels)
[19]:
0.256
[20]:
# aggregate graph
adjacency_aggregate = propagation.aggregate_
[21]:
average = normalize(get_membership(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
[22]:
image = visualize_graph(adjacency_aggregate, position_aggregate, counts, labels=labels_unique,
                    display_node_weight=True, node_weights=counts)
SVG(image)
[22]:
../../_images/tutorials_clustering_propagation_25_0.svg
[23]:
# probability distribution over clusters
probs = propagation.predict_proba()
[24]:
# scores for cluster 1
scores = probs[:,1]
[25]:
image = visualize_graph(adjacency, position, scores=scores)
SVG(image)
[25]:
../../_images/tutorials_clustering_propagation_28_0.svg

二部图

[26]:
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
[27]:
propagation = PropagationClustering()
propagation.fit(biadjacency)
labels_row = propagation.labels_row_
labels_col = propagation.labels_col_
[28]:
image = visualize_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)
[28]:
../../_images/tutorials_clustering_propagation_32_0.svg
[29]:
get_modularity(biadjacency, labels_row, labels_col)
[29]:
0.0
[30]:
# aggregate graph
biadjacency_aggregate = propagation.aggregate_
[31]:
labels_unique_row, counts_row = np.unique(labels_row, return_counts=True)
labels_unique_col, counts_col = np.unique(labels_col, return_counts=True)
[32]:
image = visualize_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)
[32]:
../../_images/tutorials_clustering_propagation_36_0.svg
[33]:
# probability distribution over clusters
probs_row = propagation.predict_proba()
probs_col = propagation.predict_proba(columns=True)