始める無料で始める

まだ一緒に編集していない共同編集者を推薦する

最後に、オープントライアングルの考え方を活用して、GitHub で協業すべきユーザーを推薦していきます!

この演習はコースの一部です

Pythonで学ぶネットワーク分析入門

コースを見る

演習の手順

  • 互いに協業を推薦すべき GitHub ユーザーの一覧を作成します。次の手順で行ってください。
    • 最初の for ループでは、data=True を指定して、G 内のすべてのノード(メタデータ付き)を反復処理します。
    • 2 つ目の for ループでは、size2 にした combinations() 関数を使い、三角関係になり得るすべての組み合わせを反復処理します。
    • もし n1n2 の間にエッジがない場合、これら 2 つのノード(ユーザー)は協業を推奨すべきなので、このケースでは recommended 辞書の (n1), (n2) の値をインクリメントします。n1n2 の間にエッジがあるかどうかは .has_edge() メソッドで確認できます。
  • リスト内包表記を使って、協業を推薦すべきユーザーの上位 10 組を特定します。イテラブルrecommended 辞書のキーと値のペア(.items() メソッドで取得)にし、条件は countall_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)
コードを編集して実行