Skip to content
Course Notes
Use this workspace to take notes, store code snippets, or build your own interactive cheatsheet! The datasets used in this course are available in the datasets
folder.
# Import any packages you want to use here
Take Notes
Add notes here about the concepts you've learned and code cells with code you want to keep.
Add your notes here
# Add your code snippets here
Add the degree centrality score of each node to their metadata dictionary
dcs = nx.degree_centrality(G) for n in G.nodes(): G.nodes[n]['centrality'] = dcs[n]
# Create the CircosPlot object: c
c = CircosPlot(G, node_color='bipartite', node_grouping='bipartite', node_order='centrality')
# Draw c to the screen
c.draw()
# Display the plot
plt.show()
# Define get_nodes_from_partition()
def get_nodes_from_partition(G, partition):
# Initialize an empty list for nodes to be returned
nodes = []
# Iterate over each node in the graph G
for n in G.nodes(data=False):
# Check that the node belongs to the particular partition
if G.nodes[n]['bipartite'] == partition:
# If so, append it to the list of nodes
nodes.append(n)
return nodes
# Print the number of nodes in the 'projects' partition
print(len(get_nodes_from_partition(G, 'projects')))
# Print the number of nodes in the 'users' partition
print(len(get_nodes_from_partition(G, 'users')))
# Import matplotlib
import matplotlib.pyplot as plt
# Get the 'users' nodes: user_nodes
user_nodes = get_nodes_from_partition(G, 'users')
# Compute the degree centralities: dcs
dcs = nx.degree_centrality(G)
# Get the degree centralities for user_nodes: user_dcs
user_dcs = [dcs[n] for n in user_nodes]
# Plot the degree distribution of users_dcs
plt.yscale('log')
plt.hist(user_dcs, bins=20)
plt.show()
# Get the 'projects' nodes: project_nodes
project_nodes = get_nodes_from_partition(G, 'projects')
# Compute the degree centralities: dcs
dcs = nx.degree_centrality(G)
# Get the degree centralities for project_nodes: project_dcs
project_dcs = [dcs[n] for n in project_nodes]
# Plot the degree distribution of project_dcs
plt.yscale('log')
plt.hist(project_dcs, bins=20)
plt.show()
def shared_partition_nodes(G, node1, node2):
# Check that the nodes belong to the same partition
assert G.nodes[node1]['bipartite'] == G.nodes[node2]['bipartite']
# Get neighbors of node 1: nbrs1
nbrs1 = G.neighbors(node1)
# Get neighbors of node 2: nbrs2
nbrs2 = G.neighbors(node2)
# Compute the overlap using set intersections
overlap = set(nbrs1).intersection(nbrs2)
return overlap
# Print the number of shared repositories between users 'u7909' and 'u2148'
print(len(shared_partition_nodes(G, 'u7909', 'u2148')))
def user_similarity(G, user1, user2, proj_nodes):
# Check that the nodes belong to the 'users' partition
assert G.nodes[user1]['bipartite'] == 'users'
assert G.nodes[user2]['bipartite'] == 'users'
# Get the set of nodes shared between the two users
shared_nodes = shared_partition_nodes(G, user1, user2)
# Return the fraction of nodes in the projects partition
return len(shared_nodes) / len(proj_nodes)
# Compute the similarity score between users 'u4560' and 'u1880'
project_nodes = get_nodes_from_partition(G, 'projects')
similarity_score = user_similarity(G, 'u4560', 'u1880', project_nodes)
print(similarity_score)