Skip to content

Commit

Permalink
global refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspanel committed Dec 19, 2014
1 parent 68650b6 commit 0b98a03
Show file tree
Hide file tree
Showing 58 changed files with 2,466 additions and 2,952 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -15,3 +15,9 @@ npm-debug.log
node_modules
build
.idea

# vagrant
Vagrantfile
.vagrant

test/reports
61 changes: 61 additions & 0 deletions .jshintrc
@@ -0,0 +1,61 @@
{
"predef": [
"console",
"describe",
"it",
"after",
"afterEach",
"before",
"beforeEach"
],

"indent": 4,
"node": true,
"devel": true,

"bitwise": false,
"curly": false,
"eqeqeq": true,
"forin": false,
"immed": true,
"latedef": false,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": true,
"plusplus": false,
"regexp": false,
"undef": true,
"quotmark": "single",
"strict": true,
"trailing": true,
"camelcase": true,

"asi": false,
"boss": true,
"debug": false,
"eqnull": true,
"es5": false,
"esnext": false,
"evil": false,
"expr": false,
"funcscope": false,
"globalstrict": false,
"iterator": false,
"lastsemic": false,
"laxbreak": true,
"laxcomma": false,
"loopfunc": true,
"multistr": false,
"onecase": true,
"regexdash": false,
"scripturl": false,
"smarttabs": false,
"shadow": false,
"sub": false,
"supernew": true,
"validthis": false,

"nomen": false,
"white": true
}
4 changes: 4 additions & 0 deletions .node-svmrc
@@ -0,0 +1,4 @@
{
"reduce" : false,
"timeout": 10000
}
56 changes: 56 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,56 @@
'use strict';

module.exports = function (grunt) {
require('load-grunt-tasks')(grunt);
grunt.initConfig({
gyp: {
addon: {}
},
simplemocha: {
options: {
reporter: 'spec',
timeout: '5000'
},
full: {
src: ['test/**/*.spec.js']
},
short: {
options: {
reporter: 'dot'
},
src: ['test/*.spec.js']
}
},
jshint: {
options: {
jshintrc: '.jshintrc'
},
files: [
'Gruntfile.js',
'bin/*',
'lib/**/*.js',
'test/**/*.js',
'examples/**/*.js',
'!test/reports/**/*'
]
},
exec: {
cover: {
command: 'STRICT_REQUIRE=1 node node_modules/istanbul/lib/cli.js cover --dir ./test/reports node_modules/mocha/bin/_mocha -- -R dot test/**/*.spec.js'
},
coveralls: {
command: 'node node_modules/.bin/coveralls < test/reports/lcov.info'
}
},
watch: {
files: ['<%= jshint.files %>'],
tasks: ['jshint', 'simplemocha:short']
}
});
grunt.registerTask('test', ['jshint', 'gyp:addon', 'simplemocha:full']);
grunt.registerTask('cover', 'exec:cover');
grunt.registerTask('coveralls', 'exec:coveralls');
grunt.registerTask('travis', ['test', 'cover', 'coveralls']);

grunt.registerTask('default', 'test');
};
Empty file added bin/node-svm.js
Empty file.
67 changes: 67 additions & 0 deletions examples/classification-basic-example.js
@@ -0,0 +1,67 @@
/**
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.
*/

Expand Up @@ -12,40 +12,29 @@
**/
'use strict';

var nodesvm = require('../lib/nodesvm'),
_ = require('underscore'),
hd = require("humanize-duration"),
var nodesvm = require('../lib'),
fileName = './examples/datasets/webspam_unigram_subset20000.ds',
nFold= 4,
start = new Date();

var svm = new nodesvm.CSVC({
kernelType: nodesvm.KernelTypes.RBF,
gamma: [8, 2, 0.5],
C: [0.5, 2, 8],
nFold: 3,
// gamma: 8,
// c: 8,
kFold: 4,
normalize: false,
reduce: true, // default value
retainedVariance: 0.99 // default value
});

svm.on('training-progressed', function (progressRate, remainingTime){
console.log('%d% - %s remaining...', progressRate * 100, hd(remainingTime));
});

svm.once('dataset-reduced', function(oldDim, newDim, retainedVar){
console.log('Dataset dimensions reduced from %d to %d features using PCA.', oldDim, newDim);
console.log('%d% of the variance have been retained.', retainedVar* 100);
});

svm.once('trained', function(report){
console.log('SVM trained. report :\n%s', JSON.stringify(report, null, '\t'));
console.log('Total training time : %s', hd(new Date() - start));
process.exit(0);
});

console.log('Start training. May take a while...');
svm.trainFromFile(fileName);
nodesvm.read(fileName)
.then(function (dataset) {
console.log('start training...');
return svm.train(dataset);
})
.spread(function (model, report) {
console.log('SVM trained. \nReport :\n%s', JSON.stringify(report, null, '\t'));
}).done(function () {
console.log('done.');
});

/* OUTPUT
Dataset dimensions reduced from 254 to 28 features using PCA.
Expand Down
54 changes: 0 additions & 54 deletions examples/classificationBasicExample.js

This file was deleted.

6 changes: 6 additions & 0 deletions examples/datasets/xor.json
@@ -0,0 +1,6 @@
[
[[0, 0], 0],
[[0, 1], 1],
[[1, 0], 1],
[[1, 1], 0]
]
35 changes: 18 additions & 17 deletions examples/evaluationExample.js → examples/evaluation-example.js
Expand Up @@ -5,34 +5,35 @@
training set : svmguide1.ds
test set : svmguide1.t.ds
NOTE : No scaling / normalization used. Expect accuracy with default params to be 66.925%
NOTE : No scaling / normalization used. Expect 66.925% accuracy with default parameters
*/
'use strict';

var nodesvm = require('../lib/nodesvm'),
var Q = require('q'),
nodesvm = require('../lib'),
trainingFile = './examples/datasets/svmguide1.ds',
testingFile = './examples/datasets/svmguide1.t.ds';

var svm = new nodesvm.CSVC({
kernelType: nodesvm.KernelTypes.RBF,
gamma: 0.25,
C: [0.5, 1], // allow you to evaluate several values during training
normalize: false,
reduce: false
gamma: 0.25,
c: 1, // allow you to evaluate several values during training
normalize: false,
reduce: false,
kFold: 1 // disable k-fold cross-validation
});

svm.once('trained', function(report) {
console.log('SVM trained. Training report :\n%s', JSON.stringify(report, null, '\t'));

nodesvm.readDatasetAsync(testingFile, function(testset){
svm.evaluate(testset, function(evalReport){
console.log('Evaluation report against the testset:\n%s', JSON.stringify(evalReport, null, '\t'));
process.exit(0);
});
});
Q.all([
nodesvm.read(trainingFile),
nodesvm.read(testingFile)
]).spread(function (trainingSet, testingSet) {
return svm.train(trainingSet)
.then(function () {
return svm.evaluate(testingSet);
});
}).done(function (evaluationReport) {
console.log('Accuracy against the testset:\n', JSON.stringify(evaluationReport, null, '\t'));
});

svm.trainFromFile(trainingFile);

/* OUTPUT
SVM trained. Training report :
Expand Down

0 comments on commit 0b98a03

Please sign in to comment.