js-regression
Package provides javascript implementation of linear regression and logistic regression
Install
npm install js-regression
Usage
Linear Regression
The sample code below illustrates how to run the multiple linear regression (polynomial in this case):
var jsregression = ; // === training data generated from y = 2.0 + 5.0 * x + 2.0 * x^2 === //var data = ;forvar x = 10; x < 1000; x += 10 var y = 20 + 50 * x + 20 * x * x + Math * 10; data; // Note that the last column should be y the output // === Create the linear regression === //var regression = alpha: 0001 // iterations: 300 lambda: 00;// can also use default configuration: var regression = new jsregression.LinearRegression(); // === Train the linear regression === //var model = regression; // === Print the trained model === //console; // === Testing the trained linear regression === //var testingData = ;forvar x = 10; x < 1000; x += 10 var actual_y = 20 + 50 * x + 20 * x * x + Math * 10; var predicted_y = regression; console;
Logistic Regression
The sample code below illustrates how to run the logistic regression on the iris datsets to classify whether a data row belong to species Iris-virginica:
var jsregression = ;var iris = ; // === Create the linear regression === //var logistic = alpha: 0001 iterations: 1000 lambda: 00;// can also use default configuration: var logistic = new jsregression.LogisticRegression(); // === Create training data and testing data ===//iris; var trainingDataSize = Math;var trainingData = ;var testingData = ;forvar i=0; i < irisrowCount ; ++i var row = ; row; // sepalLength; row; // sepalWidth; row; // petalLength; row; // petalWidth; row; // output which is 1 if species is Iris-virginica; 0 otherwise ifi < trainingDataSize trainingData; else testingData; // === Train the logistic regression === //var model = logistic; // === Print the trained model === //console; // === Testing the trained logistic regression === //forvar i=0; i < testingDatalength; ++i var probabilityOfSpeciesBeingIrisVirginica = logistic; var predicted = logistic >= logisticthreshold ? 1 : 0; console; console;
Multi-Class Classification using One-vs-All Logistic Regression
The sample code below illustrates how to run the multi-class classifier on the iris datasets to classifiy the species of each data row:
var classifier = alpha: 0001 iterations: 1000 lambda: 00; iris; var trainingDataSize = Math;var trainingData = ;var testingData = ;forvar i=0; i < irisrowCount ; ++i var row = ; row; // sepalLength; row; // sepalWidth; row; // petalLength; row; // petalWidth; row; // output is species ifi < trainingDataSize trainingData; else testingData; var result = classifier; console; forvar i=0; i < testingDatalength; ++i var predicted = classifier; console;
Usage In HTML
Include the "node_modules/js-regression/build/jsregression.min.js" (or "node_modules/js-regression/src/jsregression.js") in your HTML <script> tag
The codes in the following html files illustrates how to use them in html pages: