Machine Learning library for node.js

Overview

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

You might also like...

Fork, customize and deploy your Candy Machine v2 super quickly

Fork, customize and deploy your Candy Machine v2 super quickly

Candy Machine V2 Frontend This is a barebones implementation of Candy Machine V2 frontend, intended for users who want to quickly get started selling

Oct 24, 2022

Deep Learning in Javascript. Train Convolutional Neural Networks (or ordinary ones) in your browser.

ConvNetJS ConvNetJS is a Javascript implementation of Neural networks, together with nice browser-based demos. It currently supports: Common Neural Ne

Dec 31, 2022

K-nearest neighbors algorithm for supervised learning implemented in javascript

kNear Install npm install knear --save About kNear is a javascript implementation of the k-nearest neighbors algorithm. It is a supervised machine lea

Mar 7, 2022

🤖chat discord bot powered by Deep learning algorithm🧠

✨ Akaya ✨ ❗ Discord integration functionality not implemented yet! Only the deep-learning module working. Install git clone https://github.com/LyeZinh

Jun 23, 2022

architecture-free neural network library for node.js and the browser

architecture-free neural network library for node.js and the browser

Synaptic Important: Synaptic 2.x is in stage of discussion now! Feel free to participate Synaptic is a javascript neural network library for node.js a

Dec 27, 2022

FANN (Fast Artificial Neural Network Library) bindings for Node.js

node-fann node-fann is a FANN bindings for Node.js. FANN (Fast Artificial Neural Network Library) is a free open source neural network library, which

Oct 31, 2022

general natural language facilities for node

natural "Natural" is a general natural language facility for nodejs. It offers a broad range of functionalities for natural language processing. Docum

Jan 9, 2023

Run XGBoost model and make predictions in Node.js

XGBoost-Node eXtreme Gradient Boosting Package in Node.js XGBoost-Node is a Node.js interface of XGBoost. XGBoost is a library from DMLC. It is design

Nov 15, 2022

Powerful Neural Network for Node.js

NeuralN Powerful Neural Network for Node.js NeuralN is a C++ Neural Network library for Node.js with multiple advantages compared to existing solution

Dec 15, 2022
Comments
  • Multiple Linear Regression - Cannot read property 'e' of nul

    Multiple Linear Regression - Cannot read property 'e' of nul

    First of all, thank you for this awesome lib!

    Dataset (without null values)

    ,TV,radio,newspaper,sales
    1,230.1,37.8,69.2,22.1
    2,44.5,39.3,45.1,10.4
    3,17.2,45.9,69.3,9.3
    4,151.5,41.3,58.5,18.5
    5,180.8,10.8,58.4,12.9
    6,8.7,48.9,75,7.2
    7,57.5,32.8,23.5,11.8
    

    Then gathering X and Y to analyze later:

    function dressData(csvData) {
    	let X = [],
    		Y = []
    	csvData.forEach(row => {
    		let k = []
    		k.push(f(row['TV']))
    		k.push(f(row['radio']))
    		k.push(f(row['newspaper']))
    		X.push(k)
    		Y.push(f(row['sales']))
    	})
    
    	return [X, Y]
    }
    

    When I train the model, everything seems OK:

    const ml = require('shaman')
    
    function performRegression(X, Y) {
    	const lr = new ml.LinearRegression(X, Y, {
    		debug: true,
    		algorithm: 'NormalEquation'
    	})
    	lr.train(err => {
    		if (err) console.log(err)
    		else predictOutput(lr)
    	})
    }
    

    The prediction works when I use Linear Regression, but with Multiple Linear Regression it fails with the error in the title: Cannot read property 'e' of null.

    Stack trace: at LinearRegression.predict (C:\Users\facun\xxxxx\node_modules\shaman\lib\linear_regression.js:263:17)

    opened by FMGordillo 1
Owner
Luc Castera
Luc Castera
Machine-learning for Node.js

Limdu.js Limdu is a machine-learning framework for Node.js. It supports multi-label classification, online learning, and real-time classification. The

Erel Segal-Halevi 1k Dec 16, 2022
machinelearn.js is a Machine Learning library written in Typescript

machinelearn.js is a Machine Learning library written in Typescript. It solves Machine Learning problems and teaches users how Machine Learning algorithms work.

machinelearn.js 522 Jan 2, 2023
A JavaScript deep learning and reinforcement learning library.

neurojs is a JavaScript framework for deep learning in the browser. It mainly focuses on reinforcement learning, but can be used for any neural networ

Jan 4.4k Jan 4, 2023
Machine learning tools in JavaScript

ml.js - Machine learning tools in JavaScript Introduction This library is a compilation of the tools developed in the mljs organization. It is mainly

ml.js 2.3k Jan 1, 2023
Train and test machine learning models for your Arduino Nano 33 BLE Sense in the browser.

Tiny Motion Trainer Train and test IMU based TFLite models on the Web Overview Since 2009, coders have created thousands of experiments using Chrome,

Google Creative Lab 59 Nov 21, 2022
JavaScript Machine Learning Toolkit

The JavaScript Machine Learning Toolkit, or JSMLT, is an open source JavaScript library for education in machine learning.

JSMLT 25 Nov 23, 2022
Friendly machine learning for the web! 🤖

Read our ml5.js Code of Conduct and software licence here! This project is currently in development. Friendly machine learning for the web! ml5.js aim

ml5 5.9k Jan 2, 2023
Unsupervised machine learning with multivariate Gaussian mixture model which supports both offline data and real-time data stream.

Gaussian Mixture Model Unsupervised machine learning with multivariate Gaussian mixture model which supports both offline data and real-time data stre

Luka 26 Oct 7, 2022
Automated machine learning for analytics & production

auto_ml Automated machine learning for production and analytics Installation pip install auto_ml Getting started from auto_ml import Predictor from au

Preston Parry 1.6k Dec 26, 2022
Support Vector Machine (SVM) library for nodejs

node-svm Support Vector Machine (SVM) library for nodejs. Support Vector Machines Wikipedia : Support vector machines are supervised learning models t

Nicolas Panel 296 Nov 6, 2022