Aan de slagGa gratis aan de slag

Extract the mean degree centrality day-by-day on the students partition

Here, you're going to see if the mean degree centrality over all nodes is correlated with the number of edges that are plotted over time. There might not necessarily be a strong correlation, and you'll take a look to see if that's the case.

Deze oefening maakt deel uit van de cursus

Intermediate Network Analysis in Python

Cursus bekijken

Oefeninstructies

  • Instantiate a new graph called G_sub containing a subset of edges.
  • Add nodes from G, including the node metadata.
  • Add in edges that fulfill the criteria, using the .add_edges_from() method.
  • Get the students projection G_student_sub from G_sub using the nx.bipartite.projected_graph() function.
  • Compute the degree centrality of the students projection using nx.degree_centrality() (don't use the bipartite version).
  • Append the mean degree centrality to the list mean_dcs. Be sure to convert dc.values() to a list first.
  • Hit 'Submit Answer' to view the plot!

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

from datetime import datetime, timedelta
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

# Initialize a new list: mean_dcs
mean_dcs = []
curr_day = dayone
td = timedelta(days=2)

while curr_day < lastday:
    if curr_day.day == 1:
        print(curr_day)  
    # Instantiate a new graph containing a subset of edges: G_sub
    G_sub = ____
    # Add nodes from G
    G_sub.____(____)
    # Add in edges that fulfill the criteria
    G_sub.____([(u, v, d) for u, v, d in G.edges(data=True) if d['date'] >= curr_day and d['date'] < curr_day + td])
    
    # Get the students projection
    G_student_sub = ____
    # Compute the degree centrality of the students projection
    dc = ____
    # Append mean degree centrality to the list mean_dcs
    mean_dcs.____(np.mean(____(dc.values())))
    # Increment the time
    curr_day += td
    
plt.plot(mean_dcs)
plt.xlabel('Time elapsed')
plt.ylabel('Degree centrality.')
plt.show()
Code bewerken en uitvoeren