Get startedGet started for free

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

View Course

Exercise instructions

  • Complete the user_similarity() function to calculate the similarity between user1 and user2.
    • Use assert statements to check that user1 and user2 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 users user1 and user2.
    • Return the fraction of nodes in the projects partition. That is, divide the number of shared_nodes by the total number of nodes in the 'projects' partition.
  • Compute the similarity score between users 'u4560' and 'u1880'. To do this:
    • First obtain the nodes in the 'projects' partition using your get_nodes_from_partition() function.
    • Then use your user_similarity() function to compute the score.

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