Nepersonalizované návrhy

Tvorba doporučovacích systémů v Pythonu

Robert O'Callaghan

Director of Data

Identifikace párů

0     User_223           The Great Gatsby <---| Přečteno stejným uživatelem
1     User_223     The Catcher in the Rye <---|
2     User_131        The Lord of the Rings
3     User_965               Little Women <---| Přečteno stejným uživatelem
4     User_965       Fifty Shades of Grey <---|
... ...
Tvorba doporučovacích systémů v Pythonu

Permutace versus kombinace

Uživatel book_title
User_233 The Great Gatsby
User_233 The Catcher in the Rye

Na:

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

Knihy zobrazené s The Great Gatsby -> The Catcher in the Rye

Knihy zobrazené s The Catcher in the Rye -> The Great Gatsby

Tvorba doporučovacích systémů v Pythonu

Vytvoření funkce pro párování

from itertools import permutations

def create_pairs(x):


  return pairs
Tvorba doporučovacích systémů v Pythonu

Vytvoření funkce pro párování

from itertools import permutations

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

  return pairs
  • permutations(list, length_of_permutations)) Generuje iterovatelný objekt obsahující všechny permutace
Tvorba doporučovacích systémů v Pythonu

Vytvoření funkce pro párování

from itertools import permutations

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

  return pairs
  • permutations(list, length_of_permutations)) Generuje iterovatelný objekt obsahující všechny permutace

  • list() Převede tento objekt na použitelný seznam

Tvorba doporučovacích systémů v Pythonu

Vytvoření funkce pro párování

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)) Generuje iterovatelný objekt obsahující všechny permutace

  • list() Převede tento objekt na použitelný seznam

  • pd.DataFrame() Převede seznam na DataFrame se sloupci book_a a book_b

Tvorba doporučovacích systémů v Pythonu

Použití funkce na data

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
                                                                            ...
Tvorba doporučovacích systémů v Pythonu

Úprava výsledků

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
                                                                 ...
Tvorba doporučovacích systémů v Pythonu

Počítání párů

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
                                                                         ...
Tvorba doporučovacích systémů v Pythonu

Vyhledávání doporučení

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
                                                                     ...
Tvorba doporučovacích systémů v Pythonu

Lass uns üben!

Tvorba doporučovacích systémů v Pythonu

Preparing Video For Download...