UNPKG

qminer

Version:

A C++ based data analytics platform for processing large-scale real-time streams containing structured and unstructured data

557 lines (553 loc) 24.8 kB
<!doctype html> <html> <head> <meta name="generator" content="JSDoc 3"> <meta charset="utf-8"> <title>Class: SVC</title> <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Karla:400,400i,700,700i" type="text/css"> <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Noto+Serif:400,400i,700,700i" type="text/css"> <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Inconsolata:500" type="text/css"> <link href="css/baseline.css" rel="stylesheet"> </head> <body onload="prettyPrint()"> <nav id="jsdoc-navbar" role="navigation" class="jsdoc-navbar"> <div id="jsdoc-navbar-container"> <div id="jsdoc-navbar-content"> <a href="index.html" class="jsdoc-navbar-package-name">QMiner JavaScript API v9.4.0</a> </div> </div> </nav> <div id="jsdoc-body-container"> <div id="jsdoc-content"> <div id="jsdoc-content-container"> <div id="jsdoc-main" role="main"> <header class="page-header"> <div class="symbol-detail-labels"><span class="label label-kind">class</span>&nbsp;<span class="label label-static">static</span></div> <h1><small><a href="module-analytics.html">analytics</a>.<wbr></small><span class="symbol-name">SVC</span></h1> <p class="source-link">Source: <a href="analyticsdoc.js.html#source-line-101">analyticsdoc.<wbr>js:101</a></p> <div class="symbol-classdesc"> <p>Support Vector Machine Classifier. Implements a soft margin linear support vector classifier using the PEGASOS algorithm, see: <a href="http://ttic.uchicago.edu/~nati/Publications/PegasosMPB.pdf">Pegasos: Primal Estimated sub-GrAdient SOlver for SVM</a>.</p> </div> <dl class="dl-compact"> </dl> </header> <section id="summary"> <div class="summary-callout"> <h2 class="summary-callout-heading">Property</h2> <div class="summary-content"> <div class="summary-column"> <dl class="dl-summary-callout"> <dt><a href="module-analytics.SVC.html#weights">weights</a></dt> <dd> </dd> </dl> </div> <div class="summary-column"> </div> <div class="summary-column"> </div> </div> </div> <div class="summary-callout"> <h2 class="summary-callout-heading">Methods</h2> <div class="summary-content"> <div class="summary-column"> <dl class="dl-summary-callout"> <dt><a href="module-analytics.SVC.html#decisionFunction">decisionFunction(X)</a></dt> <dd> </dd> <dt><a href="module-analytics.SVC.html#fit">fit(X, y)</a></dt> <dd> </dd> <dt><a href="module-analytics.SVC.html#getModel">getModel()</a></dt> <dd> </dd> </dl> </div> <div class="summary-column"> <dl class="dl-summary-callout"> <dt><a href="module-analytics.SVC.html#getParams">getParams()</a></dt> <dd> </dd> <dt><a href="module-analytics.SVC.html#predict">predict(X)</a></dt> <dd> </dd> <dt><a href="module-analytics.SVC.html#save">save(fout)</a></dt> <dd> </dd> </dl> </div> <div class="summary-column"> <dl class="dl-summary-callout"> <dt><a href="module-analytics.SVC.html#setParams">setParams(param)</a></dt> <dd> </dd> </dl> </div> </div> </div> </section> <section> <h2 id="SVC">new&nbsp;<span class="symbol-name">SVC</span><span class="signature"><span class="signature-params">([arg])</span></span></h2> <p>SVC</p> <section> <h3> Example </h3> <div> <pre class="prettyprint"><code>// import modules var la &#x3D; require(&#x27;qminer&#x27;).la; var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; // CLASSIFICATION WITH SVC // set up fake train and test data // four training examples with number of features &#x3D; 2 var featureMatrix &#x3D; new la.Matrix({ rows: 2, cols: 4, random: true }); // classification targets for four examples var targets &#x3D; new la.Vector([-1, -1, 1, 1]); // set up the classification model var SVC &#x3D; new analytics.SVC({ verbose: false }); // train classifier SVC.fit(featureMatrix, targets); // set up a fake test vector var test &#x3D; new la.Vector([1.1, -0.5]); // predict the target value var prediction &#x3D; SVC.predict(test);</code></pre> </div> </section> <section> <h3>Parameter</h3> <table class="jsdoc-details-table"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Optional</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td> <p>arg</p> </td> <td> <p>(<a href="module-analytics.html#~SVMParam">module:analytics~SVMParam</a> or <a href="module-fs.FIn.html">module:fs.FIn</a>)</p> </td> <td> <p>Yes</p> </td> <td> <p>Construction arguments. There are two ways of constructing: <br>1. Using the <a href="module-analytics.html#~SVMParam">module:analytics~SVMParam</a> object, <br>2. using the file input stream <a href="module-fs.FIn.html">module:fs.FIn</a>. </p> </td> </tr> </tbody> </table> </section> <dl class="dl-compact"> </dl> </section> <section> <h2>Property</h2> <section> <h3 id="weights"><span class="symbol-name">weights</span></h3> <p>Gets the vector of coefficients of the linear model. Type <a href="module-la.Vector.html">module:la.Vector</a>.</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>// import the analytics and la modules var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; var la &#x3D; require(&#x27;qminer&#x27;).la; // create a new SVC object var SVC &#x3D; new analytics.SVC(); // create the matrix containing the input features and the input vector for each matrix. var matrix &#x3D; new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]); var vec &#x3D; new la.Vector([1, 1, -1, -1]); // fit the model SVC.fit(matrix, vec); // get the weights var weights &#x3D; SVC.weights; // returns the coefficients of the normal vector of the hyperplane gained from the model: [1, 1]</code></pre> </div> </section> <dl class="dl-compact"> </dl> </section> <h2>Methods</h2> <section> <h3 id="decisionFunction"><span class="symbol-name">decisionFunction</span><span class="signature"><span class="signature-params">(X)</span>&nbsp;&rarr; <span class="signature-returns"> (number or <a href="module-la.Vector.html">module:la.Vector</a>)</span></span></h3> <p>Sends vector through the model and returns the distance to the decision boundery.</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>// import the analytics and la modules var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; var la &#x3D; require(&#x27;qminer&#x27;).la; // create a new SVC object var SVC &#x3D; new analytics.SVC(); // create the matrix containing the input features and the input vector for each matrix var matrix &#x3D; new la.Matrix([[1, 0], [0, -1]]); var vec &#x3D; new la.Vector([1, -1]); // fit the model SVC.fit(matrix, vec); // create the vector you want to get the distance from the model var vec2 &#x3D; new la.Vector([2, 3]); // use the decisionFunction to get the distance of vec2 from the model var distance &#x3D; SVC.decisionFunction(vec2); // returns something close to 5</code></pre> </div> </section> <section> <h4>Parameter</h4> <table class="jsdoc-details-table"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Optional</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td> <p>X</p> </td> <td> <p>(<a href="module-la.Vector.html">module:la.Vector</a>, <a href="module-la.SparseVector.html">module:la.SparseVector</a>, <a href="module-la.Matrix.html">module:la.Matrix</a>, or <a href="module-la.SparseMatrix.html">module:la.SparseMatrix</a>)</p> </td> <td> <p>&nbsp;</p> </td> <td> <p>Input feature vector or matrix with feature vectors as columns.</p> </td> </tr> </tbody> </table> </section> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code>(number or <a href="module-la.Vector.html">module:la.Vector</a>)</code>B Distance: <br>1. Real number, if <code>X</code> is <a href="module-la.Vector.html">module:la.Vector</a> or <a href="module-la.SparseVector.html">module:la.SparseVector</a>. <br>2. <a href="module-la.Vector.html">module:la.Vector</a>, if <code>X</code> is <a href="module-la.Matrix.html">module:la.Matrix</a> or <a href="module-la.SparseMatrix.html">module:la.SparseMatrix</a>. <br>Sign of the number corresponds to the class and the magnitude corresponds to the distance from the margin (certainty). </p> </dd> </dl> <h3 id="fit"><span class="symbol-name">fit</span><span class="signature"><span class="signature-params">(X, y)</span>&nbsp;&rarr; <span class="signature-returns"> <a href="module-analytics.SVC.html">module:analytics.SVC</a></span></span></h3> <p>Fits a SVM classification model, given column examples in a matrix and vector of targets.</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>// import the analytics and la modules var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; var la &#x3D; require(&#x27;qminer&#x27;).la; // create a new SVC object var SVC &#x3D; new analytics.SVC(); // create the matrix containing the input features and the input vector for each matrix. var matrix &#x3D; new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]); var vec &#x3D; new la.Vector([1, 1, -1, -1]); // fit the model SVC.fit(matrix, vec); // creates a model, where the hyperplane has the normal semi-equal to [1, 1]</code></pre> </div> </section> <section> <h4>Parameters</h4> <table class="jsdoc-details-table"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Optional</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td> <p>X</p> </td> <td> <p>(<a href="module-la.Matrix.html">module:la.Matrix</a> or <a href="module-la.SparseMatrix.html">module:la.SparseMatrix</a>)</p> </td> <td> <p>&nbsp;</p> </td> <td> <p>Input feature matrix where columns correspond to feature vectors.</p> </td> </tr> <tr> <td> <p>y</p> </td> <td> <p><a href="module-la.Vector.html">module:la.Vector</a></p> </td> <td> <p>&nbsp;</p> </td> <td> <p>Input vector of targets, one for each column of X.</p> </td> </tr> </tbody> </table> </section> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code><a href="module-analytics.SVC.html">module:analytics.SVC</a></code>B Self. The model has been created.</p> </dd> </dl> <h3 id="getModel"><span class="symbol-name">getModel</span><span class="signature"><span class="signature-params">()</span>&nbsp;&rarr; <span class="signature-returns"> Object</span></span></h3> <p>Get the model.</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>// import analytics module var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; // create a SVC model var SVC &#x3D; new analytics.SVC(); // get the properties of the model var model &#x3D; SVC.getModel();</code></pre> </div> </section> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code>Object</code>B The <code>svmModel</code> object containing the property: <br> 1. <code>svmModel.weights</code> - The weights of the model. Type <a href="module-la.Vector.html">module:la.Vector</a>. </p> </dd> </dl> <h3 id="getParams"><span class="symbol-name">getParams</span><span class="signature"><span class="signature-params">()</span>&nbsp;&rarr; <span class="signature-returns"> <a href="module-analytics.html#~SVMParam">module:analytics~SVMParam</a></span></span></h3> <p>Gets the SVC parameters.</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>// import analytics module var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; // create a new SVC model with json var SVC &#x3D; new analytics.SVC({ c: 5, j: 10, batchSize: 2000, maxIterations: 12000, maxTime: 2, minDiff: 1e-10, verbose: true }); // get the parameters of the SVC model // returns { algorithm: &#x27;SGD&#x27; c: 5, j: 10, eps: 0.1, batchSize: 2000, maxIterations: 12000, maxTime: 2, minDiff: 1e-10, verbose: true } var json &#x3D; SVC.getParams();</code></pre> </div> </section> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code><a href="module-analytics.html#~SVMParam">module:analytics~SVMParam</a></code>B Parameters of the classifier model.</p> </dd> </dl> <h3 id="predict"><span class="symbol-name">predict</span><span class="signature"><span class="signature-params">(X)</span>&nbsp;&rarr; <span class="signature-returns"> (number or <a href="module-la.Vector.html">module:la.Vector</a>)</span></span></h3> <p>Sends vector through the model and returns the prediction as a real number.</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>// import the analytics and la modules var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; var la &#x3D; require(&#x27;qminer&#x27;).la; // create a new SVC object var SVC &#x3D; new analytics.SVC(); // create the matrix containing the input features and the input vector for each matrix var matrix &#x3D; new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]); var vec &#x3D; new la.Vector([1, 1, -1, -1]); // fit the model SVC.fit(matrix, vec); // create a vector you want to predict var vec2 &#x3D; new la.Vector([3, 5]); // predict the vector var prediction &#x3D; SVC.predict(vec2); // returns 1</code></pre> </div> </section> <section> <h4>Parameter</h4> <table class="jsdoc-details-table"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Optional</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td> <p>X</p> </td> <td> <p>(<a href="module-la.Vector.html">module:la.Vector</a>, <a href="module-la.SparseVector.html">module:la.SparseVector</a>, <a href="module-la.Matrix.html">module:la.Matrix</a>, or <a href="module-la.SparseMatrix.html">module:la.SparseMatrix</a>)</p> </td> <td> <p>&nbsp;</p> </td> <td> <p>Input feature vector or matrix with feature vectors as columns.</p> </td> </tr> </tbody> </table> </section> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code>(number or <a href="module-la.Vector.html">module:la.Vector</a>)</code>B Prediction: <br>1. Real number, if <code>X</code> is <a href="module-la.Vector.html">module:la.Vector</a> or <a href="module-la.SparseVector.html">module:la.SparseVector</a>. <br>2. <a href="module-la.Vector.html">module:la.Vector</a>, if <code>X</code> is <a href="module-la.Matrix.html">module:la.Matrix</a> or <a href="module-la.SparseMatrix.html">module:la.SparseMatrix</a>. <br>1 for positive class and -1 for negative. </p> </dd> </dl> <h3 id="save"><span class="symbol-name">save</span><span class="signature"><span class="signature-params">(fout)</span>&nbsp;&rarr; <span class="signature-returns"> <a href="module-fs.FOut.html">module:fs.FOut</a></span></span></h3> <p>Saves model to output file stream.</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>// import the analytics and la modules var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; var la &#x3D; require(&#x27;qminer&#x27;).la; var fs &#x3D; require(&#x27;qminer&#x27;).fs; // create a new SVC object var SVC &#x3D; new analytics.SVC(); // create the matrix containing the input features and the input vector for each matrix column. var matrix &#x3D; new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]); var vec &#x3D; new la.Vector([1, 0, -1, -2]); // fit the model SVC.fit(matrix, vec); // create output stream var fout &#x3D; fs.openWrite(&#x27;svc_example.bin&#x27;); // save SVC object (model and parameters) to output stream and close it SVC.save(fout); fout.close(); // create input stream var fin &#x3D; fs.openRead(&#x27;svc_example.bin&#x27;); // create a SVC object that loads the model and parameters from input stream var SVC2 &#x3D; new analytics.SVC(fin);</code></pre> </div> </section> <section> <h4>Parameter</h4> <table class="jsdoc-details-table"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Optional</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td> <p>fout</p> </td> <td> <p><a href="module-fs.FOut.html">module:fs.FOut</a></p> </td> <td> <p>&nbsp;</p> </td> <td> <p>Output stream.</p> </td> </tr> </tbody> </table> </section> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code><a href="module-fs.FOut.html">module:fs.FOut</a></code>B The output stream <code>fout</code>.</p> </dd> </dl> <h3 id="setParams"><span class="symbol-name">setParams</span><span class="signature"><span class="signature-params">(param)</span>&nbsp;&rarr; <span class="signature-returns"> <a href="module-analytics.SVC.html">module:analytics.SVC</a></span></span></h3> <p>Sets the SVC parameters.</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>// import analytics module var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; // create a default SVC model var SVC &#x3D; new analytics.SVC(); // change the parameters of the SVC with the json { j: 5, maxIterations: 12000, minDIff: 1e-10 } SVC.setParams({ j: 5, maxIterations: 12000, minDiff: 1e-10 }); // returns self</code></pre> </div> </section> <section> <h4>Parameter</h4> <table class="jsdoc-details-table"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Optional</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td> <p>param</p> </td> <td> <p><a href="module-analytics.html#~SVMParam">module:analytics~SVMParam</a></p> </td> <td> <p>&nbsp;</p> </td> <td> <p>Classifier training parameters.</p> </td> </tr> </tbody> </table> </section> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code><a href="module-analytics.SVC.html">module:analytics.SVC</a></code>B Self. Updated the training parameters.</p> </dd> </dl> </section> </section> </div> </div> <nav id="jsdoc-toc-nav" role="navigation"></nav> </div> </div> <footer id="jsdoc-footer" class="jsdoc-footer"> <div id="jsdoc-footer-container"> <p> </p> </div> </footer> <script src="scripts/jquery.min.js"></script> <script src="scripts/tree.jquery.js"></script> <script src="scripts/prettify.js"></script> <script src="scripts/jsdoc-toc.js"></script> <script src="scripts/linenumber.js"></script> <script src="scripts/scrollanchor.js"></script> </body> </html>