analytics. SVC
Source: analyticsdoc.
Support Vector Machine Classifier. Implements a soft margin linear support vector classifier using the PEGASOS algorithm, see: Pegasos: Primal Estimated sub-GrAdient SOlver for SVM.
Property
new SVC([arg])
SVC
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);
Parameter
Name | Type | Optional | Description |
---|---|---|---|
arg |
Yes |
Construction arguments. There are two ways of constructing:
|
Property
weights
Gets the vector of coefficients of the linear model. Type 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]
Methods
decisionFunction(X) → (number or module:la.Vector)
Sends vector through the model and returns the distance to the decision boundery.
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
Parameter
Name | Type | Optional | Description |
---|---|---|---|
X |
(module:la.Vector, module:la.SparseVector, module:la.Matrix, or module:la.SparseMatrix) |
|
Input feature vector or matrix with feature vectors as columns. |
- Returns
-
(number or module:la.Vector)
B Distance:
1. Real number, ifX
is module:la.Vector or module:la.SparseVector.
2. module:la.Vector, ifX
is module:la.Matrix or module:la.SparseMatrix.
Sign of the number corresponds to the class and the magnitude corresponds to the distance from the margin (certainty).
fit(X, y) → module:analytics.SVC
Fits a SVM classification model, given column examples in a matrix and vector of targets.
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]
Parameters
Name | Type | Optional | Description |
---|---|---|---|
X |
|
Input feature matrix where columns correspond to feature vectors. |
|
y |
|
Input vector of targets, one for each column of X. |
- Returns
-
module:analytics.SVC
B Self. The model has been created.
getModel() → Object
Get the model.
Example
// import analytics module
var analytics = require('qminer').analytics;
// create a SVC model
var SVC = new analytics.SVC();
// get the properties of the model
var model = SVC.getModel();
- Returns
-
Object
B ThesvmModel
object containing the property:
1.svmModel.weights
- The weights of the model. Type module:la.Vector.
getParams() → module:analytics~SVMParam
Gets the SVC parameters.
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();
- Returns
-
module:analytics~SVMParam
B Parameters of the classifier model.
predict(X) → (number or module:la.Vector)
Sends vector through the model and returns the prediction as a real number.
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
Parameter
Name | Type | Optional | Description |
---|---|---|---|
X |
(module:la.Vector, module:la.SparseVector, module:la.Matrix, or module:la.SparseMatrix) |
|
Input feature vector or matrix with feature vectors as columns. |
- Returns
-
(number or module:la.Vector)
B Prediction:
1. Real number, ifX
is module:la.Vector or module:la.SparseVector.
2. module:la.Vector, ifX
is module:la.Matrix or module:la.SparseMatrix.
1 for positive class and -1 for negative.
save(fout) → module:fs.FOut
Saves model to output file stream.
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);
Parameter
Name | Type | Optional | Description |
---|---|---|---|
fout |
|
Output stream. |
- Returns
-
module:fs.FOut
B The output streamfout
.
setParams(param) → module:analytics.SVC
Sets the SVC 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
Parameter
Name | Type | Optional | Description |
---|---|---|---|
param |
|
Classifier training parameters. |
- Returns
-
module:analytics.SVC
B Self. Updated the training parameters.