The compiled C++ exemplar of this scenario, programmed against the library API (CMake target demo52t); the config-file form of the same demo is demos/demo52t.cfg, described on the Demos page of the FST4 web site.
#include <memory>
#include <exception>
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
{
try{
const unsigned int max_threads=2;
typedef double RETURNTYPE; typedef double DATATYPE;
typedef unsigned int IDXTYPE; typedef unsigned int DIMTYPE; typedef short BINTYPE;
typedef std::shared_ptr<FST::Data_Splitter<INTERVALLER,IDXTYPE> > PSPLITTER;
std::cout << "Starting Example 52t: (Threaded SFRS) Result regularization using secondary criterion..." << std::endl;
PSPLITTER dsp_outer(new SPLITTER5050());
PSPLITTER dsp_inner(new SPLITTERCV(3));
std::shared_ptr<std::vector<PSPLITTER> > splitters(new std::vector<PSPLITTER>);
splitters->push_back(dsp_outer); splitters->push_back(dsp_inner);
std::shared_ptr<DATAACCESSOR> da(new DATAACCESSOR("data/waveform_40.trn",splitters,dsc));
da->initialize();
da->setSplittingDepth(0);
if(!da->getFirstSplit())
throw FST::fst_error(
"50/50 data split failed.");
da->setSplittingDepth(1);
if(!da->getFirstSplit())
throw FST::fst_error(
"3-fold cross-validation failure.");
std::shared_ptr<SUBSET> sub(new SUBSET(da->getNoOfFeatures())); sub->deselect_all();
std::shared_ptr<CLASSIFIERKNN> cknn(new CLASSIFIERKNN); cknn->set_k(3);
std::shared_ptr<WRAPPERKNN> wknn(new WRAPPERKNN);
wknn->initialize(cknn,da);
std::shared_ptr<EVALUATOR> eval(new EVALUATOR(EVALUATOR::PEvaluatorEngine(new ENGINE(max_threads))));
srch.set_search_direction(FST::FORWARD);
std::shared_ptr<TRACKER> tracker(new TRACKER);
eval->enable_result_tracking(tracker);
std::cout << "Feature selection setup:" << std::endl << *da << std::endl << srch << std::endl << *wknn << std::endl << *tracker << std::endl << std::endl;
RETURNTYPE critval_train, critval_test;
if(!srch.search(0,critval_train,sub,wknn,std::cout))
throw FST::fst_error(
"Search not finished.");
da->setSplittingDepth(0);
cknn->train(da,sub);
cknn->test(critval_test,da);
std::cout << "Validated "<<cknn->get_k()<<"-NN accuracy=" << critval_test << std::endl << std::endl;
std::shared_ptr<CRITSUBSIZE> critsubsiz(new CRITSUBSIZE);
std::shared_ptr<NEGATIVECRIT> regulcrit(new NEGATIVECRIT(critsubsiz));
for(unsigned int i=1; i<10; i++)
{
RETURNTYPE margin=(double)i*0.001;
da->setSplittingDepth(1);
if(!tracker->optimize_within_margin(margin,critval_train,critval_test,sub,regulcrit))
throw FST::fst_error(
"tracker->optimize_within_margin() failed.");
std::cout << std::endl << "Regularized (margin="<<margin<<") result: " << std::endl << *sub << "Criterion value=" << critval_train << std::endl;
da->setSplittingDepth(0);
cknn->train(da,sub);
cknn->test(critval_test,da);
std::cout << "Validated "<<cknn->get_k()<<"-NN accuracy=" << critval_test << std::endl << std::endl;
}
}
catch(
FST::fst_error &e) {std::cerr<<
"FST ERROR: "<< e.what() <<
", code=" << e.
code() << std::endl;
return 1;}
catch(std::exception &e) {std::cerr<<"non-FST ERROR: "<< e.what() << std::endl; return 1;}
return 0;
}
Implements concurrent evaluation of a criterion on a set of subset candidates.
Implements concurrent evaluation of a criterion on a set of subset candidates.
Definition candidate_evaluator_threaded.hpp:156
Implements k-Nearest Neighbor classifier.
Definition classifier_knn.hpp:95
Returns the negative of another Criterion's result.
Definition criterion_negative.hpp:99
Returns the (positive) subset size sub->get_d() - since FST4 searches maximize, this rates LARGER sub...
Definition criterion_subsetsize.hpp:105
Wraps Classifier objects to serve as feature selection criterion.
Definition criterion_wrapper.hpp:95
Implements data access to data cached entirely in memory, read once from a TRN file.
Definition data_accessor_splitting_memTRN.hpp:91
Container to hold list of Data_Interval; implements nested interval reduction.
Definition data_intervaller.hpp:102
Void data scaler, to bypass data normalization.
Definition data_scaler_void.hpp:94
Implements train/test data splitting: first 50% of original data for training, second 50% for testing...
Definition data_splitter_5050.hpp:99
Implements train/test data splitting: by means of k-fold cross-validation.
Definition data_splitter_cv.hpp:106
Implements Euclidean (L_2) distance.
Definition distance_euclid.hpp:92
Collects multiple results. Enables eventual selection of alternative solution based on secondary crit...
Definition result_tracker_regularizer.hpp:104
Implements Sequential_Forward_Retreating_Search and Sequential_Backward_Retreating_Search.
Definition search_seq_sfrs.hpp:104
Implements standard sequential selection step in sequential search type of methods.
Definition seq_step_straight.hpp:103
Stores the info on currently selected features, enables generating permutations, etc.
Definition subset.hpp:104
FST4 specific error passing class.
Definition error.hpp:61
int code() const
returns the numeric error code supplied at construction (0 if none was given)
Definition error.hpp:67
Implements k-Nearest Neighbor classifier.
Criterion_Negative returns the negative of another Criterion's result.
Criterion_Subset_Size returns the (positive) subset size - rates larger subsets higher; wrap in Crite...
Criterion_Wrapper adapts Classifier objects to serve as feature selection criterion.
Implements data access to data cached entirely in memory, read once from ARFF files.
Implements data access to data cached entirely in memory, read once from TRN files.
Container to hold a list of Data_Interval; implements nested interval reduction.
Defines interface for data scaling implementations (for use in data accessors).
Void data scaler, to bypass data normalization.
Defines interface for data splitting implementations (for use in data accessors).
Implements train/test data splitting: first 50% of original data for training, second 50% for testing...
Implements train/test data splitting: by means of k-fold cross-validation.
int main()
Definition demo10.cpp:132
Implements Euclidean (L_2) distance.
FST4 specific error passing class.
@ NORMAL
informs about new found solutions and time elapsed etc.
Definition global.hpp:69
Enables eventual selection of a different subset.
Implements Sequential_Forward_Retreating_Search and Sequential_Backward_Retreating_Search.
Implements standard sequential selection step in sequential search type of methods.
Stores the info on currently selected features, enables generating permutations, etc.