神经网络图形绘制

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

这样绘制出图形如下:

相关推荐
Purepisces19 分钟前
深度学习笔记: 最详尽解释预测系统的分类指标(精确率、召回率和 F1 值)
人工智能·笔记·python·深度学习·机器学习·分类
Jurio.33 分钟前
【ACM出版】第13届亚洲膜计算会议(ACMC2024)暨 2024年机器学习、模式识别与自动化工程国际学术会议(MLPRAE 2024,8月7日-9)
运维·机器学习·自动化·国际会议·模式识别·膜计算
coolkidlan41 分钟前
【AI原理解析】—k-means原理
人工智能·机器学习·kmeans
jiayoushijie-泽宣42 分钟前
深入浅出3D感知中的优化与基于学习的技术 (第二章) 原创教程
人工智能·学习·算法·机器学习·3d·机器人
handuoduo12341 小时前
计算经纬度坐标之间的真实距离
人工智能·算法·机器学习
魔云连洲1 小时前
UNet进行病理图像分割
python·深度学习·机器学习
HyperAI超神经2 小时前
入选顶会ICML,清华AIR等联合发布蛋白质语言模型ESM-AA,超越传统SOTA
人工智能·深度学习·机器学习·语言模型·自然语言处理·清华大学·蛋白质语言模型
Linux运维老纪2 小时前
通俗易懂的chatgpg的原理简介
深度学习·算法·机器学习·chatgpt·gpt-3
Leventure_轩先生3 小时前
[DASP]1. 采样与重构
算法·机器学习·重构
oscar9993 小时前
AI基本概念(人工智能、机器学习、深度学习)
人工智能·深度学习·机器学习·gai