图分析的一些简单函数
这些函数所求得的值都已在《复杂网络理论基础》学习笔记中学习,不做多的赘述。
这部分学习主要用到的库有pandas、numpy和matplotlib.pyplot
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
edges = pd.DataFrame()
edges["sources"] = [1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5]
edges["targets"] = [2, 4, 5, 3, 1, 2, 5, 1, 5, 1, 3, 4]
edges["weights"] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
G = nx.from_pandas_edgelist(edges,source="sources",target="targets",edge_attr="weights")
nx.draw_networkx(G)
plt.show()
#输出度
print(nx.degree(G))
#输出连通分量
print(list(nx.connected_components(G)))
#输出图直径
print(nx.diameter(G))
#输出度中心性
print(nx.degree_centrality(G))
#输出特征向量中心性
print((nx.eigenvector_centrality(G)))
#输出介数(中介)中心性
print(nx.betweenness_centrality(G))
#输出接近中心性
print(nx.closeness_centrality(G))
#PageRank
print(nx.pagerank(G))
#HITS,前面的值是Hub的值,后面的authorities
print((nx.hits(G)))
度输出(点名,度值):[(1, 3), (2, 2), (4, 2), (5, 3), (3, 2)]
连通分量:[{1, 2, 3, 4, 5}]
直径:2
度中心性(点名,度中心性值){1: 0.75, 2: 0.5, 4: 0.5, 5: 0.75, 3: 0.5}
特征向量中心性(点名,特征向量中心性值){1: 0.5298988890761731, 2: 0.35775191431708964, 4: 0.4271316779596084, 5: 0.5298988890761731, 3: 0.35775191431708964}
介数(中介)中心性(点名,介数(中介)中心性值):{1: 0.25, 2: 0.08333333333333333, 4: 0.0, 5: 0.25, 3: 0.08333333333333333}
接近中心性(点名,接近中心性值):{1: 0.8, 2: 0.6666666666666666, 4: 0.6666666666666666, 5: 0.8, 3: 0.6666666666666666}
PageRank值:{1: 0.24369622576677996, 2: 0.17225629712058638, 4: 0.16809495422526693, 5: 0.2436962257667799, 3: 0.17225629712058638}
#HITS,前面的值是Hub的值,后面的authorities({1: 0.24059715195481507, 2: 0.1624345647450478, 4: 0.19393656660027417, 5: 0.2405971519548151, 3: 0.1624345647450478}, {1: 0.2405971522393837, 2: 0.1624345646565165, 4: 0.19393656620819955, 5: 0.2405971522393837, 3: 0.1624345646565165})