-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollaborativeFiltering.java
152 lines (147 loc) · 6.91 KB
/
CollaborativeFiltering.java
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
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* Created by dakle on 21/3/17.
*/
public class CollaborativeFiltering {
public static void main(String[] args) {
DataManager manager = new DataManager(args[0]);
manager.generateData();
System.out.println("Data loaded successfully!");
calculateWeights(manager);
System.out.println("Offline process complete!");
System.out.println("Starting online process!");
testCF(manager, args[1], Integer.parseInt(args[2]));
}
public static void calculateWeights(DataManager manager) {
float au_mean_vote, ou_mean_vote;
short auMovieId[], ouMovieId[];
byte auRating[], ouRating[];
float au_error, ou_error;
short weights[];
double numerator, denominator1, denominator2, denominator;
Double weight;
StorageManager manager1 = new StorageManager("output.txt");
manager1.openFile();
User activeUser, user;
for (Integer activeUserId: manager.userIdSet) {
activeUser = manager.userList.get(manager.userMap.get(activeUserId));
au_mean_vote = activeUser.mean_vote;
auMovieId = activeUser.movieIds;
auRating = activeUser.ratings;
weights = new short[manager.userIdSet.size()];
for(Integer userId: manager.userIdSet) {
if(activeUserId < userId) {
user = manager.userList.get(manager.userMap.get(userId));
ou_mean_vote = user.mean_vote;
ouMovieId = user.movieIds;
ouRating = user.ratings;
numerator = denominator1 = denominator2 = denominator = 0;
if (auMovieId.length > ouMovieId.length) {
for (int i = 0, j = 0; i < ouMovieId.length; i++) {
while (j < auMovieId.length && auMovieId[j] < ouMovieId[i]) {
j++;
}
if(j >= auMovieId.length) break;
if (ouMovieId[i] == auMovieId[j]) {
au_error = auRating[j] - au_mean_vote;
ou_error = ouRating[i] - ou_mean_vote;
numerator += (au_error * ou_error);
denominator1 += au_error * au_error;
denominator2 += ou_error * ou_error;
j++;
}
}
} else {
for (int i = 0, j = 0; i < auMovieId.length; i++) {
while (j < ouMovieId.length && ouMovieId[j] < auMovieId[i]) {
j++;
}
if(j >= auMovieId.length) break;
if (ouMovieId[j] == auMovieId[i]) {
au_error = auRating[i] - au_mean_vote;
ou_error = ouRating[j] - ou_mean_vote;
numerator += (au_error * ou_error);
denominator1 += (au_error * au_error);
denominator2 += (ou_error * ou_error);
j++;
}
}
}
denominator = Math.sqrt(denominator1 * denominator2);
if(numerator != 0 && denominator != 0) {
weight = (numerator / denominator) * 1000;
weights[manager.userMap.get(userId)] = weight.shortValue();
}
}
}
manager1.writeLine(weights);
}
manager1.closeFile();
}
public static void testCF(DataManager manager, String testFilePath, int userCount) {
DataManager manager1 = new DataManager(testFilePath);
RetrievalManager manager2 = new RetrievalManager("output.txt");
manager2.openFile();
String[] splitLine;
short[] userWeights = new short[manager.userIdSet.size()];
int[] movieUsers;
byte[] userRatings;
int userId;
short movieId;
Double dRating, mae = 0.0, rms = 0.0;
double kappa;
byte rating;
double predictedRating;
float mean_vote;
int counter = 0;
if(manager1.openFile()) {
String line;
double diff;
while ((line = manager1.getLine()) != null) {
counter++;
splitLine = line.split(",");
movieId = Short.parseShort(splitLine[0]);
userId = Integer.parseInt(splitLine[1]);
dRating = Double.parseDouble(splitLine[2]);
rating = dRating.byteValue();
predictedRating = 0;
kappa = 0;
movieUsers = manager.movieList.get(manager.movieMap.get(movieId)).userIds;
userRatings = manager.movieList.get(manager.movieMap.get(movieId)).ratings;
for(int i = 0; i < movieUsers.length; i++) {
if(userId > movieUsers[i]) {
userWeights = byteToShort(manager2.readBytes(2, (manager.userIdSet.headSet(movieUsers[i]).size() * userWeights.length * 2) + (manager.userMap.get(userId) + 2)));
}
else {
userWeights = byteToShort(manager2.readBytes(2, (manager.userIdSet.headSet(userId).size() * userWeights.length * 2) + (manager.userMap.get(movieUsers[i]) + 2)));
}
kappa += userWeights[0];
mean_vote = manager.userList.get(manager.userMap.get(movieUsers[i])).mean_vote;
predictedRating += ((userWeights[0] / 1000) * (userRatings[i] - mean_vote));
}
predictedRating = manager.userList.get(manager.userMap.get(userId)).mean_vote + ((1 / kappa) * predictedRating);
diff = predictedRating - rating;
mae += Math.abs(diff);
rms += (diff * diff);
System.out.println(line);
System.out.print("Absolute Error: " + Math.abs(diff));
System.out.print(" Squared Error: " + (diff * diff) + ".\n");
if(userCount > 0 && counter == userCount) {
break;
}
}
manager1.closeFile();
mae = mae / counter;
rms = Math.sqrt(rms / counter);
System.out.print("Final Mean Absolute Error: " + mae);
System.out.print(" Final Root Mean Squared Error: " + rms + ".\n");
}
manager2.closeFile();
}
public static short[] byteToShort(byte[] arr) {
short[] shorts = new short[arr.length/2];
ByteBuffer.wrap(arr).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
return shorts;
}
}