In [1]:
import networkx as nx # you have to install networkx 2.x through `pip install --upgrade networkx`
import pandas as pd
import numpy as np
# download the data here: https://bitbucket.org/dipolemoment/analyticsvidhya/src
In [2]:
data = pd.read_csv("airlines.csv")
data.head()
Out[2]:
In [3]:
data['std'] = data.sched_dep_time.astype(str).str.replace('(\d{2}$)', '') + ':' + data.sched_dep_time.astype(str).str.extract('(\d{2}$)', expand=False) + ':00'
data.head()
Out[3]:
In [4]:
FG = nx.from_pandas_edgelist(data, source='origin', target='dest', edge_attr=True)
In [5]:
FG.nodes()
Out[5]:
In [6]:
FG.edges()
Out[6]:
In [17]:
%matplotlib inline
nx.draw_networkx(FG, with_labels=True, node_color='green')
In [20]:
# degree centrality measures the number of edges connected to a node
nx.algorithms.degree_centrality(FG)
Out[20]:
In [22]:
# average density of the graph
## Density - how many edges a graph. A complete undirect graph has 1 as density
## Some graphs has 1+ density, such as a graph with loops
nx.density(FG)
Out[22]:
In [24]:
# average shortest path among all the paths
nx.average_shortest_path_length(FG)
Out[24]:
In [26]:
# average of a node's neighbours' degree, for each node with degree k
nx.average_degree_connectivity(FG)
Out[26]:
In [28]:
for path in nx.all_simple_paths(FG, source='LAX', target='SEA'):
print(path)
break
In [29]:
# find dijkstra path between LA and Seattle
dijpath = nx.dijkstra_path(FG, source='LAX', target='SEA')
dijpath
Out[29]:
In [30]:
# find dijkstra path between LA and Seattle, weighted by air_time
weighted_dijpath = nx.dijkstra_path(FG, source='LAX', target='SEA', weight='air_time')
weighted_dijpath
Out[30]:
In [ ]:
"""
For more algorithms provided by networkx: https://networkx.github.io/documentation/stable/reference/algorithms/index.html
Reference: https://www.analyticsvidhya.com/blog/2018/04/introduction-to-graph-theory-network-analysis-python-codes/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+AnalyticsVidhya+%28Analytics+Vidhya%29
"""