mlpack: a scalable c++ machine learning library
mlpack  2.0.2
linear_regression.hpp
Go to the documentation of this file.
1 
15 #ifndef mlpack_METHODS_LINEAR_REGRESSION_LINEAR_REGRESSION_HPP
16 #define mlpack_METHODS_LINEAR_REGRESSION_LINEAR_REGRESSION_HPP
17 
18 #include <mlpack/core.hpp>
19 
20 namespace mlpack {
21 namespace regression {
22 
29 {
30  public:
40  LinearRegression(const arma::mat& predictors,
41  const arma::vec& responses,
42  const double lambda = 0,
43  const bool intercept = true,
44  const arma::vec& weights = arma::vec());
45 
51  LinearRegression(const LinearRegression& linearRegression);
52 
58  LinearRegression() : lambda(0.0), intercept(true) { }
59 
72  void Train(const arma::mat& predictors,
73  const arma::vec& responses,
74  const bool intercept = true,
75  const arma::vec& weights = arma::vec());
76 
83  void Predict(const arma::mat& points, arma::vec& predictions) const;
84 
102  double ComputeError(const arma::mat& points,
103  const arma::vec& responses) const;
104 
106  const arma::vec& Parameters() const { return parameters; }
108  arma::vec& Parameters() { return parameters; }
109 
111  double Lambda() const { return lambda; }
113  double& Lambda() { return lambda; }
114 
116  bool Intercept() const { return intercept; }
117 
121  template<typename Archive>
122  void Serialize(Archive& ar, const unsigned int /* version */)
123  {
124  ar & data::CreateNVP(parameters, "parameters");
125  ar & data::CreateNVP(lambda, "lambda");
126  ar & data::CreateNVP(intercept, "intercept");
127  }
128 
129  private:
134  arma::vec parameters;
135 
140  double lambda;
141 
143  bool intercept;
144 };
145 
146 } // namespace linear_regression
147 } // namespace mlpack
148 
149 #endif // mlpack_METHODS_LINEAR_REGRESSION_HPP
Linear algebra utility functions, generally performed on matrices or vectors.
FirstShim< T > CreateNVP(T &t, const std::string &name, typename boost::enable_if< HasSerialize< T >>::type *=0)
Call this function to produce a name-value pair; this is similar to BOOST_SERIALIZATION_NVP(), but should be used for types that have a Serialize() function (or contain a type that has a Serialize() function) instead of a serialize() function.
A simple linear regression algorithm using ordinary least squares.
bool intercept
Indicates whether first parameter is intercept.
double & Lambda()
Modify the Tikhonov regularization parameter for ridge regression.
arma::vec parameters
The calculated B.
void Train(const arma::mat &predictors, const arma::vec &responses, const bool intercept=true, const arma::vec &weights=arma::vec())
Train the LinearRegression model on the given data.
bool Intercept() const
Return whether or not an intercept term is used in the model.
double Lambda() const
Return the Tikhonov regularization parameter for ridge regression.
double lambda
The Tikhonov regularization parameter for ridge regression (0 for linear regression).
double ComputeError(const arma::mat &points, const arma::vec &responses) const
Calculate the L2 squared error on the given predictors and responses using this linear regression mod...
const arma::vec & Parameters() const
Return the parameters (the b vector).
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
arma::vec & Parameters()
Modify the parameters (the b vector).
void Predict(const arma::mat &points, arma::vec &predictions) const
Calculate y_i for each data point in points.
void Serialize(Archive &ar, const unsigned int)
Serialize the model.