Recommending co-editors who have yet to edit together
Finally, you're going to leverage the concept of open triangles to recommend users on GitHub to collaborate!
This exercise is part of the course
Introduction to Network Analysis in Python
Exercise instructions
- Compile a list of GitHub users that should be recommended to collaborate with one another. To do this:
- In the first
for
loop, iterate over all the nodes inG
, including the metadata (by specifyingdata=True
). - In the second
for
loop, iterate over all the possible triangle combinations, which can be identified using thecombinations()
function with asize
of2
. - If
n1
andn2
do not have an edge between them, a collaboration between these two nodes (users) should be recommended, so increment the(n1), (n2)
value of therecommended
dictionary in this case. You can check whether or notn1
andn2
have an edge between them using the.has_edge()
method.
- In the first
- Using a list comprehension, identify the top 10 pairs of users that should be recommended to collaborate. The iterable should be the key-value pairs of the
recommended
dictionary (which can be accessed with the.items()
method), while the conditional should be satisfied ifcount
is greater than the top 10 inall_counts
. Note thatall_counts
is sorted in ascending order, so you can access the top 10 withall_counts[-10]
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import necessary modules
from itertools import combinations
from collections import defaultdict
# Initialize the defaultdict: recommended
recommended = defaultdict(int)
# Iterate over all the nodes in G
for n, d in ____:
# Iterate over all possible triangle relationship combinations
for n1, n2 in ____(list(G.neighbors(n)), ____):
# Check whether n1 and n2 do not have an edge
if not G.has_edge(____, ____):
# Increment recommended
____[(____, ____)] += 1
# Identify the top 10 pairs of users
all_counts = sorted(recommended.values())
top10_pairs = [pair for pair, count in ____ if ____ > ____]
print(top10_pairs)