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:
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 = XY;lr;
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 = XY algorithm: 'GradientDescent';lr;
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 = XY algorithm: 'GradientDescent' numberOfIterations: 1000 // defaults to 8500 learningRate: 05 // 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 = XY algorithm: 'GradientDescent' saveCosts: true // defaults to false;lr;
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 = XY algorithm: 'GradientDescent' debug: true // defaults to false;lr;
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).
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.
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.
Clustering (k-means)
shaman implements the k-means clustering algorithm.
Usage
var KMeans = KMeans; var kmeans = K; kmeans;
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.