In [1]:
%config InlineBackend.figure_format ='retina'
%matplotlib inline
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Set options
sns.set_style('darkgrid')
pd.set_option("display.max_rows", 10)
In [2]:
users = [
{ "id": 0, "name": "Hero" },
{ "id": 1, "name": "Dunn" },
{ "id": 2, "name": "Sue" },
{ "id": 3, "name": "Chi" },
{ "id": 4, "name": "Thor" },
{ "id": 5, "name": "Clive" },
{ "id": 6, "name": "Hicks" },
{ "id": 7, "name": "Devin" },
{ "id": 8, "name": "Kate" },
{ "id": 9, "name": "Klein" }
]
friendships = [(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4),
(4, 5), (5, 6), (5, 7), (6, 8), (7, 8), (8, 9)]
for user in users:
user["friends"] = []
for i, j in friendships:
users[i]["friends"].append(users[j])
users[j]["friends"].append(users[i])
In [3]:
g = nx.Graph()
g.add_nodes_from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
g.add_edges_from(friendships)
In [4]:
g.number_of_nodes()
Out[4]:
In [5]:
g.number_of_edges()
Out[5]:
In [6]:
nx.draw_networkx(g)
plt.axis('off')
A node's degree is the number of connections it has.
In [7]:
nx.degree(g, 3)
Out[7]:
In [8]:
nx.degree(g, 4)
Out[8]:
The local clustering coefficient is the fraction of a node's connections that are also connected.
In [9]:
nx.clustering(g, 0)
Out[9]:
In [10]:
nx.clustering(g, 4)
Out[10]:
In [11]:
nx.clustering(g, 1)
Out[11]:
Betweenness centrality estimates which nodes are important in connecting networks
In [12]:
df = pd.DataFrame()
df["node"] = g.nodes()
df["centrality"] = pd.Series(nx.degree_centrality(g))
df["eigenvector"] = pd.Series(nx.eigenvector_centrality(g))
df["betweenness"] = pd.Series(nx.betweenness_centrality(g, normalized = True, endpoints = True))
df
Out[12]:
In [13]:
nx.betweenness_centrality(g, normalized = True, endpoints = True)
Out[13]:
In [14]:
nx.betweenness_centrality(g, normalized = True, endpoints = False)
Out[14]:
In [ ]: