airport file:///home/segatron/Downloads/airport.
html
US airports http://konect.uni-koblenz.de/networks/opsahl-
usairport (http://konect.uni-koblenz.de/networks/opsahl-
usairport)
In [38]: #for creating the graph and calculating the attribute values
import networkx as nx
import operator
In [40]: #reading the data
l = []
with open('out.txt') as f:
for line in f:
il = line.split(" ")
il[-1] = il[-1][:-1]
il = map(int, il)
l.append(il)
print l[:10]
[[1, 2, 1], [1, 3, 7], [4, 5, 3], [6, 7, 5], [6, 8, 14137], [9, 10, 8], [11,
2, 8], [12, 5, 413], [13, 14, 2], [13, 15, 2]]
In [4]: nodes = []
for i in l:
nodes.append(i[0])
nodes.append(i[1])
nodes = sorted(set(nodes))
new_l = []
for i in l:
new_l.append((i[0],i[1],i[2]))
Creating a graph. First adding all the nodes and then the edges between them
In [5]: graph = nx.Graph()
for i in nodes:
graph.add_node(i)
graph.add_weighted_edges_from(new_l)
In [41]: #flights from each node
node_dict = {}
for i in l:
if i[0] in node_dict:
node_dict[i[0]]+=i[2]
else:
node_dict[i[0]] = i[2]
if i[1] in node_dict:
node_dict[i[1]]+=i[2]
else:
node_dict[i[1]] = i[2]
1 of 3 17/02/19, 11:58 pm
airport file:///home/segatron/Downloads/airport.html
In [ ]: sorted_node_dict = sorted(node_dict.items(), key=operator.itemgetter(1))[::-
1]
The degree centrality value ranges from 0.0006 to 0.1996
In [42]: deg_centrality = nx.degree_centrality(graph)
sorted_deg_centrality = sorted(deg_centrality.items(), key=operator.itemgett
er(1))[::-1]
print (sorted_deg_centrality[0])
print (sorted_deg_centrality[-1])
(46, 0.19961856325492688)
(4, 0.0006357279084551812)
The Eigen-Vector centrality value ranges from 9.827749622790314e-18 to 0.11825493026777034
In [43]: ev_centrality = nx.eigenvector_centrality(graph)
sorted_ev_centrality = sorted(ev_centrality.items(), key=operator.itemgetter
(1))[::-1]
print (sorted_ev_centrality[0])
print (sorted_ev_centrality[-1])
(88, 0.11825493026777034)
(1487, 9.827749622790314e-18)
The Betweeness centrality value ranges from 0 to 0.21612697041610068.
In [44]: btwn_centrality = nx.betweenness_centrality(graph)
sorted_btwn_centrality = sorted(btwn_centrality.items(), key=operator.itemge
tter(1))[::-1]
print (sorted_btwn_centrality[0])
print (sorted_btwn_centrality[-1])
(32, 0.21612697041610068)
(1, 0.0)
The Closeness centrality value ranges from 0.0006357279084551812 to 0.49777999457856403
In [45]: clsn_centrality = nx.closeness_centrality(graph)
sorted_clsn_centrality = sorted(clsn_centrality.items(), key=operator.itemge
tter(1))[::-1]
print (sorted_clsn_centrality[0])
print (sorted_clsn_centrality[-1])
(46, 0.49777999457856403)
(1487, 0.0006357279084551812)
The average clustering coefficient is 0.50415
In [46]: clustering_coefficient = nx.clustering(graph)
average_cl = nx.average_clustering(graph)
print average_cl
0.50415253921
In [19]: transitivity_value = nx.transitivity(graph)
2 of 3 17/02/19, 11:58 pm
airport file:///home/segatron/Downloads/airport.html
In [21]: reciprocity_value = nx.reciprocity(graph)
In [48]: print transitivity_value
print reciprocity_value
0.384143446645
0.0
In [37]: top_50_sorted_node_dict =dict( sorted_node_dict[:50])
In [25]: def give_accuracy(centrality_value):
top_50 = centrality_value[:50]
count = 0
for i in top_50:
# print i
if i[0] in top_50_sorted_node_dict:
count+=1
return count
In [47]: print give_accuracy(sorted_ev_centrality)
print give_accuracy(sorted_btwn_centrality)
print give_accuracy(sorted_deg_centrality)
37
29
39
Inferences:- The degree centrality would be good measure to determine the importance of a node.
1. The node with highest in degree of centrality denotes that the airport has highest number of flights scheduled.
2. It also gives the highest accuracy.
In [ ]:
In [ ]:
3 of 3 17/02/19, 11:58 pm