User similarity metric
Having written a function to calculate the set of nodes that are shared between two nodes, you're now going to write a function to compute a metric of similarity between two users: the number of projects shared between two users divided by the total number of nodes in the other partition. This can then be used to find users that are similar to one another.
This exercise is part of the course
Intermediate Network Analysis in Python
Exercise instructions
- Complete the
user_similarity()
function to calculate the similarity betweenuser1
anduser2
.- Use
assert
statements to check thatuser1
anduser2
belong to the'users'
partition. - Use your
shared_partition_nodes()
function from the previous exercise to get the set of nodes shared between the two usersuser1
anduser2
. - Return the fraction of nodes in the
projects
partition. That is, divide the number ofshared_nodes
by the total number of nodes in the'projects'
partition.
- Use
- Compute the similarity score between users
'u4560'
and'u1880'
. To do this:- First obtain the nodes in the
'projects'
partition using yourget_nodes_from_partition()
function. - Then use your
user_similarity()
function to compute the score.
- First obtain the nodes in the
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
def user_similarity(G, user1, user2, proj_nodes):
# Check that the nodes belong to the 'users' partition
____ G.nodes[____]['bipartite'] == '____'
____ G.nodes[____]['bipartite'] == '____'
# Get the set of nodes shared between the two users
shared_nodes = ____
# Return the fraction of nodes in the projects partition
return len(____) / len(____)
# Compute the similarity score between users 'u4560' and 'u1880'
project_nodes = ____
similarity_score = ____
print(similarity_score)