JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://raw.github.com/lagodiuk/decision-tree-js/master/decision-tree-min.js"></script>
<b>Comic guy:</b>
<div id="testingItem"></div>
<br/>
<b>Decision Tree prediction:</b>
<div id="decisionTreePrediction"></div>
<br/>
<b>Random Forest prediction:</b>
<div id="randomForestPrediction"></div>
<br/>
<b>Decision Tree:</b>
<br/>
<div class="tree" id="displayTree"></div>
CSS
/*
Transforming nested lists to pretty tree
<div class="tree">
<ul>
<li>
<ul>
...
</ul>
</li>
...
</ul>
</div>
Source: http://thecodeplayer.com/walkthrough/css3-family-tree
Some other advices about displaying trees: http://stackoverflow.com/questions/1695115/how-do-i-draw-the-lines-of-a-family-tree-using-html-css
*/
* {
margin: 0;
padding: 0;
}
.tree ul {
padding-top: 20px;
position: relative;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.tree li {
white-space: nowrap;
float: left;
text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
/*We will use ::before and ::after to draw the connectors*/
.tree li::before, .tree li::after{
content: '';
position: absolute;
top: 0;
right: 50%;
border-top: 1px solid #ccc;
width: 50%;
height: 20px;
}
.tree li::after{
right: auto;
left: 50%;
border-left: 1px solid #ccc;
}
/*We need to remove left-right connectors from elements without
any siblings*/
.tree li:only-child::after, .tree li:only-child::before {
display: none;
}
/*Remove space from the top of single children*/
.tree li:only-child{
padding-top: 0;
}
/*Remove left connector from first child and
right connector from last child*/
.tree li:first-child::before, .tree li:last-child::after{
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.tree li:last-child::before{
border-right: 1px solid #ccc;
border-radius: 0 5px 0 0;
-webkit-border-radius: 0 5px 0 0;
-moz-border-radius: 0 5px 0 0;
}
.tree li:first-child::after{
border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
}
/*Time to add downward connectors from parents*/
.tree ul ul::before{
content: '';
position: absolute;
top: 0;
left:...
JavaScript
// Training set
var data =
[{person: 'Homer', hairLength: 0, weight: 250, age: 36, sex: 'male'},
{person: 'Marge', hairLength: 10, weight: 150, age: 34, sex: 'female'},
{person: 'Bart', hairLength: 2, weight: 90, age: 10, sex: 'male'},
{person: 'Lisa', hairLength: 6, weight: 78, age: 8, sex: 'female'},
{person: 'Maggie', hairLength: 4, weight: 20, age: 1, sex: 'female'},
{person: 'Abe', hairLength: 1, weight: 170, age: 70, sex: 'male'},
{person: 'Selma', hairLength: 8, weight: 160, age: 41, sex: 'female'},
{person: 'Otto', hairLength: 10, weight: 180, age: 38, sex: 'male'},
{person: 'Krusty', hairLength: 6, weight: 200, age: 45, sex: 'male'}];
// Configuration
var config = {
trainingSet: data,
categoryAttr: 'sex',
ignoredAttributes: ['person']
};
// Building Decision Tree
var decisionTree = new dt.DecisionTree(config);
// Building Random Forest
var numberOfTrees = 3;
var randomForest = new dt.RandomForest(config, numberOfTrees);
// Testing Decision Tree and Random Forest
var comic = {person: 'Comic guy', hairLength: 8, weight: 290, age: 38};
var decisionTreePrediction = decisionTree.predict(comic);
var randomForestPrediction = randomForest.predict(comic);
// Displaying predictions
document.getElementById('testingItem').innerHTML = JSON.stringify(comic, null, 0);
document.getElementById('decisionTreePrediction').innerHTML = JSON.stringify(decisionTreePrediction, null, 0);
document.getElementById('randomForestPrediction').innerHTML = JSON.stringify(randomForestPrediction, null, 0);
// Displaying Decision Tree
document.getElementById('displayTree').innerHTML = treeToHtml(decisionTree.root);
// Recursive (DFS) function for displaying inner structure of decision tree
function treeToHtml(tree) {
// only leafs containing category
if (tree.category) {
return '<ul>' +
'<li>' +
'<a href="#">' +
'<b>' + tree.category...