Incremental Random Forest Implementation
https://github.com/yjhjstz/node-irf.git
Incremental Random Forest =========================
An implementation in C++ (with node.js and Python bindings) of a variant of Leo Breiman's Random Forests
The forest is maintained incrementally as samples are added or removed - rather than fully rebuilt from scratch every time - to save effort.
It is not a streaming implementation, all the samples are stored and will be reseen when required to recursively rebuild invalidated subtrees. The effort to update each individual tree can vary substantially but the overall effort to update the forest is averaged across the trees so tends not to vary so much.
IRF is licensed under the MIT license.
Features and limitations
npm install node-irf
Node.js usage
var irf = require('node-irf');
var f = new irf.IRF(99); // create forest of 99 trees
f.add('1', {1:1, 3:1, 5:1}, 0); // add a sample identified as '1' with the given feature values, classified as 0
f.add('2', {1:0, 3:0, 4:1}, 0); // features are stored sparsely, when a value is not given it will be taken as 0
f.add('3', {2:0, 3:0, 5:0}, 0); // but 0s can also be given explicitly
// ...
var y = f.classify({1:1, 3:1, 5:1}); // classify feature vector
// the forest will be lazily updated before classification
f.commit(); // but you can force an update at any time
// you get a probability estimate from 0 to 1 for belong to class 1
var c = Math.round(y); // round to nearest to get class (0 or 1)
f.remove('8'); // remove a sample
f.add('8', {1:0, 2:0, 3:0, 4:0, 5:1}, 0); // and add it again with new values
console.log(f.asJSON()); // serialize to json (for classification, not suitable for incremental update)
f.each(function(suid, features, y) {
// ...
});
var b = f.toBuffer(); // serialize (complete) to buffer
var f2 = new irf.IRF(b); // construct from buffer contents
Python setup
Python usage
import irf
f = irf.IRF(99) # create forest of 99 trees
f.add('1', {1:1, 3:1, 5:1}, 0) # add a sample identified as '1' with the given feature values, classified as 0
f.add('2', {1:0, 3:0, 4:1}, 0) # features are stored sparsely, when a value is not given it will be taken as 0
f.add('3', {2:0, 3:0, 5:0}, 0) # but 0s can also be given explicitly
# ...
y = f.classify({1:1, 2:1, 5:1}); print y, int(round(y)) # classify feature vector, round to nearest to get class
f.save('simple.rf') # save forest to file
f = irf.load('simple.rf') # load forest from file
f.remove('8') # remove a sample
f.add('8', {1:0, 2:0, 3:0, 4:0, 5:1}, 0) # and add it again with new values
y = f.classify({1:1, 2:1, 5:1}); print y, int(round(y)) # the forest will be lazily updated before classification
# f.commit() # but you can force it
for (sId, x, y) in f.samples(): # iterate through samples in the forest, in lexicographic ID order
print sId, x, y # and print them
C++ usage
Dependencies
Tests