mlpack  master
logistic_regression.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_HPP
14 #define MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_HPP
15 
16 #include <mlpack/prereqs.hpp>
18 
20 
21 namespace mlpack {
22 namespace regression {
23 
33 template<typename MatType = arma::mat>
35 {
36  public:
52  LogisticRegression(const MatType& predictors,
53  const arma::Row<size_t>& responses,
54  const double lambda = 0);
55 
72  LogisticRegression(const MatType& predictors,
73  const arma::Row<size_t>& responses,
74  const arma::vec& initialPoint,
75  const double lambda = 0);
76 
87  LogisticRegression(const size_t dimensionality = 0,
88  const double lambda = 0);
89 
101  template<template<typename> class OptimizerType>
103  OptimizerType<LogisticRegressionFunction<MatType>>& optimizer);
104 
118  template<
119  template<typename> class OptimizerType = mlpack::optimization::L_BFGS
120  >
121  void Train(const MatType& predictors,
122  const arma::Row<size_t>& responses);
123 
138  template<
139  template<typename> class OptimizerType = mlpack::optimization::L_BFGS
140  >
141  void Train(OptimizerType<LogisticRegressionFunction<MatType>>& optimizer);
142 
144  const arma::vec& Parameters() const { return parameters; }
146  arma::vec& Parameters() { return parameters; }
147 
149  const double& Lambda() const { return lambda; }
151  double& Lambda() { return lambda; }
152 
166  void Predict(const MatType& predictors,
167  arma::Row<size_t>& responses,
168  const double decisionBoundary = 0.5) const;
169 
181  template<typename VecType>
182  size_t Classify(const VecType& point,
183  const double decisionBoundary = 0.5) const;
184 
196  void Classify(const MatType& dataset,
197  arma::Row<size_t>& labels,
198  const double decisionBoundary = 0.5) const;
199 
206  void Classify(const MatType& dataset,
207  arma::mat& probabilities) const;
208 
223  double ComputeAccuracy(const MatType& predictors,
224  const arma::Row<size_t>& responses,
225  const double decisionBoundary = 0.5) const;
226 
235  double ComputeError(const MatType& predictors,
236  const arma::Row<size_t>& responses) const;
237 
239  template<typename Archive>
240  void Serialize(Archive& ar, const unsigned int /* version */);
241 
242  private:
244  arma::vec parameters;
246  double lambda;
247 };
248 
249 } // namespace regression
250 } // namespace mlpack
251 
252 // Include implementation.
253 #include "logistic_regression_impl.hpp"
254 
255 #endif // MLPACK_METHODS_LOGISTIC_REGRESSION_LOGISTIC_REGRESSION_HPP
void Predict(const MatType &predictors, arma::Row< size_t > &responses, const double decisionBoundary=0.5) const
Predict the responses to a given set of predictors.
The log-likelihood function for the logistic regression objective function.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
void Train(const MatType &predictors, const arma::Row< size_t > &responses)
Train the LogisticRegression model on the given input data.
void Serialize(Archive &ar, const unsigned int)
Serialize the model.
The core includes that mlpack expects; standard C++ includes and Armadillo.
const double & Lambda() const
Return the lambda value for L2-regularization.
double ComputeError(const MatType &predictors, const arma::Row< size_t > &responses) const
Compute the error of the model.
double ComputeAccuracy(const MatType &predictors, const arma::Row< size_t > &responses, const double decisionBoundary=0.5) const
Compute the accuracy of the model on the given predictors and responses, optionally using the given d...
double & Lambda()
Modify the lambda value for L2-regularization.
const arma::vec & Parameters() const
Return the parameters (the b vector).
double lambda
L2-regularization penalty parameter.
arma::vec & Parameters()
Modify the parameters (the b vector).
LogisticRegression(const MatType &predictors, const arma::Row< size_t > &responses, const double lambda=0)
Construct the LogisticRegression class with the given labeled training data.
The generic L-BFGS optimizer, which uses a back-tracking line search algorithm to minimize a function...
Definition: lbfgs.hpp:34
arma::vec parameters
Vector of trained parameters (size: dimensionality plus one).
size_t Classify(const VecType &point, const double decisionBoundary=0.5) const
Classify the given point.
The LogisticRegression class implements an L2-regularized logistic regression model, and supports training with multiple optimizers and classification.