開始使用免費開始

使用者相似度指標

你已經寫好一個函式,能計算兩個節點之間的共用節點集合。接下來要撰寫一個函式來計算兩位使用者之間的「相似度指標」:兩位使用者共用的專案數量,除以另一個分區中節點的總數。之後就能用這個指標來找出彼此相似的使用者。

本練習屬於課程

Python 網路分析進階

檢視課程

練習說明

  • 完成 user_similarity() 函式,用來計算 user1user2 的相似度。
    • 使用 assert 敘述檢查 user1user2 都屬於 'users' 分區。
    • 使用你在上一題寫的 shared_partition_nodes() 函式,取得兩位使用者 user1user2 之間的共用節點集合。
    • 回傳 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)
編輯並執行程式碼