analytics. RidgeReg
Source: analyticsdoc.
Ridge regression minimizes the value ||A' x - b||^2 + ||gamma x||^2
.
Uses Tikhonov regularization.
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 |
Yes |
Construction arguments. There are two ways of constructing:
|
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 |
|
Feature vector. |
- Returns
-
number
B 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 |
|
Column matrix which stores the feature vectors. |
|
y |
|
Response variable. |
- Returns
-
module:analytics.RidgeReg
B Self. The model is fitted byX
andy
.
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
-
Object
B TheridgeRegModel
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~RidgeRegParam
B 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 |
|
Feature vector. |
- Returns
-
number
B 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 |
|
Output stream. |
- Returns
-
module:fs.FOut
B The output streamfout
.
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.RidgeReg
B Self. The parameter is set togamma
.