network analysis in python i · network analysis in python i network structure hugo: id: 1, age: 34...

34
NETWORK ANALYSIS IN PYTHON I Introduction to networks

Upload: others

Post on 06-Aug-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

NETWORK ANALYSIS IN PYTHON I

Introduction to networks

Page 2: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Networks!● Examples:

● Social

● Transportation

● Model relationships between entities

Page 3: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Networks!● Insights:

● Important entities: influencers in social network

● Pathfinding: most efficient transport path

● Clustering: finding communities

Page 4: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Network structure

Node

Node

Edge

Graph

Page 5: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Network structure

Hugo: id: 1, age: 34

Friendship: date: 2016-05-21

Social Graph

Eric: id: 2, age: 29

Page 6: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

NetworkX API basicsIn [1]: import networkx as nx

In [2]: G = nx.Graph()

In [4]: G.add_nodes_from([1, 2, 3])

In [5]: G.nodes() Out[5]: [1, 2, 3]

In [6]: G.add_edge(1, 2)

In [7]: G.edges() Out[7]: [(1, 2)]

Page 7: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

NetworkX API basicsIn [8]: G.node[1]['label'] = 'blue'

In [9]: G.nodes(data=True) Out[9]: [(1, {'label': 'blue'}), (2, {}), (3, {})]

Page 8: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

NetworkX API basicsIn [10]: nx.draw(G)

In [11]: import matplotlib.pyplot as plt

In [12]: plt.show()

Page 9: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

NETWORK ANALYSIS IN PYTHON I

Let’s practice!

Page 10: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

NETWORK ANALYSIS IN PYTHON

Types of graphs

Page 11: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

● Facebook social graph

Undirected graphs

Page 12: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Undirected graphsIn [1]: import networkx as nx

In [2]: G = nx.Graph()

In [3]: type(G) Out[3]: networkx.classes.graph.Graph

Page 13: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

● Directed: Twi"er social graph

Directed graphs

Page 14: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Directed graphsIn [4]: D = nx.DiGraph()

In [5]: type(D) Out[5]: networkx.classes.digraph.DiGraph

Page 15: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

● Multi(Di)Graph: Trip records between bike sharing stations

Types of graphs

Page 16: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Multi-edge (Directed) graphsIn [6]: M = nx.MultiGraph()

In [7]: type(M) Out[7]: networkx.classes.multigraph.MultiGraph

In [8]: MD = nx.MultiDiGraph()

In [9]: type(MD) Out[9]: networkx.classes.multidigraph.MultiDiGraph

Page 17: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

● Edges can contain weights

Weights on graphs

3

Page 18: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

● Nodes that are connected to themselves

Self-loops

Page 19: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

NETWORK ANALYSIS IN PYTHON I

Let’s practice!

Page 20: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

NETWORK ANALYSIS IN PYTHON I

Network visualization

Page 21: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Irrational vs. Rational visualizations

Page 22: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Visualizing networks● Matrix plots

● Arc plots

● Circos plots

Page 23: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Visualizing networks● Matrix plots

● Arc plots

● Circos plots

Page 24: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Matrix plot

A B C

A

B

C

A

B

C

Page 25: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Matrix plot

A B C

A

B

C

A

B

C

Page 26: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Matrix plot

A B C

A

B

C

A

B

C

Page 27: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Matrix plot

A B C

A

B

C

A

B

C

Page 28: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Directed matrices

A B C

A

B

C

A

B

C

Page 29: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Visualizing networks● Matrix Plots

● Arc Plots

● Circos Plots

Page 30: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Arc plot

A B CA

B

C

ordered axis

Page 31: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Visualizing networks● Matrix Plots

● Arc Plots

● Circos Plots

Page 32: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

Circos plot

A

C

B

D

E

F

A

C

BD

E

F

Page 33: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

Network Analysis in Python I

nxviz APIIn [1]: import nxviz as nv

In [2]: import matplotlib.pyplot as plt

In [3]: ap = nv.ArcPlot(G)

In [4]: ap.draw()

In [5]: plt.show()

Page 34: NETWORK ANALYSIS IN PYTHON I · Network Analysis in Python I Network structure Hugo: id: 1, age: 34 Friendship: date: 2016-05-21 Social Graph Eric: id: 2, age: 29

NETWORK ANALYSIS IN PYTHON I

Let’s practice!