Graph differences over time
Now, you'll compute the graph differences over time! To look at the simplest case, here you'll use a window of (month, month + 1), and then keep track of the edges gained or lost over time. This exercise is preparation for the next exercise, in which you will visualize the changes over time.
Este exercício faz parte do curso
Intermediate Network Analysis in Python
Instruções do exercício
- Inside the
for
loop:- Assign
Gs[i]
tog1
andGs[i + window]
tog2
. - Using
nx.difference()
compute the difference betweeng2
andg1
. Append the result toadded
. - Append the difference between
g1
andg2
toremoved
.
- Assign
- Print
fractional_changes
.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
import networkx as nx
# Instantiate a list of graphs that show edges added: added
added = []
# Instantiate a list of graphs that show edges removed: removed
removed = []
# Here's the fractional change over time
fractional_changes = []
window = 1
for i in range(len(Gs) - window):
g1 = Gs[____]
g2 = Gs[____ + ____]
# Compute graph difference here
added.append(____)
removed.append(____)
# Compute change in graph size over time
fractional_changes.append((len(g2.edges()) - len(g1.edges())) / len(g1.edges()))
# Print the fractional change
print(____)