SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LeastAngleRegression.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2012 Chiyuan Zhang
8  * Copyright (C) 2012 Chiyuan Zhang
9  */
10 
11 #ifndef LEASTANGLEREGRESSION_H__
12 #define LEASTANGLEREGRESSION_H__
13 
14 #include <shogun/lib/config.h>
15 
16 #include <vector>
18 
19 namespace shogun
20 {
21 
22 class CFeatures;
23 
73 {
74 public:
75 
78 
83  CLeastAngleRegression(bool lasso = true);
84 
86  virtual ~CLeastAngleRegression();
87 
92  void set_max_non_zero(int32_t n)
93  {
94  m_max_nonz = n;
95  }
96 
99  int32_t get_max_non_zero() const
100  {
101  return m_max_nonz;
102  }
103 
109  {
110  m_max_l1_norm = norm;
111  }
112 
116  {
117  return m_max_l1_norm;
118  }
119 
125  void switch_w(int32_t num_variable)
126  {
127  REQUIRE(w.vlen > 0,"Please train the model (i.e. run the model's train() method) before updating its weights.\n")
128  REQUIRE(size_t(num_variable) < m_beta_idx.size() && num_variable >= 0,
129  "Cannot switch to an estimator of %d non-zero coefficients.\n", num_variable)
130  if (w.vector == NULL)
132 
133  std::copy(m_beta_path[m_beta_idx[num_variable]].begin(),
134  m_beta_path[m_beta_idx[num_variable]].end(), w.vector);
135  }
136 
145  int32_t get_path_size() const
146  {
147  return m_beta_idx.size();
148  }
149 
160  {
161  return SGVector<float64_t>(&m_beta_path[m_beta_idx[num_var]][0], w.vlen, false);
162  }
163 
169  {
170  return CT_LARS;
171  }
172 
174  void set_epsilon(float64_t epsilon)
175  {
176  m_epsilon = epsilon;
177  }
178 
181  {
182  return m_epsilon;
183  }
184 
186  virtual const char* get_name() const { return "LeastAngleRegression"; }
187 
188 protected:
198  bool train_machine(CFeatures * data);
199 
200  template <typename ST>
202  const SGMatrix<ST>& X_active, SGMatrix<ST>& R, int32_t i_max_corr, int32_t num_active);
203 
204  template <typename ST>
205  SGMatrix<ST> cholesky_delete(SGMatrix<ST>& R, int32_t i_kick);
206 
207  template <typename ST>
208  static void plane_rot(ST x0, ST x1,
209  ST &y0, ST &y1, SGMatrix<ST> &G);
210 
211  #ifndef SWIG
212  template <typename ST>
213  static void find_max_abs(const std::vector<ST> &vec, const std::vector<bool> &ignore_mask,
214  int32_t &imax, ST& vmax);
215  #endif
216 
217 private:
223  template <typename ST>
224  bool train_machine_templated(CDenseFeatures<ST> * data);
225 
226  void activate_variable(int32_t v)
227  {
228  m_num_active++;
229  m_active_set.push_back(v);
230  m_is_active[v] = true;
231  }
232 
233  void deactivate_variable(int32_t v_idx)
234  {
235  m_num_active--;
236  m_is_active[m_active_set[v_idx]] = false;
237  m_active_set.erase(m_active_set.begin() + v_idx);
238  }
239 
240  bool m_lasso;
241 
242  int32_t m_max_nonz;
243  float64_t m_max_l1_norm;
244 
245  std::vector<std::vector<float64_t> > m_beta_path;
246  std::vector<int32_t> m_beta_idx;
247  std::vector<int32_t> m_active_set;
248  std::vector<bool> m_is_active;
249  int32_t m_num_active;
250  float64_t m_epsilon;
251 }; // class LARS
252 
253 } // namespace shogun
254 
255 #endif // LEASTANGLEREGRESSION_H__
EMachineType
Definition: Machine.h:33
MACHINE_PROBLEM_TYPE(PT_REGRESSION)
SGMatrix< ST > cholesky_delete(SGMatrix< ST > &R, int32_t i_kick)
The class DenseFeatures implements dense feature matrices.
Definition: LDA.h:40
#define REQUIRE(x,...)
Definition: SGIO.h:206
static void find_max_abs(const std::vector< ST > &vec, const std::vector< bool > &ignore_mask, int32_t &imax, ST &vmax)
index_t vlen
Definition: SGVector.h:492
static void plane_rot(ST x0, ST x1, ST &y0, ST &y1, SGMatrix< ST > &G)
virtual EMachineType get_classifier_type()
void switch_w(int32_t num_variable)
double float64_t
Definition: common.h:50
Class for Least Angle Regression, can be used to solve LASSO.
SGVector< float64_t > w
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:63
void set_max_l1_norm(float64_t norm)
void set_epsilon(float64_t epsilon)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SGMatrix< ST > cholesky_insert(const SGMatrix< ST > &X, const SGMatrix< ST > &X_active, SGMatrix< ST > &R, int32_t i_max_corr, int32_t num_active)
SGVector< float64_t > get_w_for_var(int32_t num_var)
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual const char * get_name() const

SHOGUN Machine Learning Toolbox - Documentation