Shared nodes in other partition
In order to build up your concept of recommendation systems, we are going to start with the fundamentals. The focus here is on computing user similarity in bipartite graphs.
Your job is to write a function that takes in two nodes, and returns the set of repository nodes that are shared between the two user nodes.
You'll find the following methods and functions helpful in this exercise - .neighbors()
, set()
, and .intersection()
- besides, of course, the shared_partition_nodes
function that you will define!
Diese Übung ist Teil des Kurses
Intermediate Network Analysis in Python
Anleitung zur Übung
- Write a function called
shared_partition_nodes()
that takes in 3 arguments - a graphG
,node1
, andnode2
- and returns the set of nodes that are shared betweennode1
andnode2
.- Check that
node1
andnode2
belong to the same partition using an assert statement and the'bipartite'
keyword. - Obtain the neighbors of
node1
and store them asnbrs1
. - Obtain the neighbors of
node2
and store them asnbrs2
.
- Check that
- Compute the overlap between
nbrs1
andnbrs2
using the set.intersection()
method. - Print the number of shared repositories between users
'u7909'
and'u2148'
using yourshared_partition_nodes()
function together with thelen()
function.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
def ____:
# Check that the nodes belong to the same partition
assert G.nodes[____]['bipartite'] == G.nodes[____]['bipartite']
# Get neighbors of node 1: nbrs1
nbrs1 = ____
# Get neighbors of node 2: nbrs2
nbrs2 = ____
# Compute the overlap using set intersections
overlap = ____(____).____(____)
return overlap
# Print the number of shared repositories between users 'u7909' and 'u2148'
print(____(____))