Skip to content

Commit

Permalink
Use Lodash to determine equality between objects (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
omniphx authored and serendipious committed Oct 16, 2016
1 parent a60d381 commit 0592555
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
13 changes: 13 additions & 0 deletions data/object-evaluation.json
@@ -0,0 +1,13 @@
{
"features": ["foo", "bar","flim"],
"data": [
{"foo":true, "bar":true, "flim":true, "classification":{"description":"foo bar flim"}},
{"foo":false, "bar":true, "flim":true, "classification":{"description":"bar flim"}},
{"foo":true, "bar":false, "flim":true, "classification":{"description":"foo flim"}},
{"foo":false, "bar":false, "flim":true, "classification":{"description":"flim"}},
{"foo":true, "bar":true, "flim":false, "classification":{"description":"foo bar"}},
{"foo":false, "bar":true, "flim":false, "classification":{"description":"bar"}},
{"foo":true, "bar":false, "flim":false, "classification":{"description":"foo"}},
{"foo":false, "bar":false, "flim":false, "classification":{"description":"none"}}
]
}
2 changes: 1 addition & 1 deletion lib/decision-tree.js
Expand Up @@ -75,7 +75,7 @@ module.exports = (function() {
total++;
var pred = instance.predict(s);
var actual = s[target];
if (pred == actual) {
if (_.isEqual(pred, actual)) {
correct++;
}
});
Expand Down
49 changes: 46 additions & 3 deletions tst/evaluation.js
@@ -1,8 +1,9 @@
const TIC_TAC_TOE_DATASET = require('data/tic-tac-toe.json');
const VOTING_DATASET = require('data/voting.json');
const OBJECT_EVALUATION_DATASET = require('data/object-evaluation.json');
const TIC_TAC_TOE_DATASET = require('data/tic-tac-toe.json');
const VOTING_DATASET = require('data/voting.json');

var assert = require('assert');
var ID3 = require('lib/decision-tree');
var ID3 = require('lib/decision-tree');

describe('ID3 Decision Tree', function() {
describe('Tic Tac Toe Dataset', function() {
Expand Down Expand Up @@ -38,4 +39,46 @@ describe('ID3 Decision Tree', function() {
assert.equal(accuracy, 1);
});
});

describe('Object Evaluation Dataset', function() {
var dt;
before(function() {
dt = new ID3(OBJECT_EVALUATION_DATASET.data, 'classification', OBJECT_EVALUATION_DATASET.features);
});

it('should initialize on training dataset', function() {
assert.ok(dt);
assert.ok(dt.toJSON());
});

it('should evaluate perfectly on training dataset', function() {
var data = [
{"foo":true, "bar":true, "flim":true, "classification":{"description":"foo bar flim"}},
{"foo":false, "bar":true, "flim":true, "classification":{"description":"bar flim"}},
{"foo":true, "bar":false, "flim":true, "classification":{"description":"foo flim"}},
{"foo":false, "bar":false, "flim":true, "classification":{"description":"flim"}},
{"foo":true, "bar":true, "flim":false, "classification":{"description":"foo bar"}},
{"foo":false, "bar":true, "flim":false, "classification":{"description":"bar"}},
{"foo":true, "bar":false, "flim":false, "classification":{"description":"foo"}},
{"foo":false, "bar":false, "flim":false, "classification":{"description":"none"}}
];
var accuracy = dt.evaluate(data);
assert.equal(accuracy, 1);
});

it('should evaluate 87.5% on training dataset', function() {
var data = [
{"foo":true, "bar":true, "flim":true, "classification":{"description":"foo bar flim"}},
{"foo":false, "bar":true, "flim":true, "classification":{"description":"bar flim"}},
{"foo":true, "bar":false, "flim":true, "classification":{"description":"foo flim"}},
{"foo":false, "bar":false, "flim":true, "classification":{"description":"flim"}},
{"foo":true, "bar":true, "flim":false, "classification":{"description":"foo bar"}},
{"foo":false, "bar":true, "flim":false, "classification":{"description":"bar"}},
{"foo":true, "bar":false, "flim":false, "classification":{"description":"foo"}},
{"foo":false, "bar":false, "flim":false, "classification":{}}
];
var accuracy = dt.evaluate(data);
assert.equal(accuracy, 0.875);
});
});
});

0 comments on commit 0592555

Please sign in to comment.