Skip to content

Commit 0013cef

Browse files
author
maasencioh
committed
implement the kernel part
1 parent ba93442 commit 0013cef

File tree

6 files changed

+130
-47
lines changed

6 files changed

+130
-47
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ build/Release
2525
# Dependency directory
2626
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
2727
node_modules
28+
29+
# generated by webstorm, I think
30+
.idea

.idea/workspace.xml

+42-46
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
exports.svm = require('./svm');
1+
exports.svm = require('./svm');
2+
exports.kernel = require('./kernel');

src/kernel.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
module.exports = function kernel(x1,x2,func,par) {
4+
/**
5+
* Kernel function to return the dot product for different spaces
6+
*
7+
* @param x1: first set of input vectors
8+
* @param x2: second set of input vectors
9+
* @param func: the kind of transformation
10+
* @param par: parameter used in the polynomial and the radial function
11+
* @return ans: calculus of the dot product using the function
12+
* */
13+
14+
func = (typeof func === 'undefined') ? 'lineal' : func;
15+
par = (typeof par === 'undefined') ? 2 : par;
16+
17+
// The dot product between the p1 and p2 vectors
18+
var dot = function (p1, p2) {
19+
if (p1.length !== p2.length) {
20+
console.log("Don't have the same length");
21+
return undefined;
22+
}
23+
var l = p1.length;
24+
var prod = 0;
25+
26+
for (var i = 0; i < l; i++) {
27+
prod += p1[i] * p2[i];
28+
}
29+
30+
return prod;
31+
};
32+
33+
var p = dot(x1,x2);
34+
if (func === 'lineal'){
35+
return p;
36+
}
37+
else if(func === 'polynomial') {
38+
return Math.pow((p + 1), par);
39+
}
40+
else if(func === 'radial') {
41+
var l = x1.length;
42+
var rest = new Array(l);
43+
for (var i = 0; i < l; i++) {
44+
rest[i] = x1[i] - x2[i];
45+
}
46+
var norm = dot(rest, rest);
47+
return Math.exp((norm)/(-2*par*par));
48+
}
49+
else {
50+
return undefined;
51+
}
52+
};

src/svm.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
'use strict';
2+
3+
var kernel = require("./kernel");
14
module.exports = function svm() {
25
return 0;
36
};

test/kernel.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
var kernel = require('../src/kernel.js');
4+
5+
var v1 = [0, 1, 4, 6, 2];
6+
var v2 = [0, 2, 5, 6, 7];
7+
8+
describe('kernel test', function () {
9+
10+
it('no change product', function () {
11+
kernel(v1,v2).should.equal(72);
12+
kernel(v1,v2,"lineal").should.equal(72);
13+
});
14+
15+
it('polynomial product', function () {
16+
kernel(v1,v2,"polynomial").should.equal(5329);
17+
kernel(v1,v2,"polynomial",2).should.equal(5329);
18+
kernel(v1,v2,"polynomial",3).should.equal(389017);
19+
});
20+
21+
it('radial product', function () {
22+
kernel(v1,v2,"radial").should.be.approximately(0.03421811831,0.0001);
23+
kernel(v1,v2,"radial",2).should.be.approximately(0.03421811831,0.0001);
24+
kernel(v1,v2,"radial",3).should.be.approximately(0.22313016014,0.0001);
25+
});
26+
27+
28+
});

0 commit comments

Comments
 (0)