ユーザー類似度メトリック
2 つのノード間で共有されるノード集合を計算する関数を作成したので、次は 2 人のユーザー間の「類似度メトリック」を計算する関数を書きます。ここでは、2 人のユーザーが共有しているプロジェクト数を、もう一方のパーティション内のノード総数で割った値を用います。これにより、互いに似ているユーザーを見つけられます。
この演習はコースの一部です
Python 中級ネットワーク解析
演習の手順
user_similarity()関数を完成させ、user1とuser2の類似度を計算します。assert文で、user1とuser2が'users'パーティションに属していることを確認します。- 前の演習で作成した
shared_partition_nodes()関数を使って、user1とuser2の間で共有されるノード集合を取得します。 projectsパーティション内のノード数に対する比率を返します。つまり、shared_nodesの数を'projects'パーティションのノード総数で割ります。
- ユーザー
'u4560'と'u1880'の類似度スコアを計算します。次を行ってください。- まず、
get_nodes_from_partition()関数を使って、'projects'パーティションのノードを取得します。 - その後、
user_similarity()関数でスコアを計算します。
- まず、
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
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)