Skip to content

Commit bcbc9ca

Browse files
committed
add one-class classification example
1 parent 0081bcc commit bcbc9ca

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

examples/oneClass.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
function oneClass(SVM) {
4+
let svm = new SVM({
5+
kernel: SVM.KERNEL_TYPES.RBF,
6+
type: SVM.SVM_TYPES.ONE_CLASS,
7+
gamma: 1,
8+
cost: 1,
9+
nu: 0.1
10+
});
11+
const features = [[0, 0], [1, 1], [1, 0], [0, 1]];
12+
const toPredict = [[0.5, 0.5], [1.5, 1]];
13+
const expected = [1, -1];
14+
const labels = [0, 0, 0, 0];
15+
svm.train(features, labels);
16+
for (let i = 0; i < toPredict.length; i++) {
17+
const pred = svm.predictOne(toPredict[i]);
18+
console.log(`pred: ${pred}, expected: ${expected[i]}`);
19+
}
20+
}
21+
22+
async function execWasm() {
23+
console.log('wasm');
24+
let SVM;
25+
try {
26+
SVM = await require('../wasm');
27+
} catch (e) {
28+
console.log(e);
29+
}
30+
oneClass(SVM);
31+
}
32+
33+
try {
34+
execWasm(); // Asynchronous
35+
} catch (e) {
36+
console.log('failed');
37+
console.log(e);
38+
}

0 commit comments

Comments
 (0)