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 #ifdef HAVE_LAPACK
17 #include <vector>
19 
20 namespace shogun
21 {
22 
23 class CFeatures;
24 
74 {
75 public:
76 
79 
84  CLeastAngleRegression(bool lasso = true);
85 
87  virtual ~CLeastAngleRegression();
88 
93  void set_max_non_zero(int32_t n)
94  {
95  m_max_nonz = n;
96  }
97 
100  int32_t get_max_non_zero() const
101  {
102  return m_max_nonz;
103  }
104 
110  {
111  m_max_l1_norm = norm;
112  }
113 
117  {
118  return m_max_l1_norm;
119  }
120 
126  void switch_w(int32_t num_variable)
127  {
128  if (w.vlen <= 0)
129  SG_ERROR("Please train the model before updating its parameters")
130  if (size_t(num_variable) >= m_beta_idx.size() || num_variable < 0)
131  SG_ERROR("cannot switch to an estimator of %d non-zero coefficients", num_variable)
132  if (w.vector == NULL)
134 
135  std::copy(m_beta_path[m_beta_idx[num_variable]].begin(),
136  m_beta_path[m_beta_idx[num_variable]].end(), w.vector);
137  }
138 
147  int32_t get_path_size() const
148  {
149  return m_beta_idx.size();
150  }
151 
162  {
163  return SGVector<float64_t>(&m_beta_path[m_beta_idx[num_var]][0], w.vlen, false);
164  }
165 
171  {
172  return CT_LARS;
173  }
174 
176  void set_epsilon(float64_t epsilon)
177  {
178  m_epsilon = epsilon;
179  }
180 
183  {
184  return m_epsilon;
185  }
186 
188  virtual const char* get_name() const { return "LeastAngleRegression"; }
189 
190 protected:
201  bool train_machine(CFeatures * data);
202 
203  template <typename ST>
205  const SGMatrix<ST>& X_active, SGMatrix<ST>& R, int32_t i_max_corr, int32_t num_active);
206 
207  template <typename ST>
208  SGMatrix<ST> cholesky_delete(SGMatrix<ST>& R, int32_t i_kick);
209 
210  template <typename ST>
211  static void plane_rot(ST x0, ST x1,
212  ST &y0, ST &y1, SGMatrix<ST> &G);
213 
214  template <typename ST>
215  static void find_max_abs(const std::vector<ST> &vec, const std::vector<bool> &ignore_mask,
216  int32_t &imax, ST& vmax);
217 
218 private:
224  template <typename ST>
225  bool train_machine_templated(CDenseFeatures<ST> * data);
226 
227  void activate_variable(int32_t v)
228  {
229  m_num_active++;
230  m_active_set.push_back(v);
231  m_is_active[v] = true;
232  }
233 
234  void deactivate_variable(int32_t v_idx)
235  {
236  m_num_active--;
237  m_is_active[m_active_set[v_idx]] = false;
238  m_active_set.erase(m_active_set.begin() + v_idx);
239  }
240 
241  bool m_lasso;
242 
243  int32_t m_max_nonz;
244  float64_t m_max_l1_norm;
245 
246  std::vector<std::vector<float64_t> > m_beta_path;
247  std::vector<int32_t> m_beta_idx;
248  std::vector<int32_t> m_active_set;
249  std::vector<bool> m_is_active;
250  int32_t m_num_active;
251  float64_t m_epsilon;
252 }; // class LARS
253 
254 } // namespace shogun
255 
256 #endif // HAVE_LAPACK
257 #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 SG_ERROR(...)
Definition: SGIO.h:129
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