Scikit-network-07:层次聚类Hierarchy

Hierarchy|层次聚类

Paris

介绍基于Paris算法的图层次聚类, Paris是基于linkage算聚类距离的。

from IPython.display import SVG
import numpy as np
from sknetwork.data import karate_club, painters, movie_actor

from sknetwork.hierarchy import Paris, cut_straight, dasgupta_score, tree_sampling_divergence
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph, svg_dendrogram

graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position

paris = Paris()
dendrogram = paris.fit_transform(adjacency)  # 谱系结构

image = svg_dendrogram(dendrogram)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第1张图片

# cuts of the dendrogram
labels = cut_straight(dendrogram)
print(labels)

# [1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0]
n_clusters = 4
labels, dendrogram_aggregate = cut_straight(dendrogram, n_clusters, return_dendrogram=True)
print(labels)

# [0 0 0 0 3 3 3 0 1 0 3 0 0 0 1 1 3 0 1 0 1 0 1 2 2 2 2 2 2 2 1 2 1 1]

_, counts = np.unique(labels, return_counts=True)
print(_, counts)

[0 1 2 3] [12  9  8  5]

# aggregate dendrogram
image = svg_dendrogram(dendrogram_aggregate, names=counts, rotate_names=False)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第2张图片

# corresponding clustering

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

Scikit-network-07:层次聚类Hierarchy_第3张图片

有向图

graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names

# hierarchical clustering
paris = Paris()
dendrogram = paris.fit_transform(adjacency)

image = svg_dendrogram(dendrogram, names, n_clusters=3, rotate=True)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第4张图片

# cut with 3 clusters

labels = cut_straight(dendrogram, n_clusters = 3)
print(labels)
# [0 0 1 0 1 1 2 0 0 1 0 0 0 2]

image = svg_digraph(adjacency, position, names=names, labels=labels)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第5张图片

# metrics

dasgupta_score(adjacency, dendrogram)
# 0.5842857142857143

二部图

graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col

# hierarchical clustering
paris = Paris()
paris.fit(biadjacency)
dendrogram_row = paris.dendrogram_row_
dendrogram_col = paris.dendrogram_col_
dendrogram_full = paris.dendrogram_full_

image = svg_dendrogram(dendrogram_row, names_row, n_clusters=4, rotate=True)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第6张图片

image = svg_dendrogram(dendrogram_col, names_col, n_clusters=4, rotate=True)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第7张图片

# cuts
labels = cut_straight(dendrogram_full, n_clusters=4)
n_row = biadjacency.shape[0]
labels_row = labels[:n_row]
labels_col = labels[n_row:]

image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第8张图片


Ward

介绍基于Ward算法的图层次聚类, Ward是基于离差平方和ESS算聚类距离的。

from IPython.display import SVG
import numpy as np
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.embedding import Spectral
from sknetwork.hierarchy import Ward, cut_straight, dasgupta_score, tree_sampling_divergence
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph, svg_dendrogram

graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position

# hierachical clustering
ward = Ward()
dendrogram = ward.fit_transform(adjacency)
image = svg_dendrogram(dendrogram)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第9张图片

# cuts
labels = cut_straight(dendrogram)
print(labels)
# [0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1]

n_cluster = 4
labels, dendrogram_aggregate = cut_straight(dendrogram, n_cluster, return_dendrogram=True)
print(labels)
# [0 2 0 3 0 0 0 3 0 0 0 0 3 3 1 1 0 2 1 2 1 2 1 0 0 0 0 0 0 0 0 0 1 1]

_, counts = np.unique(labels, return_counts=True)

# aggragate dendrogram
image = svg_dendrogram(dendrogram_aggregate, names=counts, rotate_names=False)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第10张图片

# clustering
image = svg_graph(adjacency, position, labels=labels)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第11张图片

# metrics
dasgupta_score(adjacency, dendrogram)
# 0.5082956259426847

# other embedding
ward = Ward(embedding_method=Spectral(4))

有向图

graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names

# hierarchical clustering
ward = Ward()
dendrogram = ward.fit_transform(adjacency)

image = svg_dendrogram(dendrogram, names, n_clusters=3, rotate=True)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第12张图片

# cut with 3 clusters
labels = cut_straight(dendrogram, n_clusters=3)
print(labels)
# [0 0 1 1 0 2 0 0 0 2 0 1 0 0]

image = svg_digraph(adjacency, position, names=names, labels=labels)
SVG(image)

# metrics
dasgupta_score(adjacency, dendrogram)
# 0.31285714285714294

Scikit-network-07:层次聚类Hierarchy_第13张图片

二部图

graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col

# hierarchical clustering
ward = Ward(co_cluster=True)
ward.fit(biadjacency)

dendrogram_row = ward.dendrogram_row_
dendrogram_col = ward.dendrogram_col_
dendrogram_full = ward.dendrogram_full_

image = svg_dendrogram(dendrogram_row, names_row, n_clusters=4, rotate=True)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第14张图片

image = svg_dendrogram(dendrogram_col, names_col, n_clusters=4, rotate=True)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第15张图片

# cuts
labels = cut_straight(dendrogram_full, n_clusters = 4)
n_row = biadjacency.shape[0]
labels_row = labels[:n_row]
labels_col = labels[n_row:]

image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第16张图片


Louvain

介绍基于Louvain算法的图层次聚类, Louvain是基于模块度算聚类距离的。

from IPython.display import SVG
import numpy as np
from sknetwork.data import karate_club, painters, movie_actor
from sknetwork.hierarchy import LouvainHierarchy
from sknetwork.hierarchy import cut_straight, dasgupta_score, tree_sampling_divergence
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph, svg_dendrogram

graph = karate_club(metadata=True)
adjacency = graph.adjacency
position = graph.position

# hierarchical clustering
louvain_hierarchy = LouvainHierarchy()
dendrogram = louvain_hierarchy.fit_transform(adjacency)

image = svg_dendrogram(dendrogram)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第17张图片

# cuts
labels = cut_straight(dendrogram)
print(labels)
# [0 0 0 0 3 3 3 0 1 0 3 0 0 0 1 1 3 0 1 0 1 0 1 2 2 2 1 2 2 1 1 2 1 1]

labels, dendrogram_aggregate = cut_straight(dendrogram, n_clusters=4, return_dendrogram=True)
print(labels)
# [0 0 0 0 3 3 3 0 1 0 3 0 0 0 1 1 3 0 1 0 1 0 1 2 2 2 1 2 2 1 1 2 1 1]

_, counts = np.unique(labels, return_counts=True)
image = svg_dendrogram(dendrogram_aggregate, names=counts, rotate_names=False)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第18张图片

image = svg_graph(adjacency, position, labels=labels)
SVG(image)
# metrics
dasgupta_score(adjacency, dendrogram)
# 0.6293363499245852

Scikit-network-07:层次聚类Hierarchy_第19张图片

有向图

graph = painters(metadata=True)
adjacency = graph.adjacency
position = graph.position
names = graph.names

# hierarchical clustering
louvain_hierarchy = LouvainHierarchy()
dendrogram = louvain_hierarchy.fit_transform(adjacency)

image = svg_dendrogram(dendrogram, names, rotate=True)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第20张图片

# cut with 3 clusters
labels = cut_straight(dendrogram, n_clusters=3)
print(labels)
# [1 0 2 0 2 2 1 0 1 2 1 0 0 1]

image = svg_digraph(adjacency, position, names=names, labels=labels)
SVG(image)

# metrics
dasgupta_score(adjacency, dendrogram)
# 0.53

Scikit-network-07:层次聚类Hierarchy_第21张图片

二部图

graph = movie_actor(metadata=True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col

# hierarchical clustering
louvain_hierarchy = LouvainHierarchy()
louvain_hierarchy.fit(biadjacency)
dendrogram_row = louvain_hierarchy.dendrogram_row_
dendrogram_col = louvain_hierarchy.dendrogram_col_
dendrogram_full = louvain_hierarchy.dendrogram_full_

image = svg_dendrogram(dendrogram_row, names_row, n_clusters=4, rotate=True)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第22张图片

image = svg_dendrogram(dendrogram_col, names_col, n_clusters=4, rotate=True)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第23张图片

# cuts
labels = cut_straight(dendrogram_full, n_clusters=4)
n_row = biadjacency.shape[0]
labels_row = labels[:n_row]
labels_col = labels[n_row:]

image = svg_bigraph(biadjacency, names_row, names_col, labels_row, labels_col)
SVG(image)

Scikit-network-07:层次聚类Hierarchy_第24张图片

参考

  • https://scikit-network.readthedocs.io/en/latest/tutorials/hierarchy/louvain_recursion.html

你可能感兴趣的:(#,Scikit-network,scikit-network,python,层次聚类,hierarchy)