1
$\begingroup$

I'm writing a popularity-based recommendation system, where I have data on posts and likes the posts have. I need to recommend posts to a user based on their popularity (obtained likes). Packages and a recommender engine "recommender":

import pandas as pd
import numpy as np
import sklearn
from sklearn.model_selection import train_test_split
import sklearn.externals
import joblib
import recommender as recommender

Data on posts and likes:

df_posts

POST_ID LIKES
0   1   5
1   2   10
2   3   15
3   4   20
4   5   25
5   6   30
6   7   35
7   8   40

User data:

df_users

USER_UUID
0   778B9C5F-5BB6-49F4-AA34-F1FD1F8CAD0E
1   878B9C5F-5BB7-49F4-AA34-F1FD1F8CAD0E
2   678B9C5F-5BB7-49F4-AA34-F1FD1F8CAD0E
3   578B9C5F-5BB7-49F4-AA34-F1FD1F8CAD0E
4   478B9C5F-5BB7-49F4-AA34-F1FD1F8CAD0E

I tried to write the following script for a recommender engine called 'recommender':

#Class for PopularSity based Recommender System model
class popularity_recommender_py():
    def __init__(self):
        self.train_data = None
        self.USER_UUID= None
        self.POST_ID = None
        self.LIKES = None
        self.popularity_recommendations = None

    #Create the popularity based recommender system model
    def create(self, train_data, POST_ID, LIKES):
        self.train_data = train_data
        self.POST_ID = POST_ID
        self.LIKES = LIKES

        #Sort the posts based upon the number of likes
        train_data_sort = train_data.sort_values(['LIKES', self.POST_ID], ascending = [0,1])

        #Generate a recommendation rank based upon score
        train_data_sort['Rank'] = train_data_sort['LIKES'].rank(ascending=0, method='first')

        #Get the top 10 recommendations
        self.popularity_recommendations = train_data_sort.head(10)

    #Use the popularity based recommender system model to
    #make recommendations
    def recommend(self, USER_UUID):
        user_recommendations = self.popularity_recommendations

        #Add user_id column for which the recommendations are being generated
        user_recommendations['USER_UUID'] = USER_UUID

        #Bring user_id column to the front
        cols = user_recommendations.columns.tolist()
        cols = cols[-1:] + cols[:-1]
        user_recommendations = user_recommendations[cols]

        return user_recommendations

Training the model and giving a recommendation:

#Training data
train_data, test_data = train_test_split(df_posts, test_size = 0.20, random_state=0)

#Creating an instance based on popularity
pm = recommender.popularity_recommender_py()

pm.create(train_data, 'USER_UUID', 'POST_ID', 'LIKES')

#Selecting a user to give a recommendation
users_uuid = df_users.iloc[:,0]
USER_UUID = users_uuid[]
USER_UUID

#Giving recommendation
pm.recommend(USER_UUID)

I need to get the following output:

USER_UUID                               POST_ID LIKES
0   778B9C5F-5BB6-49F4-AA34-F1FD1F8CAD0E    8   40
1   778B9C5F-5BB6-49F4-AA34-F1FD1F8CAD0E    7   35
2   778B9C5F-5BB6-49F4-AA34-F1FD1F8CAD0E    6   30
3   778B9C5F-5BB6-49F4-AA34-F1FD1F8CAD0E    5   25
4   778B9C5F-5BB6-49F4-AA34-F1FD1F8CAD0E    4   20
5   778B9C5F-5BB6-49F4-AA34-F1FD1F8CAD0E    3   15
6   778B9C5F-5BB6-49F4-AA34-F1FD1F8CAD0E    2   10
7   778B9C5F-5BB6-49F4-AA34-F1FD1F8CAD0E    1   5

Could anyone can help me with a NameError: name 'USER_UUID' is not defined after running pm.recommend(USER_UUID)? Also, I would appreciate, if someone can help to check the entire code and help me to make the engine workable.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.