-
Notifications
You must be signed in to change notification settings - Fork 4
/
nodeml.cf.js
38 lines (29 loc) · 870 Bytes
/
nodeml.cf.js
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
'use strict';
const {sample, CF, evaluation} = require('../index');
// load movie dataset: user_id movie_id rating like
const movie = sample.movie();
// split data as train & test
let train = [], test = [];
for (let i = 0; i < movie.length; i++) {
if (Math.random() > 0.8) test.push(movie[i]);
else train.push(movie[i]);
}
// set data
const cf = new CF();
cf.maxRelatedItem = 50;
cf.maxRelatedUser = 50;
cf.train(train, 'user_id', 'movie_id', 'rating');
// select 100 data for recommendation
let gt = cf.gt(test, 'user_id', 'movie_id', 'rating');
let gtr = {};
let users = [];
for (let user in gt) {
gtr[user] = gt[user];
users.push(user);
if (users.length === 100) break;
}
// recommend for 100 users, each 40 movie
let result = cf.recommendToUsers(users, 40);
// evaluate by ndcg
let ndcg = evaluation.ndcg(gtr, result);
console.log(ndcg);