Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
demo52t.cpp — Example 52t: (Threaded SFRS) Result regularization using secondary criterion

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.

/* =========================================================================
Feature Selection Toolbox 4 source code
---------------------------------------
*/ /*
=========================================================================
Copyright and license:
* FST4 software (with the exception of any bundled or externally linked
third-party components) is Copyright (C) 2000-present Institute of
Information Theory and Automation of the Czech Academy of Sciences (UTIA).
* FST4 is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the
Free Software Foundation, version 3. The full text of the license is in
the file LICENSE in the distribution root and at
https://www.gnu.org/licenses/agpl-3.0.html.
* FST4 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 Affero General Public
License for more details.
* Closed-source use without the AGPL's copyleft obligations requires a
commercial license from UTIA; in case of interest write to
utia@utia.cas.cz. LICENSING.md in the distribution root explains both
options, the contributor license agreement that every contribution
requires, and the contacts (technical questions and pull requests:
somol@utia.cas.cz, with "FST" in the subject line).
========================================================================== */
#include <memory>
#include <exception>
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include "error.hpp"
#include "global.hpp"
#include "subset.hpp"
//#include "data_splitter_holdout.hpp"
//#include "data_splitter_leave1out.hpp"
//#include "data_splitter_resub.hpp"
//#include "data_splitter_randrand.hpp"
//#include "data_splitter_randfix.hpp"
#include "data_scaler.hpp"
//#include "data_scaler_to01.hpp"
//#include "data_scaler_white.hpp"
//#include "criterion_normal_bhattacharyya.hpp"
//#include "criterion_normal_gmahalanobis.hpp"
//#include "criterion_normal_divergence.hpp"
//#include "criterion_multinom_bhattacharyya.hpp"
//#include "criterion_wrapper_bias_estimate.hpp"
//#include "criterion_sumofweights.hpp"
//#include "distance_L1.hpp"
//#include "distance_Lp.hpp"
//#include "classifier_normal_bayes.hpp"
//#include "classifier_multinom_naivebayes.hpp"
//#include "classifier_svm.hpp"
//#include "search_ranking_bif.hpp"
//#include "search_monte_carlo.hpp"
//#include "search_exhaustive.hpp"
//#include "branch_and_bound_predictor_averaging.hpp"
//#include "search_branch_and_bound_basic.hpp"
//#include "search_branch_and_bound_improved.hpp"
//#include "search_branch_and_bound_partial_prediction.hpp"
//#include "search_branch_and_bound_fast.hpp"
#include "candidate_evaluator_threaded.hpp" // thread-pooled candidate evaluation: pass its engine to the step constructor
//#include "seq_step_hybrid.hpp"
//#include "seq_step_ensemble.hpp"
//#include "search_seq_sfs.hpp"
//#include "search_seq_sffs.hpp"
//#include "search_seq_os.hpp"
//#include "search_seq_dos.hpp"
//#include "result_tracker_dupless.hpp"
//#include "result_tracker_feature_stats.hpp"
//#include "result_tracker_stabileval.hpp"
int main()
{
try{
const unsigned int max_threads=2; // worker threads of the candidate-evaluation engine (results are identical for any count)
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;
typedef FST::Data_Accessor_Splitting_MemTRN<DATATYPE,IDXTYPE,INTERVALLER> DATAACCESSOR; // uncomment for TRN data format
//typedef FST::Data_Accessor_Splitting_MemARFF<DATATYPE,IDXTYPE,INTERVALLER> DATAACCESSOR; // uncomment for ARFF data format
typedef FST::Candidate_Evaluator_Threaded<RETURNTYPE,DIMTYPE,SUBSET,WRAPPERKNN> ENGINE; // the thread-pooled candidate-evaluation engine injected into the step
std::cout << "Starting Example 52t: (Threaded SFRS) Result regularization using secondary criterion..." << std::endl;
// keep second half of data for independent testing of final classification performance
PSPLITTER dsp_outer(new SPLITTER5050());
// in the course of search use the first half of data by 3-fold cross-validation in wrapper FS criterion evaluation
PSPLITTER dsp_inner(new SPLITTERCV(3));
// do not scale data
std::shared_ptr<FST::Data_Scaler<DATATYPE> > dsc(new FST::Data_Scaler_void<DATATYPE>());
// set-up data access
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();
// initiate access to split data parts
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.");
// initiate the storage for subset to-be-selected
std::shared_ptr<SUBSET> sub(new SUBSET(da->getNoOfFeatures())); sub->deselect_all();
// set-up 3-Nearest Neighbor classifier based on Euclidean distances
std::shared_ptr<CLASSIFIERKNN> cknn(new CLASSIFIERKNN); cknn->set_k(3);
// wrap the 3-NN classifier to enable its usage as FS criterion (criterion value will be estimated by 3-fold cross-val.)
std::shared_ptr<WRAPPERKNN> wknn(new WRAPPERKNN);
wknn->initialize(cknn,da);
// set-up the standard sequential search step object with a thread-pooled evaluation engine of max_threads workers (options: hybrid, ensemble)
std::shared_ptr<EVALUATOR> eval(new EVALUATOR(EVALUATOR::PEvaluatorEngine(new ENGINE(max_threads))));
// set-up Sequential Forward Retreating Search procedure
srch.set_search_direction(FST::FORWARD);
// set-up the regularizing result tracker
std::shared_ptr<TRACKER> tracker(new TRACKER);
// register the result tracker with the used search step object
eval->enable_result_tracking(tracker);
// run the search
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;
srch.set_output_detail(FST::NORMAL); // set FST::SILENT to disable all text output in the course of search (FST::NORMAL is default)
if(!srch.search(0,critval_train,sub,wknn,std::cout)) throw FST::fst_error("Search not finished.");
// (optionally) validate result by estimating kNN accuracy on selected feature sub-space on independent test data
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;
// set-up the secondary criterion (regularization criterion); in this case to minimize subset size
std::shared_ptr<CRITSUBSIZE> critsubsiz(new CRITSUBSIZE); //Criterion_Subset_Size does not need to be initialized
std::shared_ptr<NEGATIVECRIT> regulcrit(new NEGATIVECRIT(critsubsiz)); //Criterion_Negative does not need to be initialized
// select final solution among those recorded by tracker (show more alternatives for various margins)
tracker->set_output_detail(FST::NORMAL); // set FST::SILENT to disable all text output in the course of search (FST::NORMAL is default)
for(unsigned int i=1; i<10; i++)
{
RETURNTYPE margin=(double)i*0.001;
da->setSplittingDepth(1); // necessary with criteria that need access to training data
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;
// (optionally) validate result by estimating kNN accuracy on selected feature sub-space on independent test data
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.
Global definitions.
@ 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.
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.