@__username/decision-tree 中文文档教程
Decision Tree for NodeJS
该模块包含决策树的 NodeJS 实现,使用 ID3 算法
Table Of Contents
Installation
npm install decision-tree
Usage
导入模块:
var DecisionTree = require('decision-tree');
准备训练数据集:
var training_data = [ {"color":"blue", "shape":"square", "liked":false}, {"color":"red", "shape":"square", "liked":false}, {"color":"blue", "shape":"circle", "liked":true}, {"color":"red", "shape":"circle", "liked":true}, {"color":"blue", "shape":"hexagon", "liked":false}, {"color":"red", "shape":"hexagon", "liked":false}, {"color":"yellow", "shape":"hexagon", "liked":true}, {"color":"yellow", "shape":"circle", "liked":true} ];
准备测试数据集:
var test_data = [ {"color":"blue", "shape":"hexagon", "liked":false}, {"color":"red", "shape":"hexagon", "liked":false}, {"color":"yellow", "shape":"hexagon", "liked":true}, {"color":"yellow", "shape":"circle", "liked":true} ];
设置目标用于预测的类:
var class_name = "liked";
设置决策树要使用的特征:
var features = ["color", "shape"];
创建决策树和训练模型:
var dt = new DecisionTree(training_data, class_name, features);
预测实例的类标签:
var predicted_class = dt.predict({ color: "blue", shape: "hexagon" });
评估数据集上的模型:
var accuracy = dt.evaluate(test_data);
导出基础模型以进行可视化或检查:
var treeModel = dt.toJSON();
Decision Tree for NodeJS
This module contains the NodeJS Implementation of Decision Tree using ID3 Algorithm
Table Of Contents
Installation
npm install decision-tree
Usage
Import the module:
var DecisionTree = require('decision-tree');
Prepare training dataset:
var training_data = [ {"color":"blue", "shape":"square", "liked":false}, {"color":"red", "shape":"square", "liked":false}, {"color":"blue", "shape":"circle", "liked":true}, {"color":"red", "shape":"circle", "liked":true}, {"color":"blue", "shape":"hexagon", "liked":false}, {"color":"red", "shape":"hexagon", "liked":false}, {"color":"yellow", "shape":"hexagon", "liked":true}, {"color":"yellow", "shape":"circle", "liked":true} ];
Prepare test dataset:
var test_data = [ {"color":"blue", "shape":"hexagon", "liked":false}, {"color":"red", "shape":"hexagon", "liked":false}, {"color":"yellow", "shape":"hexagon", "liked":true}, {"color":"yellow", "shape":"circle", "liked":true} ];
Setup Target Class used for prediction:
var class_name = "liked";
Setup Features to be used by decision tree:
var features = ["color", "shape"];
Create decision tree and train model:
var dt = new DecisionTree(training_data, class_name, features);
Predict class label for an instance:
var predicted_class = dt.predict({ color: "blue", shape: "hexagon" });
Evaluate model on a dataset:
var accuracy = dt.evaluate(test_data);
Export underlying model for visualization or inspection:
var treeModel = dt.toJSON();