| pktools 2.6.7 Processing Kernel for geospatial data

Public Member Functions | List of all members
FeatureSelector Class Reference

## Public Member Functions
template<class T >
double | forward (std::vector< Vector2d< T > > &v, CostFactory &theCostFactory, std::list< int > &subset, int maxFeatures=0, short verbose=0)
template<class T >
double | backward (std::vector< Vector2d< T > > &v, CostFactory &theCostFactory, std::list< int > &subset, int minFeatures, short verbose=0)
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)
template<class T >
double | bruteForce (std::vector< Vector2d< T > > &v, CostFactory &theCostFactory, std::list< int > &subset, int maxFeatures=0, short verbose=0)

## Detailed Description

Definition at line 35 of file FeatureSelector.h.

## Constructor & Destructor Documentation

## ◆ FeatureSelector()

FeatureSelector::FeatureSelector | ( |  | ) |
| inline

Definition at line 38 of file FeatureSelector.h.

38{};

## ◆ ~FeatureSelector()

FeatureSelector::~FeatureSelector | ( |  | ) |
| inline

Definition at line 39 of file FeatureSelector.h.

39{};

## Member Function Documentation

## ◆ backward()

template<class T >

double FeatureSelector::backward | ( | std::vector< Vector2d< T > > & | v,
|  | CostFactory & | theCostFactory,
|  | std::list< int > & | subset,
|  | int | minFeatures,
|  | short | verbose = `0`
| ) |  |

Definition at line 130 of file FeatureSelector.h.

130 {
131 //Select features with least effect on cost when removed (obtain minFeatures eventually)
132 double maxCost=0;
133 int removedFeature;
134 if(subset.empty()){
135 for(int iFeature=0;iFeature<v[0][0].size();++iFeature)
136 subset.push_back(iFeature);
137 }
138 if(subset.size()==minFeatures)
139 maxCost=theCostFactory.getCost(v);
140 while(subset.size()>minFeatures){
141 maxCost=removeFeature(v,theCostFactory,subset,removedFeature,verbose);
142 if(verbose){
143 for(std::list<int>::const_iterator lit=subset.begin();lit!=subset.end();++lit)
144 std::cout << *lit << " ";
145 std::cout << std::endl;
146 // std::cout << "removed " << removedFeature << ", " << subset.size() << "/" << minFeatures << " features remain with cost: " << maxCost << std::endl;
147 }
148 }//while
149 return maxCost;
150}

## ◆ bruteForce()

template<class T >

double FeatureSelector::bruteForce | ( | std::vector< Vector2d< T > > & | v,
|  | CostFactory & | theCostFactory,
|  | std::list< int > & | subset,
|  | int | maxFeatures = `0`,
|  | short | verbose = `0`
| ) |  |

Definition at line 212 of file FeatureSelector.h.

212 {
213 int maxLevels=v[0][0].size();
214 if(maxFeatures<1)
215 maxFeatures=maxLevels;
216 int k=subset.size();
217 if(k>=maxFeatures)
218 return -1;
219// gslmm::combination c(v1[0].size(),maxFeatures,false);
220 gsl_combination *c;
221 c=gsl_combination_calloc(v[0][0].size(),maxFeatures);
222
223 std::list<int> tmpset;//temporary set of selected features (levels)
224 std::vector< Vector2d<T> > tmpv(v.size());
225 std::list<int> catchset;//restore set in case of catch all the way to last level (no better cost)
226 //initialize maxCost with actual cost for current subset (-1 if empty subset)
227 double maxCost=-1;
228 double cost;
229 if(subset.size()>=maxLevels)
230 return maxCost;
231 gsl_combination_next(c);
232 do{
233 for(int ifeature=0;ifeature<maxFeatures;++ifeature)
234 tmpset.push_back(c->data[ifeature]);
235 for(int iclass=0;iclass<v.size();++iclass)
236 v[iclass].selectCols(tmpset,tmpv[iclass]);
237 try{
238 cost=theCostFactory.getCost(tmpv);
239 }
240 catch(...){ //singular matrix encountered
241 catchset=tmpset;//this tmpset resulted in failure of getCost
242 if(verbose){
243 std::cout << "Could not get cost from set: " << std::endl;
244 gsl_combination_fprintf(stdout,c," %u");
245 printf("\n");
246 }
247 tmpset.clear();
248 continue;
249 }
250 if(maxCost<cost){ //set with better cost is found
251 maxCost=cost;
252 subset=tmpset;
253 }
254 tmpset.clear();
255 }while(gsl_combination_next(c)==GSL_SUCCESS);
256 gsl_combination_free(c);
257// }while(c.next());
258 if(maxCost<0) //no level added to better maxCost than current subset (catchset)
259 subset=catchset;
260 //test: assert list contains no duplicate elements
261 for(std::list<int>::iterator lit=subset.begin();lit!=subset.end();++lit){
262 std::list<int>::iterator lit2=lit;//start searching from next element
263 assert(find(++lit2,subset.end(),*lit)==subset.end());
264 }
265 return maxCost;
266}

## ◆ floating()

template<class T >

double FeatureSelector::floating | ( | std::vector< Vector2d< T > > & | v,
|  | CostFactory & | theCostFactory,
|  | std::list< int > & | subset,
|  | int | maxFeatures = `0`,
|  | double | epsilon = `0.001`,
|  | short | verbose = `0`
| ) |  |

Definition at line 153 of file FeatureSelector.h.

153 {
154 std::vector<T> cost;
155 int maxLevels=v[0][0].size();
156 if(maxFeatures<1)
157 maxFeatures=maxLevels;
158 int k=subset.size();
159 if(k>=maxFeatures)
160 return -1;
161 while(cost.size()<subset.size())
162 cost.push_back(1);//init original features as cost 1
163 cost.push_back(addFeature(v,theCostFactory,subset,verbose));
164 ++k;
165 if(verbose>1)
166 std::cout << "added " << subset.back() << ", " << subset.size() << "/" << maxFeatures << " features selected with cost: " << cost.back() << std::endl;
167 else if(verbose){
168 for(std::list<int>::const_iterator lit=subset.begin();lit!=subset.end();++lit)
169 std::cout << *lit << " ";
170 std::cout << std::endl;
171 }
172 while(k<maxFeatures){
173 cost.push_back(addFeature(v,theCostFactory,subset,verbose));
174 ++k;
175 if(verbose>1)
176 std::cout << "added " << subset.back() << ", " << subset.size() << "/" << maxFeatures << " features selected with cost: " << cost.back() << std::endl;
177 else if(verbose){
178 for(std::list<int>::const_iterator lit=subset.begin();lit!=subset.end();++lit)
179 std::cout << *lit << " ";
180 std::cout << " (cost: " << cost.back() << ")" << std::endl;
181 }
182
183 while(k>1){
184 int x_r;
185 double cost_r=removeFeature(v,theCostFactory,subset,x_r,verbose);
186 if(cost_r>cost[k-1]+epsilon){
187 --k;
188 cost[k]=cost_r;
189 cost.pop_back();
190 if(verbose>1)
191 std::cout << "removed " << x_r << ", " << subset.size() << "/" << maxFeatures << " features remain with cost: " << cost_r << std::endl;
192 else if(verbose){
193 for(std::list<int>::const_iterator lit=subset.begin();lit!=subset.end();++lit)
194 std::cout << *lit << " ";
195 std::cout << " (cost: " << cost.back() << ")" << std::endl;
196 }
197 continue;
198 }
199 else if(cost_r>=0){
200 subset.push_back(x_r);
201 break;
202 }
203 else if(verbose)
204 std::cout << "could not remove any feature" << std::endl;
205 cost.pop_back();
206 }
207 }
208 return cost.back();
209}

## ◆ forward()

template<class T >

double FeatureSelector::forward | ( | std::vector< Vector2d< T > > & | v,
|  | CostFactory & | theCostFactory,
|  | std::list< int > & | subset,
|  | int | maxFeatures = `0`,
|  | short | verbose = `0`
| ) |  |

Definition at line 111 of file FeatureSelector.h.

111 {
112 //Select feature with the best value (get maximal cost for 1 feature)
113 double maxCost=0;
114 int maxLevels=v[0][0].size();
115 if(!maxFeatures)
116 maxFeatures=maxLevels;
117 while(subset.size()<maxFeatures){
118 maxCost=addFeature(v,theCostFactory,subset,verbose);
119 if(verbose){
120 for(std::list<int>::const_iterator lit=subset.begin();lit!=subset.end();++lit)
121 std::cout << *lit << " ";
122 std::cout << std::endl;
123 // std::cout << "added " << subset.back() << ", " << subset.size() << "/" << maxFeatures << " features selected with cost: " << maxCost << std::endl;
124 }
125 }//while
126 return maxCost;
127}

The documentation for this class was generated from the following file:

- /tmp/slackdce/slackrepo.NvCr9x/build_pktools/PKTOOLS-2.6.7.6/src/algorithms/FeatureSelector.h