Scikit-network-06:聚类

聚类

Louvain

下面说明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='<U28'),
 array(['Leonardo DiCaprio', 'Marion Cotillard', 'Joseph Gordon Lewitt',
        'Christian Bale', 'Ryan Gosling', 'Brad Pitt', 'Carey Mulligan',
        'Emma Stone', 'Steve Carell', 'Lea Seydoux', 'Ralph Fiennes',
        'Jude Law', 'Willem Dafoe', 'Christophe Waltz', 'Johnny Depp',
        'Owen Wilson'], dtype='<U20'))
# 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)

在这里插入图片描述


Propagation|标签传播

介绍标签传播的图聚类。

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

介绍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)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uncle_ll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值