qminer
Version:
A C++ based data analytics platform for processing large-scale real-time streams containing structured and unstructured data
1,003 lines • 229 kB
JavaScript
/**
* 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 = require('qminer');
* var analytics = 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 `min(mat.rows, mat.cols)`.
* @param {Object} [json] - Algorithm options.
* @param {number} [json.iter = 100] - The number of iterations used for the algorithm.
* @param {number} [json.tol = 1e-3] - The tolerance.
* @param {boolean} [json.verbose = false] - If false, the console output is supressed.
* @returns {Object} The json object `nmfRes` containing the non-negative matrices U and V:
* <br> `nmfRes.U`- The {@link module:la.Matrix} representation of the matrix U,
* <br> `nmfRes.V`- The {@link module:la.Matrix} representation of the matrix V.
* @example <caption>Asynchronous function</caption>
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a matrix
* var mat = 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 = result.U;
* var V = result.V;
* });
* @example <caption>Synchronous function</caption>
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a matrix
* var mat = new la.Matrix({ rows: 10, cols: 5, random: true });
* // compute the non-negative matrix factorization
* var result = analytics.nmf(mat, 3, { iter: 100, tol: 1e-4 });
* var U = result.U;
* var V = result.V;
*/
exports.prototype.nmf = function (mat, k, json) { return { "U": Object.create(require('qminer').la.Matrix.prototype), "V": Object.create(require('qminer').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='SGD'] - The algorithm procedure. Possible options are `'SGD'` and `'LIBSVM'`. `'PR_LOQO'`is not supported anymore.
* @property {number} [c=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=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=n is like adding n-1 copies of the positive training examples to the data set.
* @property {number} [eps=1e-3] - Epsilon insensitive loss parameter. Larger values result in fewer support vectors (smaller model complexity)
* @property {number} [batchSize=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=10000] - Maximum number of iterations.
* @property {number} [maxTime=1] - Maximum runtime in seconds.
* @property {number} [minDiff=1e-6] - Stopping criterion tolerance.
* @property {string} [type='C_SVC'] - The subalgorithm procedure in LIBSVM. Possible options are `'C_SVC'`, `'NU_SVC'` and `'ONE_CLASS'` for classification and `'EPSILON_SVR'`, `'NU_SVR'` and `'ONE_CLASS'` for regression.
* @property {string} [kernel='LINEAR'] - Kernel type in LIBSVM. Possible options are `'LINEAR'`, `'POLY'`, 'RBF'`, 'SIGMOID'` and `'PRECOMPUTED'`.
* @property {number} [gamma=1.0] - Gamma parameter in LIBSVM. Set gamma in kernel function.
* @property {number} [p=1e-1] - P parameter in LIBSVM. Set the epsilon in loss function of epsilon-SVR.
* @property {number} [degree=1] - Degree parameter in LIBSVM. Set degree in kernel function.
* @property {number} [nu=1e-2] - Nu parameter in LIBSVM. Set the parameter nu of nu-SVC, one-class SVM, and nu-SVR.
* @property {number} [coef0=1.0] - Coef0 parameter in LIBSVM. Set coef0 in kernel function.
* @property {number} [cacheSize=100] - Set cache memory size in MB (default 100) in LIBSVM.
* @property {boolean} [verbose=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:
* <br>1. Using the {@link module:analytics~SVMParam} object,
* <br>2. using the file input stream {@link module:fs.FIn}.
* @example
* // import modules
* var la = require('qminer').la;
* var analytics = require('qminer').analytics;
* // CLASSIFICATION WITH SVC
* // set up fake train and test data
* // four training examples with number of features = 2
* var featureMatrix = new la.Matrix({ rows: 2, cols: 4, random: true });
* // classification targets for four examples
* var targets = new la.Vector([-1, -1, 1, 1]);
* // set up the classification model
* var SVC = new analytics.SVC({ verbose: false });
* // train classifier
* SVC.fit(featureMatrix, targets);
* // set up a fake test vector
* var test = new la.Vector([1.1, -0.5]);
* // predict the target value
* var prediction = SVC.predict(test);
*/
exports.SVC = function(arg) { return Object.create(require('qminer').analytics.SVC.prototype); };
/**
* Gets the SVC parameters.
* @returns {module:analytics~SVMParam} Parameters of the classifier model.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create a new SVC model with json
* var SVC = 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: 'SGD' c: 5, j: 10, eps: 0.1, batchSize: 2000, maxIterations: 12000, maxTime: 2, minDiff: 1e-10, verbose: true }
* var json = SVC.getParams();
*/
exports.SVC.prototype.getParams = function() { return { algorithm: '', 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 = require('qminer').analytics;
* // create a default SVC model
* var SVC = 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 = function(param) { return Object.create(require('qminer').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 = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new SVC object
* var SVC = new analytics.SVC();
* // create the matrix containing the input features and the input vector for each matrix.
* var matrix = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
* var vec = new la.Vector([1, 1, -1, -1]);
* // fit the model
* SVC.fit(matrix, vec);
* // get the weights
* var weights = SVC.weights; // returns the coefficients of the normal vector of the hyperplane gained from the model: [1, 1]
*/
exports.SVC.prototype.weights = Object.create(require('qminer').la.Vector.prototype);
/**
* Saves model to output file stream.
* @param {module:fs.FOut} fout - Output stream.
* @returns {module:fs.FOut} The output stream `fout`.
* @example
* // import the analytics and la modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* var fs = require('qminer').fs;
* // create a new SVC object
* var SVC = new analytics.SVC();
* // create the matrix containing the input features and the input vector for each matrix column.
* var matrix = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
* var vec = new la.Vector([1, 0, -1, -2]);
* // fit the model
* SVC.fit(matrix, vec);
* // create output stream
* var fout = fs.openWrite('svc_example.bin');
* // save SVC object (model and parameters) to output stream and close it
* SVC.save(fout);
* fout.close();
* // create input stream
* var fin = fs.openRead('svc_example.bin');
* // create a SVC object that loads the model and parameters from input stream
* var SVC2 = new analytics.SVC(fin);
*/
exports.SVC.prototype.save = function(fout) { return Object.create(require('qminer').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:
* <br>1. Real number, if `X` is {@link module:la.Vector} or {@link module:la.SparseVector}.
* <br>2. {@link module:la.Vector}, if `X` is {@link module:la.Matrix} or {@link module:la.SparseMatrix}.
* <br>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 = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new SVC object
* var SVC = new analytics.SVC();
* // create the matrix containing the input features and the input vector for each matrix
* var matrix = new la.Matrix([[1, 0], [0, -1]]);
* var vec = 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 = new la.Vector([2, 3]);
* // use the decisionFunction to get the distance of vec2 from the model
* var distance = SVC.decisionFunction(vec2); // returns something close to 5
*/
exports.SVC.prototype.decisionFunction = function(X) { return (X instanceof require('qminer').la.Vector | X instanceof require('qminer').la.SparseVector) ? 0 : Object.create(require('qminer').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:
* <br>1. Real number, if `X` is {@link module:la.Vector} or {@link module:la.SparseVector}.
* <br>2. {@link module:la.Vector}, if `X` is {@link module:la.Matrix} or {@link module:la.SparseMatrix}.
* <br>1 for positive class and -1 for negative.
* @example
* // import the analytics and la modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new SVC object
* var SVC = new analytics.SVC();
* // create the matrix containing the input features and the input vector for each matrix
* var matrix = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
* var vec = new la.Vector([1, 1, -1, -1]);
* // fit the model
* SVC.fit(matrix, vec);
* // create a vector you want to predict
* var vec2 = new la.Vector([3, 5]);
* // predict the vector
* var prediction = SVC.predict(vec2); // returns 1
*/
exports.SVC.prototype.predict = function(X) { return (X instanceof require('qminer').la.Vector | X instanceof require('qminer').la.SparseVector) ? 0 : Object.create(require('qminer').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 = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new SVC object
* var SVC = new analytics.SVC();
* // create the matrix containing the input features and the input vector for each matrix.
* var matrix = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
* var vec = 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 = function(X, y) { return Object.create(require('qminer').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:
* <br>1. Using the {@link module:analytics~SVMParam} object,
* <br>2. using the file input stream {@link module:fs.FIn}.
* @example
* // import module
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // REGRESSION WITH SVR
* // Set up fake train and test data.
* // Four training examples with, number of features = 2
* var featureMatrix = new la.Matrix({ rows: 2, cols: 4, random: true });
* // Regression targets for four examples
* var targets = new la.Vector([1.1, -2, 3, 4.2]);
* // Set up the regression model
* var SVR = new analytics.SVR({ verbose: false });
* // Train regression
* SVR.fit(featureMatrix, targets);
* // Set up a fake test vector
* var test = new la.Vector([1.1, -0.8]);
* // Predict the target value
* var prediction = SVR.predict(test);
*/
exports.SVR = function(arg) { return Object.create(require('qminer').analytics.SVR.prototype); };
/**
* Gets the SVR parameters.
* @returns {module:analytics~SVMParam} Parameters of the regression model.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create a new SVR object
* var SVR = new analytics.SVR({ c: 10, eps: 1e-10, maxTime: 12000, verbose: true });
* // get the parameters of SVR
* var params = SVR.getParams();
*/
exports.SVR.prototype.getParams = function() { return { algorithm: '', 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 = require('qminer').analytics;
* // create a new SVR object
* var SVR = new analytics.SVR();
* // set the parameters of the SVR object
* SVR.setParams({ c: 10, maxTime: 12000 });
*/
exports.SVR.prototype.setParams = function(param) { return Object.create(require('qminer').analytics.SVR.prototype); };
/**
* The vector of coefficients of the linear model. Type {@link module:la.Vector}.
* @example
* // import the modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new SVR object
* var SVR = new analytics.SVR({ c: 10 });
* // create a matrix and vector for the model
* var matrix = new la.Matrix([[1, -1], [1, 1]]);
* var vector = 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 = SVR.weights;
*/
exports.SVR.prototype.weights = Object.create(require('qminer').la.Vector.prototype);
/**
* Saves model to output file stream.
* @param {module:fs.FOut} fout - Output stream.
* @returns {module:fs.FOut} The output stream `fout`.
* @example
* // import the modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* var fs = require('qminer').fs;
* // create a new SVR object
* var SVR = new analytics.SVR({ c: 10 });
* // create a matrix and vector for the model
* var matrix = new la.Matrix([[1, -1], [1, 1]]);
* var vector = 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 = fs.openWrite('svr_example.bin');
* SVR.save(fout);
* fout.close();
* // construct a SVR model by loading from the binary file
* var fin = fs.openRead('svr_example.bin');
* var SVR2 = new analytics.SVR(fin);
*/
exports.SVR.prototype.save = function(fout) { return Object.create(require('qminer').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:
* <br>1. Real number if `X` is {@link module:la.Vector} or {@link module:la.SparseVector}.
* <br>2. {@link module:la.Vector}, if `X` is {@link module:la.Matrix} or {@link module:la.SparseMatrix}.
* @example
* // import the modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new SVR object
* var SVR = new analytics.SVR({ c: 10 });
* // create a matrix and vector for the model
* var matrix = new la.Matrix([[1, -1], [1, 1]]);
* var vector = 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 = new la.Vector([-5, 1]);
* var distance = SVR.decisionFunction(vec2);
*/
exports.SVR.prototype.decisionFunction = function(X) { return (X instanceof require('qminer').la.Vector | X instanceof require('qminer').la.SparseVector) ? 0 : Object.create(require('qminer').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:
* <br>1. Real number, if `X` is {@link module:la.Vector} or {@link module:la.SparseVector}.
* <br>2. {@link module:la.Vector}, if `X` is {@link module:la.Matrix} or {@link module:la.SparseMatrix}.
* @example
* // import the modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new SVR object
* var SVR = new analytics.SVR({ c: 10 });
* // create a matrix and vector for the model
* var matrix = new la.Matrix([[1, -1], [1, 1]]);
* var vector = 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 = new la.Vector([-5, 1]);
* var prediction = SVR.predict(vec2);
*/
exports.SVR.prototype.predict = function(X) { return (X instanceof require('qminer').la.Vector | X instanceof require('qminer').la.SparseVector) ? 0 : Object.create(require('qminer').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 = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new SVR object
* var SVR = new analytics.SVR({ c: 10 });
* // create a matrix and vector for the model
* var matrix = new la.Matrix([[1, -1], [1, 1]]);
* var vector = new la.Vector([1, 1]);
* // create the model by fitting the values
* SVR.fit(matrix, vector);
*/
exports.SVR.prototype.fit = function(X, y) { return Object.create(require('qminer').analytics.SVR.prototype); }
/**
* @typedef {Object} ridgeRegParam
* An object used for the construction of {@link module:analytics.RidgeReg}.
* @property {number} [gamma=0.0] - The gamma value.
*/
/**
* Ridge Regression
* @class
* @classdesc Ridge regression minimizes the value `||A' x - b||^2 + ||gamma x||^2`.
* 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:
* <br>1. Using the {@link module:analytics~ridgeRegParam} object,
* <br>2. using the file input stream {@link module:fs.FIn}.
* @example
* // import modules
* analytics = require('qminer').analytics;
* la = require('qminer').la;
* // create a new model with gamma equal to 1.0
* var regmod = new analytics.RidgeReg({ gamma: 1.0 });
* // generate a random feature matrix
* var A = la.randn(10, 100);
* // generate a random model
* var w = la.randn(10);
* // generate noise
* var n = la.randn(100).multiply(0.01);
* // generate responses (model'*data + noise)
* var b = 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 = regmod.weights.cosine(w);
*/
exports.RidgeReg = function(arg) { return Object.create(require('qminer').analytics.RidgeReg.prototype) };
/**
* Gets the parameters.
* @returns {model:analytics~RidgeRegParam} The object containing the parameters.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create a new Ridge Regression object
* var regmod = new analytics.RidgeReg({ gamma: 5 });
* // get the parameters
* // returns a json object { gamma: 5 }
* var param = regmod.getParams();
*/
exports.RidgeReg.prototype.getParams = 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 `gamma`.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create a new Ridge Regression object
* var regmod = new analytics.RidgeReg({ gamma: 5 });
* // set the parameters of the object
* var param = regmod.setParams({ gamma: 10 });
*/
exports.RidgeReg.prototype.setParams = function (gamma) { return Object.create(require('qminer').analytics.RidgeReg.prototype); }
/**
* Fits a column matrix of feature vectors `X` onto the response variable `y`.
* @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 `X` and `y`.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new Ridge Regression object
* var regmod = new analytics.RidgeReg();
* // create the test matrix and vector
* var X = new la.Matrix([[1, 2], [1, -1]]);
* var y = 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 = function(X, y) { return Object.create(require('qminer').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 = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new Ridge Regression object
* var regmod = new analytics.RidgeReg();
* // create the test matrix and vector
* var X = new la.Matrix([[1, 2], [1, -1]]);
* var y = 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 = new la.Vector([3, 4]);
* // create the prediction
* // returns the value 10
* var prediction = regmod.decisionFunction(vec);
*/
exports.RidgeReg.prototype.decisionFunction = 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 = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new Ridge Regression object
* var regmod = new analytics.RidgeReg();
* // create the test matrix and vector
* var X = new la.Matrix([[1, 2], [1, -1]]);
* var y = 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 = new la.Vector([3, 4]);
* // create the prediction
* // returns the value 10
* var prediction = regmod.predict(vec);
*/
exports.RidgeReg.prototype.predict = function(X) { return 0.0; }
/**
* Vector of coefficients for linear regression. Type {@link module:la.Vector}.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new Ridge Regression object
* var regmod = new analytics.RidgeReg();
* // create the test matrix and vector
* var X = new la.Matrix([[1, 2], [1, -1]]);
* var y = new la.Vector([3, 3]);
* // fit the model with X and y
* regmod.fit(X, y);
* // get the weights
* var weights = regmod.weights;
*/
exports.RidgeReg.prototype.weights = Object.create(require('qminer').la.Vector.prototype);
/**
* Saves the model into the output stream.
* @param {module:fs.FOut} fout - Output stream.
* @returns {module:fs.FOut} The output stream `fout`.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* var fs = require('qminer').fs;
* // create a new Ridge Regression object
* var regmod = new analytics.RidgeReg();
* // create the test matrix and vector
* var X = new la.Matrix([[1, 2], [1, -1]]);
* var y = 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 = fs.openWrite('regmod_example.bin');
* regmod.save(fout);
* fout.close();
* // create a new Ridge Regression model by loading the model
* var fin = fs.openRead('regmod_example.bin');
* var regmod2 = new analytics.RidgeReg(fin);
*/
exports.RidgeReg.prototype.save = function(fout) { Object.create(require('qminer').fs.FOut.prototype); };
/**
* Sigmoid function (`y = 1/[1 + exp[-A*x + B]]`) fitted on decision function to mimic.
* @class
* @param {module:fs.FIn} [arg] - Construction arguments.
* @example
* // import modules
* la = require('qminer').la;
* analytics = require('qminer').analytics;
* // create a new model
* var sigmoid = new analytics.Sigmoid();
* // generate a random predictions
* var x = new la.Vector([0.5, 2.3, -0.1, 0.5, -7.3, 1.2]);
* // generate a random labels
* var y = new la.Vector([1, 1, -1, 1, -1, -1]);
* // fit model
* sigmoid.fit(x, y);
* // get predictions
* var pred1 = sigmoid.predict(1.2);
* var pred2 = sigmoid.predict(-1.2);
*/
exports.Sigmoid = function(arg) { return Object.create(require('qminer').analytics.Sigmoid.prototype); };
/**
* Get the parameters. <i>It doesn't do anything, it's only for consistency for constructing pipeline.</i>
* @returns {Object} An empty object.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create the Sigmoid model
* var s = new analytics.Sigmoid();
* // get the parameters
* // returns an empty object
* var param = s.getParams();
*/
exports.Sigmoid.prototype.getParams = function () { return {}; }
/**
* Sets the parameters. <i>It doesn't do anything, it's only for consistency for constructing pipeline.</i>
* @param {Object} arg - Json object.
* @returns {module:analytics.Sigmoid} Self. Nothing changes.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create the Sigmoid model
* var s = new analytics.Sigmoid();
* // set the parameters
* // doesn't change the model
* s.setParams({});
*/
exports.Sigmoid.prototype.setParams = function (arg) { return Object.create(require('qminer').analytics.Sigmoid.prototype); }
/**
* Gets the model.
* @returns {Object} The object `sigModel` containing the properties:
* <br> `sigModel.A` - First value of the Sigmoid model,
* <br> `sigModel.B` - Second value of the Sigmoid model.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create the Sigmoid model
* var s = new analytics.Sigmoid();
* // get the model parameters
* // returns a Json object { A: 0, B: 0 }
* var model = s.getModel();
*/
exports.Sigmoid.prototype.getModel = function () {return { A: 0, B: 0 }; }
/**
* Fits a column matrix of feature vectors `X` onto the response variable `y`.
* @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 = require('qminer').analytics;
* var la = require('qminer').la;
* // create the Sigmoid model
* var s = new analytics.Sigmoid();
* // create the predicted values and the binary labels
* var X = new la.Vector([-3, -2, -1, 1, 2, 3]);
* var y = 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 = function(X, y) { return Object.create(require('qminer').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}
* <br> 1. If `x` is a number, returns a normalized prediction score,
* <br> 2. if `x` is a {@link module:la.Vector}, returns a vector of normalized prediction scores.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create the Sigmoid model
* var s = new analytics.Sigmoid();
* // create the predicted values and the binary labels
* var X = new la.Vector([-3, -2, -1, 1, 2, 3]);
* var y = 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 = s.decisionFunction(0.5);
*/
exports.Sigmoid.prototype.decisionFunction = function(x) { return (x instanceof Object.create(require('qminer').la.Vector)) ? Object.create(require('qminer').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}
* <br> 1. If `x` is a number, returns a normalized prediction score,
* <br> 2. if `x` is a {@link module:la.Vector}, returns a vector of normalized prediction scores.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create the Sigmoid model
* var s = new analytics.Sigmoid();
* // create the predicted values and the binary labels
* var X = new la.Vector([-3, -2, -1, 1, 2, 3]);
* var y = 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 = s.predict(0.5);
*/
exports.Sigmoid.prototype.predict = function(x) { return (x instanceof Object.create(require('qminer').la.Vector)) ? Object.create(require('qminer').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 `fout`.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* var fs = require('qminer').fs;
* // create the Sigmoid model
* var s = new analytics.Sigmoid();
* // create the predicted values and the binary labels
* var X = new la.Vector([-3, -2, -1, 1, 2, 3]);
* var y = 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 = fs.openWrite('sigmoid_example.bin');
* s.save(fout);
* fout.close();
* // create a new Sigmoid model by loading the model
* var fin = fs.openRead('sigmoid_example.bin');
* var s2 = new analytics.Sigmoid(fin);
*/
exports.Sigmoid.prototype.save = function(fout) { return Object.create(require('qminer').fs.FOut.prototype); };
/**
* @typedef {Object} detectorParam
* An object used for the construction of {@link module:analytics.NearestNeighborAD}.
* @param {number} [rate=0.05] - The expected fracton of emmited anomalies (0.05 -> 5% of cases will be classified as anomalies).
* @param {number} [windowSize=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:
* <br>1. Using the {@link module:analytics~detectorParam} object,
* <br>2. using the file input stream {@link module:fs.FIn}.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD({ rate: 0.1 });
* // create a sparse matrix
* var matrix = 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 = new la.SparseVector([[0, 4], [1, 0]]);
* // predict if the vector is an anomaly or not
* var prediction = neighbor.predict(vector);
*/
exports.NearestNeighborAD = function(arg) { return Object.create(require('qminer').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 `params`.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD();
* // set it's parameters to rate: 0.1
* neighbor.setParams({ rate: 0.1 });
*/
exports.NearestNeighborAD.prototype.setParams = function (params) { return Object.create(require('qminer').analytics.NearestNeighborAD.prototype); }
/**
* Gets parameters.
* @returns {module:analytics~detectorParam} The object containing the parameters.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD();
* // get the parameters of the object
* // returns a json object { rate: 0.05 }
* var params = neighbor.getParams();
*/
exports.NearestNeighborAD.prototype.getParams = 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 `fout`.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* var fs = require('qminer').fs;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD();
* // create a new sparse matrix
* var matrix = 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 = fs.openWrite('neighbor_example.bin');
* neighbor.save(fout);
* fout.close();
* // create a new Nearest Neighbor Anomaly model by loading the model
* var fin = fs.openRead('neighbor_example.bin');
* var neighbor2 = new analytics.NearestNeighborAD(fin);
*/
exports.NearestNeighborAD.prototype.save = function(fout) { return Object.create(require('qminer').fs.FOut.prototype); }
/**
* Returns the model.
* @returns {Object} The object `neighbourModel` containing the properties:
* <br> 1. `neighbourModel.rate` - The expected fraction of emmited anomalies.
* <br> 2. `neighbourModel.thresh` - Maximal squared distance to the nearest neighbor that is not anomalous.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD({ rate: 0.1 });
* // get the model of the object
* // returns a json object { rate: 0.1, window: 0 }
* var model = neighbor.getModel();
*/
exports.NearestNeighborAD.prototype.getModel = 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 = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD();
* // create a new sparse matrix
* var matrix = 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 = new la.SparseVector([[0, 2], [1, 5]]);
* // update the model with the vector
* neighbor.partialFit(vector);
*/
exports.NearestNeighborAD.prototype.partialFit = function(X) { return Object.create(require('qminer').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 `A`.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD();
* // create a new sparse matrix
* var matrix = 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 = function(A, idVec) { return Object.create(require('qminer').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 = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD();
* // create a new sparse matrix
* var matrix = 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 = new la.SparseVector([[0, 4], [1, 0]]);
* // get the distance of the vector from the model
* var prediction = neighbor.decisionFunction(vector); // returns 1
*/
exports.NearestNeighborAD.prototype.decisionFunction = function(x) { return 0.0; }
/**
* Compares the point to the known points and returns 1 if it's too far away (based on the precalculated threshold).
* @param {module:la.SparseVector} x - Test vector.
* @returns {number} Returns 1.0 if the vector `x` is an anomaly and 0.0 otherwise.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD();
* // create a new sparse matrix
* var matrix = 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 = new la.SparseVector([[0, 4], [1, 0]]);
* // check if the vector is an anomaly
* var prediction = neighbor.predict(vector); // returns 1
*/
exports.NearestNeighborAD.prototype.predict = 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.<module:analytics~NearestNeighborADFeatureContribution>} 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).
* @property {number} newestID - The ID of the newest record in the internal buffer (the record that was added last).
*/
/**
* @typedef {Object} NearestNeighborADFeatureContribution
* An object explaining the prediction of {@link module:analytics.NearestNeighborAD#explain} in terms of a single feature.
* Contained in the object {@link module:analytics~NearestNeighborADExplain}.
* @property {number} id - The ID of the feature.
* @property {number} val - The value of the feature for the vector we are explaining.
* @property {number} nearVal - The the value of the feature for the nearest neighbor.
* @property {number} contribution - Fraction of the total distance `(v(i) - n(i))^2 / ||v - n||^2`.
*/
/**
* Returns an object that encodes the ID of the nearest neighbor and the features that contributed to the distance.
* @param {module:la.SparseVector} x - Test vector.
* @returns {module:analytics~NearestNeighborADExplain} The explanation object.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD({ rate:0.05, windowSize:3 });
* // create a new sparse matrix
* var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
* // fit the model with the matrix and provide a vector record IDs
* neighbor.fit(matrix, new la.IntVector([3541, 1112, 4244]));
* // create a new sparse vector
* var vector = new la.SparseVector([[0, 4], [1, 0]]);
* // check if the vector is an anomaly
* var explanation = neighbor.explain(vector); // returns an explanation
*/
exports.NearestNeighborAD.prototype.explain = function(x) { return {}; }
/**
* Returns true when the model has enough data to initialize. Type `boolean`.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create a new NearestNeighborAD object
* var neighbor = new analytics.NearestNeighborAD({ rate:0.05, windowSize:3 });
* // check if the model has enough data
* neighbor.init;
*/
exports.NearestNeighborAD.prototype.init = false;
/**
* @typedef {Object} recLinRegParam
* An object used for the construction of {@link module:analytics.RecLinReg}.
* @param {number} dim - The dimension of the model.
* @param {number} [regFact=1.0] - The regularization factor.
* @param {number} [forgetFact=1.0] - The forgetting factor.
*/
/**
* Recursive Linear Regression
* @classdesc Holds the Recursive Linear Regression model.
* @class
* @param {module:analytics~recLinRegParam | module:fs.FIn} arg - Construction arguments. There are two ways of constructing:
* <br>1. Using the {@link module:analytics~detectorParam} object,
* <br>2. using the file input stream {@link module:fs.FIn}.
* @example
* // import analytics module
* var analytics = require('qminer').analytics;
* // create the recursive linear regression model holder
* var linreg = new analytics.RecLinReg({ dim: 10, regFact: 1.0, forgetFact: 1.0 });
*/
exports.RecLinReg = function(arg) { return Object.create(require('qminer').analytics.RecLinReg.prototype); }
/**
* Updates the internal model.
* @param {module:la.Vector} vec - The input vector.
* @param {number} num - The target number for the vector.
* @returns {module:analytics.RecLinReg} Self. The internal model is updated.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create the Recursive Linear Regression model
* var linreg = new analytics.RecLinReg({ dim: 3.0 });
* // create a new dense vector
* var vec = new la.Vector([1, 2, 3]);
* // fit the model with the vector
* linreg.partialFit(vec, 6);
*/
exports.RecLinReg.prototype.partialFit = function (vec, num) { return Object.create(require('qminer').analytics.RecLinReg.prototype); }
/**
* Creates/updates the internal model.
* @param {module:la.Matrix} mat - The input matrix.
* @param {module:la.Vector} vec - The target numbers, where the i-th number in vector is the target number for the i-th column of the `mat`.
* @returns {module:analytics.RecLinReg} Self. The internal model is updated.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create the Recursive Linear Regression model
* var linreg = new analytics.RecLinReg({ dim: 2.0 });
* // create a new dense matrix and target vector
* var mat = new la.Matrix([[1, 2, 3], [3, 4, 5]]);
* var vec = new la.Vector([3, 5, -1]);
* // fit the model with the matrix
* linreg.fit(mat, vec);
*/
exports.RecLinReg.prototype.fit = function (mat, vec) { return Object.create(require('qminer').analytics.RecLinReg.prototype); }
/**
* Puts the vector through the model and returns the prediction as a real number.
* @param {module:la.Vector} vec - The prediction vector.
* @returns {number} The prediction.
* @example
* // import modules
* var analytics = require('qminer').analytics;
* var la = require('qminer').la;
* // create the Recursive Linear Regression model
* var linreg = new analytics.RecLinReg({ dim: 2.0, recFact: 1e-10 });
* // create a new dense matrix and target vector
*