/**********************************************************************
FeatureSelector.h: select features, typical use: feature selection for classification
Author(s): Pieter.Kempeneers@ec.europa.eu
Copyright (C) 2016-2020 European Union (Joint Research Centre)
This file is part of jiplib.
jiplib is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
jiplib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with jiplib. If not, see .
***********************************************************************/
#ifndef _FEATURESELECTOR_H_
#define _FEATURESELECTOR_H_
/* #include */
#include
#include
#include
#include
#include
#include
#include "ConfusionMatrix.h"
#include "base/IndexValue.h"
#include "base/Vector2d.h"
#include "gsl/gsl_combination.h"
#include "CostFactory.h"
class FeatureSelector
{
public:
FeatureSelector(){};
~FeatureSelector(){};
template double forward(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int maxFeatures=0, short verbose=0);
template double backward(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int minFeatures, short verbose=0);
template double floating(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int maxFeatures=0, double epsilon=0.001, short verbose=0);
template double bruteForce(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int maxFeatures=0, short verbose=0);
private:
template double addFeature(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, short verbose=0);
template double removeFeature(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int& r, short verbose=0);
template double forwardUnivariate(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int maxFeatures=0, short verbose=0);
};
//sequential forward selection Univariate (N single best features)
template double FeatureSelector::forwardUnivariate(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int maxFeatures, short verbose){
int maxLevels=v[0][0].size();
if(!maxFeatures)
maxFeatures=maxLevels;
int k=subset.size();
if(k>=maxFeatures)
return -1;
std::vector cost(maxLevels);
std::list tmpset=subset;//temporary set of selected features (levels)
std::vector< Vector2d > tmp(v.size());
for(int ilevel=0;ilevel0)
subset.push_back(cost[ilevel].position);
if(verbose)
std::cout << "feature " << subset.back() << " has cost: " << cost[ilevel].value << std::endl;
++ilevel;
}
double maxCost=-1;
while(subset.size()){
for(int iclass=0;iclass double FeatureSelector::forward(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int maxFeatures, short verbose){
//Select feature with the best value (get maximal cost for 1 feature)
double maxCost=0;
int maxLevels=v[0][0].size();
if(!maxFeatures)
maxFeatures=maxLevels;
while(subset.size()::const_iterator lit=subset.begin();lit!=subset.end();++lit)
std::cout << *lit << " ";
std::cout << std::endl;
// std::cout << "added " << subset.back() << ", " << subset.size() << "/" << maxFeatures << " features selected with cost: " << maxCost << std::endl;
}
}//while
return maxCost;
}
//sequential backward selection
template double FeatureSelector::backward(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int minFeatures, short verbose){
//Select features with least effect on cost when removed (obtain minFeatures eventually)
double maxCost=0;
int removedFeature;
if(subset.empty()){
for(int iFeature=0;iFeatureminFeatures){
maxCost=removeFeature(v,theCostFactory,subset,removedFeature,verbose);
if(verbose){
for(std::list::const_iterator lit=subset.begin();lit!=subset.end();++lit)
std::cout << *lit << " ";
std::cout << std::endl;
// std::cout << "removed " << removedFeature << ", " << subset.size() << "/" << minFeatures << " features remain with cost: " << maxCost << std::endl;
}
}//while
return maxCost;
}
//floating search
template double FeatureSelector::floating(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int maxFeatures, double epsilon, short verbose){
std::vector cost;
int maxLevels=v[0][0].size();
if(maxFeatures<1)
maxFeatures=maxLevels;
int k=subset.size();
if(k>=maxFeatures)
return -1;
while(cost.size()1)
std::cout << "added " << subset.back() << ", " << subset.size() << "/" << maxFeatures << " features selected with cost: " << cost.back() << std::endl;
else if(verbose){
for(std::list::const_iterator lit=subset.begin();lit!=subset.end();++lit)
std::cout << *lit << " ";
std::cout << std::endl;
}
while(k1)
std::cout << "added " << subset.back() << ", " << subset.size() << "/" << maxFeatures << " features selected with cost: " << cost.back() << std::endl;
else if(verbose){
for(std::list::const_iterator lit=subset.begin();lit!=subset.end();++lit)
std::cout << *lit << " ";
std::cout << " (cost: " << cost.back() << ")" << std::endl;
}
while(k>1){
int x_r;
double cost_r=removeFeature(v,theCostFactory,subset,x_r,verbose);
if(cost_r>cost[k-1]+epsilon){
--k;
cost[k]=cost_r;
cost.pop_back();
if(verbose>1)
std::cout << "removed " << x_r << ", " << subset.size() << "/" << maxFeatures << " features remain with cost: " << cost_r << std::endl;
else if(verbose){
for(std::list::const_iterator lit=subset.begin();lit!=subset.end();++lit)
std::cout << *lit << " ";
std::cout << " (cost: " << cost.back() << ")" << std::endl;
}
continue;
}
else if(cost_r>=0){
subset.push_back(x_r);
break;
}
else if(verbose)
std::cout << "could not remove any feature" << std::endl;
cost.pop_back();
}
}
return cost.back();
}
//brute force search (search for all possible combinations and select best)
template double FeatureSelector::bruteForce(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int maxFeatures, short verbose){
int maxLevels=v[0][0].size();
if(maxFeatures<1)
maxFeatures=maxLevels;
int k=subset.size();
if(k>=maxFeatures)
return -1;
// gslmm::combination c(v1[0].size(),maxFeatures,false);
gsl_combination *c;
c=gsl_combination_calloc(v[0][0].size(),maxFeatures);
std::list tmpset;//temporary set of selected features (levels)
std::vector< Vector2d > tmpv(v.size());
std::list catchset;//restore set in case of catch all the way to last level (no better cost)
//initialize maxCost with actual cost for current subset (-1 if empty subset)
double maxCost=-1;
double cost;
if(subset.size()>=maxLevels)
return maxCost;
gsl_combination_next(c);
do{
for(int ifeature=0;ifeaturedata[ifeature]);
for(int iclass=0;iclass::iterator lit=subset.begin();lit!=subset.end();++lit){
std::list::iterator lit2=lit;//start searching from next element
assert(find(++lit2,subset.end(),*lit)==subset.end());
}
return maxCost;
}
template double FeatureSelector::addFeature(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, short verbose){
//select feature with the best value (get maximal cost for 1 feature)
std::list tmpset=subset;//temporary set of selected features (levels)
std::vector< Vector2d > tmp(v.size());
std::list catchset;//restore set in case of catch all the way to last level (no better cost)
//initialize maxCost with actual cost for current subset (-1 if empty subset)
double maxCost=-1;
double cost;
int maxLevels=v[0][0].size();
if(subset.size()>=maxLevels)
return maxCost;
for(int ilevel=0;ilevel::iterator lit=subset.begin();lit!=subset.end();++lit){
std::list::iterator lit2=lit;//start searching from next element
assert(find(++lit2,subset.end(),*lit)==subset.end());
}
return maxCost;
}
template double FeatureSelector::removeFeature(std::vector< Vector2d >& v, CostFactory& theCostFactory, std::list& subset, int& r, short verbose){
//find the feature that has the least effect on the cost when it is removed from subset
std::list tmpset=subset;//temporary set of selected features (levels)
std::vector< Vector2d > tmp(v.size());
int nFeatures=subset.size();
std::list catchset;//restore set in case of catch all the way to last level (no better cost)
//initialize maxCost with actual cost for current subset (-1 if empty subset)
double maxCost=-1;
int last;
double cost;
int maxLevels=v[0][0].size();
if(subset.size()>maxLevels||subset.empty()){
return maxCost;
}
std::list::const_iterator it;
for(int i=0;i::iterator lit=subset.begin();lit!=subset.end();++lit){
std::list::iterator lit2=lit;//start searching from next element
assert(find(++lit2,subset.end(),*lit)==subset.end());
}
return maxCost;
}
#endif /* _FEATURESELECTOR_H_ */