forked from pratyushjagaty/Recommender-System-Mini-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
199 lines (159 loc) · 6.15 KB
/
main.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
#include <bits/stdc++.h>
using namespace std;
const int BASE_MAXN = 80000;
const int TEST_MAXN = 20000;
const int USERS = 943;
const int ITEMS = 1682;
const double BETA = 0.5;
int database[1000][2000];
// This function initializes the training database
void init() {
// open the database and initialize the ratings to 0
freopen("u1_base.txt", "r", stdin);
memset(database, 0, sizeof(database));
// Take input from the base file
// Input format is mentioned below
// UserID | ItemID | Rating | Timestamp
for (int i = 1; i <= BASE_MAXN; ++i) {
int userId, itemId, rating, timeStamp;
cin >> userId >> itemId >> rating >> timeStamp;
database[userId][itemId] = rating;
}
}
// This function calculates the user mean for all the users
// in the training data
void calcUserMean(map<int, double> &userMean) {
for (int user = 1; user <= USERS; ++user) {
int total = 0, count = 0;
for (int item = 1; item <= ITEMS; ++item) {
if (database[user][item]) {
total += database[user][item];
count++;
}
}
if (count) {
userMean[user] = total / (double) count;
}
}
}
// This function calculates the item mean for all the items
// in the training data
void calcItemMean(map<int, double> &itemMean) {
for (int item = 1; item <= ITEMS; ++item) {
int total = 0, count = 0;
for (int users = 1; users <= USERS; ++users) {
if (database[users][item]) {
total += database[users][item];
count++;
}
}
if (count) {
itemMean[item] = total / (double) count;
}
}
}
// This function calculates the user tendency for all the users
// in the training data
void calcUserTendency(map<int, double> &userTendency, map<int, double> &itemMean) {
for (int user = 1; user <= USERS; ++user) {
double tendency = 0.0;
int countItems = 0;
for (int item = 1; item <= ITEMS; ++item) {
if (database[user][item]) {
tendency += (database[user][item] - itemMean[item]);
countItems++;
}
}
userTendency[user] = tendency / countItems;
}
}
// This function calculates the item tendency for all the items
// in the training data
void calcItemTendency(map<int, double> &itemTendency, map<int, double> &userMean) {
for (int item = 1; item <= ITEMS; ++item) {
double tendency = 0.0;
int countUsers = 0;
for (int user = 1; user <= USERS; ++user) {
if (database[user][item]) {
tendency += (database[user][item] - userMean[user]);
countUsers++;
}
}
itemTendency[item] = tendency / countUsers;
}
}
// This function calculates
// MAE,
// RMSE,
// Precision,
// Recall,
// F1-measure
void evaluate(map<int, double> &userMean, map<int, double> &userTendency, map<int, double> &itemMean,
map<int, double> &itemTendency) {
freopen("u1_test.txt", "r", stdin);
double MAE = 0.0, RMSE = 0.0, precision, recall, f1Measure;
int countOfRecommended = 0, countOfRelevant = 0, countOfIntersection = 0;
int count = 0;
for (int i = 1; i <= TEST_MAXN; ++i) {
int userId, itemId, rating, timeStamp;
cin >> userId >> itemId >> rating >> timeStamp;
double tendencyUser = userTendency[userId];
double tendencyItem = itemTendency[itemId];
double predictedRating;
if (tendencyItem >= 0.0 and tendencyUser >= 0.0) {
predictedRating = max(tendencyItem + userMean[userId], tendencyUser + itemMean[itemId]);
} else if (tendencyItem < 0 and tendencyUser < 0) {
predictedRating = min(tendencyItem + userMean[userId], tendencyUser + itemMean[itemId]);
} else if (tendencyItem < 0 and tendencyUser >= 0 and (itemMean[itemId] >= userMean[userId])) {
double temp = BETA * (tendencyItem + userMean[userId]) + (1 - BETA) * (tendencyUser + itemMean[itemId]);
predictedRating = min(max(userMean[userId], temp), itemMean[itemId]);
} else if (tendencyUser >= 0 and tendencyItem < 0 and (itemMean[itemId] < userMean[userId])) {
double temp = BETA * (tendencyItem + userMean[userId]) + (1 - BETA) * (tendencyUser + itemMean[itemId]);
predictedRating = min(max(itemMean[itemId], temp), userMean[userId]);
} else {
predictedRating = userMean[userId] * BETA + (1 - BETA) * itemMean[itemId];
}
if (predictedRating >= 3.0 and rating >= 3.0) {
countOfIntersection++;
countOfRelevant++;
countOfRecommended++;
} else if (predictedRating >= 3.0 and rating < 3.0) {
countOfRecommended++;
} else if (predictedRating < 3.0 and rating >= 3.0) {
countOfRelevant++;
}
MAE += abs(rating - predictedRating);
RMSE += (rating - predictedRating) * (rating - predictedRating);
count++;
//cout << "Actual Rating: " << rating << " Predicted Rating: " << predictedRating << "\n";
}
MAE /= count;
RMSE /= count;
RMSE = sqrt(RMSE);
precision = countOfIntersection / (double) countOfRecommended;
recall = countOfIntersection / (double) countOfRelevant;
f1Measure = (2.0 * precision * recall) / (precision + recall);
cout << "MAE: " << MAE << "\n";
cout << "RMSE: " << RMSE << "\n";
cout << "Precision: " << precision << "\n";
cout << "Recall: " << recall << "\n";
cout << "F1-Measure: " << f1Measure << "\n";
}
int main() {
double startt = clock();
map<int, double> userMean, userTendency;
map<int, double> itemMean, itemTendency;
//initialize the database
init();
//calculate the user mean
calcUserMean(userMean);
// calculate the item mean
calcItemMean(itemMean);
//calculate the user tendency
calcUserTendency(userTendency, itemMean);
// calculate the item tendency
calcItemTendency(itemTendency, userMean);
evaluate(userMean, userTendency, itemMean, itemTendency);
cout << "Time taken: " << (clock() - startt) / CLOCKS_PER_SEC << "s\n";
return 0;
}