视频链接:【图神经网络】GNN从入门到精通
参考资料:复杂网络特征与networkx实现
import numpy as np
import pandas as pd
import networkx as nx
edges = pd.DataFrame()
edges['sources'] = [0,1,2,3,4,4,6,7,7,9,1,4,4,4,6,7,5,8,9,8]
edges['targets'] = [1,4,4,4,6,7,5,8,9,8,0,1,2,3,4,4,6,7,7,9]
#edges['weights'] = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
G = nx.from_pandas_edgelist(edges, source='sources',target='targets')
from_pandas_edgelist(df, source='source', target='target', edge_attr=None, create_using=None)
Return a graph from Pandas DataFrame containing an edge list
无向图中,与节点v 相连的节点数量d(v)或 ki
有向图中, 分 出度和入度,入度 其他节点指向节点v的个数 d i n ( v ) d_in(v) din(v)
出度,节点v指向其他节点的个数
网络平均度 D = a v g i , j d ( i , j ) D=avg_{i,j} d(i,j) D=avgi,jd(i,j)
print(nx.degree(G))
out:[(0, 1), (1, 2), (4, 5), (2, 1), (3, 1), (6, 2), (7, 3), (5, 1), (8, 2), (9, 2)]
print(list(nx.connected_components(G)))
out:[{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}]
nx.shortest_path(G)
print(nx.diameter(G))
out:4
print('度中心性',nx.degree_centrality(G))
out:度中心性 {0: 0.1111111111111111, 1: 0.2222222222222222, 4: 0.5555555555555556, 2: 0.1111111111111111, 3: 0.1111111111111111, 6: 0.2222222222222222, 7: 0.3333333333333333, 5: 0.1111111111111111, 8: 0.2222222222222222, 9: 0.2222222222222222}
Degree_distribution = nx.degree_histogram(G)
print('特征向量中心性',nx.eigenvector_centrality(G))
out:特征向量中心性 {0: 0.105811214865129, 1: 0.26822822377524, 4: 0.5741403289553503, 2: 0.22648750077754073, 3: 0.22648750077754073, 6: 0.26822822377524, 7: 0.4660017030178054, 5: 0.10581121486512898, 8: 0.30358469410769445, 9: 0.30358469410769445}
print('betweenness',nx.betweenness_centrality((G)))
out:betweenness {0: 0.0, 1: 0.2222222222222222, 4: 0.861111111111111, 2: 0.0, 3: 0.0, 6: 0.2222222222222222, 7: 0.38888888888888884, 5: 0.0, 8: 0.0, 9: 0.0}
# closeness
print('closeness',nx.closeness_centrality(G))
out:closeness {0: 0.3333333333333333, 1: 0.47368421052631576, 4: 0.6923076923076923, 2: 0.42857142857142855, 3: 0.42857142857142855, 6: 0.47368421052631576, 7: 0.5294117647058824, 5: 0.3333333333333333, 8: 0.375, 9: 0.375}
# pagerank
print('pagerank',nx.pagerank(G))
out:pagerank {0: 0.060355785347628935, 1: 0.10672083674530218, 4: 0.2377489585500428, 2: 0.055417825677585866, 3: 0.055417825677585866, 6: 0.10672083674530217, 7: 0.13351185545449856, 5: 0.060355785347628935, 8: 0.09187514522721238, 9: 0.09187514522721238}
# HITS
print('HITS',nx.hits(G,tol=0.00001))
out:HITS ({0: 0.03714676345146127, 1: 0.09416945704005145, 4: 0.2015639552399654, 2: 0.07951530507800952, 3: 0.07951530507800952, 6: 0.09416945704005145, 7: 0.16360612693272353, 5: 0.03714676345146127, 8: 0.10658343334413334, 9: 0.10658343334413334}, {0: 0.037148174598460704, 1: 0.09416679027466031, 4: 0.20157075236690655, 2: 0.07951310912238051, 3: 0.07951310912238051, 6: 0.09416679027466031, 7: 0.16360344366482985, 5: 0.037148174598460704, 8: 0.10658482798863028, 9: 0.10658482798863028})