ML.js实现CART(Classification and regression trees)决策树
ML.js
ml-cart
API document
<html>
<head>
<script src="https://www.lactame.com/lib/ml/3.3.0/ml.js"></script>
</head>
<body>
</body>
<script>
let trainingSet = [
[1, 1, 0, 0, 1, 0, 1],
[1, 1, 1, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0, 1],
[0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 0, 1],
[1, 1, 0, 0, 1, 0, 1],
[0, 1, 0, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 1, 0],
[0, 1, 0, 1, 1, 1, 1],
[1, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 0],
[1, 0, 0, 1, 1, 1, 0],
[0, 0, 1, 0, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 0],
[1, 0, 1, 1, 1, 1, 0]
];
let predictions = [
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[1],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
[0],
];
let options = {
gainFunction: 'gini',
maxDepth: 10,
minNumSamples: 3
};
let classifier = new ML.DecisionTreeClassifier(options);
classifier.train(trainingSet, predictions);
let result = classifier.predict([[1,1,1,1,1,1,1]]);
console.log(result);
</script>
</html>