神经网络图形绘制

在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()

这样绘制出图形如下:

相关推荐
苦瓜汤补钙34 分钟前
论文阅读:WildGS-SLAM:Monocular Gaussian Splatting SLAM in Dynamic Environments
linux·论文阅读·机器学习
智算菩萨1 小时前
传统机器学习在信用卡交易预测中的卓越表现:从R²=-0.0075到1.0000的华丽转身
人工智能·机器学习·r语言
大连好光景2 小时前
L1正则化 VS L2正则化
人工智能·深度学习·机器学习
国家不保护废物2 小时前
深度学习
人工智能·深度学习·机器学习
今天炼丹了吗2 小时前
RTDETR融合[WACV 2025]SEM-Net中的模块
python·深度学习·机器学习
深度学习机器17 小时前
OCRFlux-3B:开源 OCR + LLM 模型的新标杆,支持跨页表格合并
人工智能·机器学习·语言模型·ocr
大千AI助手18 小时前
TinyBERT:知识蒸馏驱动的BERT压缩革命 | 模型小7倍、推理快9倍的轻量化引擎
人工智能·深度学习·机器学习·自然语言处理·bert·蒸馏·tinybert
Ao00000019 小时前
脑电分析入门指南:信号处理、特征提取与机器学习
人工智能·机器学习·信号处理
胖哥真不错21 小时前
基于MATLAB的Lasso回归的数据回归预测方法应用
机器学习·matlab·项目实战·lasso回归
CH3_CH2_CHO1 天前
DAY01:【ML 第一弹】机器学习概述
人工智能·机器学习