Git Product home page Git Product logo

woodlands.js's Introduction

woodlands.js Build Status

A Javascript library for creating a Random Forest of Decision Tree Classifiers (with bootstrapping & feature bagging)

// define our TRAINING data
var trainingData = [
	{ 'a': 0, 'b': 0, 'output': 0 },
	{ 'a': 0, 'b': 1, 'output': 1 },
	{ 'a': 1, 'b': 0, 'output': 1 },
	{ 'a': 1, 'b': 1, 'output': 0 }
];

// define our TEST data
var testingData = [
	{ 'a': 0, 'b': 0, 'output': 0 },
	{ 'a': 0, 'b': 1, 'output': 1 }
];


// define the feature class we want to predict
var class_name = 'output';

// what features of the training data should we use to train on?
var features = ['a','b'];



// Train a single tree
var dt = new woodlands.DecisionTree(trainingData, class_name, features);

// Evaluate their accuracy on training & test data
console.log('Single Training Accuracy: '+dt.evaluate(trainingData));	// 1
console.log('Single Test Accuracy: '+dt.evaluate(testingData));			// 1

// predict a value
var prediction = dt.predict({ a: 0, b: 1 });
console.log(prediction); // 1


// Train a forest of trees
var rf = new woodlands.RandomForest(trainingData, class_name, features, {
	numTrees: 100,			// how many trees should we use (results are averaged together)
	percentData: 1,			// what percentage of training data should each tree see (bootstrapping) - For larger datasets I find .15 works well
	percentFeatures: 1		// what percentage of features should each tree see (feature bagging) - For larger datasets I find .7 works well
});


// Evaluate our forest with the test data
console.log('Evaluating Forest...');
console.log(JSON.stringify(rf.evaluate(testingData), null, "\t"));


// and predict a value on potentially unseen data

// return percentage of trees that agree on each class
var prediction = rf.predictProbability({ a: 0, b: 1 }); 
console.log(prediction);	// {"1": 1, "0": 0}

// returns a single class that has the consensus vote
var prediction = rf.predictClass({ a: 0, b: 1 });	
console.log(prediction);	// "1"

woodlands.js's People

Contributors

scrivna avatar

Watchers

James Cloos avatar

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.