Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
demo10.cpp — Example 10: Basic Filter-based feature selection

The compiled C++ exemplar of this scenario, programmed against the library API (CMake target demo10); the config-file form of the same demo is demos/demo10.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_cv.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_divergence.hpp"
//#include "criterion_multinom_bhattacharyya.hpp"
//#include "criterion_wrapper.hpp"
//#include "criterion_wrapper_bias_estimate.hpp"
//#include "criterion_subsetsize.hpp"
//#include "criterion_sumofweights.hpp"
//#include "criterion_negative.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 any search/step constructor
//#include "seq_step_hybrid.hpp"
//#include "seq_step_ensemble.hpp"
//#include "search_seq_sffs.hpp"
//#include "search_seq_sfrs.hpp"
//#include "search_seq_os.hpp"
//#include "search_seq_dos.hpp"
//#include "result_tracker_dupless.hpp"
//#include "result_tracker_regularizer.hpp"
//#include "result_tracker_feature_stats.hpp"
//#include "result_tracker_stabileval.hpp"
int main()
{
try{
typedef double RETURNTYPE; typedef double DATATYPE; typedef double REALTYPE;
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 10: Basic Filter-based feature selection..." << std::endl;
// in the course of search use the first half of data for feature selection and the second half for testing using 3-NN classifier
PSPLITTER dsp(new SPLITTER5050());
// 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);
std::shared_ptr<DATAACCESSOR> da(new DATAACCESSOR("data/wdbc_30.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.");
// initiate the storage for subset to-be-selected
std::shared_ptr<SUBSET> sub(new SUBSET(da->getNoOfFeatures())); sub->deselect_all();
// set-up the normal Generalized Mahalanobis criterion
std::shared_ptr<FILTERCRIT> crit(new FILTERCRIT);
crit->initialize(da); // initialization = normal model parameter estimation on training data part
// set-up the standard sequential search step object (options: hybrid, ensemble, etc.)
std::shared_ptr<EVALUATOR> eval(new EVALUATOR);
// set-up Sequential Forward Selection search procedure
srch.set_search_direction(FST::FORWARD); // try FST::BACKWARD
// run the search
std::cout << "Feature selection setup:" << std::endl << *da << std::endl << srch << std::endl << *crit << std::endl << std::endl;
RETURNTYPE critval_train, critval_test;
const DIMTYPE d=7; // request subset of size d; if set to 0, cardinality will be decided in the course of search
if(!srch.search(d,critval_train,sub,crit,std::cout)) throw FST::fst_error("Search not finished.");
// (optionally) the following line is included here just for illustration because srch.search() reports results in itself
std::cout << std::endl << "Search result: " << std::endl << *sub << std::endl << "Criterion value=" << critval_train << std::endl << std::endl;
// (optionally) validate result by estimating kNN accuracy on selected feature sub-space on independent test data
std::shared_ptr<CLASSIFIERKNN> cknn(new CLASSIFIERKNN); cknn->set_k(3);
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 k-Nearest Neighbor classifier.
Definition classifier_knn.hpp:95
Implements Generalized Mahalanobis distance based on normal (gaussian) model to serve as feature sele...
Definition criterion_normal_gmahalanobis.hpp:107
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 Euclidean (L_2) distance.
Definition distance_euclid.hpp:92
Implements Sequential_Forward_Selection and Sequential_Backward_Selection.
Definition search_seq_sfs.hpp:92
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.
Implements Generalized Mahalanobis distance based on normal (gaussian) model to serve as feature sele...
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...
int main()
Definition demo10.cpp:132
Implements Euclidean (L_2) distance.
FST4 specific error passing class.
Global definitions.
implements Sequential_Forward_Selection and Sequential_Backward_Selection
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.