Debian Codegraph @

src/algorithms/FeatureSelector.h

raw symbols SCIP
1 /**********************************************************************
2 FeatureSelector.h: select features, typical use: feature selection for classification
3 Author(s): [email protected]
4 Copyright (C) 2016-2020 European Union (Joint Research Centre)
5
6 This file is part of jiplib.
7
8 jiplib is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 jiplib is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with jiplib. If not, see <https://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 #ifndef _FEATURESELECTOR_H_
22 #define _FEATURESELECTOR_H_
23
24 /* #include <math.h> */
25 #include <cmath>
26 #include <vector>
27 #include <list>
28 #include <algorithm>
29 #include <iostream>
30 #include <iomanip>
31 #include "ConfusionMatrix.h"
32 #include "base/IndexValue.h"
33 #include "base/Vector2d.h"
34 #include "gsl/gsl_combination.h"
35 #include "CostFactory.h"
36
37 class FeatureSelector
38 {
39 public:
40 FeatureSelector(){};
41 ~FeatureSelector(){};
42 template<class T> double forward(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int maxFeatures=0, short verbose=0);
43 template<class T> double backward(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int minFeatures, short verbose=0);
44 template<class T> double floating(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int maxFeatures=0, double epsilon=0.001, short verbose=0);
45 template<class T> double bruteForce(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int maxFeatures=0, short verbose=0);
46
47 private:
48 template<class T> double addFeature(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, short verbose=0);
49 template<class T> double removeFeature(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int& r, short verbose=0);
50 template<class T> double forwardUnivariate(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int maxFeatures=0, short verbose=0);
51 };
52
53 //sequential forward selection Univariate (N single best features)
54 template<class T> double FeatureSelector::forwardUnivariate(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int maxFeatures, short verbose){
55 int maxLevels=v[0][0].size();
56 if(!maxFeatures)
57 maxFeatures=maxLevels;
58 int k=subset.size();
59 if(k>=maxFeatures)
60 return -1;
61 std::vector<IndexValue> cost(maxLevels);
62 std::list<int> tmpset=subset;//temporary set of selected features (levels)
63 std::vector< Vector2d<T> > tmp(v.size());
64 for(int ilevel=0;ilevel<maxLevels;++ilevel){
65 if(find(tmpset.begin(),tmpset.end(),ilevel)==tmpset.end()){
66 tmpset.push_back(ilevel);
67 for(int iclass=0;iclass<v.size();++iclass){
68 // tmp[iclass].resize(v[iclass].size());
69 v[iclass].selectCols(tmpset,tmp[iclass]);
70 }
71 try{
72 IndexValue pv;
73 pv.position=ilevel;
74 pv.value=theCostFactory.getCost(tmp);
75 cost[ilevel]=pv;
76 }
77 catch(...){
78 IndexValue pv;
79 pv.position=ilevel;
80 pv.value=-1;
81 cost[ilevel]=pv;
82 }
83 tmpset.pop_back();
84 }
85 }
86 sort(cost.begin(),cost.end(),Compare_IndexValue());//decreasing order
87 int ilevel=0;
88 while((subset.size()<maxFeatures)&&(ilevel<maxLevels)){
89 if(cost[ilevel].value>0)
90 subset.push_back(cost[ilevel].position);
91 if(verbose)
92 std::cout << "feature " << subset.back() << " has cost: " << cost[ilevel].value << std::endl;
93 ++ilevel;
94 }
95 double maxCost=-1;
96 while(subset.size()){
97 for(int iclass=0;iclass<v.size();++iclass){
98 // tmp[iclass].resize(v[iclass].size());
99 v[iclass].selectCols(subset,tmp[iclass]);
100 }
101 try{
102 maxCost=theCostFactory.getCost(tmp);
103 }
104 catch(...){
105 subset.pop_back();
106 continue;
107 }
108 return maxCost;
109 }
110 }
111
112 //sequential forward selection Multivariate (Combination of N best features)
113 template<class T> double FeatureSelector::forward(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int maxFeatures, short verbose){
114 //Select feature with the best value (get maximal cost for 1 feature)
115 double maxCost=0;
116 int maxLevels=v[0][0].size();
117 if(!maxFeatures)
118 maxFeatures=maxLevels;
119 while(subset.size()<maxFeatures){
120 maxCost=addFeature(v,theCostFactory,subset,verbose);
121 if(verbose){
122 for(std::list<int>::const_iterator lit=subset.begin();lit!=subset.end();++lit)
123 std::cout << *lit << " ";
124 std::cout << std::endl;
125 // std::cout << "added " << subset.back() << ", " << subset.size() << "/" << maxFeatures << " features selected with cost: " << maxCost << std::endl;
126 }
127 }//while
128 return maxCost;
129 }
130
131 //sequential backward selection
132 template<class T> double FeatureSelector::backward(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int minFeatures, short verbose){
133 //Select features with least effect on cost when removed (obtain minFeatures eventually)
134 double maxCost=0;
135 int removedFeature;
136 if(subset.empty()){
137 for(int iFeature=0;iFeature<v[0][0].size();++iFeature)
138 subset.push_back(iFeature);
139 }
140 if(subset.size()==minFeatures)
141 maxCost=theCostFactory.getCost(v);
142 while(subset.size()>minFeatures){
143 maxCost=removeFeature(v,theCostFactory,subset,removedFeature,verbose);
144 if(verbose){
145 for(std::list<int>::const_iterator lit=subset.begin();lit!=subset.end();++lit)
146 std::cout << *lit << " ";
147 std::cout << std::endl;
148 // std::cout << "removed " << removedFeature << ", " << subset.size() << "/" << minFeatures << " features remain with cost: " << maxCost << std::endl;
149 }
150 }//while
151 return maxCost;
152 }
153
154 //floating search
155 template<class T> double FeatureSelector::floating(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int maxFeatures, double epsilon, short verbose){
156 std::vector<T> cost;
157 int maxLevels=v[0][0].size();
158 if(maxFeatures<1)
159 maxFeatures=maxLevels;
160 int k=subset.size();
161 if(k>=maxFeatures)
162 return -1;
163 while(cost.size()<subset.size())
164 cost.push_back(1);//init original features as cost 1
165 cost.push_back(addFeature(v,theCostFactory,subset,verbose));
166 ++k;
167 if(verbose>1)
168 std::cout << "added " << subset.back() << ", " << subset.size() << "/" << maxFeatures << " features selected with cost: " << cost.back() << std::endl;
169 else if(verbose){
170 for(std::list<int>::const_iterator lit=subset.begin();lit!=subset.end();++lit)
171 std::cout << *lit << " ";
172 std::cout << std::endl;
173 }
174 while(k<maxFeatures){
175 cost.push_back(addFeature(v,theCostFactory,subset,verbose));
176 ++k;
177 if(verbose>1)
178 std::cout << "added " << subset.back() << ", " << subset.size() << "/" << maxFeatures << " features selected with cost: " << cost.back() << std::endl;
179 else if(verbose){
180 for(std::list<int>::const_iterator lit=subset.begin();lit!=subset.end();++lit)
181 std::cout << *lit << " ";
182 std::cout << " (cost: " << cost.back() << ")" << std::endl;
183 }
184
185 while(k>1){
186 int x_r;
187 double cost_r=removeFeature(v,theCostFactory,subset,x_r,verbose);
188 if(cost_r>cost[k-1]+epsilon){
189 --k;
190 cost[k]=cost_r;
191 cost.pop_back();
192 if(verbose>1)
193 std::cout << "removed " << x_r << ", " << subset.size() << "/" << maxFeatures << " features remain with cost: " << cost_r << std::endl;
194 else if(verbose){
195 for(std::list<int>::const_iterator lit=subset.begin();lit!=subset.end();++lit)
196 std::cout << *lit << " ";
197 std::cout << " (cost: " << cost.back() << ")" << std::endl;
198 }
199 continue;
200 }
201 else if(cost_r>=0){
202 subset.push_back(x_r);
203 break;
204 }
205 else if(verbose)
206 std::cout << "could not remove any feature" << std::endl;
207 cost.pop_back();
208 }
209 }
210 return cost.back();
211 }
212
213 //brute force search (search for all possible combinations and select best)
214 template<class T> double FeatureSelector::bruteForce(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int maxFeatures, short verbose){
215 int maxLevels=v[0][0].size();
216 if(maxFeatures<1)
217 maxFeatures=maxLevels;
218 int k=subset.size();
219 if(k>=maxFeatures)
220 return -1;
221 // gslmm::combination c(v1[0].size(),maxFeatures,false);
222 gsl_combination *c;
223 c=gsl_combination_calloc(v[0][0].size(),maxFeatures);
224
225 std::list<int> tmpset;//temporary set of selected features (levels)
226 std::vector< Vector2d<T> > tmpv(v.size());
227 std::list<int> catchset;//restore set in case of catch all the way to last level (no better cost)
228 //initialize maxCost with actual cost for current subset (-1 if empty subset)
229 double maxCost=-1;
230 double cost;
231 if(subset.size()>=maxLevels)
232 return maxCost;
233 gsl_combination_next(c);
234 do{
235 for(int ifeature=0;ifeature<maxFeatures;++ifeature)
236 tmpset.push_back(c->data[ifeature]);
237 for(int iclass=0;iclass<v.size();++iclass)
238 v[iclass].selectCols(tmpset,tmpv[iclass]);
239 try{
240 cost=theCostFactory.getCost(tmpv);
241 }
242 catch(...){ //singular matrix encountered
243 catchset=tmpset;//this tmpset resulted in failure of getCost
244 if(verbose){
245 std::cout << "Could not get cost from set: " << std::endl;
246 gsl_combination_fprintf(stdout,c," %u");
247 printf("\n");
248 }
249 tmpset.clear();
250 continue;
251 }
252 if(maxCost<cost){ //set with better cost is found
253 maxCost=cost;
254 subset=tmpset;
255 }
256 tmpset.clear();
257 }while(gsl_combination_next(c)==GSL_SUCCESS);
258 gsl_combination_free(c);
259 // }while(c.next());
260 if(maxCost<0) //no level added to better maxCost than current subset (catchset)
261 subset=catchset;
262 //test: assert list contains no duplicate elements
263 for(std::list<int>::iterator lit=subset.begin();lit!=subset.end();++lit){
264 std::list<int>::iterator lit2=lit;//start searching from next element
265 assert(find(++lit2,subset.end(),*lit)==subset.end());
266 }
267 return maxCost;
268 }
269
270 template<class T> double FeatureSelector::addFeature(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, short verbose){
271 //select feature with the best value (get maximal cost for 1 feature)
272 std::list<int> tmpset=subset;//temporary set of selected features (levels)
273 std::vector< Vector2d<T> > tmp(v.size());
274 std::list<int> catchset;//restore set in case of catch all the way to last level (no better cost)
275 //initialize maxCost with actual cost for current subset (-1 if empty subset)
276 double maxCost=-1;
277 double cost;
278 int maxLevels=v[0][0].size();
279 if(subset.size()>=maxLevels)
280 return maxCost;
281 for(int ilevel=0;ilevel<maxLevels;++ilevel){
282 if(find(tmpset.begin(),tmpset.end(),ilevel)!=tmpset.end())
283 continue;
284 tmpset.push_back(ilevel);
285 for(int iclass=0;iclass<v.size();++iclass){
286 // tmp[iclass].resize(v[iclass].size());
287 v[iclass].selectCols(tmpset,tmp[iclass]);
288 }
289 try{
290 cost=theCostFactory.getCost(tmp);
291 }
292 catch(...){
293 catchset=tmpset;//this tmpset resulted in singular matrix
294 if(verbose)
295 std::cout << "Could not add feature " << tmpset.back() << std::endl;
296 tmpset.pop_back();
297 continue;
298 }
299 if(maxCost<cost){ //level with better cost is found
300 maxCost=cost;
301 subset=tmpset;
302 }
303 tmpset.pop_back();
304 }
305 if(maxCost<0) //no level added to better maxCost than current subset (catchset)
306 subset=catchset;
307 //test: assert list contains no duplicate elements
308 for(std::list<int>::iterator lit=subset.begin();lit!=subset.end();++lit){
309 std::list<int>::iterator lit2=lit;//start searching from next element
310 assert(find(++lit2,subset.end(),*lit)==subset.end());
311 }
312 return maxCost;
313 }
314
315 template<class T> double FeatureSelector::removeFeature(std::vector< Vector2d<T> >& v, CostFactory& theCostFactory, std::list<int>& subset, int& r, short verbose){
316 //find the feature that has the least effect on the cost when it is removed from subset
317 std::list<int> tmpset=subset;//temporary set of selected features (levels)
318 std::vector< Vector2d<T> > tmp(v.size());
319 int nFeatures=subset.size();
320 std::list<int> catchset;//restore set in case of catch all the way to last level (no better cost)
321 //initialize maxCost with actual cost for current subset (-1 if empty subset)
322 double maxCost=-1;
323 int last;
324 double cost;
325 int maxLevels=v[0][0].size();
326 if(subset.size()>maxLevels||subset.empty()){
327 return maxCost;
328 }
329 std::list<int>::const_iterator it;
330 for(int i=0;i<nFeatures;++i){
331 last=tmpset.back();
332 tmpset.pop_back();
333 for(int iclass=0;iclass<v.size();++iclass){
334 // tmp[iclass].resize(v[iclass].size());
335 v[iclass].selectCols(tmpset,tmp[iclass]);
336 }
337 try{
338 cost=theCostFactory.getCost(tmp);
339 }
340 catch(...){
341 catchset=tmpset;//this tmpset resulted in singular matrix
342 if(verbose)
343 std::cout << "Could not remove feature " << last << std::endl;
344 tmpset.push_front(last);
345 continue;
346 }
347 if(maxCost<cost){ //level with better cost is found
348 maxCost=cost;
349 subset=tmpset;
350 r=last;
351 }
352 tmpset.push_front(last);
353 }
354 if(maxCost<0){//all levels removed were caught
355 subset=catchset;
356 }
357 //test: assert list contains no duplicate elements
358 for(std::list<int>::iterator lit=subset.begin();lit!=subset.end();++lit){
359 std::list<int>::iterator lit2=lit;//start searching from next element
360 assert(find(++lit2,subset.end(),*lit)==subset.end());
361 }
362 return maxCost;
363 }
364
365 #endif /* _FEATURESELECTOR_H_ */