Get startedGet started for free

Visualize the degree centrality distribution of the students projection

In this exercise, you will visualize the degree centrality distribution of the students projection. This is a recap of two previous concepts you've learned: degree centralities, and projections.

This exercise is part of the course

Intermediate Network Analysis in Python

View Course

Exercise instructions

  • Get the nodes of the 'student' partition into a list called student_nodes.
    • Use a list comprehension to do this, iterating over all the nodes of G (including the metadata), and checking to see if the 'bipartite' keyword of d equals 'student'.
  • Create the students nodes projection as a graph called G_students. Use the nx.bipartite.projected_graph() function to do this. Be sure to specify the keyword argument nodes=student_nodes.
  • Calculate the degree centrality of G_students using nx.degree_centrality(). Store the result as dcs.
  • Plot the histogram of degree centrality values.

Hands-on interactive exercise

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

# Import necessary modules
import matplotlib.pyplot as plt
import networkx as nx

# Get the student partition's nodes: student_nodes
student_nodes = [n for n, d in ____ if d['____'] == '____']

# Create the students nodes projection as a graph: G_students
G_students = ____

# Calculate the degree centrality using nx.degree_centrality: dcs
dcs = ____

# Plot the histogram of degree centrality values
plt.hist(list(____))
plt.yscale('log')  
plt.show() 
Edit and Run Code