Gadzan

ML.js 实现决策树

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>

打赏码

知识共享许可协议 本作品采用知识共享署名 4.0 国际许可协议进行许可。

评论