npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

random-forest

v0.0.7

Published

Random Forest with WebAssembly and WebWorkers

Downloads

67

Readme

random-forest

Random forest method. Ported to JavaScript with WebAssembly. Async computation and multithreading implemented with WebWorkers (for browsers). No tests yet. See examples in the example folder.

  • Fast
  • Sync/async modes
  • Threads support
  • CommonJS module

Install

npm install -S random-forest

Sync mode

Init

const { RandomForestClassifier, RandomForestRegressor } = require('random-forest')

const rf = new RandomForestClassifier({
  nEstimators: 100,
  maxDepth: 10,
  maxFeatures: 'auto',
  minSamplesLeaf: 5,
  minInfoGain: 0,
})

Training, Predicting

rf.train(Xtrain, ytrain)
const ypred = rf.predict(Xtest)

Saving, Loading models

const model = rf.save()
fs.writeFileSync('example.model', model)
const modelLoaded = new Uint8Array(fs.readFileSync('example.model'))
rf.load(modelLoaded)
const ypred = rf.predict(Xtest)

Some browsers doesn't allow running WebAssembly in a sync mode. In such case, you can try async mode described below.

Async mode

const { RandomForestClassifier, RandomForestRegressor } = require('random-forest/async')

// ! Don't miss /async part in require('random-forest/async') !

;(async function f () {
  const rf = new RandomForestClassifier({
    nEstimators: 100,
    maxDepth: 10,
    maxFeatures: 'auto',
    minSamplesLeaf: 5,
    minInfoGain: 0,
    nJobs: 4 // Control the number of threads (workers) with this param
  })
  await rf.init()
  await rf.train(Xtrain, ytrain)
  const ypred = await rf.predict(Xtest)
  console.log(ypred, ytest)
})()

Currently the async mode doesn't support loading/saving models.

Development

Contributions are very welcomed. Some insights on how everything works:

Building steps:

  1. The native code is loaded from the native-forest repo, a fork from RandomForests, a C++ implementation of random forests
  2. Custom C++ interfaces are in src/api.cpp and src/api.h.
  3. Emscripten compiles the native-forest code with defined interfaces into native/native.js and native/native.wasm. Compilation settings located in Makefile
  4. To load WebAssembly in sync mode, prepare-wasm.js script converts the wasm file into a Uint8 array and stores it in the wrappers folder
  5. Then src/base.js loads wrapper/native.bin.js as a regular CommonJS module, initializes it using the native/native.js module utils and then inititalizes native functions with cwrap
  6. That's all what needed for the sync mode to work. Now prepare async version. To make it easier loading and bundling the module, a WebWorker script is bundled, rather than uses importScript. It's also loaded not as a separate file, but Blob. To generate the Blob we need the worker to be compiled first, then loaded as a string
  7. Bundle src/worker.js into dist/worker.js
  8. Use prepare-worker.js to read code of dist/worker.js and save it as a module in wrapper/worker.code.js
  9. Load wrapped code in src/async.js, init Blob, the URL, and WebWorkers
  10. In async mode results are aggregates