Thursday, May 25, 2017

Open Source: JavaScript implementation of Content Collaborative Filtering Recommender

js-recommender

Package provides java implementation of content collaborative filtering for recommend-er system

Build Status Coverage Status

Install

npm install js-recommender

Usage

The sample code below tries to predict the missing rating of [user, movie] as shown in the table below:
movie-recommender
var jsrecommender = require("js-recommender");

var recommender = new jsrecommender.Recommender();
      
var table = new jsrecommender.Table();

  // table.setCell('[movie-name]', '[user]', [score]);
table.setCell('Love at last', 'Alice', 5);
table.setCell('Remance forever', 'Alice', 5);
table.setCell('Nonstop car chases', 'Alice', 0);
table.setCell('Sword vs. karate', 'Alice', 0);
table.setCell('Love at last', 'Bob', 5);
table.setCell('Cute puppies of love', 'Bob', 4);
table.setCell('Nonstop car chases', 'Bob', 0);
table.setCell('Sword vs. karate', 'Bob', 0);
table.setCell('Love at last', 'Carol', 0);
table.setCell('Cute puppies of love', 'Carol', 0);
table.setCell('Nonstop car chases', 'Carol', 5);
table.setCell('Sword vs. karate', 'Carol', 5);
table.setCell('Love at last', 'Dave', 0);
table.setCell('Remance forever', 'Dave', 0);
table.setCell('Nonstop car chases', 'Dave', 4);

var model = recommender.fit(table);
console.log(model);

predicted_table = recommender.transform(table);

console.log(predicted_table);


for (var i = 0; i < predicted_table.columnNames.length; ++i) {
    var user = predicted_table.columnNames[i];
    console.log('For user: ' + user);
    for (var j = 0; j < predicted_table.rowNames.length; ++j) {
        var movie = predicted_table.rowNames[j];
        console.log('Movie [' + movie + '] has actual rating of ' + Math.round(table.getCell(movie, user)));
        console.log('Movie [' + movie + '] is predicted to have rating ' + Math.round(predicted_table.getCell(movie, user)));
    }
}
To configure the recommender, can overwrite its parameters in its constructor:
var recommender = new jsrecommender.Recommender({
    alpha: 0.01, // learning rate
    lambda: 0.0, // regularization parameter
    iterations: 500, // maximum number of iterations in the gradient descent algorithm
    kDim: 2 // number of hidden features for each movie
});

Open Source: JavaScript implementation of Linear and Kernel SVM using Gradient Descent

js-svm

This package provides javascript implementation of linear SVM and SVM with gaussian kernel:

Features

  • Support for binary classification
  • Support for multi-class classification

Install

npm install js-svm

Usage

SVM Binary Classifier

The sample code below show how to use SVM binary classifier on the iris datsets to classify whether a data row belong to species Iris-virginica:
var jssvm = require('js-svm');
var iris = require('js-datasets-iris');

var svm = new jssvm.BinarySvmClassifier();

iris.shuffle();

var trainingDataSize = Math.round(iris.rowCount * 0.9);
var trainingData = [];
var testingData = [];
for(var i=0; i < iris.rowCount ; ++i) {
   var row = [];
   row.push(iris.data[i][0]); // sepalLength;
   row.push(iris.data[i][1]); // sepalWidth;
   row.push(iris.data[i][2]); // petalLength;
   row.push(iris.data[i][3]); // petalWidth;
   row.push(iris.data[i][4] == "Iris-virginica" ? 1.0 : 0.0); // output which is 1 if species is Iris-virginica; 0 otherwise
   if(i < trainingDataSize){
        trainingData.push(row);
   } else {
       testingData.push(row);
   }
}


var result = svm.fit(trainingData);

console.log(result);

for(var i=0; i < testingData.length; ++i){
   var predicted = svm.transform(testingData[i]);
   console.log("actual: " + testingData[i][4] + " predicted: " + predicted);
}
To configure the BinarySvmClassifier, use the following code when it is created:
var svm = new jssvm.BinarySvmClassifier({
   alpha: 0.01, // learning rate
   iterations: 1000, // maximum iterations
   C: 5.0, // panelty term
   trace: false // debug tracing
});

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 jssvm = require('js-svm');
var iris = require('js-datasets-iris');

var classifier = new jssvm.MultiClassSvmClassifier();

iris.shuffle();

var trainingDataSize = Math.round(iris.rowCount * 0.9);
var trainingData = [];
var testingData = [];
for(var i=0; i < iris.rowCount ; ++i) {
   var row = [];
   row.push(iris.data[i][0]); // sepalLength;
   row.push(iris.data[i][1]); // sepalWidth;
   row.push(iris.data[i][2]); // petalLength;
   row.push(iris.data[i][3]); // petalWidth;
   row.push(iris.data[i][4]); // output is species
   if(i < trainingDataSize){
        trainingData.push(row);
   } else {
       testingData.push(row);
   }
}


var result = classifier.fit(trainingData);

console.log(result);

for(var i=0; i < testingData.length; ++i){
   var predicted = classifier.transform(testingData[i]);
   console.log("svm prediction testing: actual: " + testingData[i][4] + " predicted: " + predicted);
}
To configure the MultiClassSvmClassifier, use the following code when it is created:
var classifier = new jssvm.MultiClassSvmClassifier({
   alpha: 0.01, // learning rate
   iterations: 1000, // maximum iterations
   C: 5.0 // panelty term
   sigma: 1.0 // the standard deviation for the gaussian kernel
});

Switch between linear and guassian kernel

By default the kernel used by the binary and multi-class classifier is "linear" which can be printed by:
console.log(classifier.kernel);
To switch to use gaussian kernel, put the property 'kernel: "gaussian"' in the config data when the classifier is created:
var svm = new jssvm.BinarySvmClassifier({
   ...,
   kernel: 'gaussian'
});

....

var svm = new jssvm.MultiClassSvmClassifier({
   ...,
   kernel: 'gaussian'
});

Usage In HTML

Include the "node_modules/js-svm/build/jssvm.min.js" (or "node_modules/js-svm/src/jssvm.js") in your HTML <script> tag
The code in the script tag looks sth like this:
var classifier = new jssvm.BinarySvmClassifier();

Open Source: JavaScript implementation of Linear and Logistic Regression

https://github.com/chen0040/js-regression

This npm package provides JavaScript implementation of linear regression and logistic regression
Build Status Coverage Status

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 = require('js-regression');

// === training data generated from y = 2.0 + 5.0 * x + 2.0 * x^2 === //
var data = [];
for(var x = 1.0; x < 100.0; x += 1.0) {
  var y = 2.0 + 5.0 * x + 2.0 * x * x + Math.random() * 1.0;
  data.push([x, x * x, y]); // Note that the last column should be y the output
}

// === Create the linear regression === //
var regression = new jsregression.LinearRegression({
  alpha: 0.001, // 
  iterations: 300,
  lambda: 0.0
});
// can also use default configuration: var regression = new jsregression.LinearRegression(); 

// === Train the linear regression === //
var model = regression.fit(data);

// === Print the trained model === //
console.log(model);


// === Testing the trained linear regression === //
var testingData = [];
for(var x = 1.0; x < 100.0; x += 1.0) {
  var actual_y = 2.0 + 5.0 * x + 2.0 * x * x + Math.random() * 1.0;
  var predicted_y = regression.transform([x, x * x]);
  console.log("actual: " + actual_y + " predicted: " + predicted_y); 
}

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 = require('js-regression');
var iris = require('js-datasets-iris');

// === Create the linear regression === //
var logistic = new jsregression.LogisticRegression({
   alpha: 0.001,
   iterations: 1000,
   lambda: 0.0
});
// can also use default configuration: var logistic = new jsregression.LogisticRegression(); 

// === Create training data and testing data ===//
iris.shuffle();

var trainingDataSize = Math.round(iris.rowCount * 0.8);
var trainingData = [];
var testingData = [];
for(var i=0; i < iris.rowCount ; ++i) {
   var row = [];
   row.push(iris.data[i][0]); // sepalLength;
   row.push(iris.data[i][1]); // sepalWidth;
   row.push(iris.data[i][2]); // petalLength;
   row.push(iris.data[i][3]); // petalWidth;
   row.push(iris.data[i][4] == "Iris-virginica" ? 1.0 : 0.0); // output which is 1 if species is Iris-virginica; 0 otherwise
   if(i < trainingDataSize) {
        trainingData.push(row);
   } else {
       testingData.push(row);
   }
}


// === Train the logistic regression === //
var model = logistic.fit(trainingData);

// === Print the trained model === //
console.log(model);

// === Testing the trained logistic regression === //
for(var i=0; i < testingData.length; ++i){
   var probabilityOfSpeciesBeingIrisVirginica = logistic.transform(testingData[i]);
   var predicted = logistic.transform(testingData[i]) >= logistic.threshold ? 1 : 0;
   console.log("actual: " + testingData[i][4] + " probability of being Iris-virginica: " + probabilityOfSpeciesBeingIrisVirginica);
   console.log("actual: " + testingData[i][4] + " predicted: " + predicted);
}

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 = new jsregression.MultiClassLogistic({
   alpha: 0.001,
   iterations: 1000,
   lambda: 0.0
});

iris.shuffle();

var trainingDataSize = Math.round(iris.rowCount * 0.9);
var trainingData = [];
var testingData = [];
for(var i=0; i < iris.rowCount ; ++i) {
   var row = [];
   row.push(iris.data[i][0]); // sepalLength;
   row.push(iris.data[i][1]); // sepalWidth;
   row.push(iris.data[i][2]); // petalLength;
   row.push(iris.data[i][3]); // petalWidth;
   row.push(iris.data[i][4]); // output is species
   if(i < trainingDataSize){
        trainingData.push(row);
   } else {
       testingData.push(row);
   }
}


var result = classifier.fit(trainingData);

console.log(result);

for(var i=0; i < testingData.length; ++i){
   var predicted = classifier.transform(testingData[i]);
   console.log("actual: " + testingData[i][4] + " predicted: " + predicted);
}

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 code in the script tag looks sth like this:
var logistic = new jsregression.LogisticRegression();