Beispiel #1
0
    def predict(self,
                user,
                books,
                ratings,
                prediction_num,
                already_read=None,
                graph=None):
        # Predicts top ten unread books for a given user ID
        if graph is None:
            if already_read is None:
                already_read = ratings[ratings['user_id'] ==
                                       user]['book_id'].unique()

            book_data = np.array(
                list(set(books[~books['book_id'].isin(already_read)].book_id)))
            user_data = np.array([user for i in range(len(book_data))])

            predictions = self.model.predict([user_data, book_data],
                                             batch_size=512)
            predictions = np.array([item[0] for item in predictions])
            recommended_book_ids = (-predictions).argsort()

            recommended_books = books[books['book_id'].isin(
                recommended_book_ids)]
            result = []
            for _, book in recommended_books.iterrows():
                b = Book(book['book_id'], book['title'], book['authors'],
                         book['average_rating'], book['image_url'])
                result.append(b)

            return result
        else:
            with graph.as_default():
                if already_read is None:
                    already_read = ratings[ratings['user_id'] ==
                                           user]['book_id'].unique()

                book_data = np.array(
                    list(
                        set(books[~books['book_id'].isin(already_read)].book_id
                            )))
                user_data = np.array([user for i in range(len(book_data))])

                predictions = self.model.predict([user_data, book_data],
                                                 batch_size=512)
                predictions = np.array([item[0] for item in predictions])
                recommended_book_ids = (
                    -predictions).argsort()[:prediction_num]

                recommended_books = books[books['book_id'].isin(
                    recommended_book_ids)]
                result = []
                for _, book in recommended_books.iterrows():
                    b = Book(book['book_id'], book['title'], book['authors'],
                             book['average_rating'], book['image_url'])
                    result.append(b)

                return result
Beispiel #2
0
def convert(recommendations):
    result = []
    for _, book in recommendations.iterrows():
        b = Book(book['book_id'], book['title'], book['authors'],
                 book['average_rating'], book['image_url'])
        result.append(b)

    return result
Beispiel #3
0
def search(search_param, ):
    global results, books

    found_books = books[books['title'].str.contains(search_param)][:10]
    results = []
    for _, row in found_books.iterrows():
        book = Book(row['book_id'], row['title'], row['authors'],
                    row['average_rating'], row['image_url'])
        results.append(book)
    return results
Beispiel #4
0
def neumf_predict(user_id=None):
    global results, ncf, selected, graph

    if user_id is not None:
        user_id = int(user_id)
        rated = ratings[ratings['user_id'] == user_id]['book_id'].unique()
        read = books[books['book_id'].isin(rated)][:num_predict]

        already_read = []
        for _, book in read.iterrows():
            b = Book(book['book_id'], book['title'], book['authors'],
                     book['average_rating'], book['image_url'])
            already_read.append(b)
        results = ncf.predict(user_id, books, ratings, num_predict, None,
                              graph)
        return render_template('index.html',
                               result=results,
                               selected=already_read)
    else:
        results = ncf.predict(0, books, ratings, num_predict, selected, graph)
        return render_template('index.html',
                               result=results,
                               selected=selected_titles)