/
classification-basic-example.js
67 lines (57 loc) · 1.29 KB
/
classification-basic-example.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
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
/**
Simple example using C-SVC classificator (default) to predict the xor function
Dataset : xor problem
Note : because XOR dataset is to small, we set k-fold paramater to 1 to avoid cross validation
**/
'use strict';
var nodesvm = require('../lib');
var xor = [
[[0, 0], 0],
[[0, 1], 1],
[[1, 0], 1],
[[1, 1], 0]
];
// initialize predictor
var svm = new nodesvm.CSVC({
kFold: 1
});
svm.train(xor)
.spread(function (model, report) {
console.log('SVM trained. \nReport :\n%s', JSON.stringify(report, null, '\t'));
console.log('Lets predict XOR values');
xor.forEach(function(ex){
var prediction = svm.predictSync(ex[0]);
console.log('%d XOR %d => %d', ex[0][0], ex[0][1], prediction);
});
}).done(function () {
console.log('done.');
});
/* OUTPUT
SVM trained.
Report :
{
"accuracy": 1,
"fscore": 1,
"recall": 1,
"precision": 1,
"class": {
"0": {
"precision": 1,
"recall": 1,
"fscore": 1
},
"1": {
"precision": 1,
"recall": 1,
"fscore": 1
}
},
"retainedVariance": 1
}
Lets predict XOR values
0 XOR 0 => 0
0 XOR 1 => 1
1 XOR 0 => 1
1 XOR 1 => 0
done.
*/