def rating_pred(user_id,item_list,algo): pred = {} for i in item_list: prediction = algo.predict(user_id, i) pred[i] = prediction.est return pred
from data_loader import data, user_ids, movie_ids from recommender import algo # Load the training data trainingSet = data.build_full_trainset() print("Please have patience computing takes a minute.") # Train the NN algo.fit(trainingSet) while True: print("\n") user = int(input("Please enter user: "******"Please enter movie: ")) if user <= len(user_ids) and movie <= len(movie_ids): # Predict and display the results prediction = algo.predict(user,movie) result = prediction.est print("Prediction for this movie: " + str(result)) else: print("User or movie doesn't exist!")
test_user_tag_dict = {} for user in user_list: spe_user_df = test_df.loc[test_df['user_id'] == user] spe_user_tag_list = list( set(spe_user_df['hashtag'].explode('hashtag').tolist())) test_user_tag_dict[user] = spe_user_tag_list trainingSet = data.build_full_trainset() algo.fit(trainingSet) testF2 = open( './' + dataPath + classifierPath + '/test' + classifierPath + '2.dat', "a") preF = open( './' + dataPath + classifierPath + '/pre' + classifierPath + '.txt', 'a') for user_num, user in enumerate(user_list): print(user_num) testF2.write(f"# query {user_num + 1}\n") positive_tag_list = list(set(test_user_tag_dict[user])) for tag in positive_tag_list: testF2.write(f"1 qid:{user_num+1}\n") precision = algo.predict(user, tag) preF.write(f"{precision.est}\n") negative_tag_list = list(set(test_tag_list) - set(positive_tag_list)) for tag in negative_tag_list: testF2.write(f"0 qid:{user_num + 1}\n") precision = algo.predict(user, tag) preF.write(f"{precision.est}\n") testF2.close() preF.close()
# -*- coding: utf-8 -*- """ Created on Sun Jul 28 14:15:51 2019 @author: PAVAN """ from recommender import data, algo trainingSet = data.build_full_trainset() algo.fit(trainingSet) prediction = algo.predict('E', 2) prediction.est
from load_data import data from recommender import algo import pandas as pd import csv import math import functions trainingSet = data.build_full_trainset() algo.fit(trainingSet) test_data = pd.read_csv('test.csv') errors = [] with open('test2.csv', mode='w', newline='') as thefile: writer = csv.writer(thefile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) writer.writerow( ['item', 'user', 'actual rating', 'predicted rating', 'error']) for i in test_data.values: predicted_value = algo.predict(i[1], i[0]).est error = math.fabs(i[2] - predicted_value) errors.append(error) rms_error = functions.rms(errors, len(errors)) print('Mean error:', rms_error)