-
Notifications
You must be signed in to change notification settings - Fork 78
/
main.py
78 lines (70 loc) · 2.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding = utf-8 -*-
"""
Main function to build recommendation systems.
Created on 2018-04-16
@author: fuxuemingzhu
"""
import utils
from ItemCF import ItemBasedCF
from LFM import LFM
from UserCF import UserBasedCF
from dataset import DataSet
from most_popular import MostPopular
from random_pred import RandomPredict
from utils import LogTime
def run_model(model_name, dataset_name, test_size=0.3, clean=False):
print('*' * 70)
print('\tThis is %s model trained on %s with test_size = %.2f' % (model_name, dataset_name, test_size))
print('*' * 70 + '\n')
model_manager = utils.ModelManager(dataset_name, test_size)
try:
trainset = model_manager.load_model('trainset')
testset = model_manager.load_model('testset')
except OSError:
ratings = DataSet.load_dataset(name=dataset_name)
trainset, testset = DataSet.train_test_split(ratings, test_size=test_size)
model_manager.save_model(trainset, 'trainset')
model_manager.save_model(testset, 'testset')
'''Do you want to clean workspace and retrain model again?'''
'''if you want to change test_size or retrain model, please set clean_workspace True'''
model_manager.clean_workspace(clean)
if model_name == 'UserCF':
model = UserBasedCF()
elif model_name == 'ItemCF':
model = ItemBasedCF()
elif model_name == 'Random':
model = RandomPredict()
elif model_name == 'MostPopular':
model = MostPopular()
elif model_name == 'UserCF-IIF':
model = UserBasedCF(use_iif_similarity=True)
elif model_name == 'ItemCF-IUF':
model = ItemBasedCF(use_iuf_similarity=True)
elif model_name == 'LFM':
# K, epochs, alpha, lamb, n_rec_movie
model = LFM(10, 20, 0.1, 0.01, 10)
else:
raise ValueError('No model named ' + model_name)
model.fit(trainset)
recommend_test(model, [1, 100, 233, 666, 888])
model.test(testset)
def recommend_test(model, user_list):
for user in user_list:
recommend = model.recommend(str(user))
print("recommend for userid = %s:" % user)
print(recommend)
print()
if __name__ == '__main__':
main_time = LogTime(words="Main Function")
dataset_name = 'ml-100k'
# dataset_name = 'ml-1m'
# model_type = 'UserCF'
# model_type = 'UserCF-IIF'
# model_type = 'ItemCF'
model_type = 'Random'
# model_type = 'MostPopular'
# model_type = 'ItemCF-IUF'
# model_type = 'LFM'
test_size = 0.1
run_model(model_type, dataset_name, test_size, False)
main_time.finish()