{"id":21732190,"url":"https://github.com/nicolaspanel/node-svm","last_synced_at":"2025-04-05T17:04:30.864Z","repository":{"id":15384780,"uuid":"18116327","full_name":"nicolaspanel/node-svm","owner":"nicolaspanel","description":"Support Vector Machines for nodejs","archived":false,"fork":false,"pushed_at":"2019-03-26T12:35:59.000Z","size":13251,"stargazers_count":298,"open_issues_count":10,"forks_count":47,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-03-29T16:11:08.498Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nicolaspanel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-25T20:57:04.000Z","updated_at":"2025-03-22T23:40:40.000Z","dependencies_parsed_at":"2022-08-04T04:30:25.740Z","dependency_job_id":null,"html_url":"https://github.com/nicolaspanel/node-svm","commit_stats":{"total_commits":253,"total_committers":10,"mean_commits":25.3,"dds":"0.18577075098814233","last_synced_commit":"38f0718e25ecf449c9e0636de50c0a677ae84266"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicolaspanel%2Fnode-svm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicolaspanel%2Fnode-svm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicolaspanel%2Fnode-svm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicolaspanel%2Fnode-svm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nicolaspanel","download_url":"https://codeload.github.com/nicolaspanel/node-svm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247369953,"owners_count":20927928,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-26T04:29:08.409Z","updated_at":"2025-04-05T17:04:30.842Z","avatar_url":"https://github.com/nicolaspanel.png","language":"JavaScript","readme":"# node-svm\n\nSupport Vector Machine (SVM) library for [nodejs](http://nodejs.org/).\n\n[![NPM](https://nodei.co/npm/node-svm.png)](https://nodei.co/npm/node-svm/)\n[![Build Status](https://travis-ci.org/nicolaspanel/node-svm.png)](https://travis-ci.org/nicolaspanel/node-svm) [![Coverage Status](https://coveralls.io/repos/nicolaspanel/node-svm/badge.png?branch=master)](https://coveralls.io/r/nicolaspanel/node-svm?branch=master)\n\n# Support Vector Machines\n[Wikipedia](http://en.wikipedia.org/wiki/Support_vector_machine)  :\n\n\u003eSupport vector machines are supervised learning models that analyze data and recognize patterns. \n\u003eA special property is that they simultaneously minimize the empirical classification error and maximize the geometric margin; hence they are also known as maximum margin classifiers.\n\u003e[![Wikipedia image](http://upload.wikimedia.org/wikipedia/commons/1/1b/Kernel_Machine.png)](http://en.wikipedia.org/wiki/File:Kernel_Machine.png)\n\n# Installation\n`npm install --save node-svm`\n\n# Quick start\nIf you are not familiar with SVM I highly recommend this [guide](http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf).\n\nHere's an example of using [node-svm](https://github.com/nicolaspanel/node-svm) to approximate the XOR function :\n\n```javascript\nvar svm = require('node-svm');\n\nvar xor = [\n    [[0, 0], 0],\n    [[0, 1], 1],\n    [[1, 0], 1],\n    [[1, 1], 0]\n];\n\n// initialize a new predictor\nvar clf = new svm.CSVC();\n\nclf.train(xor).done(function () {\n    // predict things\n    xor.forEach(function(ex){\n        var prediction = clf.predictSync(ex[0]);\n        console.log('%d XOR %d =\u003e %d', ex[0][0], ex[0][1], prediction);\n    });\n});\n\n/******** CONSOLE ********\n    0 XOR 0 =\u003e 0\n    0 XOR 1 =\u003e 1\n    1 XOR 0 =\u003e 1\n    1 XOR 1 =\u003e 0\n */\n```\n\nMore examples are available [here](https://github.com/nicolaspanel/node-svm/tree/master/examples).\n\n__Note__: There's no reason to use SVM to figure out XOR BTW...\n\n\n# API\n\n## Classifiers\n\nPossible classifiers are:\n\n| Classifier  | Type                   | Params         | Initialization                |\n|-------------|------------------------|----------------|-------------------------------|\n| C_SVC       | multi-class classifier | `c`            | `= new svm.CSVC(opts)`        |\n| NU_SVC      | multi-class classifier | `nu`           | `= new svm.NuSVC(opts)`       |\n| ONE_CLASS   | one-class classifier   | `nu`           | `= new svm.OneClassSVM(opts)` |\n| EPSILON_SVR | regression             | `c`, `epsilon` | `= new svm.EpsilonSVR(opts)`  |\n| NU_SVR      | regression             | `c`, `nu`      | `= new svm.NuSVR(opts)`       |\n\n## Kernels\n\nPossible kernels are:\n\n| Kernel  | Parameters                     |\n|---------|--------------------------------|\n| LINEAR  | No parameter                   |\n| POLY    | `degree`, `gamma`, `r`         |\n| RBF     |`gamma`                         |\n| SIGMOID | `gamma`, `r`                   |\n\n\n## Parameters and options\n\nPossible parameters/options are:  \n\n| Name             | Default value(s)       | Description                                                                                           |\n|------------------|------------------------|-------------------------------------------------------------------------------------------------------|\n| svmType          | `C_SVC`                | Used classifier                                                                                       | \n| kernelType       | `RBF`                  | Used kernel                                                                                           |\n| c                | `[0.01,0.125,0.5,1,2]` | Cost for `C_SVC`, `EPSILON_SVR` and `NU_SVR`. Can be a `Number` or an `Array` of numbers              |\n| nu               | `[0.01,0.125,0.5,1]`   | For `NU_SVC`, `ONE_CLASS` and `NU_SVR`. Can be a `Number` or an `Array` of numbers                    |\n| epsilon          | `[0.01,0.125,0.5,1]`   | For `EPSILON_SVR`. Can be a `Number` or an `Array` of numbers                                         |\n| degree           | `[2,3,4]`              | For `POLY` kernel. Can be a `Number` or an `Array` of numbers                                         |\n| gamma            | `[0.001,0.01,0.5]`     | For `POLY`, `RBF` and `SIGMOID` kernels. Can be a `Number` or an `Array` of numbers                   |\n| r                | `[0.125,0.5,0,1]`      | For `POLY` and `SIGMOID` kernels. Can be a `Number` or an `Array` of numbers                          |\n| kFold            | `4`                    | `k` parameter for [k-fold cross validation]( http://en.wikipedia.org/wiki/Cross-validation_(statistics)#k-fold_cross-validation). `k` must be \u003e= 1. If `k===1` then entire dataset is use for both testing and training.  |\n| normalize        | `true`                 | Whether to use [mean normalization](http://en.wikipedia.org/wiki/Normalization_(statistics)) during data pre-processing  |\n| reduce           | `true`                 | Whether to use [PCA](http://en.wikipedia.org/wiki/Principal_component_analysis) to reduce dataset's dimensions during data pre-processing  |\n| retainedVariance | `0.99`                 | Define the acceptable impact on data integrity (require `reduce` to be `true`)                        |\n| eps              | `1e-3`                 | Tolerance of termination criterion                                                                    |\n| cacheSize        | `200`                  | Cache size in MB.                                                                                     |\n| shrinking        | `true`                 | Whether to use the shrinking heuristics                                                               |\n| probability      | `false`                | Whether to train a SVC or SVR model for probability estimates                                         |\n\nThe example below shows how to use them:\n\n```javascript\nvar svm = require('node-svm');\n\nvar clf = new svm.SVM({\n    svmType: 'C_SVC',\n    c: [0.03125, 0.125, 0.5, 2, 8], \n    \n    // kernels parameters\n    kernelType: 'RBF',  \n    gamma: [0.03125, 0.125, 0.5, 2, 8],\n    \n    // training options\n    kFold: 4,               \n    normalize: true,        \n    reduce: true,           \n    retainedVariance: 0.99, \n    eps: 1e-3,              \n    cacheSize: 200,               \n    shrinking : true,     \n    probability : false     \n});\n```\n\n__Notes__ :   \n * You can override default values by  creating a `.nodesvmrc` file (JSON) at the root of your project.\n * If at least one parameter has multiple values, [node-svm](https://github.com/nicolaspanel/node-svm/) will go through all possible combinations to see which one gives the best results (it performs grid-search to maximize [f-score](http://en.wikipedia.org/wiki/F1_score) for classification and minimize [Mean Squared Error](http://en.wikipedia.org/wiki/Mean_squared_error) for regression).\n\n\n##Training\n\nSVMs can be trained using `svm#train(dataset)` method.\n\nPseudo code : \n```javascript\nvar clf = new svm.SVM(options);\n\nclf\n.train(dataset)\n.progress(function(rate){\n    // ...\n})\n.spread(function(trainedModel, trainingReport){\n    // ...\n});\n```\n\n__Notes__ :  \n * `trainedModel` can be used to restore the predictor later (see [this example](https://github.com/nicolaspanel/node-svm/blob/master/examples/save-prediction-model-example.js) for more information).\n * `trainingReport` contains information about predictor's accuracy (such as MSE, precison, recall, fscore, retained variance etc.)\n\n## Prediction\nOnce trained, you can use the classifier object to predict values for new inputs. You can do so : \n * Synchronously using `clf#predictSync(inputs)`\n * Asynchronously using `clf#predict(inputs).then(function(predicted){ ... });`\n\n**If you enabled probabilities during initialization**  you can also predict probabilities for each class  : \n * Synchronously using `clf#predictProbabilitiesSync(inputs)`. \n * Asynchronously using `clf#predictProbabilities(inputs).then(function(probabilities){ ... })`.\n\n__Note__ : `inputs` must be a 1d array of numbers\n\n## Model evaluation\nOnce the predictor is trained it can be evaluated against a test set. \n\nPseudo code : \n```javascript\nvar svm = require('node-svm');\nvar clf = new svm.SVM(options);\n \nsvm.read(trainFile)\n.then(function(dataset){\n    return clf.train(dataset);\n})\n.then(function(trainedModel, trainingReport){\n     return svm.read(testFile);\n})\n.then(function(testset){\n    return clf.evaluate(testset);\n})\n.done(function(report){\n    console.log(report);\n});\n ```\n# CLI\n\n[node-svm](https://github.com/nicolaspanel/node-svm/) comes with a build-in Command Line Interpreter.\n\nTo use it you have to install [node-svm](https://github.com/nicolaspanel/node-svm/) globally using `npm install -g node-svm`.\n\nSee `$ node-svm -h` for complete command line reference.\n\n\n## help\n```shell\n$ node-svm help [\u003ccommand\u003e]\n```\nDisplay help information about [node-svm](https://github.com/nicolaspanel/node-svm/) \n\n\n## train\n```shell\n$ node-svm train \u003cdataset file\u003e [\u003cwhere to save the prediction model\u003e] [\u003coptions\u003e]\n```\nTrain a new model with given data set\n\n__Note__: use `$ node-svm train \u003cdataset file\u003e -i` to set parameters values dynamically.\n\n## evaluate\n```shell\n$ node-svm evaluate \u003cmodel file\u003e \u003ctestset file\u003e [\u003coptions\u003e]\n```\nEvaluate model's accuracy against a test set\n\n# How it work\n\n`node-svm` uses the official libsvm C++ library, version 3.20. \n\nFor more information see also : \n * [libsvm web site](http://www.csie.ntu.edu.tw/~cjlin/libsvm/)\n * Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for support vector machines. ACM Transactions on Intelligent Systems and Technology, 2:27:1--27:27, 2011.\n * [Wikipedia article about SVM](https://en.wikipedia.org/wiki/Support_vector_machine)\n * [node addons](http://nodejs.org/api/addons.html)\n\n# Contributions\nFeel free to fork and improve/enhance `node-svm` in any way your want.\n\nIf you feel that the community will benefit from your changes, please send a pull request : \n * Fork the project.\n * Make your feature addition or bug fix.\n * Add documentation if necessary.\n * Add tests for it. This is important so I don't break it in a future version unintentionally (run `grunt` or `npm test`).\n * Send a pull request to the `develop` branch. \n\n#FAQ\n###Segmentation fault\nQ : Node returns 'segmentation fault' error during training. What's going on?\n\nA1 : Your dataset is empty or its format is incorrect.\n\nA2 : Your dataset is too big.\n\n###Difference between nu-SVC and C-SVC\nQ : What is the difference between nu-SVC and C-SVC?\n\nA : [Answer here](http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f411)\n\n###Other questions\n * Take a look at [libsvm's FAQ](http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html).\n * Create [an issue](https://github.com/nicolaspanel/node-svm/issues)\n\n# License\nMIT\n\n[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/92d9dd8573d8b458d19a240629fea97a \"githalytics.com\")](http://githalytics.com/nicolaspanel/node-svm)\n","funding_links":[],"categories":["Javascript","JavaScript","[](https://github.com/josephmisiti/awesome-machine-learning/blob/master/README.md#javascript)Javascript"],"sub_categories":["Tools","[Tools](#tools-1)","Speech Recognition"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicolaspanel%2Fnode-svm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnicolaspanel%2Fnode-svm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicolaspanel%2Fnode-svm/lists"}