사용자 유사도 지표
두 노드 사이에 공유되는 노드 집합을 계산하는 함수를 이미 작성하셨으니, 이제 두 사용자 간의 유사도를 계산하는 함수를 작성해 보겠습니다. 여기서의 유사도 지표는 두 사용자가 함께한 프로젝트 수를 다른 파티션의 전체 노드 수로 나눈 값입니다. 이 지표를 사용해 서로 유사한 사용자를 찾을 수 있어요.
이 연습은 강의의 일부입니다
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)