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

(module:analytics~SVMParam or module:fs.FIn)

Yes

Construction arguments. There are two ways of constructing:
1. Using the module:analytics~SVMParam object,
2. using the file input stream module:fs.FIn.

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) Distance:
1. Real number, if X is module:la.Vector or module:la.SparseVector.
2. module:la.Vector, if X 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

(module:la.Matrix or module:la.SparseMatrix)

 

Input feature matrix where columns correspond to feature vectors.

y

module:la.Vector

 

Input vector of targets, one for each column of X.

Returns

module:analytics.SVC 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 The svmModel 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 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) Prediction:
1. Real number, if X is module:la.Vector or module:la.SparseVector.
2. module:la.Vector, if X 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

module:fs.FOut

 

Output stream.

Returns

module:fs.FOut The output stream fout.

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

module:analytics~SVMParam

 

Classifier training parameters.

Returns

module:analytics.SVC Self. Updated the training parameters.