개인화되지 않은 추천

Python으로 추천 엔진 만들기

Robert O'Callaghan

Director of Data

쌍 식별하기

0     User_223           The Great Gatsby <---| 동일 사용자가 읽음
1     User_223     The Catcher in the Rye <---|
2     User_131        The Lord of the Rings
3     User_965               Little Women <---| 동일 사용자가 읽음
4     User_965       Fifty Shades of Grey <---|
... ...
Python으로 추천 엔진 만들기

순열 vs 조합

User book_title
User_233 The Great Gatsby
User_233 The Catcher in the Rye

To:

Book A Book B
0 The Great Gatsby The Catcher in the Rye
1 The Catcher in the Rye The Great Gatsby

The Great Gatsby와 함께 본 도서 -> The Catcher in the Rye

The Catcher in the Rye와 함께 본 도서 -> The Great Gatsby

Python으로 추천 엔진 만들기

페어링 함수 만들기

from itertools import permutations

def create_pairs(x):


  return pairs
Python으로 추천 엔진 만들기

페어링 함수 만들기

from itertools import permutations

def create_pairs(x):
  pairs =                   permutations(x.values, 2)

  return pairs
  • permutations(list, length_of_permutations)) 모든 순열을 담은 이터러블 생성
Python으로 추천 엔진 만들기

페어링 함수 만들기

from itertools import permutations

def create_pairs(x):
  pairs =              list(permutations(x.values, 2))

  return pairs
  • permutations(list, length_of_permutations)) 모든 순열을 담은 이터러블 생성

  • list() 이 객체를 사용 가능한 리스트로 변환

Python으로 추천 엔진 만들기

페어링 함수 만들기

from itertools import permutations

def create_pairs(x):
  pairs = pd.DataFrame(list(permutations(x.values, 2)), 
                           columns=['book_a','book_b'])
  return pairs
  • permutations(list, length_of_permutations)) 모든 순열을 담은 이터러블 생성

  • list() 이 객체를 사용 가능한 리스트로 변환

  • pd.DataFrame() 리스트를 book_a, book_b 열을 가진 DataFrame으로 변환

Python으로 추천 엔진 만들기

데이터에 함수 적용하기

book_pairs = book_df.groupby('userId')['book_title'].apply(perm_function)
print(book_pairs.head())
                                book_a                                   book_b
userId                                                   
User_223     0        The Great Gatsby                   The Catcher in the Rye
             1  The Catcher in the Rye                         The Great Gatsby
User_965     0            Little Women                        40 Shades of Grey
             1       40 Shades of Grey                             Little Women
User_773     0       The Twilight Saga    Harry Potter and the Sorcerer's Stone
                                                                            ...
Python으로 추천 엔진 만들기

결과 정리하기

book_pairs = book_pairs.reset_index(drop=True)
print(book_pairs.head())
                     book_a                                   book_b
0          The Great Gatsby                   The Catcher in the Rye
1    The Catcher in the Rye                         The Great Gatsby
3              Little Women                        40 Shades of Grey
4         40 Shades of Grey                             Little Women
5         The Twilight Saga    Harry Potter and the Sorcerer's Stone
                                                                 ...
Python으로 추천 엔진 만들기

페어 수 세기

pair_counts = book_pairs.groupby(['book_a', 'book_b']).size()
book_a                                book_b                             
The Twilight Saga                     Fifty Shades of Grey           16
                                      Pride and Prejudice            12
                                                                    ...
pair_counts_df = pair_counts.to_frame(name = 'size').reset_index()
print(pair_counts_df.head())
     book_a                                book_b                       size    
1    The Twilight Saga                     Fifty Shades of Grey           16
2    The Twilight Saga                     Pride and Prejudice            12
                                                                         ...
Python으로 추천 엔진 만들기

추천 조회하기

pair_counts_sorted = pair_counts_df.sort_values('size', ascending=False)
pair_counts_sorted[pair_counts_sorted['book_a'] == 'Lord of the Rings']
                  book_a                                     book_b size
137    Lord of the Rings                                 The Hobbit   12
147    Lord of the Rings      Harry Potter and the Sorcerer's Stone   10
143    Lord of the Rings                        The Colour of Magic    9
                                                                     ...
Python으로 추천 엔진 만들기

연습해 봅시다!

Python으로 추천 엔진 만들기

Preparing Video For Download...