Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Machine Learning Framework for Node
nodeml.feature.tfidf
: tfidfnodeml.Bayes
: Bayesnodeml.kNN
: k-Nearest Neighbornodeml.CNN
: Convolutional Neural Network (CNN)nodeml.kMeans
: k-Meansnodeml.CF
: User based Collaborative Filteringnodeml.accuracy
: Precision, Recall, F-Measure, Accuracynodeml.ndcg
: NDCGinstallation 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);
FAQs
node.js machine learning package
The npm package nodeml receives a total of 318 weekly downloads. As such, nodeml popularity was classified as not popular.
We found that nodeml demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.