Add some comments and fix a little bit of formatting.
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
* @file logistic_regression.hpp
|
||||
* @author Sumedh Ghaisas
|
||||
*
|
||||
* The LogisticRegression class, which implements logistic regression.
|
||||
* The LogisticRegression class, which implements logistic regression. This
|
||||
* implements supports L2-regularization.
|
||||
*/
|
||||
#ifndef __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_HPP
|
||||
#define __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_HPP
|
||||
@@ -21,10 +22,31 @@ template<
|
||||
class LogisticRegression
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Construct the LogisticRegression class with the given labeled training
|
||||
* data. This will train the model. Optionally, specify lambda, which is the
|
||||
* penalty parameter for L2-regularization. If not specified, it is set to 0,
|
||||
* which results in standard (unregularized) logistic regression.
|
||||
*
|
||||
* @param predictors Input training variables.
|
||||
* @param responses Outputs resulting from input training variables.
|
||||
* @param lambda L2-regularization parameter.
|
||||
*/
|
||||
LogisticRegression(arma::mat& predictors,
|
||||
arma::vec& responses,
|
||||
const double lambda = 0);
|
||||
|
||||
/**
|
||||
* Construct the LogisticRegression class with the given labeled training
|
||||
* data. This will train the model. Optionally, specify lambda, which is the
|
||||
* penalty parameter for L2-regularization. If not specified, it is set to 0,
|
||||
* which results in standard (unregularized) logistic regression.
|
||||
*
|
||||
* @param predictors Input training variables.
|
||||
* @param responses Outputs results from input training variables.
|
||||
* @param initialPoint Initial model to train with.
|
||||
* @param lambda L2-regularization parameter.
|
||||
*/
|
||||
LogisticRegression(arma::mat& predictors,
|
||||
arma::vec& responses,
|
||||
const arma::mat& initialPoint,
|
||||
@@ -35,9 +57,9 @@ class LogisticRegression
|
||||
//! Modify the parameters (the b vector).
|
||||
arma::vec& Parameters() { return parameters; }
|
||||
|
||||
//! Return the lambda value
|
||||
//! Return the lambda value for L2-regularization.
|
||||
const double& Lambda() const { return lambda; }
|
||||
//! Modify the lambda value
|
||||
//! Modify the lambda value for L2-regularization.
|
||||
double& Lambda() { return lambda; }
|
||||
|
||||
double LearnModel();
|
||||
@@ -51,7 +73,7 @@ class LogisticRegression
|
||||
const arma::vec& responses,
|
||||
const double decisionBoundary = 0.5);
|
||||
|
||||
double ComputeError(arma::mat& predictors,const arma::vec& responses);
|
||||
double ComputeError(arma::mat& predictors, const arma::vec& responses);
|
||||
|
||||
private:
|
||||
arma::vec parameters;
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
* @file logistic_regression_impl.hpp
|
||||
* @author Sumedh Ghaisas
|
||||
*
|
||||
* Implementation of the LogisticRegression class.
|
||||
* Implementation of the LogisticRegression class. This implementation supports
|
||||
* L2-regularization.
|
||||
*/
|
||||
#ifndef __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_IMPL_HPP
|
||||
#define __MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_IMPL_HPP
|
||||
@@ -20,7 +21,7 @@ LogisticRegression<OptimizerType>::LogisticRegression(
|
||||
const double lambda) :
|
||||
predictors(predictors),
|
||||
responses(responses),
|
||||
errorFunction(LogisticRegressionFunction(predictors,responses,lambda)),
|
||||
errorFunction(LogisticRegressionFunction(predictors, responses, lambda)),
|
||||
optimizer(OptimizerType<LogisticRegressionFunction>(errorFunction)),
|
||||
lambda(lambda)
|
||||
{
|
||||
@@ -35,7 +36,7 @@ LogisticRegression<OptimizerType>::LogisticRegression(
|
||||
const double lambda) :
|
||||
predictors(predictors),
|
||||
responses(responses),
|
||||
errorFunction(LogisticRegressionFunction(predictors,responses)),
|
||||
errorFunction(LogisticRegressionFunction(predictors, responses)),
|
||||
optimizer(OptimizerType<LogisticRegressionFunction>(errorFunction)),
|
||||
lambda(lambda)
|
||||
{
|
||||
@@ -71,7 +72,7 @@ double LogisticRegression<OptimizerType>::ComputeError(
|
||||
ones.ones(predictors.n_cols);
|
||||
predictors.insert_rows(0, ones);
|
||||
|
||||
double out = errorFunction.Evaluate(predictors,responses,parameters);
|
||||
double out = errorFunction.Evaluate(predictors, responses, parameters);
|
||||
|
||||
predictors.shed_row(0);
|
||||
|
||||
@@ -84,11 +85,11 @@ double LogisticRegression<OptimizerType>::ComputeAccuracy(
|
||||
const arma::vec& responses,
|
||||
const double decisionBoundary)
|
||||
{
|
||||
arma::vec temp_responses;
|
||||
Predict(predictors,temp_responses,decisionBoundary);
|
||||
arma::vec tempResponses;
|
||||
Predict(predictors, tempResponses, decisionBoundary);
|
||||
int count = 0;
|
||||
for (size_t i = 0; i < responses.n_rows; i++)
|
||||
if (responses(i, 0) == temp_responses(i, 0))
|
||||
if (responses(i, 0) == tempResponses(i, 0))
|
||||
count++;
|
||||
|
||||
return (double) (count * 100) / responses.n_rows;
|
||||
|
||||
Reference in New Issue
Block a user