-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp_4.1_test.py
88 lines (70 loc) · 2.61 KB
/
exp_4.1_test.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
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 5 15:21:43 2015
@author: thalita
Experiment 4.1
Ensemble of BMF through RPs of different dimensions
re-using BMFRP from exp 3
"""
import data.MovieLens100k.dbread as dbread
from databases import MatrixDatabase
from evaluation import HoldoutBMF, HoldoutRatingsView
import recommender as rec
import ensemble as ens
from itertools import chain
import os
import sys
import traceback
from pickle import load
RP_folder = 'results/exp_3_results/'
trained_models = [RP_folder + f for f in os.listdir(RP_folder)
if f.find('trained.pkl') > -1]
result_folder = 'results/exp_4.1_results/'
database = MatrixDatabase(dbread.read_matrix())
holdout_view = HoldoutRatingsView(database, dbread.PATH, nsplits=1,
pct_hidden=0.2, threshold=3)
if not os.path.isdir(result_folder):
os.makedirs(result_folder)
RS_type = [ens.AvgRatingEnsemble, ens.RankSumEnsemble, ens.MajorityEnsemble]
dim_red = [0.25, 0.5, 0.75, 0.8, 0.9]
nneighbor = chain([5], range(10,61,10))
RP_type = ['sparse', 'gaussian']
neighbor_type = ['user','item']
args = [(nn, rp, nntype)
for nn in nneighbor
for rp in RP_type
for nntype in neighbor_type]
for nn, rp, nntype in args:
files = [f for f in trained_models
if f.find('nneighbors_%d'%nn) > -1
if f.find('RPtype_%s'%rp) > -1
if f.find('neighbortype_%s'%nntype) > -1]
if files != []:
RS_list = []
for d in dim_red:
fname = [f for f in files
if f.find('dimred_%d'%d) > -1][0]
with open(fname, 'rb') as f:
RS_list.append(load(f))
def factory(**RS_args):
global RS_list
return RS_list
RS_arguments = {'RS_factory': factory,
'n_neighbors': nn,
'threshold': 3,
'RP_type': rp}
for i in range(len(RS_type)):
print('Running %d' % i + str(RS_type[i]))
evalu = HoldoutBMF(holdout_view, RS_type[i], RS_arguments,
result_folder, threshold=3, topk=20)
print('Training %d' % i + str(RS_type[i]))
evalu.train()
print('Done training %d' % i + str(RS_type[i]))
print('Testing %d' % i + str(RS_type[i]))
evalu.test()
print('Done testing %d' % i + str(RS_type[i]))
try:
pass
except:
with open(evalu.fname_prefix+'_error_log_%d.out' % i, 'w') as f:
traceback.print_exception(*sys.exc_info(), file=f)