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
Exercise instructions
- Get the nodes of the
'student'
partition into a list calledstudent_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 ofd
equals'student'
.
- Use a list comprehension to do this, iterating over all the nodes of
- Create the students nodes projection as a graph called
G_students
. Use thenx.bipartite.projected_graph()
function to do this. Be sure to specify the keyword argumentnodes=student_nodes
. - Calculate the degree centrality of
G_students
usingnx.degree_centrality()
. Store the result asdcs
. - 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()