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 class CFeatures;
23 
73 {
74 public:
77 
82  CLeastAngleRegression(bool lasso=true);
83 
85  virtual ~CLeastAngleRegression();
86 
91  void set_max_non_zero(int32_t n)
92  {
93  m_max_nonz = n;
94  }
95 
98  int32_t get_max_non_zero() const
99  {
100  return m_max_nonz;
101  }
102 
108  {
109  m_max_l1_norm = norm;
110  }
111 
115  {
116  return m_max_l1_norm;
117  }
118 
123  void switch_w(int32_t num_variable)
124  {
125  if (w.vlen <= 0)
126  SG_ERROR("cannot swith estimator before training")
127  if (size_t(num_variable) >= m_beta_idx.size() || num_variable < 0)
128  SG_ERROR("cannot switch to an estimator of %d non-zero coefficients", num_variable)
129  if (w.vector == NULL)
131  std::copy(m_beta_path[m_beta_idx[num_variable]].begin(),
132  m_beta_path[m_beta_idx[num_variable]].end(), w.vector);
133  }
134 
143  int32_t get_path_size() const
144  {
145  return m_beta_idx.size();
146  }
147 
158  {
159  return SGVector<float64_t>(&m_beta_path[m_beta_idx[num_var]][0], w.vlen, false);
160  }
161 
167  {
168  return CT_LARS;
169  }
170 
172  void set_epsilon(float64_t epsilon)
173  {
174  m_epsilon = epsilon;
175  }
176 
179  {
180  return m_epsilon;
181  }
182 
184  virtual const char* get_name() const { return "LeastAngleRegression"; }
185 
186 protected:
187  virtual bool train_machine(CFeatures* data=NULL);
188 
190  SGMatrix<float64_t>& R, int32_t i_max_corr, int32_t num_active);
191 
193 
194 private:
195  void activate_variable(int32_t v)
196  {
197  m_num_active++;
198  m_active_set.push_back(v);
199  m_is_active[v] = true;
200  }
201  void deactivate_variable(int32_t v_idx)
202  {
203  m_num_active--;
204  m_is_active[m_active_set[v_idx]] = false;
205  m_active_set.erase(m_active_set.begin() + v_idx);
206  }
207 
208  bool m_lasso;
209 
210  int32_t m_max_nonz;
211  float64_t m_max_l1_norm;
212 
213  std::vector<std::vector<float64_t> > m_beta_path;
214  std::vector<int32_t> m_beta_idx;
215  std::vector<int32_t> m_active_set;
216  std::vector<bool> m_is_active;
217  int32_t m_num_active;
218  float64_t m_epsilon;
219 }; // class LARS
220 
221 } // namespace shogun
222 
223 #endif // HAVE_LAPACK
224 #endif // LEASTANGLEREGRESSION_H__
EMachineType
Definition: Machine.h:33
MACHINE_PROBLEM_TYPE(PT_REGRESSION)
#define SG_ERROR(...)
Definition: SGIO.h:129
SGMatrix< float64_t > cholesky_insert(const SGMatrix< float64_t > &X, const SGMatrix< float64_t > &X_active, SGMatrix< float64_t > &R, int32_t i_max_corr, int32_t num_active)
virtual bool train_machine(CFeatures *data=NULL)
SGMatrix< float64_t > cholesky_delete(SGMatrix< float64_t > &R, int32_t i_kick)
index_t vlen
Definition: SGVector.h:492
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
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