-
Notifications
You must be signed in to change notification settings - Fork 0
Decision Tree learning algorithms
There are three algorithms implemented - CART, ID3, C4.5.
Use CART or ID3 in decision tree learning process with library is very simple. You need to define learning data, features array, pass it to algorithm, build tree and... That's all! After that, you can use tree and classify test data. CART and ID3 support only discrete data.
const MLeduJS = require('ml_edu_js');
const CART = MLeduJS.CART;
// define learning data
const learningData = [
['Green', 'Sweet', 'Apple'],
['Orange', 'Bitter', 'Grape'],
['Orange', 'Sweet', 'Orange'],
['Purple', 'Bitter', 'Plum'],
];
//define feature labels
const labels = {
color: {
type: 'string',
order: 0
},
taste: {
type: 'string',
order: 1
}
};
// init and run algorithm
const cartAlgorithm = new CART();
const tree = cartAlgorithm.buildTree(learningData, labels);
// check it ;)
const prediction = tree.classify(['Purple', 'Bitter', 'Plum']);
console.log(prediction);
const MLeduJS = require('ml_edu_js');
const ID3 = MLeduJS.ID3;
// define learning data
const learningData = [
['Green', 'Sweet', 'Apple'],
['Orange', 'Bitter', 'Grape'],
['Orange', 'Sweet', 'Orange'],
['Purple', 'Bitter', 'Plum'],
];
//define feature labels
const labels = {
color: {
type: 'string',
order: 0
},
taste: {
type: 'numeric',
order: 1
}
};
// init and run algorithm
const id3Algorithm = new ID3();
const tree = id3Algorithm.buildTree(learningData, labels);
// check it ;)
const prediction = tree.classify(['Purple', 'Bitter', 'Plum']);
console.log(prediction);
Full example of CART algorithm you can find here and ID3 here
To turn on descriptive mode - you need to pass options object algorithm constructor. With descriptive mode you can observe results of learning algorithm in the console. It will work effectively for small trees.
const options = {
descriptive: true,
}
const cartAlgorithm = new ID3(options);
C4.5 algorithm is the extension of the ID3. It additionally provides possibilities to add continues data, missing value. It also change a little splitting rule and add possibility to prune tree. Usage of C4.5 is similar to other algorithms but there are one main difference: options. To use C4.5 do as follows:
const MLeduJS = require('ml_edu_js');
const C45 = MLeduJS.C45;
const learningData = [
['Green', 8, 'Apple'],
['Orange', 1 , 'Grape'],
['Orange', 6, 'Orange'],
['Purple', 7, 'Plum'],
['Red', 5, 'Orange'],
['Red', 7, 'Orange'],
['Red', 9, 'Apple'],
];
//define feature labels
const labels = {
color: {
type: 'string',
order: 0
},
taste: {
type: 'numeric',
order: 1
}
};
// init and run algorithm, turn on descriptive mode and with prunning
const options = {
descriptive: true,
prune: true
};
const cartAlgorithm = new C45(options);
const tree = cartAlgorithm.buildTree(learningData, labels);
tree.printTree();
// check it ;)
const prediction = tree.classify(['?', '?', 'Orange']);
console.log(`\n Prediction for our test sample:`);
console.log(prediction);