forked from Osmik/SAS-Recommendation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn_user.sas
427 lines (313 loc) · 9.05 KB
/
knn_user.sas
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**************************************************************************************/
LIBNAME reco'/folders/myfolders/KNN/Data'; /* Data directory specification */
%let InDS= reco._base; /* Basic DataSet */
%let RandomSeed = 955; /* Dividing random number seed) */
%let k=80; /* default 50 */ /* Count of nearest neighbors to find */
%let DistanceMethod=cosine; /* Distance measure method */
%let N=2; /* default 20 */ /* number of principal components to be computed*/
/**************************************************************************************/
/*** Sampling - divide to training (L) and testing(T) ***/
data reco.Sample;
set &InDS;
if _n_=1 then
call streaminit(&randomseed);
U=rand('uniform');
if U<=0.80
then DevSample='L';
else DevSample='T';
run;
/* Sort */
proc sort data=reco.Sample;
by UserId ItemId;
run;
/*Max & Min Ratings*/
Title3 "Max Rating";
Proc sql noprint;
select max(rating)
into: MaxRating
from &InDS;
quit;
Title3 "Min Rating";
Proc sql noprint;
select min(rating)
into: MinRating
from &InDS;
quit;
/*AVG Rating to variable: AvgRating */
Title3 "AVG DataSet Rating";
proc sql;
SELECT AVG(Rating)
Into: AvgRating
FROM reco.Sample
where DevSample= "L";
quit;
/*AVG Item Rating */
proc sql;
create table reco.AVG_ItemID as
select ItemID, avg (Rating) as AvgRatingOnItem
from reco.Sample
where DevSample = "L" group by ItemID;
quit;
/*AVG User Rating */
proc sql;
create table reco.AVG_UserID as
select UserID, avg (Rating) as AvgUserRating, avg (Rating) - &AvgRating as Bias
from reco.Sample
where DevSample = "L" group by UserID;
quit;
/*** Sparse to dense ***/
proc iml;
/* Read data*/
use reco.sample;
read all var{Rating UserId ItemId} where(DevSample="L");
close;
/* combine UserId ItemId Rating into a sparse matrix */
/* Value || Row || Col */
sparse = Rating || UserId || ItemId;
/* Conversion sparse to dense matrix*/
/* ItemID */
/* UserID Rating */
dense = full(sparse);
/* Store data */
create reco.base_dense from dense;
append from dense;
close reco.base_dense;
quit;
/*** Rating: Set missing values instead 0 ***/
data reco.base_dense_null;
set reco.base_dense;
array nums _numeric_;
do over nums;
if nums=0 then nums=.;
end;
run;
/* Rating normalization: Substract userAVG from rating ************************************************ NORMALIZATION */
/* base_dense_normalized OUT */
/* base_dense_null IN */
proc iml;
use reco.base_dense_null;
read all into ratings;
close;
use reco.AVG_ItemID;
read all into avgs;
close;
do user = 1 to nrow(ratings);
do item = 1 to ncol(ratings);
if ratings [user,item] ^=. then do;
ratings [user,item] = ratings [user,item] - avgs [user, 2];
end;
end;
end;
create reco.base_dense_normalized from ratings ;
append from ratings ;
close reco.base_dense_normalized ;
quit;
/* Item AVG to Missing rating */ /******************************************************************* null >>> ItemAVG */
/* base_dense_avged OUT */
/* base_dense_null IN */
proc iml;
use reco.base_dense_null;
read all into rating;
close;
do item = 1 to ncol(rating);
itemAVG = /*mean(rating[ ,item])*/ sum(rating[ ,item])/countn(rating[ ,item]);
do replacement = 1 to nrow(rating);
if rating [replacement ,item] =. then do;
rating [replacement ,item] = itemAVG ;
end;
end;
end;
create reco.base_dense_avged from rating ;
append from rating ;
close reco.base_dense_avged;
quit;
/* Replace missing when no one has ever watched the movie */
data reco.base_dense_avged;
set reco.base_dense_avged;
array nums _numeric_;
do over nums;
if nums=. then nums=&AvgRating;
end;
run;
/* Ridit – transform rating to 0-1 interval ************************************************************ RIDIT */
/* base_dense_ridit OUT */
/* base_dense_null IN */
PROC FREQ DATA=reco.sample(where=(DevSample="L"))
ORDER=INTERNAL
noprint;
TABLES Rating /
OUT=work.OneWay
SCORES=ridit;
by UserId;
RUN;
data reco.ridit;
set work.OneWay;
retain cumsum;
if UserId ^= lag(UserId) then cumsum = 0;
cumsum = cumsum + percent;
ridit = (cumsum - percent/2)/100;
run;
proc sql;
create table reco.ridit_sparse as
select a.UserID
, a.ItemID
, a.rating
, b.ridit
from reco.sample a
left join reco.ridit b
on a.UserID = b.UserID and a.rating = b.rating
where DevSample="L";
quit;
proc iml;
use reco.ridit_sparse;
read all var{ridit UserId ItemId};
close;
sparse = ridit|| UserId || ItemId;
dense = full(sparse);
create reco.ridit_dense from dense;
append from dense;
close reco.ridit_dense;
quit;
/* optional: SVD for euclid distance ***************************************************************** SVD */
/* Dimensionality reduction **********/
proc princomp
data=reco.base_dense
out=work.base_svd
noprint
cov
n=&N;
var Col1-Col1000; /* 1000 original: Should use all ItemIds */
run;
proc iml;
use work.base_svd ;
read all var _num_ into inputData;
close;
principal = inputData[, 1683:ncol(inputData)];
create reco.svd from principal ;
append from principal ;
close reco.svd;
quit;
/* Users distances ************************************************************************************ DISTANCE */
proc distance
SHAPE=SQR
REPONLY
data=reco.ridit_dense /* ridit*/ /* avged */ /* normalized */
method= &DistanceMethod
out=reco.distance;
var ratio /*interval*/ (Col1-Col1682);
run;
/* Remove diagonal distances */
proc iml;
use reco.distance;
read all into inputData;
close;
do d = 1 to ncol(inputData);
inputData[d,d] =. ;
end;
create reco.distance_diag from inputData;
append from inputData;
close reco.distance_diag ;
quit;
/*** k-NN *************************************************************************************************** k-NN */
proc iml;
/* Read data */
use reco.base_dense_avged ;
read all var _num_ into rating;
close;
use reco.distance_diag;
read all var _all_ into distances;
close;
/* Settings */
k = &k;
/* Initialisation */
nearestNeighbor = J(nrow(rating ), k, 0); /* Matrix of nearest neighbors */
recommendation = J(nrow(rating), ncol(rating), 0); /* Matrix with the estimated rating */
distance = J(nrow(rating ), 1, 0 ) /*10**10*/; /* Vector of distances *************************************************************/
/* Loop - get the nearest neighbours */
do reference = 1 to nrow(rating );
distance = distances[ ,reference];
/* Sort distances in ascending (1–N) order, return indices */
call sortndx(ndx, distance, {1}, {1} /*,descend*/);
/* Store k nearest neighbours */
nearestNeighbor[reference,] = ndx[1:k]`; /**/
/* Get recommendation (average recommendation of the nearest neighbours) */
recommendation[reference,] = mean(rating[nearestNeighbor[reference,],]);
end;
/* Convert dense to sparse matrix */
result = sparse(recommendation);
/* Store data */
create reco.knn_all from result;
append from result;
close reco.knn_all;
quit;
/* Rename columns */
proc datasets library=reco nolist;
modify knn_all;
rename Col1 = PredRating;
rename Col2 = UserId;
rename Col3 = ItemId;
quit;
/* Debias k-NN rating prediction and bound to limits */
data reco.knn_all_debiased (keep=UserID ItemID PredRating PredRatingBounded);
merge reco.knn_all (keep=UserID ItemID PredRating in=a)
reco.AVG_UserID (keep=UserId Bias in=b);
by UserId;
if a & b;
PredRating = PredRating + Bias;
PredRatingBounded = min(max(PredRating, &MinRating ),&MaxRating);
run;
/******************************************************/
/********************* EVALUATION *********************/
/******************************************************/
/* Data to evaluate*/
%let PredRating = PredRatingBounded; /* PredRatingBounded */
%let tableName = reco.knn_all_debiased; /* reco.knn_all_debiased_II */
/* Tell SAS that the table is sorted to accelerate subsequent queries */
proc sort data=&tableName presorted;
by UserId ItemId;
run;
/* Merge target and prediction & calculate Square Error */
data reco.rmse_merged(keep=SquareDiff);
merge reco.sample(keep=UserId ItemID Rating DevSample where=(DevSample="T") in=a)
&tableName(keep=UserId ItemID &PredRating in=b);
by UserId ItemID;
if a & b;
SquareDiff = (Rating-&PredRating)*(Rating-&PredRating);
Diff = round(sqrt((Rating-&PredRating )*(Rating-&PredRating )));
run;
/* Save rounded Predictions */
proc sql;
create table reco.rmse_success as
select round(sqrt(SquareDiff)) as Diff
from reco.rmse_merged;
quit;
Title3 "RMSE";
/* Print RMSE */
proc sql;
select sqrt(avg(SquareDiff))
into: RMSE
from reco.rmse_merged;
quit;
/* Report Prediction Succes */
Title3 "Differences: Success, Diff1-Diff4, SUM";
proc iml;
use reco.rmse_success ;
read all var {Diff} into diff;
close;
t = countn (diff);
counts = J(2, 6, 0);
do d = 1 to 5;
c=0;
do dr = 1 to nrow(diff);
if diff[dr] = d-1 then do;
c = c+1;
end;
end;
p = 100 * c / t;
counts[1,d] = c;
counts[2,d] = p;
end;
counts[1,6] = sum (counts [1, ]);
counts[2,6] = sum (counts [2, ]);
print counts;
quit;