做关于车的网站好,东营微信开发网站建设,wordpress手机pc,洛阳建筑公司排名目录
一、实验介绍
二、实验环境
1. 配置虚拟环境
2. 库版本介绍
3. IDE
三、实验内容
0. 导入必要的工具
1. 生成邻接矩阵simulate_G:
2. 计算节点的聚集系数 CC(G):
3.计算节点的介数中心性 BC(G)
4. 计算节点的度中心性 DC(G)
5. 综合centrality(G)
6. 代…目录
一、实验介绍
二、实验环境
1. 配置虚拟环境
2. 库版本介绍
3. IDE
三、实验内容
0. 导入必要的工具
1. 生成邻接矩阵simulate_G:
2. 计算节点的聚集系数 CC(G):
3.计算节点的介数中心性 BC(G)
4. 计算节点的度中心性 DC(G)
5. 综合centrality(G)
6. 代码整合 一、实验介绍 本实验实现了计算图网络中节点的中心性指标包括聚集系数、介数中心性、度中心性等 二、实验环境 本系列实验使用了PyTorch深度学习框架相关操作如下基于深度学习系列文章的环境
1. 配置虚拟环境
深度学习系列文章的环境
conda create -n DL python3.7
conda activate DL
pip install torch1.8.1cu102 torchvision0.9.1cu102 torchaudio0.8.1 -f https://download.pytorch.org/whl/torch_stable.htmlconda install matplotlib
conda install scikit-learn
新增加
conda install pandas
conda install seaborn
conda install networkx
conda install statsmodels
pip install pyHSICLasso
注本人的实验环境按照上述顺序安装各种库若想尝试一起安装天知道会不会出问题
2. 库版本介绍
软件包本实验版本目前最新版matplotlib3.5.33.8.0numpy1.21.61.26.0python3.7.16scikit-learn0.22.11.3.0torch1.8.1cu1022.0.1torchaudio0.8.12.0.2torchvision0.9.1cu1020.15.2
新增
networkx2.6.33.1pandas1.2.32.1.1pyHSICLasso1.4.21.4.2seaborn0.12.20.13.0statsmodels0.13.50.14.0 3. IDE 建议使用Pycharm其中pyHSICLasso库在VScode出错尚未找到解决办法…… win11 安装 Anaconda2022.10pycharm2022.3/2023.1.4配置虚拟环境_QomolangmaH的博客-CSDN博客https://blog.csdn.net/m0_63834988/article/details/128693741https://blog.csdn.net/m0_63834988/article/details/128693741https://blog.csdn.net/m0_63834988/article/details/128693741 三、实验内容
0. 导入必要的工具 import numpy as np
import networkx as nx
import matplotlib.pyplot as plt 1. 生成邻接矩阵simulate_G: def simulate_G(d):B np.random.binomial(1, 0.3, size(d, d))return np.triu(B, 1) np.triu(B, 1).T生成一个随机的邻接矩阵表示的图其中节点数为 d。 使用numpy 库的 random.binomial 函数生成一个具有一定概率连接的邻接矩阵通过 triu 函数提取出上三角部分不包括对角线然后与其转置相加得到一个无向图的邻接矩阵。 2. 计算节点的聚集系数 CC(G):
def CC(G):cc {}# single_source_dijkstra_path_length 从点i到其他点的最短路径长度# nx.single_source_dijkstra_path(G_nx, i)for i in range(G.shape[0]):pre_num 0for k, v in nx.single_source_dijkstra_path_length(G_nx, i).items():pre_num vcc[len(cc)] (G.shape[0] - 1) / pre_numreturn cc 通过遍历图中的每个节点使用 networkx 库的 single_source_dijkstra_path_length 函数计算该节点到其他节点的最短路径长度并将这些路径长度求和。然后通过计算 (节点总数 - 1) / 最短路径长度之和得到该节点的聚集系数。 3. 计算节点的介数中心性 BC(G)
def BC(G):bc_res {}bc [0.] * G.shape[0]for i in range(G.shape[0]):for j in range(G.shape[0]):shortest_paths list(nx.all_shortest_paths(G_nx, i, j))for v in shortest_paths:for pre in v[1:-1]:bc[pre] 1. / len(shortest_paths)for i in range(G.shape[0]):bc_res[i] bc[i] / ((G.shape[0] - 1) * (G.shape[0] - 2))return bc_res遍历图中的每对节点使用 networkx 库的 all_shortest_paths 函数找到它们之间的所有最短路径并对每条路径上的中间节点进行计数。然后通过计算每个节点的介数值即通过该节点的最短路径数除以所有最短路径数的总和得到节点的介数中心性。 4. 计算节点的度中心性 DC(G)
def DC(G):dc_res {}degree np.sum(G, axis1)dc degree / (G.shape[0] - 1)for index, item in enumerate(dc):dc_res[index] itemreturn dc_res 计算节点的度中心性degree centrality。首先计算每个节点的度与其相连的边的数量然后将度除以节点总数减去 1得到节点的度中心性。
5. 综合 centrality(G)
def centrality(G):cc CC(G)bc BC(G)dc DC(G)return dc, cc, bc 这个函数是一个综合函数用于计算节点的三种中心性指标度中心性、聚集系数和介数中心性。它调用上述三个函数并返回这些中心性指标的字典。 6. 代码整合 import numpy as np
import networkx as nx
import matplotlib.pyplot as pltdef simulate_G(d):B np.random.binomial(1, 0.3, size(d, d))return np.triu(B, 1) np.triu(B, 1).Tdef CC(G):cc {}# single_source_dijkstra_path_length 从点i到其他点的最短路径长度# nx.single_source_dijkstra_path(G_nx, i)for i in range(G.shape[0]):pre_num 0for k, v in nx.single_source_dijkstra_path_length(G_nx, i).items():pre_num vcc[len(cc)] (G.shape[0] - 1) / pre_numreturn ccdef BC(G):bc_res {}bc [0.] * G.shape[0]for i in range(G.shape[0]):for j in range(G.shape[0]):shortest_paths list(nx.all_shortest_paths(G_nx, i, j))for v in shortest_paths:for pre in v[1:-1]:bc[pre] 1. / len(shortest_paths)for i in range(G.shape[0]):bc_res[i] bc[i] / ((G.shape[0] - 1) * (G.shape[0] - 2))return bc_resdef DC(G):dc_res {}degree np.sum(G, axis1)dc degree / (G.shape[0] - 1)for index, item in enumerate(dc):dc_res[index] itemreturn dc_resdef centrality(G):cc CC(G)bc BC(G)dc DC(G)return dc, cc, bcif __name__ __main__:# np.random.seed(0)# G simulate_G(8)G np.array([[0, 1, 0, 1, 1],[1, 0, 1, 0, 0],[0, 1, 0, 0, 1],[1, 0, 0, 0, 1],[1, 0, 1, 1, 0]])G_nx nx.from_numpy_matrix(G)nx.draw_kamada_kawai(G_nx, with_labelsTrue)plt.show()dc, cc, bc centrality(G)print(dc_nx, nx.degree_centrality(G_nx))print(dc , dc)print(cc_nx, nx.closeness_centrality(G_nx))print(cc , cc)print(bc_nx, nx.betweenness_centrality(G_nx))print(bc , bc)