Get startedGet started for free

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.

This exercise is part of the course

Intermediate Network Analysis in Python

View Course

Exercise instructions

  • 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!

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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()
Edit and Run Code