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

shaman

v0.3.2

Published

Machine Learning library for node.js

Downloads

62

Readme

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 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