Uh oh!
There was an error while loading. Please reload this page.
forked from cjlin1/libmf
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmf-predict.cpp
More file actions
Latest commit
207 lines (189 loc) · 5.26 KB
/
Copy pathmf-predict.cpp
File metadata and controls
207 lines (189 loc) · 5.26 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
#include<stdexcept>
#include<vector>
#include"mf.h"
usingnamespacestd;
usingnamespacemf;
structOption
{
Option() : eval(RMSE) {}
string test_path, model_path, output_path;
mf_int eval;
};
string predict_help()
{
returnstring(
"usage: mf-predict [options] test_file model_file [output_file]\n"
"\n"
"options:\n"
"-e <eval>: specify the evaluation criterion (default 0)\n"
"\t 0 -- root mean square error\n"
"\t 1 -- mean absolute error\n"
"\t 2 -- generalized KL-divergence\n"
"\t 5 -- logarithmic error\n"
"\t 6 -- accuracy\n"
"\t10 -- row-wise mean percentile rank\n"
"\t11 -- column-wise mean percentile rank\n"
"\t12 -- row-wise area under the curve\n"
"\t13 -- column-wise area under the curve\n");
}
Option parse_option(int argc, char **argv)
{
vector<string> args;
for(int i = 0; i < argc; i++)
args.push_back(string(argv[i]));
if(argc == 1)
throwinvalid_argument(predict_help());
Option option;
mf_int i;
for(i = 1; i < argc; i++)
{
if(args[i].compare("-e") == 0)
{
if((i+1) >= argc)
throwinvalid_argument("need to specify evaluation criterion after -e");
i++;
option.eval = atoi(argv[i]);
if(option.eval != RMSE &&
option.eval != MAE &&
option.eval != GKL &&
option.eval != LOGLOSS &&
option.eval != ACC &&
option.eval != ROW_AUC &&
option.eval != COL_AUC &&
option.eval != ROW_MPR &&
option.eval != COL_MPR)
throwinvalid_argument("unknown evaluation criterion");
}
else
break;
}
if(i >= argc-1)
throwinvalid_argument("testing data and model file not specified");
option.test_path = string(args[i++]);
option.model_path = string(args[i++]);
if(i < argc)
{
option.output_path = string(args[i]);
}
elseif(i == argc)
{
constchar *ptr = strrchr(&*option.test_path.begin(), '/');
if(!ptr)
ptr = option.test_path.c_str();
else
++ptr;
option.output_path = string(ptr) + ".out";
}
else
{
throwinvalid_argument("invalid argument");
}
return option;
}
voidpredict(string test_path, string model_path, string output_path, mf_int eval)
{
mf_problem prob = read_problem(test_path);
ofstream f_out(output_path);
if(!f_out.is_open())
throwruntime_error("cannot open " + output_path);
mf_model *model = mf_load_model(model_path.c_str());
if(model == nullptr)
throwruntime_error("cannot load model from " + model_path);
for(mf_int i = 0; i < prob.nnz; i++)
{
mf_float r = mf_predict(model, prob.R[i].u, prob.R[i].v);
f_out << r << endl;
}
switch(eval)
{
caseRMSE:
{
auto rmse = calc_rmse(&prob, model);
cout << fixed << setprecision(4) << "RMSE = " << rmse << endl;
break;
}
caseMAE:
{
auto mae = calc_mae(&prob, model);
cout << fixed << setprecision(4) << "MAE = " << mae << endl;
break;
}
caseGKL:
{
auto gkl = calc_gkl(&prob, model);
cout << fixed << setprecision(4) << "GKL = " << gkl << endl;
break;
}
caseLOGLOSS:
{
auto logloss = calc_logloss(&prob, model);
cout << fixed << setprecision(4) << "LOGLOSS = " << logloss << endl;
break;
}
caseACC:
{
auto acc = calc_accuracy(&prob, model);
cout << fixed << setprecision(4) << "ACCURACY = " << acc << endl;
break;
}
caseROW_AUC:
{
auto row_wise_auc = calc_auc(&prob, model, false);
cout << fixed << setprecision(4) << "Row-wise AUC = " << row_wise_auc << endl;
break;
}
caseCOL_AUC:
{
auto col_wise_auc = calc_auc(&prob, model, true);
cout << fixed << setprecision(4) << "Colmn-wise AUC = " << col_wise_auc << endl;
break;
}
caseROW_MPR:
{
auto row_wise_mpr = calc_mpr(&prob, model, false);
cout << fixed << setprecision(4) << "Row-wise MPR = " << row_wise_mpr << endl;
break;
}
caseCOL_MPR:
{
auto col_wise_mpr = calc_mpr(&prob, model, true);
cout << fixed << setprecision(4) << "Column-wise MPR = " << col_wise_mpr << endl;
break;
}
default:
{
throwinvalid_argument("unknown evaluation criterion");
break;
}
}
mf_destroy_model(&model);
}
intmain(int argc, char **argv)
{
Option option;
try
{
option = parse_option(argc, argv);
}
catch(invalid_argument &e)
{
cout << e.what() << endl;
return1;
}
try
{
predict(option.test_path, option.model_path, option.output_path, option.eval);
}
catch(runtime_error &e)
{
cout << e.what() << endl;
return1;
}
return0;
}