-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_BMF.py
61 lines (48 loc) · 1.61 KB
/
gen_BMF.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
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 21 19:54:55 2015
@author: thalita
Generate BMFs with diferent coverages and binarization thresholds
"""
import data.MovieLens100k.dbread as dbread
from databases import MatrixDatabase
from evaluation import HoldoutBMF, HoldoutRatingsView
import recommender as rec
from multiprocessing import Pool
import sys
import traceback
from numpy.random import shuffle
if 'parallel' in set(sys.argv):
PARALLEL = True
else:
PARALLEL = False
coverages = [1, 0.8, 0.6]
bin_thresh = [i for i in range(0, 5)]
RS_type = rec.BMFrecommender
RS_arguments = [{'threshold': t,
'min_coverage': coverage}
for t in bin_thresh
for coverage in coverages]
shuffle(RS_arguments)
result_folder = '/dev/null'
database = MatrixDatabase(dbread.read_matrix())
holdout_view = HoldoutRatingsView(database, dbread.PATH, nsplits=1,
pct_hidden=0.2, threshold=3)
def run(i):
global holdout_view, RS_type, RS_arguments, result_folder
print('Running %d' % i + str(RS_arguments[i]))
evalu = HoldoutBMF(holdout_view, RS_type, RS_arguments[i],
result_folder, threshold=3, topk=20)
try:
print('Training %d' % i + str(RS_arguments[i]))
evalu.train()
print('Done training %d' % i + str(RS_arguments[i]))
except:
with open(evalu.fname_prefix+'_error_log_%d.out' % i, 'w') as f:
traceback.print_exception(*sys.exc_info(), file=f)
if PARALLEL:
pool = Pool()
pool.map(run, range(len(RS_arguments)))
else:
for i in range(len(RS_arguments)):
run(i)