UNPKG

@__username/decision-tree

Version:

NodeJS implementation of decision tree using ID3 algorithm

106 lines (90 loc) 2.9 kB
var assert = require('assert'); var ID3 = require('lib/decision-tree'); describe('Decision Tree Validation', function() { describe('constructor', function() { describe('feature list', function() { it('should not be NULL', function() { assert.throws(function() { new ID3("a"); }); }); it('should not be EMPTY', function() { assert.throws(function() { new ID3("a", []); }); }); it('should be list of strings', function() { assert.throws(function() { new ID3("a", ["b", 1]); }); }); }); describe('target class', function() { it('should not be NULL', function() { assert.throws(function() { new ID3(null, ["b"]); }); }); it('should not be BLANK', function() { assert.throws(function() { new ID3(" ", ["b"]); }); }); it('should not be in feature list', function() { assert.throws(function() { new ID3("a", ["a", "b"]); }); }); }); }); describe('training', function() { describe('sample data-set', function() { var dt; beforeEach(function() { dt = new ID3("a", ["b"]); }); it('should be a List<Map<String, Primitive>>', function() { assert.doesNotThrow(function() { dt.train([{a:1,b:'x',c:new Date,d:true}]); }); assert.throws(function() { dt.train([{a:1,b:[1,2]},{a:3,b:1}]); }); assert.throws(function() { dt.train([{a:1,b:{a:1,b:2}},{a:3,b:1}]); }); }); it('should not be missing the target-class', function() { assert.doesNotThrow(function() { dt.train([{a:1,b:"x",c:50}, {b:"y",c:10}]); }); }); it('should not have missing features', function() { assert.doesNotThrow(function() { new ID3("a", ["d"]) .train([{a:1,b:"x",c:50}, {b:"y",c:10}]); }); }); }); }); describe('prediction', function() { describe('sample to predict on', function() { const SAMPLE_DATA_SET = require('data/sample.json'); const SAMPLE_DATASET_CLASS_NAME = 'liked'; it('should not contain untrained features', function() { assert.throws(function() { var dt = new ID3(SAMPLE_DATASET_CLASS_NAME, SAMPLE_DATA_SET.featureList) dt.train(SAMPLE_DATA_SET.data); dt.predict({ non_existent_feature: "non-existent feature" }); }); }); it('should not contain feature with unseen values', function() { assert.throws(function() { var dt = new ID3(SAMPLE_DATASET_CLASS_NAME, SAMPLE_DATA_SET.featureList, { verbose: true }); dt.train(SAMPLE_DATA_SET.data); dt.predict({ color: "green", shape: "square" }); }); }); }); }); });