下面说明Louvain算法的图聚类。
from IPython.display import SVG
import numpy as np
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.clustering import Louvain, get_modularity
from sknetwork.linalg import normalize
from sknetwork.utils import get_membership
from sknetwork.visualization import svg_graph, svg_bigraph
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
louvain = Louvain()
labels = louvain.fit_transform(adjacency)
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
image = svg_graph(adjacency, position, labels=labels)
SVG(image)
[0 1 2 3] [12 11 6 5]
# 衡量metric-模块度
modularity(adjacency, labels)
# 0.4188034188034188
# 团内聚合
adjacency_aggregate = louvain.aggregate_
average = normalize(membership_matrix(labels).T)
position_aggregate = average.dot(position)
image = svg_graph(
adjacency_aggregate, position_aggregate, counts,
labels=labels_uniq, display_edge_weight=True, node_weights=counts)
SVG(image)
# soft clustering (here probability of label 1), 对标签1里面进一步的进行聚类
scores = louvain.membership_[:,1].toarray().ravel()
image = svg_graph(adjacency, position, scores=scores)
SVG(image)
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
# clustering
louvain = Louvain()
labels = louvain.fit_transform(adjacency)
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
image = svg_digraph(adjacency, position, names=names, labels=labels)
SVG(image)
[0 1 2] [5 5 4]
# metric
modularity(adjacency, labels)
# aggregate graph
adjacency_aggregate = louvain.aggregate_
average = normalize(membership_matrix(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
image = svg_digraph(adjacency_aggregate, position_aggregate, counts, labels=labels_unique,
display_node_weight=True, node_weights=counts)
SVG(image)
# soft clustering
scores = louvain.membership_[:,1].toarray().ravel()
image = svg_graph(adjacency, position, scores=scores)
SVG(image)
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
names_row, names_col
(array(['Inception', 'The Dark Knight Rises', 'The Big Short', 'Drive',
'The Great Gatsby', 'La La Land', 'Crazy Stupid Love', 'Vice',
'The Grand Budapest Hotel', 'Aviator', '007 Spectre',
'Inglourious Basterds', 'Midnight In Paris',
'Murder on the Orient Express', 'Fantastic Beasts 2'], dtype='
# cluster
louvain = Louvain()
louvain.fit(biadjacency)
labels_row = louvain.labels_row_
labels_col = louvain.labels_col_
labels_row, labels_col
(array([3, 3, 1, 4, 4, 1, 1, 1, 0, 2, 0, 0, 0, 2, 2]),
array([2, 3, 3, 1, 1, 0, 4, 1, 1, 0, 0, 2, 2, 0, 2, 0]))
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)
# metric
bimodularity(biadjacency, labels_row, labels_col)
# 0.5742630385487529
# aggregate graph
biadjacency_aggregate = louvain.aggregate_
labels_unique_row, counts_row = np.unique(labels_row, return_counts=True)
labels_unique_col, counts_col = np.unique(labels_col, return_counts=True)
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)
SVG(image)
在标签1里进行软聚类
# soft cluster
scores_row = louvain.membership_row_[:,1].toarray().ravel()
scores_col = louvain.membership_col_[:,1].toarray().ravel()
image = svg_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col)
SVG(image)
介绍标签传播的图聚类。
from IPython.display import SVG
import numpy as np
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.clustering import PropagationClustering, modularity, bimodularity
from sknetwork.linalg import normalize
from sknetwork.utils import bipartite2undirected, membership_matrix
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
propagation = PropagationClustering()
labels = propagation.fit_transform(adjacency)
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
# [0 1] [19 15], 聚类为2类,后面跟的是个数
# metric, 模块度
modularity(adjacency, labels)
# 0.35231755424063116
# aggregate graph 压缩图
adjacency_aggregate = propagation.aggregate_
average = normalize(membership_matrix(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
image = svg_graph(
adjacency_aggregate, position_aggregate, counts,
labels=labels_unique, display_node_weight=True, node_weights=counts)
SVG(image)
# soft clustering, 对标签1继续聚类
scores = propagation.membership_[:,1].toarray().ravel()
image = svg_graph(adjacency, position, scores=scores)
SVG(image)
graph = painters(metadata=True)
adjacency = graph.adjacency
names = graph.names
position = graph.position
propagation = PropagationClustering()
labels = propagation.fit_transform(adjacency)
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts)
# [0 1] [10 4] 分两类,每类数量为10和4
image = svg_graph(adjacency, position, names=names, labels=labels)
SVG(image)
# metric
modularity(adjacency, labels)
# 0.256
# aggregate graph 聚合图
adjacency_aggregate = propagation.aggregate_
average = normalize(membership_matrix(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
image = svg_graph(
adjacency_aggregate, position_aggregate, counts, labels=labels_unique,
display_edge_weight=True, node_weights=counts)
SVG(image)
# soft clustering, 标签为0的内部聚类
scores = propagation.membership_[:,0].toarray().ravel()
image = svg_graph(adjacency, position, scores=scores)
SVG(image)
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
propagation = PropagationClustering()
propagation.fit_transform(biadjacency)
labels_row = propagation.labels_row_
labels_col = propagation.labels_col_
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)
# metric
bimodularity(biadjacency, labels_row, labels_col)
# 0.41496598639455773
# aggregate graph 聚合
biadjacency_aggregate = propagation.aggregate_
labels_unique_row, counts_row = np.unique(labels_row, return_counts=True)
labels_unique_col, counts_col = np.unique(labels_col, return_counts=True)
image = svg_bigraph(
biadjacency_aggregate, counts_row, counts_col, labels_unique_row,
labels_unique_col, display_edge_weight=True,
node_weights_row=counts_row, node_weights_col=counts_col)
SVG(image)
# soft clustering for label 1
scores_row = propagation.membership_row_[:, 1].toarray().ravel()
scores_col = propagation.membership_col_[:, 1].toarray().ravel()
image = svg_bigraph(
biadjacency, names_row, names_col,
scores_row=scores_row, scores_col=scores_col)
SVG(image)
介绍K-means的图聚类。该聚类涉及图在低维空间中的嵌入。
from IPython.display import SVG
import numpy as np
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.clustering import KMeans, 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
graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position
kmeans = KMeans(n_clusters=2, embedding_method=GSVD(3))
labels = kmeans.fit_transform(adjacency)
labels
labels_unique, counts = np.unique(labels, return_counts=True)
print(labels_unique, counts) # 结果可能会变化,因为最初中心点是随机选取的
image = svg_graph(adjacency, position, labels=labels)
SVG(image)
array([1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
[0 1] [20 14]
# metric
modularity(adjacency, labels)
# 0.34048323471400377
# aggregate graph
adjacency_aggregate = kmeans.aggregate_
average = normalize(membership_matrix(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
image = svg_graph(
adjacency_aggregate, position_aggregate, counts, labels=labels_unique,
display_edge_weight=True, node_weights=counts)
SVG(image)
# soft clustering (here probability of label 1)
scores = kmeans.membership_[:,1].toarray().ravel()
image = svg_graph(adjacency, position, scores=scores)
SVG(image)
graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names
kmeans = KMeans(3, GSVD(3), co_cluster=False)
labels = kmeans.fit_transform(adjacency)
image = svg_digraph(adjacency, position, names=names, labels=labels)
SVG(image)
modularity(adjacency, labels)
# 0.24
# aggragate graph
adjacency_aggregate = kmeans.aggregate_
average = normalize(membership_matrix(labels).T)
position_aggregate = average.dot(position)
labels_unique, counts = np.unique(labels, return_counts=True)
image = svg_digraph(
adjacency_aggregate, position_aggregate, counts, labels=labels_unique,
display_edge_weight=True, node_weights=counts)
SVG(image)
# soft clustering (probability of label 0)
scores = kmeans.membership_[:, 0].toarray().ravel()
image = svg_digraph(adjacency, position, scores=scores)
SVG(image)
graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col
kmeans = KMeans(3, GSVD(3), co_cluster=True)
kmeans.fit(biadjacency)
labels_row = kmeans.labels_row_
labels_col = kmeans.labels_col_
image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)
# metric
bimodularity(biadjacency, labels_row, labels_col)
# 0.4988662131519276
# aggregate graph
biadjacency_aggregate = kmeans.aggregate_
labels_unique_row, counts_row = np.unique(labels_row, return_counts=True)
labels_unique_col, counts_col = np.unique(labels_col, return_counts=True)
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)
SVG(image)
# soft clustering (here probability of label 1)
scores_row = kmeans.membership_row_[:,1].toarray().ravel()
scores_col = kmeans.membership_col_[:,1].toarray().ravel()
image = svg_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col)
SVG(image)