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

cranium

v1.3.3

Published

Streaming SVM trainer that uses stochastic gradient descent

Downloads

68

Readme

cranium

Build Status

Machine learning with streams.

var train = require('cranium').train
  , opts = {
            features: ['attr1', 'attr2']
          , classAttribute: 'class'
          }

train('input.csv', opts, function (err, machine) {
  if(machine.predict({attr1: 5, attr2: 2}) > 0)
    console.log('true')
  else
    console.log('false')
})

Accuracy rates of around 99% can be achieved once parameters are tuned for your data.

An example run with 1000 epochs on the University of Wisconsin's breast cancer data.

Accuracy v Epoch

Tradeoffs

Plenty of options for machine learning exist if your dataset can fit in memory. I recommend my friend Matt Rajca's LearnKit, or Weka if you like having a GUI.

The amount of data you need to build a good classifier increases with the number of features you have, so out of memory errors become a problem when dealing with thousands of features. For example, Weka fails to perform logistic regression with more than a couple thousand features on a 5mb dataset. Cranium never assumes that your instances can fit in memory, so you can use it on terabytes of data.

Cranium works with node streams, so you have a lot of flexibility with your input. Using streams sacrifies speed for memory efficiency -- Cranium uses a constant amount of memory that is typically below 100mb. The speed penalty is significant: Cranium runs about 500x slower than LearnKit. If your dataset can fit in memory, Cranium is probably not right for you.

Training

train

The train function creates a support vector machine and trains it on your data using stochastic gradient descent.

function train (input, opts, cb) {}

  • input - Either a CSV file's path, or a function that returns an instance stream
  • opts - An object, options are documented below
  • cb - A callback with signature function cb (err, svm)

train.opts.features

Required An array of strings. Strings should be the headers of columns in the csv that should be used as features.

train.opts.classAttribute

Required A string, the header of the column to use as the class. Only binary classification is supported right now, so valid class attributes are true and false.

train.opts.regularizer

Default 0.001. How hard to try to fit the data. Passed to the SVM.

train.opts.buffer

Default false. Set to true if your data is small enough to keep entirely in memory, and training will be sped up by about 40%. If this option is true, the input option to train must be a filename.

train.opts.stepLength

Default 0.01. The learning rate. If a float, will be held constant. If a function, should have the signature function stepLength (epoch) {} where epoch is an integer, the current epoch. It should return a float, which will be used as the step length for that epoch. You can use this to decay the step length as time goes on.

train.opts.eachEpoch

Default no-op.

function eachEpoch (epoch, svm, testStream, cb) {}

  • epoch - An integer, the current epoch
  • svm - The SVM object
  • testStream - A readable stream of instances
  • cb - Call when done to continue training

train.opts.epochs

Default 1000. How many epochs to train for.

train.opts.stepPercentage

Default 0.4. The percentage of instances to use for each epoch.

train.opts.testPercentage

Default 0.3. The percentage of instances in each epoch to leave out. These instances are fed into the test stream passed to eachEpoch.

SVM

The SVM object is a support vector machine.

SVM.predict

function predict (instance) {}

Instance is an object with the same features and classAttribute as the training data. Predict returns a float representing the output of the SVM. Values greater than zero are truthy, while values smaller than zero are falsey. The magnitude of the return value is the confidence of the prediction.

SVM.cost

function cost () {}

Returns a transform stream that accepts a stream of instances and outputs a single float. The float is the result of the cost function on the input.

SVM.accuracy

function accuracy () {}

Returns a transform stream that accepts a stream of instances and outputs a single float between zero and one. The float is the percentage of instances that were correctly classified.

License

The MIT License (MIT)

Copyright (c) 2014 Ben Ng

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.