Machine Learning Framework for Node
- Feature Selection
nodeml.feature.tfidf
: tfidf
- Classification
nodeml.Bayes
: Bayesnodeml.kNN
: k-Nearest Neighbornodeml.CNN
: Convolutional Neural Network (CNN)
- Clustering
nodeml.kMeans
: k-Means
- Recommendation
nodeml.CF
: User based Collaborative Filtering
- Evaluation
nodeml.accuracy
: Precision, Recall, F-Measure, Accuracynodeml.ndcg
: NDCG
- DBSCAN
- Support Vector Machine
- LSTM
- Logistic Regression
installation on your project
npm install --save nodeml
use example
const {Bayes} = require('nodeml');
let bayes = new Bayes();
bayes.train({'fun': 3, 'couple': 1}, 'comedy');
bayes.train({'couple': 1, 'fast': 1, 'fun': 3}, 'comedy');
bayes.train({'fast': 3, 'furious': 2, 'shoot': 2}, 'action');
bayes.train({'furious': 2, 'shoot': 4, 'fun': 1}, 'action');
bayes.train({'fly': 2, 'fast': 3, 'shoot': 2, 'love': 1}, 'action');
let result = bayes.test({'fun': 3, 'fast': 3, 'shoot': 2});
console.log(result); // this print {answer: , score: }
Sample dataset for test
const {sample} = require('nodeml');
// bbc: Function() => { dataset: [ {} , ... ], labels: [ ... ] }
// bbc news dataset, sparse matrix
const bbc = sample.bbc();
// yeast: Function() => { dataset: [ [] , ... ], labels: [ ... ] }
// yeast dataset, array data
const yeast = sample.yeast();
// iris: Function() => { dataset: [ [] , ... ], labels: [ ... ] }
// iris dataset, array data
const iris = sample.iris();
// movie: Function() => [{ movie_id: '1', user_id: '97', rating: '5', like: '17' }, ...]
// movie dataset, array data
const movie = sample.movie();
Naive Bayes classifier
const {Bayes} = require('nodeml');
let bayes = new Bayes(); // this is bayes classfier
training bayes classifier
bayes.train([0.2, 0.5, 0.7, 0.4], 1);
bayes.train({ 'my': 20, 'home': 30 }, 1);
// training bulk
bayes.train([[2, 5,], [2, 1,]], [1, 2]);
bayes.train([{}, {}], [1, 2]);
classify document
let result = bayes.test([2, 5, 1, 4]);
let result = bayes.test({'fun': 3, 'fast': 3, 'shoot': 2});
get trained result
let model = bayes.getModel();
let str = JSON.stringify(model);
set pre-trained
bayes.setModel(JSON.parse(str));
k-Nearest Neighbor Classifier
const {kNN} = require('nodeml');
let knn = new kNN();
training
knn.train([0.2, 0.5, 0.7, 0.4], 1);
knn.train({ 'my': 20, 'home': 30 }, 1);
// training bulk
knn.train([[2, 5,], [2, 1,]], [1, 2]);
knn.train([{ 'my': 20, 'home': 30 }, { 'my': 5, 'home': 10 }], [1, 2]);
classify document (default k is 3)
let result = knn.test([2, 5, 1, 4]);
let result = knn.test({'fun': 3, 'fast': 3, 'shoot': 2}, 5);
get trained result
let model = knn.getModel();
let str = JSON.stringify(model);
set pre-trained
knn.setModel(JSON.parse(str));
Convolutional Neural Network, based convnetjs
const {CNN} = require('nodeml');
let cnn = new CNN();
options object refer trainer option
at convnetjs
cnn.configure({learning_rate: 0.1, momentum: 0.001, batch_size: 5, l2_decay: 0.0001});
layer refer at convnetjs
var layer = [];
layer.push({type: 'input', out_sx: 1, out_sy: 1, out_depth: 8});
layer.push({type: 'svm', num_classes: 10});
cnn.makeLayer(layer);
// set pre-trained
cnn.setModel(JSON.parse(str));
cnn.train([0.2, 0.5, 0.7, 0.4], 1);
cnn.train({ 'my': 20, 'home': 30 }, 1);
// training bulk
cnn.train([[2, 5,], [2, 1,]], [1, 2]);
cnn.train([{}, {}], [1, 2]);
classify document
let result = cnn.test([2, 5, 1, 4]);
let result = cnn.test({'fun': 3, 'fast': 3, 'shoot': 2});
get trained result
let model = cnn.getModel();
let str = JSON.stringify(model);
k-Means Clustering
const {kMeans} = require('nodeml');
let kmeans = new kMeans();
training
kmeans.train([[2, 5,], [2, 1,]], {
k: 10, dm: 0.00001, iter: 100,
proc: (iter, j, d)=> { console.log(iter, j, d); }
});
options | description | type | default |
---|---|---|---|
init | cluster initialize function: random , fuzzy (preparing) |
string | 'random' |
k | number of cluster | integer | 3 |
dm | distortion measure | float | 0.00 |
iter | maximum iteration | integer | unlimited |
labels | supervised learning, if labels exists, detect k automatically | array | null |
proc | process handler | function | null |
classify document (default k is 3)
let result = kmeans.test([[2, 5,], [2, 1,]]);
get trained result
let model = kmeans.getModel();
let str = JSON.stringify(model);
set pre-trained
kmeans.setModel(JSON.parse(str));
Collaborative Filtering Function
const {CF, evaluation} = require('../index');
let train = [[1, 1, 2], [1, 2, 2], [1, 4, 5], [2, 3, 2],
[2, 5, 1], [3, 1, 2], [3, 2, 3], [3, 3, 3]];
let test = [[3, 4, 1]];
const cf = new CF();
cf.train(train);
let gt = cf.gt(test);
let result = cf.recommendGT(gt, 1);
let ndcg = evaluation.ndcg(gt, result);
console.log(gt);
console.log(result);
console.log(ndcg);
let {evaluate} = require('nodeml');
let original = [1, 2, 1, 1, 3]; // original label
let result = [1, 1, 2, 1, 3]; // train result label
// exec evaluate, this contains accuracy, micro/macro precision/recall/f-measure
let accuracy = evaluate.accuracy(original, result);
let {CF, evaluate} = require('nodeml');
const cf = new CF();
let gt = cf.gt(test, 'user_id', 'movie_id', 'rating');
let result = cf.recommandToUsers(users, 40);
let ndcg = evaluation.ndcg(gt, result);