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]

Scikit-network-06:聚类_第1张图片

# 衡量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)

Scikit-network-06:聚类_第2张图片

# soft clustering (here probability of label 1), 对标签1里面进一步的进行聚类
scores = louvain.membership_[:,1].toarray().ravel()
image = svg_graph(adjacency, position, scores=scores)
SVG(image)

Scikit-network-06:聚类_第3张图片

有向图

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]

Scikit-network-06:聚类_第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)

Scikit-network-06:聚类_第5张图片

# soft clustering
scores = louvain.membership_[:,1].toarray().ravel()

image = svg_graph(adjacency, position, scores=scores)
SVG(image)

Scikit-network-06:聚类_第6张图片

二部图

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)

Scikit-network-06:聚类_第7张图片

# 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)

Scikit-network-06:聚类_第8张图片

在标签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)

Scikit-network-06:聚类_第9张图片


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类,后面跟的是个数

Scikit-network-06:聚类_第10张图片

# 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)

Scikit-network-06:聚类_第11张图片

# soft clustering, 对标签1继续聚类
scores = propagation.membership_[:,1].toarray().ravel()

image = svg_graph(adjacency, position, scores=scores)
SVG(image)

Scikit-network-06:聚类_第12张图片

有向图

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)

Scikit-network-06:聚类_第13张图片

# 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)

Scikit-network-06:聚类_第14张图片

# soft clustering, 标签为0的内部聚类
scores = propagation.membership_[:,0].toarray().ravel()
image = svg_graph(adjacency, position, scores=scores)
SVG(image)

Scikit-network-06:聚类_第15张图片

二部图

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)

Scikit-network-06:聚类_第16张图片

# 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)

Scikit-network-06:聚类_第17张图片

# 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)

Scikit-network-06:聚类_第18张图片


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]

Scikit-network-06:聚类_第19张图片

# 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)

Scikit-network-06:聚类_第20张图片

# soft clustering (here probability of label 1)
scores = kmeans.membership_[:,1].toarray().ravel()

image = svg_graph(adjacency, position, scores=scores)
SVG(image)

Scikit-network-06:聚类_第21张图片

有向图

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)

Scikit-network-06:聚类_第22张图片

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)

Scikit-network-06:聚类_第23张图片

# soft clustering (probability of label 0)
scores = kmeans.membership_[:, 0].toarray().ravel()

image = svg_digraph(adjacency, position, scores=scores)
SVG(image)

Scikit-network-06:聚类_第24张图片

二部图

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)

Scikit-network-06:聚类_第25张图片

# 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)

Scikit-network-06:聚类_第26张图片

# 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)

Scikit-network-06:聚类_第27张图片

你可能感兴趣的:(#,Scikit-network,python,scikit-network,图,聚类,lpa)