-
Notifications
You must be signed in to change notification settings - Fork 3
/
recommender.cpp
229 lines (186 loc) · 6.15 KB
/
recommender.cpp
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>
#include <map>
using namespace std;
#define EMPTY -2.0
#define ll long lon
#define MAT vector<vector<double> >
class CF {
private:
MAT userToItem, itemToItem;
vector<double> average;
int itemSize, userSize;
public:
CF(MAT userToItem) {
this->userToItem = userToItem;
userSize = (int)userToItem.size()-1;
itemSize = (int)userToItem.back().size()-1;
average = vector<double> (userSize+1, 0.0);
}
void run () {
buildItemToItemC();
}
void getAverage() {
for(int i=1;i<=userSize;i++) {
int cnt = 0;
for(int j=1;j<=itemSize;j++) {
if(userToItem[i][j] == EMPTY) continue;
cnt++;
average[i] += userToItem[i][j];
}
average[i] /= (double)cnt;
}
}
void buildItemToItemC() {
itemToItem = vector<vector<double> > (itemSize+1, vector<double> (itemSize+1));
for(int i=1; i<= itemSize; i++) {
for(int j=1; j <= itemSize; j++) {
double top = 0, bleft =0, bright = 0;
int cnt = 0;
for(int k=1; k<= userSize; k++) {
if(userToItem[k][i] == EMPTY) continue;
if(userToItem[k][j] == EMPTY) continue;
cnt++;
top += userToItem[k][i]*userToItem[k][j];
bleft += userToItem[k][i]*userToItem[k][i];
bright += userToItem[k][j]*userToItem[k][j];
}
if(cnt < 1) {
itemToItem[i][j] = 0;
} else {
itemToItem[i][j] = top/(sqrt(bleft)*sqrt(bright));
}
}
}
}
void buildItemToItemP() {
getAverage();
itemToItem = vector<vector<double> > (itemSize+1, vector<double> (itemSize+1));
for(int i=1; i<= itemSize; i++) {
for(int j=1; j <= itemSize; j++) {
double top = 0, bleft =0, bright = 0;
int cnt = 0;
for(int k=1; k<= userSize; k++) {
if(userToItem[k][i] == EMPTY) continue;
if(userToItem[k][j] == EMPTY) continue;
cnt++;
top += (userToItem[k][i]-average[k])*(userToItem[k][j]-average[k]);
bleft += (userToItem[k][i]-average[k])*(userToItem[k][i]-average[k]);
bright += (userToItem[k][j]-average[k])*(userToItem[k][j]-average[k]);
}
if(cnt < 1) {
itemToItem[i][j] = 0;
} else {
itemToItem[i][j] = top/(sqrt(bleft)*sqrt(bright));
}
}
}
}
int predict(int user, int item) {
double top = 0;
double bottom = 0;
// given untrained items
if(item > itemSize) {
return 3;
}
for(int i=1;i<=itemSize; i++) {
if(userToItem[user][i] == EMPTY) continue;
bottom += abs(itemToItem[item][i]);
top += itemToItem[item][i]*userToItem[user][i];
}
if(bottom == 0) {
return 3;
}
double rating = top/bottom;
// return rating;
return rating+0.5;
}
};
struct inputTuple{
int user, item, rating;
};
class InputReader {
private:
ifstream fin;
MAT userToItem;
vector<inputTuple> input;
public:
InputReader(string filename) {
fin.open(filename);
if(!fin) {
cout << filename << " file could not be opened\n";
exit(0);
}
parse();
}
void parse() {
int maxUser = 0, maxItem = 0;
int user, item, rating, timeStamp;
while(!fin.eof()) {
fin >> user >> item >> rating >> timeStamp;
input.push_back({user, item, rating});
maxUser = max(maxUser, user);
maxItem = max(maxItem, item);
}
input.pop_back();
userToItem = vector<vector<double> > (maxUser+1, vector<double> (maxItem+1, EMPTY));
for(int i=0;i<input.size();i++) {
user = input[i].user, item = input[i].item, rating = input[i].rating;
userToItem[user][item]= rating;
}
}
vector<inputTuple> getInput() {
return input;
}
MAT getUserToItem() {
return userToItem;
}
};
class OutputPrinter {
private:
ofstream fout;
string filename;
public:
OutputPrinter(string filename) {
this->filename = filename;
fout.open(filename);
if(!fout) {
cout << filename << " file could not be writed\n";
exit(0);
}
}
void addLine(int user, int item, double rating) {
fout << user << "\t" << item << "\t" << rating << endl;
}
};
int main(int argc, const char * argv[]) {
if(argc!=3) {
cout << "Please follow this format. recommender.exe [base file name] [test file name]";
return 0;
}
string baseFileName(argv[1]);
string testFileName(argv[2]);
InputReader baseInputReader(baseFileName);
MAT userToItem = baseInputReader.getUserToItem();
CF cf(userToItem);
cf.run();
InputReader testInputReader(testFileName);
vector<inputTuple> test = testInputReader.getInput();
OutputPrinter outputPrinter(testFileName.substr(0, 2)+".base_prediction.txt");
double RSME = 0.0;
for(int i=0;i<test.size();i++) {
int user = test[i].user;
int item = test[i].item;
int rating = test[i].rating;
double predict = cf.predict(user, item);
RSME += (predict-rating)*(predict-rating);
outputPrinter.addLine(user, item, predict);
}
RSME /= (double)test.size();
RSME = sqrt(RSME);
cout << "RSME :" << RSME << endl;
return 0;
}