-
Notifications
You must be signed in to change notification settings - Fork 1
/
results_TPS.py
85 lines (71 loc) · 2.59 KB
/
results_TPS.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
@author: ogouvert
"""
import os
import glob
import pandas as pd
import cPickle as pickle
import numpy as np
from function.train_test import divide_train_test
from model.dcpf_Log import dcpf_Log
from model.dcpf_ZTP import dcpf_ZTP
from model.dcpf_Geo import dcpf_Geo
from model.dcpf_sNB import dcpf_sNB
from function import rec_eval
#%%
folder_name = 'tps'
path = 'out/'+ folder_name
#%% DATA
seed_test = 1992
prop_test = 0.2
# Pre-processed data
with open('data/tps/tps_12345_U1.63e+04_I1.21e+04_min_uc20_sc20', 'rb') as f:
out = pickle.load(f)
Y = out['Y_listen']
Y_train,Y_test = divide_train_test(Y,prop_test=prop_test,seed=seed_test)
#%% Calculate scores
for filename in glob.glob(os.path.join(path,'*')):
print filename
with open(filename,'rb') as f:
model = pickle.load(f)
W = model.Ew
H = model.Eh
#model.score={} - erase the score
for s in [0,1,2,5]:
if ~np.isin('ndcg@100s'+str(s), model.score.keys()):
ndcg = rec_eval.normalized_dcg_at_k(Y_train>0,Y_test>s,W,H,k=100)
model.score['ndcg@100s'+str(s)]=ndcg
for k in [100]:
if ~np.isin('prec_at_'+str(k), model.score.keys()):
prec = rec_eval.prec_at_k(Y_train>0,Y_test>0,W,H,k=k)
model.score['prec_at_'+str(k)]=prec
for k in [100]:
if ~np.isin('recall_at_'+str(k), model.score.keys()):
recall = rec_eval.recall_at_k(Y_train>0,Y_test>0,W,H,k=k)
model.score['recall_at_'+str(k)]=recall
model.save_dir = path
model.save_model()
#%% Read scores
appended_data = []
for filename in glob.glob(os.path.join(path,'*')):
with open(filename,'rb') as f:
model = pickle.load(f)
df_name = pd.DataFrame.from_dict([{'filename':filename, 'classname':model.classname}])
df_init = pd.DataFrame.from_dict([model.saved_args_init])
df_fit = pd.DataFrame.from_dict([model.saved_args_fit])
df_score = pd.DataFrame.from_dict([model.score])
df_loc = pd.concat([df_name,df_init,df_fit,df_score], axis=1)
df_loc['p'] = model.p
df_loc['opt_hyper'] = '_'.join(sorted(model.opt_hyper))
appended_data.append(df_loc)
if appended_data!=[]:
df = pd.concat(appended_data, axis=0)
df_grid = df[df['opt_hyper']=='beta']
df_learn = df[df['opt_hyper']=='beta_p']
#%%
res_grid = df_grid.groupby(['classname','p']).agg(['mean']).reset_index()
res_grid.columns = ["_".join(x) for x in res_grid.columns.ravel()]
res_model = df_learn.groupby(['classname']).agg(['mean']).reset_index()
res_model.columns = ["_".join(x) for x in res_model.columns.ravel()]