SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2012 Chiyuan Zhang 00008 * Copyright (C) 2012 Chiyuan Zhang 00009 */ 00010 00011 #ifndef LEASTANGLEREGRESSION_H__ 00012 #define LEASTANGLEREGRESSION_H__ 00013 00014 #include <shogun/lib/config.h> 00015 00016 #ifdef HAVE_LAPACK 00017 #include <vector> 00018 #include <shogun/machine/LinearMachine.h> 00019 00020 namespace shogun 00021 { 00022 class CFeatures; 00023 00072 class CLeastAngleRegression: public CLinearMachine 00073 { 00074 public: 00076 MACHINE_PROBLEM_TYPE(PT_REGRESSION); 00077 00082 CLeastAngleRegression(bool lasso=true); 00083 00085 virtual ~CLeastAngleRegression(); 00086 00091 void set_max_non_zero(int32_t n) 00092 { 00093 m_max_nonz = n; 00094 } 00095 00098 int32_t get_max_non_zero() const 00099 { 00100 return m_max_nonz; 00101 } 00102 00107 void set_max_l1_norm(float64_t norm) 00108 { 00109 m_max_l1_norm = norm; 00110 } 00111 00114 float64_t get_max_l1_norm() const 00115 { 00116 return m_max_l1_norm; 00117 } 00118 00123 void switch_w(int32_t num_variable) 00124 { 00125 if (w.vlen <= 0) 00126 SG_ERROR("cannot swith estimator before training") 00127 if (size_t(num_variable) >= m_beta_idx.size() || num_variable < 0) 00128 SG_ERROR("cannot switch to an estimator of %d non-zero coefficients", num_variable) 00129 if (w.vector == NULL) 00130 w = SGVector<float64_t>(w.vlen); 00131 std::copy(m_beta_path[m_beta_idx[num_variable]].begin(), 00132 m_beta_path[m_beta_idx[num_variable]].end(), w.vector); 00133 } 00134 00143 int32_t get_path_size() const 00144 { 00145 return m_beta_idx.size(); 00146 } 00147 00157 SGVector<float64_t> get_w_for_var(int32_t num_var) 00158 { 00159 return SGVector<float64_t>(&m_beta_path[m_beta_idx[num_var]][0], w.vlen, false); 00160 } 00161 00166 virtual EMachineType get_classifier_type() 00167 { 00168 return CT_LARS; 00169 } 00170 00172 virtual const char* get_name() const { return "LeastAngleRegression"; } 00173 00174 protected: 00175 virtual bool train_machine(CFeatures* data=NULL); 00176 00177 private: 00178 void activate_variable(int32_t v) 00179 { 00180 m_num_active++; 00181 m_active_set.push_back(v); 00182 m_is_active[v] = true; 00183 } 00184 void deactivate_variable(int32_t v_idx) 00185 { 00186 m_num_active--; 00187 m_is_active[m_active_set[v_idx]] = false; 00188 m_active_set.erase(m_active_set.begin() + v_idx); 00189 } 00190 00191 SGMatrix<float64_t> cholesky_insert(SGMatrix<float64_t> X, SGMatrix<float64_t> R, int32_t i_max_corr); 00192 SGMatrix<float64_t> cholesky_delete(SGMatrix<float64_t> R, int32_t i_kick); 00193 00194 00195 bool m_lasso; 00196 00197 int32_t m_max_nonz; 00198 float64_t m_max_l1_norm; 00199 00200 std::vector<std::vector<float64_t> > m_beta_path; 00201 std::vector<int32_t> m_beta_idx; 00202 00203 std::vector<int32_t> m_active_set; 00204 std::vector<bool> m_is_active; 00205 int32_t m_num_active; 00206 }; // class LARS 00207 00208 } // namespace shogun 00209 00210 #endif // HAVE_LAPACK 00211 #endif // LEASTANGLEREGRESSION_H__