Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/serendipious/nodejs-decision-tree
NodeJS Implementation of Decision Tree using ID3 Algorithm
https://github.com/serendipious/nodejs-decision-tree
Last synced: 4 months ago
JSON representation
NodeJS Implementation of Decision Tree using ID3 Algorithm
- Host: GitHub
- URL: https://github.com/serendipious/nodejs-decision-tree
- Owner: serendipious
- License: mit
- Created: 2013-04-28T11:11:36.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2022-10-22T17:11:06.000Z (about 2 years ago)
- Last Synced: 2024-04-13T21:02:34.291Z (8 months ago)
- Language: JavaScript
- Homepage: https://npmjs.org/package/decision-tree
- Size: 88.9 KB
- Stars: 210
- Watchers: 7
- Forks: 81
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE
Awesome Lists containing this project
README
Decision Tree for Node.js
========================This Node.js module implements a Decision Tree using the [ID3 Algorithm](http://en.wikipedia.org/wiki/ID3_algorithm)
# [Installation](id:installation)
npm install decision-tree# [Usage](id:usage)
## Import the module
```js
var DecisionTree = require('decision-tree');
```## Prepare training dataset
```js
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
```js
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
```js
var class_name = "liked";
```## Setup Features to be used by decision tree
```js
var features = ["color", "shape"];
```## Create decision tree and train the model
```js
var dt = new DecisionTree(class_name, features);
dt.train(training_data);
```Alternately, you can also create and train the tree when instantiating the tree itself:
```js
var dt = new DecisionTree(training_data, class_name, features);
```## Predict class label for an instance
```js
var predicted_class = dt.predict({
color: "blue",
shape: "hexagon"
});
```## Evaluate model on a dataset
```js
var accuracy = dt.evaluate(test_data);
```## Export underlying model for visualization or inspection
```js
var treeJson = dt.toJSON();
```## Create a decision tree from a previously trained model
```js
var treeJson = dt.toJSON();
var preTrainedDecisionTree = new DecisionTree(treeJson);
```Alternately, you can also import a previously trained model on an existing tree instance, assuming the features & class are the same:
```js
var treeJson = dt.toJSON();
dt.import(treeJson);
```