UNPKG

qminer

Version:

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

922 lines (921 loc) 251 kB
<!doctype html> <html> <head> <meta name="generator" content="JSDoc 3"> <meta charset="utf-8"> <title>Source: analyticsdoc.js</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">Home</a> </div> </div> </nav> <div id="jsdoc-body-container"> <div id="jsdoc-content"> <div id="jsdoc-content-container"> <div id="jsdoc-banner" role="banner"> </div> <div id="jsdoc-main" role="main"> <header class="page-header"> <h1>Source: analyticsdoc.js</h1> </header> <article> <pre class="prettyprint linenums"><code>/** * Copyright (c) 2015, Jozef Stefan Institute, Quintelligence d.o.o. and contributors * All rights reserved. * * This source code is licensed under the FreeBSD license found in the * LICENSE file in the root directory of this source tree. */ /** * Analytics module. * @module analytics * @example * // import modules * var qm &#x3D; require(&#x27;qminer&#x27;); * var analytics &#x3D; qm.analytics; * // load dataset, create model, evaluate model */ /** * Calculates the non-negative matrix factorization, see: {@link https://en.wikipedia.org/wiki/Non-negative_matrix_factorization}. * @param {(module:la.Matrix | module:la.SparseMatrix)} mat - The non-negative matrix. * @param {number} k - The reduced rank, e.g. number of columns in matrix U and number of rows in matrix V. Must be between 0 and &#x60;min(mat.rows, mat.cols)&#x60;. * @param {Object} [json] - Algorithm options. * @param {number} [json.iter &#x3D; 100] - The number of iterations used for the algorithm. * @param {number} [json.tol &#x3D; 1e-3] - The tolerance. * @param {boolean} [json.verbose &#x3D; false] - If false, the console output is supressed. * @returns {Object} The json object &#x60;nmfRes&#x60; containing the non-negative matrices U and V: * &amp;lt;br&gt; &#x60;nmfRes.U&#x60;- The {@link module:la.Matrix} representation of the matrix U, * &amp;lt;br&gt; &#x60;nmfRes.V&#x60;- The {@link module:la.Matrix} representation of the matrix V. * @example &amp;lt;caption&gt;Asynchronous function&amp;lt;/caption&gt; * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a matrix * var mat &#x3D; new la.Matrix({ rows: 10, cols: 5, random: true }); * // compute the non-negative matrix factorization * analytics.nmfAsync(mat, 3, { iter: 100, tol: 1e-4 }, function (err, result) { * if (err) { console.log(err); } * // calculation successful * var U &#x3D; result.U; * var V &#x3D; result.V; * }); * @example &amp;lt;caption&gt;Synchronous function&amp;lt;/caption&gt; * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a matrix * var mat &#x3D; new la.Matrix({ rows: 10, cols: 5, random: true }); * // compute the non-negative matrix factorization * var result &#x3D; analytics.nmf(mat, 3, { iter: 100, tol: 1e-4 }); * var U &#x3D; result.U; * var V &#x3D; result.V; */ exports.prototype.nmf &#x3D; function (mat, k, json) { return { &quot;U&quot;: Object.create(require(&#x27;qminer&#x27;).la.Matrix.prototype), &quot;V&quot;: Object.create(require(&#x27;qminer&#x27;).la.Matrix.prototype) }; } /** * @typedef {Object} SVMParam * SVM constructor parameters. Used for the construction of {@link module:analytics.SVC} and {@link module:analytics.SVR}. * @property {string} [algorithm&#x3D;&#x27;SGD&#x27;] - The algorithm procedure. Possible options are &#x60;&#x27;SGD&#x27;&#x60; and &#x60;&#x27;LIBSVM&#x27;&#x60;. &#x60;&#x27;PR_LOQO&#x27;&#x60;is not supported anymore. * @property {number} [c&#x3D;1.0] - Cost parameter. Increasing the parameter forces the model to fit the training data more accurately (setting it too large may lead to overfitting) . * @property {number} [j&#x3D;1.0] - Unbalance parameter. Increasing it gives more weight to the positive examples (getting a better fit on the positive training examples gets a higher priority). Setting j&#x3D;n is like adding n-1 copies of the positive training examples to the data set. * @property {number} [eps&#x3D;1e-3] - Epsilon insensitive loss parameter. Larger values result in fewer support vectors (smaller model complexity) * @property {number} [batchSize&#x3D;1000] - Number of examples used in the subgradient estimation. Higher number of samples slows down the algorithm, but makes the local steps more accurate. * @property {number} [maxIterations&#x3D;10000] - Maximum number of iterations. * @property {number} [maxTime&#x3D;1] - Maximum runtime in seconds. * @property {number} [minDiff&#x3D;1e-6] - Stopping criterion tolerance. * @property {string} [type&#x3D;&#x27;C_SVC&#x27;] - The subalgorithm procedure in LIBSVM. Possible options are &#x60;&#x27;C_SVC&#x27;&#x60;, &#x60;&#x27;NU_SVC&#x27;&#x60; and &#x60;&#x27;ONE_CLASS&#x27;&#x60; for classification and &#x60;&#x27;EPSILON_SVR&#x27;&#x60;, &#x60;&#x27;NU_SVR&#x27;&#x60; and &#x60;&#x27;ONE_CLASS&#x27;&#x60; for regression. * @property {string} [kernel&#x3D;&#x27;LINEAR&#x27;] - Kernel type in LIBSVM. Possible options are &#x60;&#x27;LINEAR&#x27;&#x60;, &#x60;&#x27;POLY&#x27;&#x60;, &#x27;RBF&#x27;&#x60;, &#x27;SIGMOID&#x27;&#x60; and &#x60;&#x27;PRECOMPUTED&#x27;&#x60;. * @property {number} [gamma&#x3D;1.0] - Gamma parameter in LIBSVM. Set gamma in kernel function. * @property {number} [p&#x3D;1e-1] - P parameter in LIBSVM. Set the epsilon in loss function of epsilon-SVR. * @property {number} [degree&#x3D;1] - Degree parameter in LIBSVM. Set degree in kernel function. * @property {number} [nu&#x3D;1e-2] - Nu parameter in LIBSVM. Set the parameter nu of nu-SVC, one-class SVM, and nu-SVR. * @property {number} [coef0&#x3D;1.0] - Coef0 parameter in LIBSVM. Set coef0 in kernel function. * @property {number} [cacheSize&#x3D;100] - Set cache memory size in MB (default 100) in LIBSVM. * @property {boolean} [verbose&#x3D;false] - Toggle verbose output in the console. */ /** * SVC * @classdesc Support Vector Machine Classifier. Implements a soft margin linear support vector classifier using the PEGASOS algorithm, * see: {@link http://ttic.uchicago.edu/~nati/Publications/PegasosMPB.pdf Pegasos: Primal Estimated sub-GrAdient SOlver for SVM}. * @class * @param {module:analytics~SVMParam | module:fs.FIn} [arg] - Construction arguments. There are two ways of constructing: * &amp;lt;br&gt;1. Using the {@link module:analytics~SVMParam} object, * &amp;lt;br&gt;2. using the file input stream {@link module:fs.FIn}. * @example * // 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); */ exports.SVC &#x3D; function(arg) { return Object.create(require(&#x27;qminer&#x27;).analytics.SVC.prototype); }; /** * Gets the SVC parameters. * @returns {module:analytics~SVMParam} Parameters of the classifier model. * @example * // 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(); */ exports.SVC.prototype.getParams &#x3D; function() { return { algorithm: &#x27;&#x27;, c: 0, j: 0, eps: 0.1, batchSize: 0, maxIterations: 0, maxTime: 0, minDiff: 0, verbose: true } }; /** * Sets the SVC parameters. * @param {module:analytics~SVMParam} param - Classifier training parameters. * @returns {module:analytics.SVC} Self. Updated the training parameters. * @example * // 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 */ exports.SVC.prototype.setParams &#x3D; function(param) { return Object.create(require(&#x27;qminer&#x27;).analytics.SVC.prototype); }; /** * Gets the vector of coefficients of the linear model. Type {@link module:la.Vector}. * @example * // 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] */ exports.SVC.prototype.weights &#x3D; Object.create(require(&#x27;qminer&#x27;).la.Vector.prototype); /** * Saves model to output file stream. * @param {module:fs.FOut} fout - Output stream. * @returns {module:fs.FOut} The output stream &#x60;fout&#x60;. * @example * // 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); */ exports.SVC.prototype.save &#x3D; function(fout) { return Object.create(require(&#x27;qminer&#x27;).fs.FOut.prototype); } /** * Sends vector through the model and returns the distance to the decision boundery. * @param {module:la.Vector | module:la.SparseVector | module:la.Matrix | module:la.SparseMatrix} X - Input feature vector or matrix with feature vectors as columns. * @returns {number | module:la.Vector} Distance: * &amp;lt;br&gt;1. Real number, if &#x60;X&#x60; is {@link module:la.Vector} or {@link module:la.SparseVector}. * &amp;lt;br&gt;2. {@link module:la.Vector}, if &#x60;X&#x60; is {@link module:la.Matrix} or {@link module:la.SparseMatrix}. * &amp;lt;br&gt;Sign of the number corresponds to the class and the magnitude corresponds to the distance from the margin (certainty). * @example * // 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 */ exports.SVC.prototype.decisionFunction &#x3D; function(X) { return (X instanceof require(&#x27;qminer&#x27;).la.Vector | X instanceof require(&#x27;qminer&#x27;).la.SparseVector) ? 0 : Object.create(require(&#x27;qminer&#x27;).la.Vector.prototype); } /** * Sends vector through the model and returns the prediction as a real number. * @param {module:la.Vector | module:la.SparseVector | module:la.Matrix | module:la.SparseMatrix} X - Input feature vector or matrix with feature vectors as columns. * @returns {number | module:la.Vector} Prediction: * &amp;lt;br&gt;1. Real number, if &#x60;X&#x60; is {@link module:la.Vector} or {@link module:la.SparseVector}. * &amp;lt;br&gt;2. {@link module:la.Vector}, if &#x60;X&#x60; is {@link module:la.Matrix} or {@link module:la.SparseMatrix}. * &amp;lt;br&gt;1 for positive class and -1 for negative. * @example * // 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 */ exports.SVC.prototype.predict &#x3D; function(X) { return (X instanceof require(&#x27;qminer&#x27;).la.Vector | X instanceof require(&#x27;qminer&#x27;).la.SparseVector) ? 0 : Object.create(require(&#x27;qminer&#x27;).la.Vector.prototype); } /** * Fits a SVM classification model, given column examples in a matrix and vector of targets. * @param {module:la.Matrix | module:la.SparseMatrix} X - Input feature matrix where columns correspond to feature vectors. * @param {module:la.Vector} y - Input vector of targets, one for each column of X. * @returns {module:analytics.SVC} Self. The model has been created. * @example * // 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] */ exports.SVC.prototype.fit &#x3D; function(X, y) { return Object.create(require(&#x27;qminer&#x27;).analytics.SVC.prototype); } /** * SVR * @classdesc Support Vector Machine Regression. Implements a soft margin linear support vector regression using the PEGASOS algorithm with epsilon insensitive loss, see: {@link http://ttic.uchicago.edu/~nati/Publications/PegasosMPB.pdf Pegasos: Primal Estimated sub-GrAdient SOlver for SVM}. * @class * @param {module:analytics~SVMParam | module:fs.FIn} [arg] - Construction arguments. There are two ways of constructing: * &amp;lt;br&gt;1. Using the {@link module:analytics~SVMParam} object, * &amp;lt;br&gt;2. using the file input stream {@link module:fs.FIn}. * @example * // import module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // REGRESSION WITH SVR * // 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 }); * // Regression targets for four examples * var targets &#x3D; new la.Vector([1.1, -2, 3, 4.2]); * // Set up the regression model * var SVR &#x3D; new analytics.SVR({ verbose: false }); * // Train regression * SVR.fit(featureMatrix, targets); * // Set up a fake test vector * var test &#x3D; new la.Vector([1.1, -0.8]); * // Predict the target value * var prediction &#x3D; SVR.predict(test); */ exports.SVR &#x3D; function(arg) { return Object.create(require(&#x27;qminer&#x27;).analytics.SVR.prototype); }; /** * Gets the SVR parameters. * @returns {module:analytics~SVMParam} Parameters of the regression model. * @example * // import analytics module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create a new SVR object * var SVR &#x3D; new analytics.SVR({ c: 10, eps: 1e-10, maxTime: 12000, verbose: true }); * // get the parameters of SVR * var params &#x3D; SVR.getParams(); */ exports.SVR.prototype.getParams &#x3D; function() { return { algorithm: &#x27;&#x27;, c: 0, j: 0, eps: 0, batchSize: 0, maxIterations: 0, maxTime: 0, minDiff: 0, verbose: true } }; /** * Sets the SVR parameters. * @param {module:analytics~SVMParam} param - Regression training parameters. * @returns {module:analytics.SVR} Self. Updated the training parameters. * @example * // import analytics module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create a new SVR object * var SVR &#x3D; new analytics.SVR(); * // set the parameters of the SVR object * SVR.setParams({ c: 10, maxTime: 12000 }); */ exports.SVR.prototype.setParams &#x3D; function(param) { return Object.create(require(&#x27;qminer&#x27;).analytics.SVR.prototype); }; /** * The vector of coefficients of the linear model. Type {@link module:la.Vector}. * @example * // import the modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new SVR object * var SVR &#x3D; new analytics.SVR({ c: 10 }); * // create a matrix and vector for the model * var matrix &#x3D; new la.Matrix([[1, -1], [1, 1]]); * var vector &#x3D; new la.Vector([1, 1]); * // create the model by fitting the values * SVR.fit(matrix, vector); * // get the coeficients of the linear model * var coef &#x3D; SVR.weights; */ exports.SVR.prototype.weights &#x3D; Object.create(require(&#x27;qminer&#x27;).la.Vector.prototype); /** * Saves model to output file stream. * @param {module:fs.FOut} fout - Output stream. * @returns {module:fs.FOut} The output stream &#x60;fout&#x60;. * @example * // import the 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 SVR object * var SVR &#x3D; new analytics.SVR({ c: 10 }); * // create a matrix and vector for the model * var matrix &#x3D; new la.Matrix([[1, -1], [1, 1]]); * var vector &#x3D; new la.Vector([1, 1]); * // create the model by fitting the values * SVR.fit(matrix, vector); * // save the model in a binary file * var fout &#x3D; fs.openWrite(&#x27;svr_example.bin&#x27;); * SVR.save(fout); * fout.close(); * // construct a SVR model by loading from the binary file * var fin &#x3D; fs.openRead(&#x27;svr_example.bin&#x27;); * var SVR2 &#x3D; new analytics.SVR(fin); */ exports.SVR.prototype.save &#x3D; function(fout) { return Object.create(require(&#x27;qminer&#x27;).fs.FOut.prototype); } /** * Sends vector through the model and returns the scalar product as a real number. * @param {module:la.Vector | module:la.SparseVector | module:la.Matrix | module:la.SparseMatrix} X - Input feature vector or matrix with feature vectors as columns. * @returns {number | module:la.Vector} Distance: * &amp;lt;br&gt;1. Real number if &#x60;X&#x60; is {@link module:la.Vector} or {@link module:la.SparseVector}. * &amp;lt;br&gt;2. {@link module:la.Vector}, if &#x60;X&#x60; is {@link module:la.Matrix} or {@link module:la.SparseMatrix}. * @example * // import the modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new SVR object * var SVR &#x3D; new analytics.SVR({ c: 10 }); * // create a matrix and vector for the model * var matrix &#x3D; new la.Matrix([[1, -1], [1, 1]]); * var vector &#x3D; new la.Vector([1, 1]); * // create the model by fitting the values * SVR.fit(matrix, vector); * // get the distance between the model and the given vector * var vec2 &#x3D; new la.Vector([-5, 1]); * var distance &#x3D; SVR.decisionFunction(vec2); */ exports.SVR.prototype.decisionFunction &#x3D; function(X) { return (X instanceof require(&#x27;qminer&#x27;).la.Vector | X instanceof require(&#x27;qminer&#x27;).la.SparseVector) ? 0 : Object.create(require(&#x27;qminer&#x27;).la.Vector.prototype); } /** * Sends vector through the model and returns the prediction as a real number. * @param {module:la.Vector | module:la.SparseVector | module:la.Matrix | module:la.SparseMatrix} X - Input feature vector or matrix with feature vectors as columns. * @returns {number | module:la.Vector} Prediction: * &amp;lt;br&gt;1. Real number, if &#x60;X&#x60; is {@link module:la.Vector} or {@link module:la.SparseVector}. * &amp;lt;br&gt;2. {@link module:la.Vector}, if &#x60;X&#x60; is {@link module:la.Matrix} or {@link module:la.SparseMatrix}. * @example * // import the modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new SVR object * var SVR &#x3D; new analytics.SVR({ c: 10 }); * // create a matrix and vector for the model * var matrix &#x3D; new la.Matrix([[1, -1], [1, 1]]); * var vector &#x3D; new la.Vector([1, 1]); * // create the model by fitting the values * SVR.fit(matrix, vector); * // predict the value of the given vector * var vec2 &#x3D; new la.Vector([-5, 1]); * var prediction &#x3D; SVR.predict(vec2); */ exports.SVR.prototype.predict &#x3D; function(X) { return (X instanceof require(&#x27;qminer&#x27;).la.Vector | X instanceof require(&#x27;qminer&#x27;).la.SparseVector) ? 0 : Object.create(require(&#x27;qminer&#x27;).la.Vector.prototype); } /** * Fits a SVM regression model, given column examples in a matrix and vector of targets. * @param {module:la.Matrix | module:la.SparseMatrix} X - Input feature matrix where columns correspond to feature vectors. * @param {module:la.Vector} y - Input vector of targets, one for each column of X. * @returns {module:analytics.SVR} Self. The model has been created. * @example * // import the modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new SVR object * var SVR &#x3D; new analytics.SVR({ c: 10 }); * // create a matrix and vector for the model * var matrix &#x3D; new la.Matrix([[1, -1], [1, 1]]); * var vector &#x3D; new la.Vector([1, 1]); * // create the model by fitting the values * SVR.fit(matrix, vector); */ exports.SVR.prototype.fit &#x3D; function(X, y) { return Object.create(require(&#x27;qminer&#x27;).analytics.SVR.prototype); } /** * @typedef {Object} ridgeRegParam * An object used for the construction of {@link module:analytics.RidgeReg}. * @property {number} [gamma&#x3D;0.0] - The gamma value. */ /** * Ridge Regression * @class * @classdesc Ridge regression minimizes the value &#x60;||A&#x27; x - b||^2 + ||gamma x||^2&#x60;. * Uses {@link http://en.wikipedia.org/wiki/Tikhonov_regularization Tikhonov regularization}. * @param {module:analytics~ridgeRegParam | module:fs.FIn} [arg] - Construction arguments. There are two ways of constructing: * &amp;lt;br&gt;1. Using the {@link module:analytics~ridgeRegParam} object, * &amp;lt;br&gt;2. using the file input stream {@link module:fs.FIn}. * @example * // import modules * analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new model with gamma equal to 1.0 * var regmod &#x3D; new analytics.RidgeReg({ gamma: 1.0 }); * // generate a random feature matrix * var A &#x3D; la.randn(10, 100); * // generate a random model * var w &#x3D; la.randn(10); * // generate noise * var n &#x3D; la.randn(100).multiply(0.01); * // generate responses (model&#x27;*data + noise) * var b &#x3D; A.transpose().multiply(w).plus(n); * // fit model * regmod.fit(A, b); * // compare the true with the trained model * // true model * w.print(); * // trained model; * regmod.weights.print(); * // cosine between the true and the estimated model should be close to 1 if the fit succeeded * var cos &#x3D; regmod.weights.cosine(w); */ exports.RidgeReg &#x3D; function(arg) { return Object.create(require(&#x27;qminer&#x27;).analytics.RidgeReg.prototype) }; /** * Gets the parameters. * @returns {model:analytics~RidgeRegParam} The object containing the parameters. * @example * // import analytics module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create a new Ridge Regression object * var regmod &#x3D; new analytics.RidgeReg({ gamma: 5 }); * // get the parameters * // returns a json object { gamma: 5 } * var param &#x3D; regmod.getParams(); */ exports.RidgeReg.prototype.getParams &#x3D; function () { return { gamma: 0.0 } } /** * Set the parameters. * @param {number | model:analytics~RidgeRegParam} gamma - The new parameter for the model, given as a number or as an object. * @returns {module:analytics.RidgeReg} Self. The parameter is set to &#x60;gamma&#x60;. * @example * // import analytics module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create a new Ridge Regression object * var regmod &#x3D; new analytics.RidgeReg({ gamma: 5 }); * // set the parameters of the object * var param &#x3D; regmod.setParams({ gamma: 10 }); */ exports.RidgeReg.prototype.setParams &#x3D; function (gamma) { return Object.create(require(&#x27;qminer&#x27;).analytics.RidgeReg.prototype); } /** * Fits a column matrix of feature vectors &#x60;X&#x60; onto the response variable &#x60;y&#x60;. * @param {module:la.Matrix} X - Column matrix which stores the feature vectors. * @param {module:la.Vector} y - Response variable. * @returns {module:analytics.RidgeReg} Self. The model is fitted by &#x60;X&#x60; and &#x60;y&#x60;. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new Ridge Regression object * var regmod &#x3D; new analytics.RidgeReg(); * // create the test matrix and vector * var X &#x3D; new la.Matrix([[1, 2], [1, -1]]); * var y &#x3D; new la.Vector([3, 3]); * // fit the model with X and y * // the weights of the model are 2, 1 * regmod.fit(X, y); */ exports.RidgeReg.prototype.fit &#x3D; function(X, y) { return Object.create(require(&#x27;qminer&#x27;).analytics.RidgeReg.prototype); } /** * Returns the expected response for the provided feature vector. * @param {module:la.Vector} x - Feature vector. * @returns {number} Predicted response. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new Ridge Regression object * var regmod &#x3D; new analytics.RidgeReg(); * // create the test matrix and vector * var X &#x3D; new la.Matrix([[1, 2], [1, -1]]); * var y &#x3D; new la.Vector([3, 3]); * // fit the model with X and y * regmod.fit(X, y); * // create a new vector for the prediction * var vec &#x3D; new la.Vector([3, 4]); * // create the prediction * // returns the value 10 * var prediction &#x3D; regmod.decisionFunction(vec); */ exports.RidgeReg.prototype.decisionFunction &#x3D; function(X) { return 0.0; } /** * Returns the expected response for the provided feature vector. * @param {module:la.Vector} x - Feature vector. * @returns {number} Predicted response. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new Ridge Regression object * var regmod &#x3D; new analytics.RidgeReg(); * // create the test matrix and vector * var X &#x3D; new la.Matrix([[1, 2], [1, -1]]); * var y &#x3D; new la.Vector([3, 3]); * // fit the model with X and y * regmod.fit(X, y); * // create a new vector for the prediction * var vec &#x3D; new la.Vector([3, 4]); * // create the prediction * // returns the value 10 * var prediction &#x3D; regmod.predict(vec); */ exports.RidgeReg.prototype.predict &#x3D; function(X) { return 0.0; } /** * Vector of coefficients for linear regression. Type {@link module:la.Vector}. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new Ridge Regression object * var regmod &#x3D; new analytics.RidgeReg(); * // create the test matrix and vector * var X &#x3D; new la.Matrix([[1, 2], [1, -1]]); * var y &#x3D; new la.Vector([3, 3]); * // fit the model with X and y * regmod.fit(X, y); * // get the weights * var weights &#x3D; regmod.weights; */ exports.RidgeReg.prototype.weights &#x3D; Object.create(require(&#x27;qminer&#x27;).la.Vector.prototype); /** * Saves the model into the output stream. * @param {module:fs.FOut} fout - Output stream. * @returns {module:fs.FOut} The output stream &#x60;fout&#x60;. * @example * // import 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 Ridge Regression object * var regmod &#x3D; new analytics.RidgeReg(); * // create the test matrix and vector * var X &#x3D; new la.Matrix([[1, 2], [1, -1]]); * var y &#x3D; new la.Vector([3, 3]); * // fit the model with X and y * regmod.fit(X, y); * // create an output stream object and save the model * var fout &#x3D; fs.openWrite(&#x27;regmod_example.bin&#x27;); * regmod.save(fout); * fout.close(); * // create a new Ridge Regression model by loading the model * var fin &#x3D; fs.openRead(&#x27;regmod_example.bin&#x27;); * var regmod2 &#x3D; new analytics.RidgeReg(fin); */ exports.RidgeReg.prototype.save &#x3D; function(fout) { Object.create(require(&#x27;qminer&#x27;).fs.FOut.prototype); }; /** * Sigmoid function (&#x60;y &#x3D; 1/[1 + exp[-A*x + B]]&#x60;) fitted on decision function to mimic. * @class * @param {module:fs.FIn} [arg] - Construction arguments. * @example * // import modules * la &#x3D; require(&#x27;qminer&#x27;).la; * analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create a new model * var sigmoid &#x3D; new analytics.Sigmoid(); * // generate a random predictions * var x &#x3D; new la.Vector([0.5, 2.3, -0.1, 0.5, -7.3, 1.2]); * // generate a random labels * var y &#x3D; new la.Vector([1, 1, -1, 1, -1, -1]); * // fit model * sigmoid.fit(x, y); * // get predictions * var pred1 &#x3D; sigmoid.predict(1.2); * var pred2 &#x3D; sigmoid.predict(-1.2); */ exports.Sigmoid &#x3D; function(arg) { return Object.create(require(&#x27;qminer&#x27;).analytics.Sigmoid.prototype); }; /** * Get the parameters. &amp;lt;i&gt;It doesn&#x27;t do anything, it&#x27;s only for consistency for constructing pipeline.&amp;lt;/i&gt; * @returns {Object} An empty object. * @example * // import analytics module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create the Sigmoid model * var s &#x3D; new analytics.Sigmoid(); * // get the parameters * // returns an empty object * var param &#x3D; s.getParams(); */ exports.Sigmoid.prototype.getParams &#x3D; function () { return {}; } /** * Sets the parameters. &amp;lt;i&gt;It doesn&#x27;t do anything, it&#x27;s only for consistency for constructing pipeline.&amp;lt;/i&gt; * @param {Object} arg - Json object. * @returns {module:analytics.Sigmoid} Self. Nothing changes. * @example * // import analytics module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create the Sigmoid model * var s &#x3D; new analytics.Sigmoid(); * // set the parameters * // doesn&#x27;t change the model * s.setParams({}); */ exports.Sigmoid.prototype.setParams &#x3D; function (arg) { return Object.create(require(&#x27;qminer&#x27;).analytics.Sigmoid.prototype); } /** * Gets the model. * @returns {Object} The object &#x60;sigModel&#x60; containing the properties: * &amp;lt;br&gt; &#x60;sigModel.A&#x60; - First value of the Sigmoid model, * &amp;lt;br&gt; &#x60;sigModel.B&#x60; - Second value of the Sigmoid model. * @example * // import analytics module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create the Sigmoid model * var s &#x3D; new analytics.Sigmoid(); * // get the model parameters * // returns a Json object { A: 0, B: 0 } * var model &#x3D; s.getModel(); */ exports.Sigmoid.prototype.getModel &#x3D; function () {return { A: 0, B: 0 }; } /** * Fits a column matrix of feature vectors &#x60;X&#x60; onto the response variable &#x60;y&#x60;. * @param {module:la.Vector} x - Predicted values (e.g. using {@link module:analytics.SVR}). * @param {module:la.Vector} y - Actual binary labels: 1 or -1. * @returns {module:analytics.Sigmoid} Self. The model has been created. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create the Sigmoid model * var s &#x3D; new analytics.Sigmoid(); * // create the predicted values and the binary labels * var X &#x3D; new la.Vector([-3, -2, -1, 1, 2, 3]); * var y &#x3D; new la.Vector([-1, -1, -1, 1, 1, 1]); * // fit the model * // changes the internal A and B values of the model * s.fit(X, y); */ exports.Sigmoid.prototype.fit &#x3D; function(X, y) { return Object.create(require(&#x27;qminer&#x27;).analytics.Sigmoid.prototype); } /** * Returns the expected response for the provided feature vector. * @param {number | module:la.Vector} x - Prediction score. * @returns {number | module:la.Vector} * &amp;lt;br&gt; 1. If &#x60;x&#x60; is a number, returns a normalized prediction score, * &amp;lt;br&gt; 2. if &#x60;x&#x60; is a {@link module:la.Vector}, returns a vector of normalized prediction scores. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create the Sigmoid model * var s &#x3D; new analytics.Sigmoid(); * // create the predicted values and the binary labels * var X &#x3D; new la.Vector([-3, -2, -1, 1, 2, 3]); * var y &#x3D; new la.Vector([-1, -1, -1, 1, 1, 1]); * // fit the model * s.fit(X, y); * // predict the probability of the value 0 on this model * // returns 0.5 * var prediction &#x3D; s.decisionFunction(0.5); */ exports.Sigmoid.prototype.decisionFunction &#x3D; function(x) { return (x instanceof Object.create(require(&#x27;qminer&#x27;).la.Vector)) ? Object.create(require(&#x27;qminer&#x27;).la.Vector.prototype) : 0.0; } /** * Returns the expected response for the provided feature vector. * @param {number | module:la.Vector} x - Prediction score. * @returns {number | module:la.Vector} * &amp;lt;br&gt; 1. If &#x60;x&#x60; is a number, returns a normalized prediction score, * &amp;lt;br&gt; 2. if &#x60;x&#x60; is a {@link module:la.Vector}, returns a vector of normalized prediction scores. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create the Sigmoid model * var s &#x3D; new analytics.Sigmoid(); * // create the predicted values and the binary labels * var X &#x3D; new la.Vector([-3, -2, -1, 1, 2, 3]); * var y &#x3D; new la.Vector([-1, -1, -1, 1, 1, 1]); * // fit the model * s.fit(X, y); * // predict the probability of the value 0 on this model * // returns 0.5 * var prediction &#x3D; s.predict(0.5); */ exports.Sigmoid.prototype.predict &#x3D; function(x) { return (x instanceof Object.create(require(&#x27;qminer&#x27;).la.Vector)) ? Object.create(require(&#x27;qminer&#x27;).la.Vector.prototype) : 0.0; } /** * Saves the model into the output stream. * @param {module:fs.FOut} fout - Output stream. * @returns {module:fs.FOut} The output stream &#x60;fout&#x60;. * @example * // import 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 the Sigmoid model * var s &#x3D; new analytics.Sigmoid(); * // create the predicted values and the binary labels * var X &#x3D; new la.Vector([-3, -2, -1, 1, 2, 3]); * var y &#x3D; new la.Vector([-1, -1, -1, 1, 1, 1]); * // fit the model * s.fit(X, y); * // create an output stream object and save the model * var fout &#x3D; fs.openWrite(&#x27;sigmoid_example.bin&#x27;); * s.save(fout); * fout.close(); * // create a new Sigmoid model by loading the model * var fin &#x3D; fs.openRead(&#x27;sigmoid_example.bin&#x27;); * var s2 &#x3D; new analytics.Sigmoid(fin); */ exports.Sigmoid.prototype.save &#x3D; function(fout) { return Object.create(require(&#x27;qminer&#x27;).fs.FOut.prototype); }; /** * @typedef {Object} detectorParam * An object used for the construction of {@link module:analytics.NearestNeighborAD}. * @param {number} [rate&#x3D;0.05] - The expected fracton of emmited anomalies (0.05 -&gt; 5% of cases will be classified as anomalies). * @param {number} [windowSize&#x3D;100] - Number of most recent instances kept in the model. */ /** * Nearest Neighbour Anomaly Detection * @classdesc Anomaly detector that checks if the test point is too far from the nearest known point. * @class * @param {module:analytics~detectorParam | module:fs.FIn} [arg] - Construction arguments. There are two ways of constructing: * &amp;lt;br&gt;1. Using the {@link module:analytics~detectorParam} object, * &amp;lt;br&gt;2. using the file input stream {@link module:fs.FIn}. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new NearestNeighborAD object * var neighbor &#x3D; new analytics.NearestNeighborAD({ rate: 0.1 }); * // create a sparse matrix * var matrix &#x3D; new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]); * // fit the model with the matrix * neighbor.fit(matrix); * // create a new sparse vector * var vector &#x3D; new la.SparseVector([[0, 4], [1, 0]]); * // predict if the vector is an anomaly or not * var prediction &#x3D; neighbor.predict(vector); */ exports.NearestNeighborAD &#x3D; function(arg) { return Object.create(require(&#x27;qminer&#x27;).analytics.NearestNeighborAD.prototype); }; /** * Sets parameters. * @param {module:analytics~detectorParam} params - The object containing the parameters. * @returns {module:analytics.NearestNeighborAD} Self. The parameters are updated with &#x60;params&#x60;. * @example * // import analytics module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create a new NearestNeighborAD object * var neighbor &#x3D; new analytics.NearestNeighborAD(); * // set it&#x27;s parameters to rate: 0.1 * neighbor.setParams({ rate: 0.1 }); */ exports.NearestNeighborAD.prototype.setParams &#x3D; function (params) { return Object.create(require(&#x27;qminer&#x27;).analytics.NearestNeighborAD.prototype); } /** * Gets parameters. * @returns {module:analytics~detectorParam} The object containing the parameters. * @example * // import analytics module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create a new NearestNeighborAD object * var neighbor &#x3D; new analytics.NearestNeighborAD(); * // get the parameters of the object * // returns a json object { rate: 0.05 } * var params &#x3D; neighbor.getParams(); */ exports.NearestNeighborAD.prototype.getParams &#x3D; function () { return { rate: 0.0, windowSize: 0.0 }; } /** * Saves model to provided output stream. * @param {module:fs.FOut} fout - The output stream. * @returns {module:fs.FOut} The output stream &#x60;fout&#x60;. * @example * // import 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 NearestNeighborAD object * var neighbor &#x3D; new analytics.NearestNeighborAD(); * // create a new sparse matrix * var matrix &#x3D; new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]); * // fit the model with the matrix * neighbor.fit(matrix); * // create an output stream object and save the model * var fout &#x3D; fs.openWrite(&#x27;neighbor_example.bin&#x27;); * neighbor.save(fout); * fout.close(); * // create a new Nearest Neighbor Anomaly model by loading the model * var fin &#x3D; fs.openRead(&#x27;neighbor_example.bin&#x27;); * var neighbor2 &#x3D; new analytics.NearestNeighborAD(fin); */ exports.NearestNeighborAD.prototype.save &#x3D; function(fout) { return Object.create(require(&#x27;qminer&#x27;).fs.FOut.prototype); } /** * Returns the model. * @returns {Object} The object &#x60;neighbourModel&#x60; containing the properties: * &amp;lt;br&gt; 1. &#x60;neighbourModel.rate&#x60; - The expected fraction of emmited anomalies. * &amp;lt;br&gt; 2. &#x60;neighbourModel.thresh&#x60; - Maximal squared distance to the nearest neighbor that is not anomalous. * @example * // import analytics module * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * // create a new NearestNeighborAD object * var neighbor &#x3D; new analytics.NearestNeighborAD({ rate: 0.1 }); * // get the model of the object * // returns a json object { rate: 0.1, window: 0 } * var model &#x3D; neighbor.getModel(); */ exports.NearestNeighborAD.prototype.getModel &#x3D; function () { return { rate: 0.1, threshold: 0.0 }; } /** * Adds a new point to the known points and recalculates the threshold. * @param {module:la.SparseVector} X - Test example. * @param {number} recId - Integer record ID, used in {@link module:analytics.NearestNeighborAD.prototype.explain}. * @returns {module:analytics.NearestNeighborAD} Self. The model is updated. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new NearestNeighborAD object * var neighbor &#x3D; new analytics.NearestNeighborAD(); * // create a new sparse matrix * var matrix &#x3D; new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]); * // fit the model with the matrix * neighbor.fit(matrix); * // create a new sparse vector * var vector &#x3D; new la.SparseVector([[0, 2], [1, 5]]); * // update the model with the vector * neighbor.partialFit(vector); */ exports.NearestNeighborAD.prototype.partialFit &#x3D; function(X) { return Object.create(require(&#x27;qminer&#x27;).NearestNeighborAD.prototype); } /** * Analyzes the nearest neighbor distances and calculates the detector threshold based on the rate parameter. * @param {module:la.SparseMatrix} A - Matrix whose columns correspond to known examples. Gets saved as it is part of the model. * @param {module:la.IntVector} [idVec] - An integer vector of IDs. * @returns {module:analytics.NearestNeighborAD} Self. The model is set by the matrix &#x60;A&#x60;. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new NearestNeighborAD object * var neighbor &#x3D; new analytics.NearestNeighborAD(); * // create a new sparse matrix * var matrix &#x3D; new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]); * // fit the model with the matrix * neighbor.fit(matrix); */ exports.NearestNeighborAD.prototype.fit &#x3D; function(A, idVec) { return Object.create(require(&#x27;qminer&#x27;).NearestNeighborAD.prototype); } /** * Compares the point to the known points and returns distance to the nearest one. * @param {module:la.Vector} x - Test vector. * @returns {number} Distance to the nearest point. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new NearestNeighborAD object * var neighbor &#x3D; new analytics.NearestNeighborAD(); * // create a new sparse matrix * var matrix &#x3D; new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]); * // fit the model with the matrix * neighbor.fit(matrix); * // create a new sparse vector * var vector &#x3D; new la.SparseVector([[0, 4], [1, 0]]); * // get the distance of the vector from the model * var prediction &#x3D; neighbor.decisionFunction(vector); // returns 1 */ exports.NearestNeighborAD.prototype.decisionFunction &#x3D; function(x) { return 0.0; } /** * Compares the point to the known points and returns 1 if it&#x27;s too far away (based on the precalculated threshold). * @param {module:la.SparseVector} x - Test vector. * @returns {number} Returns 1.0 if the vector &#x60;x&#x60; is an anomaly and 0.0 otherwise. * @example * // import modules * var analytics &#x3D; require(&#x27;qminer&#x27;).analytics; * var la &#x3D; require(&#x27;qminer&#x27;).la; * // create a new NearestNeighborAD object * var neighbor &#x3D; new analytics.NearestNeighborAD(); * // create a new sparse matrix * var matrix &#x3D; new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]); * // fit the model with the matrix * neighbor.fit(matrix); * // create a new sparse vector * var vector &#x3D; new la.SparseVector([[0, 4], [1, 0]]); * // check if the vector is an anomaly * var prediction &#x3D; neighbor.predict(vector); // returns 1 */ exports.NearestNeighborAD.prototype.predict &#x3D; function(x) { return 0.0; } /** * @typedef {Object} NearestNeighborADExplain * An object used for interpreting the predictions of {@link module:analytics.NearestNeighborAD#explain}. * @property {number} nearestID - The ID of the nearest neighbor. * @property {number} distance - The distance to the nearest neighbor. * @property {Array.&amp;lt;module:analytics~NearestNeighborADFeatureContribution&gt;} features - An array with feature contributions. * @property {number} oldestID - The ID of the oldest record in the internal buffer (the record that was added first)