まだ一緒に編集していない共同編集者を推薦する
最後に、オープントライアングルの考え方を活用して、GitHub で協業すべきユーザーを推薦していきます!
この演習はコースの一部です
Pythonで学ぶネットワーク分析入門
演習の手順
- 互いに協業を推薦すべき GitHub ユーザーの一覧を作成します。次の手順で行ってください。
- 最初の
forループでは、data=Trueを指定して、G内のすべてのノード(メタデータ付き)を反復処理します。 - 2 つ目の
forループでは、sizeを2にしたcombinations()関数を使い、三角関係になり得るすべての組み合わせを反復処理します。 - もし
n1とn2の間にエッジがない場合、これら 2 つのノード(ユーザー)は協業を推奨すべきなので、このケースではrecommended辞書の(n1), (n2)の値をインクリメントします。n1とn2の間にエッジがあるかどうかは.has_edge()メソッドで確認できます。
- 最初の
- リスト内包表記を使って、協業を推薦すべきユーザーの上位 10 組を特定します。イテラブル は
recommended辞書のキーと値のペア(.items()メソッドで取得)にし、条件はcountがall_countsの上位 10 件よりも「大きい」ことです。all_countsは昇順でソートされているため、上位 10 件にはall_counts[-10]でアクセスできます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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)