https://theslaps.medium.com/centrality-metrics-via-networkx-python-e13e60ba2740
import networkx as nx
from networkx.readwrite import json_graph
import json
import matplotlib.pyplot as plt
import pandas as pd
G = nx.karate_club_graph()
print(type(G))
## #nodes: 34 and #edges: 78
print('#nodes:', len(G.nodes()), 'and', '#edges:', len(G.edges()))
with open('nodes.json', encoding='UTF8') as f:
json_data = json.loads(f.read())
G = nx.Graph()
for v in json_data:
G.add_node(v['id']) // nodes.json 파일 읽어서 networkx 노드로 추가
with open('links.json', encoding='UTF8') as f:
json_data = json.loads(f.read())
for v in json_data:
G.add_edge(v['source'],v['target']) // links.json 파일 읽어서 networkx 엣지로 추가
plt.figure(figsize=(40, 40))
nx.draw(G, with_labels=True)
plt.show()
degree_centrality=nx.degree_centrality(G)
eigenvector_centrality = nx.eigenvector_centrality(G)
closeness_centrality = nx.closeness_centrality(G)
betweenness_centrality = nx.betweenness_centrality(G)
'Python' 카테고리의 다른 글
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). (0) | 2023.07.25 |
---|---|
딥러닝 클래스/데이터 불균형 (0) | 2023.07.19 |
python str.replace pattern 특정 특수문자 제외 (0) | 2023.07.09 |
excel 데이터 group by concatenate (0) | 2023.07.09 |
excel to json (0) | 2023.07.08 |