defgenerate_recommendations(self,features,data, print_rec = False): index = data[dalta['userID'] == self.userID].index.tolist() #Storig the rows into a new dataframe X = features.iloc[index] #Applying cosine similarity and storing the matrix cossim_mat = cosine_similarity(X = X.to_numpy(copy = True),Y =features.to_numpy(copy = True), dense_output= False) #Get top N recommendations recomm_indices = self.largest_indices(cossim_mat,self.N,data)
if (print_rec == True): #Print the recommendations from the obtained recomm_indices self.print_recommendations(recomm_indices,data) return else: #Return the list of recommendations recomm = [] i = 0 for x in data['courseID'][recomm_indices].unique(): i+=1 recomm.append(x) if(i==self.N): break return recomm
#ResultPage @app.route("/result", methods=["GET", "POST"]) defresult(): if request.method == "POST": #Collecting the form responses userdata = request.form
#Extracting the values for UserIndex and No. of recommendations user = int(userdata.get("index")) N = int(userdata.get("N"))
#Getting the userID from the user Index users = data['userID'].unique() userID = int(users[user])
#Running the model, generating recommendations and passing the list to the HTML page model = recommendationGenerator(userID, N) recomm = model.generate_recommendations(features,data)
return render_template("result.html",userID = userID ,rec_list= recomm) else: return"Sorry, there was an error."