Scikit-network-13:Visualization

Visualization

Graph

使用SVG可视化图

from IPython.display import SVG
import numpy as np
from sknetwork.data import karate_club, painters, movie_actor, load_netset
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph

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

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

Scikit-network-13:Visualization_第1张图片

# export
image = svg_graph(adjacency, position, labels=labels, filename='karate_club')

# adding names
image = svg_graph(adjacency, position, names=np.arange(34), name_position='below', labels=labels)
SVG(image)

Scikit-network-13:Visualization_第2张图片

# node size
image = svg_graph(
    adjacency, position, labels=labels, display_node_weight=True)
SVG(image)

Scikit-network-13:Visualization_第3张图片

# scores (here = degrees) 度数
degrees = adjacency.dot(np.ones(adjacency.shape[0]))
image = svg_graph(adjacency, position, scores=degrees)
SVG(image)

Scikit-network-13:Visualization_第4张图片

# seeds (here 2 nodes of highest degrees)
seeds = list(np.argsort(-degrees)[:2])
image = svg_graph(adjacency, position, labels=labels, seeds=seeds)
SVG(image)

Scikit-network-13:Visualization_第5张图片

weights = adjacency.dot(np.ones(adjacency.shape[0]))
image = svg_graph(adjacency, position, scores=np.log(weights), node_order=np.argsort(weights),
                  node_size_min=2, node_size_max=10, height=400, width=800,
                  display_node_weight=True, display_edges=False)
SVG(image)

Scikit-network-13:Visualization_第6张图片

有向图

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

image = svg_digraph(adjacency, position, names, name_position='above')
SVG(image)

Scikit-network-13:Visualization_第7张图片

二部图

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

# default layout
image = svg_bigraph(biadjacency, names_row, names_col, color_row='blue', color_col='red')
SVG(image)

Scikit-network-13:Visualization_第8张图片

# keep original order of rows and columns
image = svg_bigraph(biadjacency, names_row, names_col=names_col, color_row='blue', color_col='red',
                    reorder=False)
SVG(image)

Scikit-network-13:Visualization_第9张图片

Path

路径的可视化

from IPython.display import SVG
from sknetwork.data import house, cyclic_digraph, star_wars
from sknetwork.visualization import svg_graph, svg_digraph, svg_bigraph

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

path = [(0, 1), (1, 2), (2, 3)]
edge_labels = [(*edge, 0) for edge in path]
edge_labels

# [(0, 1, 0), (1, 2, 0), (2, 3, 0)]

image = svg_graph(adjacency, position, edge_width=3, edge_labels=edge_labels)
SVG(image)

Scikit-network-13:Visualization_第10张图片

image = svg_graph(None, position, edge_width=3, edge_labels=edge_labels)
SVG(image)

Scikit-network-13:Visualization_第11张图片

有向图

graph = cyclic_digraph(10, metadata=True)
adjacency = graph.adjacency
position = graph.position

paths = [[(0, 1), (1, 2), (2, 3)], [(6, 7), (7, 8)]]
edge_labels = [(*edge, label) for label, path in enumerate(paths) for edge in path]
edge_labels

# [(0, 1, 0), (1, 2, 0), (2, 3, 0), (6, 7, 1), (7, 8, 1)]
image = svg_digraph(adjacency, position, width=200, height=None, edge_width=3, edge_labels=edge_labels)
SVG(image)

Scikit-network-13:Visualization_第12张图片

二部图

graph = star_wars(True)
biadjacency = graph.biadjacency
names_row = graph.names_row
names_col = graph.names_col3

path = [(0, 1), (2, 1)]
edge_labels = [(*edge, 0) for edge in path]

image = svg_bigraph(biadjacency, names_row=names_row, names_col=names_col, edge_width=3, edge_labels=edge_labels)
SVG(image)

Scikit-network-13:Visualization_第13张图片

Dendrograms

树状图可视化

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

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

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

Scikit-network-13:Visualization_第14张图片

# hierarchical clustering, 层次聚类

paris = Paris()
dendrogram = paris.fit_transform(adjacency)
dendrogram

# visualization
image = svg_dendrogram(dendrogram)
SVG(image)

Scikit-network-13:Visualization_第15张图片

# add names, set colors
n = adjacency.shape[0]
image = svg_dendrogram(dendrogram, names=np.arange(n), n_clusters=5, color='gray')
SVG(image)

Scikit-network-13:Visualization_第16张图片

# export
svg_dendrogram(dendrogram, filename='dendrogram_karate_club')  # save to local
SVG('./dendrogram_karate_club.svg')

Scikit-network-13:Visualization_第17张图片

有向图

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

# graph
image = svg_digraph(adjacency, position, names)
SVG(image)

Scikit-network-13:Visualization_第18张图片

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

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

Scikit-network-13:Visualization_第19张图片

二部图

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

# graph
image = svg_bigraph(biadjacency, names_row, names_col)
SVG(image)

Scikit-network-13:Visualization_第20张图片

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

# visualization
image = svg_dendrogram(dendrogram_row, names_row, n_clusters=3, rotate=True)
SVG(image)

Scikit-network-13:Visualization_第21张图片

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

Scikit-network-13:Visualization_第22张图片

Pie-chart nodes

饼图可视化

from IPython.display import SVG
from scipy import sparse

from sknetwork.data import bow_tie, karate_club, painters
from sknetwork.visualization import svg_graph, svg_digraph
from sknetwork.clustering import Louvain

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

# probabilities
probs = [.5, 0, 0, 1, 1]
membership = sparse.csr_matrix([[p, 1-p] for p in probs])

image = svg_graph(adjacency, position, membership=membership, node_size=10)
SVG(image)

Scikit-network-13:Visualization_第23张图片

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

# soft clustering
louvain = Louvain()
louvain.fit(adjacency)
membership = louvain.membership_
image = svg_graph(adjacency, position, membership=membership)
SVG(image)

Scikit-network-13:Visualization_第24张图片

有向图

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

# soft clustering
louvain = Louvain()
louvain.fit(adjacency)
membership = louvain.membership_
image = svg_digraph(adjacency, names=names, position=positions, membership=membership, node_size=10)
SVG(image)

Scikit-network-13:Visualization_第25张图片

你可能感兴趣的:(#,Scikit-network,python,numpy,机器学习)