*by Georgy Lazarev* **(mlcourse slack: jorgy)**

Systems are ubiquitous these days in various fields of science, industry and daily-life situations. Majority of them (to name few, social interactions, telephone and transport networks) can be represented as networks or graphs (sets of nodes and edges where nodes characterize certain objects and edges indicate some kind of connection between them) and therefore are suitable for analysis.

We might want to analyze relationships between participants or actor in those systems to get some valuable insights:

- Which nodes are more important (influencers in network)
- Pathfinding – determining shortest paths between certain nodes
- Structure - finding cliques, community detection (we'll discuss those terms later)

In this tutorial I'll introduce you to the NetworkX - Python package for 'the creation, manipulation, and study of the structure, dynamics, and functions of complex networks'. Important properties pointed out by developers:

- provides an interface to existing numerical algorithms and code written in C, C++, and FORTRAN, thus resulting in promising speed perfomance
- the ability to painlessly work with large nonstandard data sets.

Let's start with some basic things. At first you need to install NetworkX (*'pip install networkx'* or *'conda install ..'* if you are working under Anaconda)

In [45]:

```
#importing necessary packages
import networkx as nx
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import csv
import numpy as np
%matplotlib inline
from collections import defaultdict
import warnings
warnings.filterwarnings('ignore')
```

In [46]:

```
#creating empty Graph
G = nx.Graph()
#you can now add nodes and edges
G.add_node('a')
G.add_node(1)
G.add_edge('a',1)
#or creating list of nodes/edges in advance
G.add_nodes_from([2,3,4])
G.add_edges_from([(2,4),('a',3)])
```

In [47]:

```
#setting attribute while creating node
G.add_node('John', age=26)
#setting attribute to already existing node
G.node['John']['hobby']='writing music'
nx.set_node_attributes(G,'number',7)
#graph with random weghts
weights={}
for e in G.edges():
G[e[0]][e[1]]['weight']=np.random.random()
```

In [48]:

```
print(G.edges())
print(G.edges(data=True))
#data=True prints attributes too
```

In [49]:

```
display(type(G))
```

In [50]:

```
print(nx.info(G))
```

In [51]:

```
nx.draw(G,with_labels=True,node_size=900)
```

It's probably worth mentioning that calling .draw without specifying *pos* argument will result in different layouts of nodes.

Here you can find real networks stored as graph representations (edgelist mostly) in .tsv format. For simplicity I picked quite small undirected graph, which represent frequent social interactions between 62 dolphins from community living off Doubtful Sound, a fjord in New Zealand. So this is sort of social network.

Reading graphs from files doesn't have unified way as data can be stored in different formats and graphs can be represented differently (adjacency matrix, edge lists, adjacency list). I'd recommend you go for reference here and here

In [52]:

```
edges = []
with open('datasets/out.dolphins', 'r') as f:
filereader = csv.reader(f, delimiter="\t", quotechar='"')
next(filereader) # we'll skip header row
for row in filereader:
edges.append(row[:2])
edges[:5]
```

Out[52]:

In [53]:

```
#now create graph from edgelist we made in previous step
GD = nx.from_edgelist(edges)
```

In [54]:

```
print(nx.info(GD))
```

In [55]:

```
nx.draw(GD,with_labels=True)
```

There are other types of vizualization except usual one (especially useful when you are dealing with large graphs) - ArcPlot, MatrixPlot and etc. I'll show only one here - CircosPlot Let's install and import nice visualization package for NetworkX - nxviz.

In [56]:

```
import nxviz as nv
```

In [57]:

```
c=nv.CircosPlot(GD,node_labels=True)
c.draw()
```

That was fast :-) Now let's dive into analysis of our dolphins social graph

In [58]:

```
#.has_edge method allows us to check presence of direct interaction between two nodes
GD.has_edge('40','37')
```

Out[58]:

In [59]:

```
print('shortest path from node 12 to 5 is of length', nx.shortest_path_length(GD,'12','5'),":", nx.shortest_path(GD,'12','5'))
```

For every vertex in graph we can calculate its degree – number of adjacent (connected by an edge )vertices.

In [60]:

```
display(list(GD.neighbors('9')))
print('node 9 has %d neighbors' %GD.degree()['9'])
```

In [61]:

```
GD.degree()
#returns dictionary with all nodes
```

Out[61]:

In [62]:

```
# find 5 nodes with largest amount of neighbors
sorted(dict(GD.degree()).items(), key=lambda x:x[1], reverse=True)[:5]
```

Out[62]:

That brings us to several types of centrality - metric for describing importance of a node

$$ degree \ centrality = \frac {degree\ of\ node} {number\ of \ nodes\ in \ network} $$

In [63]:

```
#We create dictionary with nodes and their degree centralities
degcent=nx.degree_centrality(GD) #for getting dictionary with nodes and
#their degree centralities as keys and values respectively
print("Networkx degree centrality for node 16:",degcent['16'])
```

Let's see 5 nodes with largest degree centrality:

In [64]:

```
sorted(degcent.items(), key=lambda x: x[1], reverse=True)[:5]
```

Out[64]:

In [65]:

```
#we'll store node with the largest degree centraliy as 'nld '
nld=sorted(degcent.items(), key=lambda x: x[1], reverse=True)[:1][0][0]
```

Here goes sort of vizualization of this node:

In [66]:

```
palette = sns.color_palette('coolwarm',2).as_hex()
palette
```

Out[66]:

In [67]:

```
#this list wi'll be used in .draw call.
node_colors=[palette[0] if n!=nld else palette[1] for n in GD.nodes() ]
```

In [68]:

```
nx.draw(GD,node_color=node_colors,with_labels=True)
```

As mentioned above you can set attributes to nodes. Let's say we want each node to keep its degree as (one of) attribute:

In [69]:

```
for n in GD.nodes():
GD.node[n]['degree centrality'] = degcent[n]
```

Now we can use it in customizing our vizualization:

In [70]:

```
c=nv.CircosPlot(GD,node_order='degree centrality',node_labels=True)
c.draw()
```

$$closeness \ centrality = \frac {N\ -\ 1} {sum\ of \ distances\ to \ each \ node \ from \ current \ one}$$

where $N = $ number of nodes

Compares how many steps (edges) it would take to reach every other node in a network (using shortest paths obviously). Intuitively you can think of this as of 'average distance' to all other nodes.

Degree centrality takes into account only direct connections thus deluding analyst sometimes as current node can be central only in local neighborhood. That's why closeness centrality is important.

In case pf being not fully connected, this algorithm computes the closeness centrality for each connected component separately. By the way, you can see connected parts of your graph this way (in our case graph is itself connected):

In [71]:

```
for comp in nx.connected_components(GD):
print(comp)
```

In [72]:

```
#calculating closeness centrality for each node
clocent = nx.closeness_centrality(GD)
```

In [73]:

```
sorted(clocent.items(), key=lambda x: x[1], reverse=True)[:5]
```

Out[73]:

In [74]:

```
for n in GD.nodes():
GD.node[n]['closeness centrality'] = clocent[n]
```

In [75]:

```
nlcc=sorted(clocent.items(), key=lambda x: x[1], reverse=True)[:1][0][0]
palette = sns.color_palette('coolwarm',2).as_hex()
node_colors=[palette[0] if n!=nlcc else palette[1] for n in GD.nodes() ]
```

In [76]:

```
nx.draw(GD,node_color=node_colors,with_labels=True)
```

We can calculate shortest path for each pair of node (if they belong to one connected component obviously). So betweeness centrality of a node indicates to how often this current node becomes a part of all shortest paths.

$$betweeness \ centrality = \frac {number \ of \ shortest \ paths \ passing \ throught \ current \ node } {totul \ number \ of \ shortest \ paths}$$

Intuitively betweeness centrality can be thought of as measure of node influence in network. For example, members of a group with 'big' influence can be useful in terms of spreading a message/information. As an other example - in telecommunication network a node with largest betweeness centrality has biggest control due to amount information transferring through it.

In [77]:

```
betcent = nx.betweenness_centrality(GD)
for n in GD.nodes():
GD.node[n]['betweeness centrality'] = betcent[n]
sorted(betcent.items(), key=lambda x: x[1], reverse=True)[:5]
```

Out[77]:

In [78]:

```
nlbc=sorted(clocent.items(), key=lambda x: x[1], reverse=True)[:1][0][0]
palette = sns.color_palette('coolwarm',2).as_hex()
node_colors=[palette[0] if n!=nlbc else palette[1] for n in GD.nodes() ]
```

In [79]:

```
nx.draw(GD,node_color=node_colors,with_labels=True)
```

$Betweeness \ centrality \ for \ edges$

I guess, this term doesn't need an explanation as its meaning is almost the same as for node

In [80]:

```
betcent_e=nx.edge_betweenness_centrality(GD)
sorted(betcent_e.items(), key=lambda x: x[1], reverse=True)[:5]
```

Out[80]:

In [81]:

```
for e in GD.edges():
GD[e[0]][e[1]]['betweeness centrality']=betcent_e[e]
```

In [82]:

```
#the bigger is betweeness centrality - the darker are edges
c=nv.CircosPlot(GD,edge_color='betweeness centrality')
c.draw()
```

In [83]:

```
elbc=sorted(betcent_e.items(), key=lambda x: x[1], reverse=True)[:1][0][0]
palette=['#bd3c14','#009688']
edge_colors=[palette[0] if e!=elbc else palette[1] for e in GD.edges() ]
```

In [84]:

```
nx.draw(GD,edge_color=edge_colors,with_labels=True,node_color='#999999')
```

The green edge is the one with the largest betweeness centrality. Colors are quite distinguishable, I hope :-)

OK, I tried to avoid this but here is real formula without words:

$$ Ax = \lambda x $$

A is adjacency matrix of graph, (one of the (if not the one) most-used representation of graph) - NxN matrix where values in intersections of columns and rows corresponds to connection between respective nodes. Non-zero values stand for connection between nodes (in case of unweighted graph 1 is the only option) and zero does mean nodes are not directly connected. $ \lambda $ is egenvalue of A and the largest eigenvalue associated with the eigenvector of the adjacency matrix A

Importance of a node depends on the importance of its neighbors.

In [85]:

```
eigcent = nx.eigenvector_centrality_numpy(GD)
sorted(eigcent.items(), key=lambda x: x[1], reverse=True)[:5]
```

Out[85]:

In [86]:

```
for n in GD.nodes():
GD.node[n]['eigenvector centrality'] = eigcent[n]
```

In [87]:

```
#you can use this function for colouring nodes according to their values of given attribute
def attribute_color(G,attribute):
attrs=[G.node[n][attribute] for n in G.nodes()]
uattrs=sorted(list(set(attrs)))
palette = sns.color_palette('Blues',len(uattrs)).as_hex()
colmap=dict(zip(uattrs,palette))
node_colors=[colmap[at] for at in attrs]
nx.draw(G,node_color=node_colors,with_labels=True)
```

Let's see all four types of centrality in one picture. Darker shades of blue corresponds to bigger values of metric

In [88]:

```
fig = plt.figure(figsize=(12, 12))
plt.subplot(221)
attribute_color(GD,'degree centrality')
plt.subplot(222)
attribute_color(GD,'closeness centrality')
plt.subplot(223)
attribute_color(GD,'betweeness centrality')
plt.subplot(224)
attribute_color(GD,'eigenvector centrality')
```