• Stars
    star
    109
  • Rank 317,871 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Machine Learning library for node.js

shaman

Machine Learning library for node.js

Linear Regression

shaman supports both simple linear regression and multiple linear regression.

It supports two different algorithms to train the model:

  1. The Normal Equation
  2. The Gradient Descent Algorithm

Usage

By default, shaman uses the Normal Equation for linear regression.

var X = [1, 2, 3, 4, 5];
var Y = [2, 2, 3, 3, 5];
var lr = new LinearRegression(X,Y);
lr.train(function(err) {
  if (err) { throw err; }
  
  // you can now start using lr.predict:
  console.log(lr.predict(1));
});

If your data does not work well with the Normal Equation, you can also use the Gradient Descent algorithm as an alternative.

var X = [1, 2, 3, 4, 5];
var Y = [2, 2, 3, 3, 5];
var lr = new LinearRegression(X,Y, {
  algorithm: 'GradientDescent'
});
lr.train(function(err) {
  if (err) { throw err; }
  
  // you can now start using lr.predict:
  console.log(lr.predict(1));
});

When using Gradient Descent, you can define the number of iterations (numberOfIterations and the learning rate (learningRate) as options to the LinearRegression function.

var lr = new LinearRegression(X,Y, {
  algorithm: 'GradientDescent',
  numberOfIterations: 1000, // defaults to 8500
  learningRate: 0.5 // defaults to 0.1
});

When using the Gradient Descent algorithm, you can ask shaman to save the results of the cost function at each iteration of the algorithm. This can be useful if you would like to plot the cost function to ensure that it is converging.

var lr = new LinearRegression(X,Y, {
  algorithm: 'GradientDescent',
  saveCosts: true // defaults to false
});
lr.train(function(err) {
  // you can now get they array of costs:
  console.log(lr.costs);
});

If you are troubleshooting, you can pass in a debug option (set to true). Shaman will then debug useful info in the console (such as the cost at every iteration of the Gradient Descent algorithem).

var lr = new LinearRegression(X,Y, {
  algorithm: 'GradientDescent',
  debug: true // defaults to false
});
lr.train(function(err) {
  // will console.log some useful info
});

Examples

Simple Linear Regression - Cars

Below to see an example of Simple Linear Regression using the Normal Equation to evaluate the price of cars based on their horsepower that was done with the shaman library. Code is in examples/cars.js).

Cars Example

Simple Linear Regression - AAPL Stock Price

Below to see an example of Simple Linear Regression applies to the stock price of AAPL using the Gradient Descent algorithm from 2008 to 2012. Code can be seen at examples/stock.js.

Stock Example

Multiple Linear Regression - Cigarettes

Below to see an example of Multiple Linear Regression to evaluate Carbon Monoxide in cigarettes from nicotine and tar content. Code can be seen at examples/cigarettes.js.

Cigarettes Example

Clustering (k-means)

shaman implements the k-means clustering algorithm.

Usage

var KMeans = require('shaman').KMeans;

var K = 4;
var kmeans = new KMeans(K);

kmeans.cluster(data, function(err, clusters, centroids) {
  if (err) { throw err; }

  console.log(clusters);
});

Example: clustering wines

Below to see an example of clustering using the k-means algorithm on the wine dataset from UCI.

The code is located at examples/wine.js.

Wine Example

License

MIT

More Repositories

1

api-throttling

Rack Middleware to impose a rate limit on a web service (aka API Throttling)
Ruby
161
star
2

blackboard

Blackboard implements a TupleSpace on top of redis
Ruby
38
star
3

redis-stuff

A collection of 'stuff' associated with Redis (Capistrano Recipe, Monit Script...)
Ruby
12
star
4

messagepub-erlang

Simple gen_server client to send messages via messagepub
Erlang
7
star
5

messagepub

The messagepub gem makes it easy to interact with the MessagePub API. Visit http://messagepub.com for details.
Ruby
6
star
6

sclogin

A sample app built to learn how to use statecharts in Sproutcore
JavaScript
5
star
7

sparechange

sparechange is a proxy for http requests
4
star
8

linear-regression-examples

Examples of linear regression in node.js
JavaScript
4
star
9

scout-nginx-status-plugin

This is a Scout plugin (http://scoutapp.com) that monitors the Nginx HTTP Server
Ruby
3
star
10

erlangdc-hacknight-jan2012

One possible solution for the prob012lem we all solved at the ErlangDC Hack Night of January 2012
Erlang
3
star
11

messagepub-csharp

C# Library for interacting with the messagepub API
C#
2
star
12

openaim

OpenAIM is a simple ruby library for OpenAIM API
Ruby
2
star
13

cabernet

Cabernet is a simple blogging engine built with Erlang and YAWS.
Erlang
2
star
14

stativus-button

An example button view backed by a stativus statechart.
2
star
15

sbir

SBIR Solicitations Browser
JavaScript
2
star
16

messagepub-java

A library to interact with the messagepub REST API using Java.
Java
1
star
17

simple_cache

simple_cache example from Erlang/OTP book
Erlang
1
star
18

node-winkdex

A node.js client library for the Winkdex API (WIP)
JavaScript
1
star