神经网络图形绘制

在python机器学习中会遇到各种各样的神经网络图形,以下为例子:

"""======================

Betweenness Centrality

======================

Betweenness centrality measures of positive gene functional associations

using WormNet v.3-GS.

Data from: https://www.inetbio.org/wormnet/downloadnetwork.php

"""

from random import sample

import networkx as nx

import matplotlib.pyplot as plt

Gold standard data of positive gene functional associations

from https://www.inetbio.org/wormnet/downloadnetwork.php

G = nx.read_edgelist("D:/OneDrive - stu.fynu.edu.cn/Gephi/WormNet.v3.benchmark.txt")

remove randomly selected nodes (to make example fast)

num_to_remove = int(len(G) / 1.5)

nodes = sample(list(G.nodes), num_to_remove)

G.remove_nodes_from(nodes)

remove low-degree nodes

low_degree = n for n, d in G.degree() if d \< 10

G.remove_nodes_from(low_degree)

largest connected component

components = nx.connected_components(G)

largest_component = max(components, key=len)

H = G.subgraph(largest_component)

compute centrality

centrality = nx.betweenness_centrality(H, k=10, endpoints=True)

compute community structure

lpc = nx.community.label_propagation_communities(H)

community_index = {n: i for i, com in enumerate(lpc) for n in com}

draw graph

fig, ax = plt.subplots(figsize=(20, 15))

pos = nx.spring_layout(H, k=0.15, seed=4572321)

node_color = community_index\[n for n in H]

node_size = v \* 20000 for v in centrality.values()

nx.draw_networkx(

H,

pos=pos,

with_labels=False,

node_color=node_color,

node_size=node_size,

edge_color="gainsboro",

alpha=0.4,

)

Title/legend

font = {"color": "k", "fontweight": "bold", "fontsize": 20}

ax.set_title("Gene functional association network (C. elegans)", font)

Change font color for legend

font"color" = "r"

ax.text(

0.80,

0.10,

"",

#"node color = community structure",

horizontalalignment="center",

transform=ax.transAxes,

fontdict=font,

)

ax.text(

0.80,

0.06,

"",

#"node size = betweenness centrality",

horizontalalignment="center",

transform=ax.transAxes,

fontdict=font,

)

Resize figure for label readability

ax.margins(0.1, 0.05)

fig.tight_layout()

plt.axis("off")

plt.savefig("D:/OneDrive - stu.fynu.edu.cn/Gephi/WormNet.v3.benchmark.png")

plt.show()

这样绘制出图形如下:

相关推荐
尘世中一位迷途小书童4 分钟前
我为什么在 Windows 上做了一个翻译软件:SnapLingo
前端·人工智能·机器学习
别动我齐刘海10 分钟前
ROS2 Jazzy + C++ 实战路线——基础学习1
c++·vscode·python·ubuntu·机器学习·自动驾驶·github
wuyk5551 小时前
3.ROS2 Humble 命令行操作详解【VMware Ubuntu22.04 实操】
物联网·机器学习·机器人
龙腾AI白云15 小时前
孪生不止在工厂:能源、医疗与农业
人工智能·机器学习·scikit-learn·知识图谱
YFJ_mily15 小时前
9.24早鸟通道即将关闭|IMRA2026智能制造机器人自动化IEEE会议|往届EI稳定检索,设有学生专属投稿权益
人工智能·机器学习·机器人·自动化·智能制造·rdlink研发家·马鞍山会议
renhongxia115 小时前
数字孪生不止在工厂:能源、医疗与农业
人工智能·深度学习·算法·机器学习·数字孪生
猿人谷17 小时前
BRP,如何用“反向构造”让黑盒对抗 Patch 更省查询?
神经网络·机器学习·图像识别
Rocky Ding*18 小时前
一文读懂Hallo数字人核心基础知识
论文阅读·人工智能·深度学习·机器学习·aigc·数字人·ai-native
Deep_Kevin20 小时前
RL-VLM-F: Reinforcement Learning from Vision Language Foundation Model Feedback
机器学习
qq_2147826120 小时前
再见Visio,再见draw.io,又一个画图Skill
人工智能·机器学习·draw.io