analytics. LogReg
Source: analyticsdoc.
Uses Newtons method to compute the weights. Before use: QMiner must be built with the OpenBLAS library.
Property
new LogReg([arg])
Logistic regression model.
Example
// import analytics module
var analytics = require('qminer').analytics;
// create the Logistic Regression model
var logreg = new analytics.LogReg({ lambda: 2 });
// create the input matrix and vector for fitting the model
var mat = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 0, -1, -2]);
// if OpenBLAS is used, fit the model
if (require('qminer').flags.blas) {
logreg.fit(mat, vec);
// create the vector for the prediction
var test = new la.Vector([1, 1]);
// get the prediction
var prediction = logreg.predict(test);
}
Parameter
Name | Type | Optional | Description |
---|---|---|---|
arg |
Yes |
Construction arguments. There are two ways of constructing:
|
Property
weights
Gives the weights of the model. Type module:la.Vector.
Example
// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create the logistic regression model
var logreg = new analytics.LogReg();
// get the weights of the model
var weights = logreg.weights;
Methods
fit(X, y[, eps]) → module:analytics.LogReg
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 the logistic regression model
var logreg = new analytics.LogReg();
// create the input matrix and vector for fitting the model
var mat = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 0, -1, -2]);
// if OpenBLAS is used, fit the model
if (require('qminer').flags.blas) {
logreg.fit(mat, vec);
}
Parameters
Name | Type | Optional | Description |
---|---|---|---|
X |
|
the column matrix which stores the feature vectors. |
|
y |
|
the response variable. |
|
eps |
number |
Yes |
the epsilon used for convergence. |
- Returns
-
module:analytics.LogReg
B Self. The model has been updated.
getParams() → module:analytics~logisticRegParam
Gets the parameters.
Example
// import analytics module
var analytics = require('qminer').analytics;
// create the Logistic Regression model
var logreg = new analytics.LogReg({ lambda: 10 });
// get the parameters of the model
var param = logreg.getParams(); // returns { lambda: 10, intercept: false }
- Returns
-
module:analytics~logisticRegParam
B The parameters of the model.
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 the logistic regression model
var logreg = new analytics.LogReg();
// create the input matrix and vector for fitting the model
var mat = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 0, -1, -2]);
// if openblas is used, fit the model and predict the value
if (require('qminer').flags.blas) {
// fit the model
logreg.fit(mat, vec);
// create the vector for the prediction
var test = new la.Vector([1, 1]);
// get the prediction
var prediction = logreg.predict(test);
};
Parameter
Name | Type | Optional | Description |
---|---|---|---|
x |
|
the feature vector. |
- Returns
-
number
B the expected 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 the logistic regression model
var logreg = new analytics.LogReg();
// create the input matrix and vector for fitting the model
var mat = new la.Matrix([[1, 0, -1, 0], [0, 1, 0, -1]]);
var vec = new la.Vector([1, 0, -1, -2]);
// if openblas is used, fit the model
if (require('qminer').flags.blas) {
logreg.fit(mat, vec);
};
// create an output stream object and save the model
var fout = fs.openWrite('logreg_example.bin');
logreg.save(fout);
fout.close();
// create input stream
var fin = fs.openRead('logreg_example.bin');
// create a Logistic Regression object that loads the model and parameters from input stream
var logreg2 = new analytics.LogReg(fin);
Parameter
Name | Type | Optional | Description |
---|---|---|---|
fout |
|
the output stream. |
- Returns
-
module:fs.FOut
B The output streamfout
.
setParams(param) → module:analytics.LogReg
Set the parameters.
Example
// import analytics module
var analytics = require('qminer').analytics;
// create a logistic regression model
var logreg = new analytics.LogReg({ lambda: 10 });
// set the parameters of the model
logreg.setParams({ lambda: 1 });
Parameter
Name | Type | Optional | Description |
---|---|---|---|
param |
|
The new parameters. |
- Returns
-
module:analytics.LogReg
B Self. The parameters are updated.