시작하기무료로 시작하기

K-최근접 이웃 단계별로 따라가기

방금 K-최근접 이웃을 사용해 비슷한 집단의 평가를 바탕으로 누군가가 어떤 항목을 어떻게 평가할지 추론하는 방법을 살펴봤어요. 이 연습 문제에서는 이 과정을 직접 단계별로 수행하면서 동작 원리를 확실히 이해해 볼게요.

시작을 도와드리기 위해, 그동안 여러 번 만들어 본 유사도 행렬 생성 단계는 미리 완료해 두었습니다. 사용자 유사도 행렬을 DataFrame으로 감싼 user_similarities로 불러왔어요.

이 DataFrame은 각 사용자가 행과 열로 있고, 교차점에는 해당하는 유사도 점수가 들어 있어요.

이번 연습에서는 user_001의 유사도 점수를 사용해 가장 가까운 이웃을 찾고, 그 이웃들이 어떤 영화에 준 평점을 바탕으로 user_001이 그 영화를 본다면 어떤 평점을 줄지 추론해 볼 거예요.

이 연습은 강의의 일부입니다

Python으로 추천 엔진 만들기

강의 보기

연습 안내

  • ordered_similarities에서 상위 10명의 사용자를 추출해 nearest_neighbors로 저장하여 User_A의 10명 최근접 이웃 ID를 찾으세요.
  • user_ratings_table에서 nearest_neighbors에 포함된 사용자들이 준 평점을 neighbor_ratings로 추출하세요.
  • 이 사용자들이 영화 Apollo 13 (1995)에 준 평점의 평균을 계산하여, User_A가 그 영화를 봤다면 줄 법한 평점을 추론하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Isolate the similarity scores for user_1 and sort
user_similarity_series = user_similarities.loc['user_001']
ordered_similarities = user_similarity_series.sort_values(ascending=False)

# Find the top 10 most similar users
nearest_neighbors = ordered_similarities[1:11].____

# Extract the ratings of the neighbors
neighbor_ratings = user_ratings_table.____(nearest_neighbors)

# Calculate the mean rating given by the users nearest neighbors
print(neighbor_ratings['Apollo 13 (1995)'].____())
코드 편집 및 실행