GithubHelp home page GithubHelp logo

serendipious / nodejs-decision-tree Goto Github PK

View Code? Open in Web Editor NEW
211.0 7.0 81.0 91 KB

NodeJS Implementation of Decision Tree using ID3 Algorithm

Home Page: https://npmjs.org/package/decision-tree

License: MIT License

JavaScript 100.00%

nodejs-decision-tree's Introduction

Decision Tree for Node.js

This Node.js module implements a Decision Tree using the ID3 Algorithm

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 the model

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:

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 treeJson = dt.toJSON();

Create a decision tree from a previously trained model

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:

var treeJson = dt.toJSON();
dt.import(treeJson);

nodejs-decision-tree's People

Contributors

benjamingr avatar dependabot[bot] avatar fedecia avatar lpelov avatar omniphx avatar scrivna avatar serendipious avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

nodejs-decision-tree's Issues

Decision Tree Error

There's an error here.

decision-tree.js:20
root = childNode.child;
^

TypeError: Cannot read property 'child' of undefined
at Object.ID3.predict (/nodejs-decision-tree-id3-master/lib/decision-tree.js:20:23)
at Object. (/nodejs-decision-tree-id3-master/example.js:22:34)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3

Saving then importing a model gives an error

When saving a model with .toJSON, and later importing it, it generates the following error:

TypeError: Cannot read property 'length' of undefined

I triple checked multiple times, this error is very real.

Thank you ๐Ÿ˜

Just want to say thank you, you made my life easier! God bless. It's a beautiful piece of code! ๐Ÿ™๐Ÿป

Export tree for visualisation

How would I go about exporting the underlying decision tree so that I could visualise it in d3 or something similar?

Build de tree

Hi, I would like to know how can I build a visual representation of tree.
I saw how to predict but I can't understand how can I build the tree.

Subsequent declarations screw up the previous models

Hello, when I have 1 model, it works great (dynamic_dt below predicts correctly), but when I declare multiple ones (dynamic_dt, mde_dt, ...etc) the results are not accurate. What is happening here, how can I fix this?

Screen Shot 2021-02-08 at 6 51 48 PM

Missing data

How does this algorithm handle cases of missing data in the dataset?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.