Property

new RidgeReg([arg])

Ridge Regression

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);

Parameter

Name Type Optional Description

arg

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

Yes

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

Property

weights

Vector of coefficients for linear regression. Type 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;

Methods

decisionFunction(x) → number

Returns the expected response for the provided feature 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);
// 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);

Parameter

Name Type Optional Description

x

module:la.Vector

 

Feature vector.

Returns

numberB Predicted response.

fit(X, y) → module:analytics.RidgeReg

Fits a column matrix of feature vectors X onto the response variable 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);

Parameters

Name Type Optional Description

X

module:la.Matrix

 

Column matrix which stores the feature vectors.

y

module:la.Vector

 

Response variable.

Returns

module:analytics.RidgeRegB Self. The model is fitted by X and y.

getModel() → Object

Gets the model.

Example

// import analytics module
var analytics = require('qminer').analytics;
// create the Ridge Regression model
var regmod = new analytics.RidgeReg();
// get the model
var model = regmod.getModel();
Returns

ObjectB The ridgeRegModel object containing the property:
1. ridgeRegModel.weights - The weights of the model. Type module:la.Vector.

getParams() → model:analytics~RidgeRegParam

Gets 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();
Returns

model:analytics~RidgeRegParamB The object containing the parameters.

predict(x) → number

Returns the expected response for the provided feature 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);
// 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);

Parameter

Name Type Optional Description

x

module:la.Vector

 

Feature vector.

Returns

numberB Predicted response.

save(fout) → module:fs.FOut

Saves the model into the output stream.

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);

Parameter

Name Type Optional Description

fout

module:fs.FOut

 

Output stream.

Returns

module:fs.FOutB The output stream fout.

setParams(gamma) → module:analytics.RidgeReg

Set the parameters.

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 });

Parameter

Name Type Optional Description

gamma

(number or model:analytics~RidgeRegParam)

 

The new parameter for the model, given as a number or as an object.

Returns

module:analytics.RidgeRegB Self. The parameter is set to gamma.