गैर-व्यक्तिगत सुझाव

Python में रिकमेंडेशन इंजन बनाना

Robert O'Callaghan

Director of Data

युग्म पहचानना

0     User_223           The Great Gatsby <---| Read by the same user
1     User_223     The Catcher in the Rye <---|
2     User_131        The Lord of the Rings
3     User_965               Little Women <---| Read by the same user
4     User_965       Fifty Shades of Grey <---|
... ...
Python में रिकमेंडेशन इंजन बनाना

परम्यूटेशन्स बनाम कॉम्बिनेशन्स

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)) सभी permutations वाला iterable ऑब्जेक्ट बनाता है
Python में रिकमेंडेशन इंजन बनाना

पेयरिंग फंक्शन बनाना

from itertools import permutations

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

  return pairs
  • permutations(list, length_of_permutations)) सभी permutations वाला iterable ऑब्जेक्ट बनाता है

  • 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)) सभी permutations वाला iterable ऑब्जेक्ट बनाता है

  • list() इस ऑब्जेक्ट को उपयोगी लिस्ट में बदलता है

  • pd.DataFrame() लिस्ट को एक DataFrame में बदलता है, जिसमें book_a और book_b कॉलम होते हैं

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...