7-6_example_network

一带一路贸易数据集的社会网络分析

  • 发布日期:2019-11-12
  • 难度:较难
  • 类别:社会网络分析
  • 标签:社会网络分析、案例、一带一路贸易数据

1. 问题描述

以一带一路国家对于丝绸商品的进出口贸易情况作为示例,按照初步探索、中心性分析、凝聚子群分析的思路进行分析和演示

In [2]:
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
# 定义函数从文件中自动导入节点、边和权重
def createGraph(filename, w_index) :
    G = nx.DiGraph()
    for line in open(filename) :
        strlist = line.split()
        n1 = strlist[0]
        n2 = strlist[1]
        weight = float(strlist[w_index])
        G.add_weighted_edges_from([(n1, n2, weight)]) 
    return G
#构建网络
G=createGraph('onebeltoneroad.txt',2)

2. 程序实现

初步探索

In [3]:
#节点数量
print(G.number_of_nodes())
#边数量
print(G.number_of_edges())
#计算图的密度
print(nx.density(G))
89
1194
0.1524514811031665
In [4]:
#度数排名前10的节点
d=nx.degree(G)#计算每个节点的度数
d1=sorted(d,key=lambda x: x[1],reverse=True)#按度数大小排序
d2=d1[0:10]#查看前10个元素
print(d2)
[('Italy', 114), ('UnitedKingdom', 103), ('Germany', 103), ('India', 99), ('Spain', 81), ('Switzerland', 81), ('Turkey', 79), ('Belgium', 75), ('France', 68), ('Poland', 52)]
In [5]:
#度数排序图
list1=[]
list2=[]
for i in range(len(d2)):
    list1.append(d2[i][0])
    list2.append(d2[i][1])
plt.xticks(np.arange(len(list1)), list1,fontsize=7,fontname='Times New Roman')
plt.yticks(fontsize=15,fontname='Times New Roman')
plt.ylabel('度', fontsize =15)
plt.xlabel('国家', fontsize =15)
plt.title('度数排序图', fontsize =25)
plt.bar(np.arange(len(list1)),list2,width=0.5)  
plt.show()

中心性分析

In [6]:
#入度排名前10的国家
dc1=sorted(nx.in_degree_centrality(G).items(),key=lambda x: x[1],reverse=True)
print(dc1[0:10])
[('UnitedKingdom', 0.4431818181818182), ('Germany', 0.4431818181818182), ('Italy', 0.4318181818181818), ('Switzerland', 0.4090909090909091), ('Spain', 0.36363636363636365), ('RussianFederation', 0.34090909090909094), ('France', 0.3068181818181818), ('Romania', 0.3068181818181818), ('Norway', 0.29545454545454547), ('India', 0.2840909090909091)]
In [7]:
#出度排名前10的国家
dc2=sorted(nx.out_degree_centrality(G).items(),key=lambda x: x[1],reverse=True)
print(dc2[0:10])
[('Italy', 0.8636363636363636), ('India', 0.8409090909090909), ('UnitedKingdom', 0.7272727272727273), ('Germany', 0.7272727272727273), ('Turkey', 0.6818181818181819), ('Belgium', 0.5681818181818182), ('Spain', 0.5568181818181819), ('Switzerland', 0.5113636363636364), ('France', 0.46590909090909094), ('Greece', 0.3522727272727273)]
In [8]:
#中介中心度排名前10的国家
bc1=sorted(nx.betweenness_centrality(G).items(),key=lambda x: x[1],reverse=True)
print(bc1[0:10])
[('Italy', 0.13727546393683493), ('Germany', 0.09858659742912791), ('India', 0.09437895195772712), ('UnitedKingdom', 0.07271147447272298), ('Turkey', 0.05206034376521512), ('RussianFederation', 0.04692379843791226), ('Spain', 0.043846606439655225), ('Switzerland', 0.037317905425240355), ('France', 0.034403179278311574), ('Greece', 0.027298456613279534)]
In [ ]:
# 将网络图导出为gexf格式文件
nx.write_gexf(G,'onebeltoneroad.gexf')

凝聚子群分析

In [9]:
#移除网络中的自环
G.remove_edges_from(nx.selfloop_edges(G)) 
#得到k核度字典
d1=nx.core_number(G)
#按k核度排序
sd1=sorted(d1.items(),key=lambda x:x[1],reverse=True)
print(sd1)
[('Italy', 26), ('India', 26), ('UnitedKingdom', 26), ('France', 26), ('Germany', 26), ('Turkey', 26), ('Australia', 26), ('Portugal', 26), ('Spain', 26), ('Switzerland', 26), ('Thailand', 26), ('RussianFederation', 26), ('Belgium', 26), ('Austria', 26), ('Netherlands', 26), ('Greece', 26), ('Hungary', 26), ('Poland', 26), ('Romania', 26), ('Denmark', 26), ('Singapore', 26), ('Sweden', 26), ('Czechia', 26), ('Lithuania', 26), ('NewZealand', 26), ('Ireland', 26), ('Bulgaria', 25), ('Slovakia', 25), ('Finland', 25), ('Estonia', 25), ('Latvia', 25), ('Norway', 25), ('UnitedArabEmirates', 24), ('Croatia', 23), ('Slovenia', 23), ('Indonesia', 22), ('Ukraine', 21), ('VietNam', 20), ('Pakistan', 18), ('TFYRofMacedonia', 18), ('Philippines', 18), ('Malaysia', 18), ('Serbia', 16), ('Egypt', 16), ('Israel', 15), ('SriLanka', 14), ('SaudiArabia', 14), ('Oman', 14), ('Cyprus', 13), ('Kuwait', 13), ('Iceland', 13), ('Morocco', 13), ('Qatar', 13), ('Jordan', 12), ('Nepal', 12), ('Kazakhstan', 11), ('Belarus', 11), ('Uzbekistan', 11), ('Luxembourg', 11), ('Azerbaijan', 10), ('Fiji', 10), ('Lebanon', 10), ('BosniaHerzegovina', 9), ('Myanmar', 9), ('Malta', 9), ('Georgia', 8), ('Albania', 6), ('Armenia', 6), ('Kyrgyzstan', 6), ('Rep.ofMoldova', 6), ('Bahrain', 6), ("LaoPeople'sDem.Rep.", 5), ('Bangladesh', 5), ('Iran', 5), ('Tajikistan', 4), ('Turkmenistan', 4), ('Montenegro', 4), ('Afghanistan', 4), ('Cambodia', 4), ('Iraq', 4), ('PapuaNewGuinea', 3), ('Maldives', 3), ('Mongolia', 3), ('Algeria', 2), ('Samoa', 2), ('Timor-Leste', 2), ('Syria', 1), ('Yemen', 1), ('Bhutan', 1)]
In [10]:
#返回26核子图
kc1=nx.k_core(G,k=26) 
print(list(kc1.nodes))
nx.draw(kc1,with_labels=True,font_size=5,node_size=200)
plt.show()
['Italy', 'India', 'UnitedKingdom', 'France', 'Germany', 'Turkey', 'Australia', 'Portugal', 'Spain', 'Switzerland', 'Thailand', 'RussianFederation', 'Belgium', 'Austria', 'Netherlands', 'Greece', 'Hungary', 'Poland', 'Romania', 'Denmark', 'Singapore', 'Sweden', 'Czechia', 'Lithuania', 'NewZealand', 'Ireland']