Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
data_splitter_randfix.hpp
Go to the documentation of this file.
1#ifndef FSTDATASPLITTERRANDFIX_H
2#define FSTDATASPLITTERRANDFIX_H
3 /*
27=========================================================================
28Copyright and license:
29 * FST4 software (with the exception of any bundled or externally linked
30 third-party components) is Copyright (C) 2000-present Institute of
31 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
32 * FST4 is free software: you can redistribute it and/or modify it under
33 the terms of the GNU Affero General Public License as published by the
34 Free Software Foundation, version 3. The full text of the license is in
35 the file LICENSE in the distribution root and at
36 https://www.gnu.org/licenses/agpl-3.0.html.
37 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
38 WARRANTY; without even the implied warranty of MERCHANTABILITY or
39 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
40 License for more details.
41 * Closed-source use without the AGPL's copyleft obligations requires a
42 commercial license from UTIA; in case of interest write to
43 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
44 options, the contributor license agreement that every contribution
45 requires, and the contacts (technical questions and pull requests:
46 somol@utia.cas.cz, with "FST" in the subject line).
47========================================================================== */
48
49#include <memory>
50#include <cassert>
51#include <iostream>
52#include <cstdlib>
53#include <ctime>
54#include <vector>
55#include "error.hpp"
56#include "global.hpp"
58
59/*============== Template parameter type naming conventions ==============
60--------- Numeric types: -------------------------------------------------
61DATATYPE - data sample values - usually real numbers (but may be integers
62 in text processing etc.)
63REALTYPE - must be real numbers - for representing intermediate results of
64 calculations like mean, covariance etc.
65IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
66 extent depends on numbers of samples in data
67DIMTYPE - index values for enumeration of features (dimensions), or classes (not
68 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
69 but be aware of expressions like _classes*_features*_features !
70 in linearized representations of feature matrices for all classes
71BINTYPE - feature selection marker type - represents ca. <10 different feature
72 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
73RETURNTYPE - criterion value: real value, but may be extended in future to support
74 multiple values
75--------- Class types: ---------------------------------------------------
76SUBSET - class of class type Subset
77CLASSIFIER - class implementing interface defined in abstract class Classifier
78EVALUATOR - class implementing interface defined in abstract class Sequential_Step
79DISTANCE - class implementing interface defined in abstract class Distance
80DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
81INTERVALCONTAINER - class of class type Data_Intervaller
82CONTAINER - STL container of class type Data_Interval
83========================================================================== */
84
85namespace FST {
86
87// NOTE: this is a special-purpose modification of Data_Splitter_RandomRandom
88// that generates train data equally to Data_Splitter_RandomRandom
89// but selects always the same test data (consecutive interval at the end of each class)
90
101template<class INTERVALCONTAINER, typename IDXTYPE, typename BINTYPE>
102class Data_Splitter_TrainRandom_TestFixed : public Clonable_Impl<Data_Splitter_TrainRandom_TestFixed<INTERVALCONTAINER,IDXTYPE,BINTYPE>,Data_Splitter_RandomRandom<INTERVALCONTAINER,IDXTYPE,BINTYPE>,CLONE_INHERIT,CLONE_INHERIT,CLONE_DEEP> {
103public:
106 Data_Splitter_TrainRandom_TestFixed(const IDXTYPE _splits, const IDXTYPE _perctrain, const IDXTYPE _perctest, const bool randomize=false) : parent_impl(_splits, _perctrain, _perctest, randomize) {notify("Data_Splitter_TrainRandom_TestFixed constructor.");}
107 Data_Splitter_TrainRandom_TestFixed(const Data_Splitter_TrainRandom_TestFixed& dsp) : parent_impl(dsp) {notify("Data_Splitter_TrainRandom_TestFixed copy constructor.");}
108 virtual ~Data_Splitter_TrainRandom_TestFixed() {notify("Data_Splitter_TrainRandom_TestFixed destructor.");}
109
110 virtual std::ostream& print(std::ostream& os) const override {os << "Data_Splitter_TrainRandom_TestFixed(splits=" << parent::splits << ", %train=" << parent::perctrain << ", %test=" << parent::perctest << ")"; return os;}
111protected:
115 virtual void makeRandomSplit(const IDXTYPE n, const std::shared_ptr<INTERVALCONTAINER> list_train, const std::shared_ptr<INTERVALCONTAINER> list_test) override;
116};
117
118template<class INTERVALCONTAINER, typename IDXTYPE, typename BINTYPE>
119void Data_Splitter_TrainRandom_TestFixed<INTERVALCONTAINER,IDXTYPE,BINTYPE>::makeRandomSplit(const IDXTYPE n, const std::shared_ptr<INTERVALCONTAINER> list_train, const std::shared_ptr<INTERVALCONTAINER> list_test)
120{
121 try {
122 assert(list_train);
123 assert(list_test);
124 assert(n>0);
126 if(!parent::_data || parent::_n_max<n) {parent::_data.reset(new BINTYPE[n]); parent::_n_max=n;}
127 const BINTYPE id_empty = 0;
128 const BINTYPE id_train = 1;
129 const IDXTYPE tesiz=(n * parent::perctest)/100;
130 const IDXTYPE trsiz= (parent::perctrain+parent::perctest==100) ? n-tesiz : (n * parent::perctrain)/100;
131 assert(trsiz+tesiz<=n);
132 IDXTYPE i;
133 for(i=0;i<n;i++) parent::_data[i]=id_empty;
134
135 // now fill randomly perctrain% for training; the test part is the fixed trailing interval
136 // NOTE: acceleration - whichever of the train set and its complement is smaller is the one
137 // actually drawn, so the number of random draws is min{perctrain,100-perctrain-perctest}
138 // rather than always perctrain; the other side is produced by pre-filling _data
140 parent::fill_randomly(n,id_empty,id_train,trsiz/*count*/,0/*minidx*/,n-1-tesiz/*maxidx*/); // randomly choose train samples
141 } else {
142 parent::fill(n,id_empty,id_train,0/*minidx*/,n-1-tesiz/*maxidx*/);
143 parent::fill_randomly(n,id_train,id_empty,n-tesiz-trsiz/*count*/,0/*minidx*/,n-1-tesiz/*maxidx*/); // randomly choose non-train samples
144 }
145
146 // now transform the binary representation to TCC::train list and create TCC::test list
147 parent::translate(n,id_train,list_train);
148 list_test->clear();
149 if(tesiz>0) {Data_Interval<IDXTYPE> tin={n-tesiz,tesiz}; list_test->push_back(tin);}
150
151 } catch(...) {
152 throw fst_error("Data_Splitter_TrainRandom_TestFixed::makeRandomSplit() error.");
153 }
154}
155
156
157} // namespace
158#endif // FSTDATASPLITTERRANDFIX_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Implements train/test data splitting: use randomly chosen x% of data samples for training and another...
Definition data_splitter_randrand.hpp:107
std::unique_ptr< BINTYPE[]> _data
pattern selection array: one BINTYPE label (id_empty/id_train/id_test) per sample index,...
Definition data_splitter_randrand.hpp:158
const IDXTYPE perctest
Definition data_splitter_randrand.hpp:161
const IDXTYPE splits
Definition data_splitter_randrand.hpp:162
IDXTYPE _n_max
actual _data size (capacity; may exceed the current class size n)
Definition data_splitter_randrand.hpp:157
void translate(const IDXTYPE n, const BINTYPE id_fill, const std::shared_ptr< INTERVALCONTAINER > lst)
Converts the binary _data labeling (id_fill vs. everything else) over indices [0,n) into lst's list o...
Definition data_splitter_randrand.hpp:204
void fill_randomly(const IDXTYPE n, const BINTYPE id_empty, const BINTYPE id_fill, const IDXTYPE count, const IDXTYPE minidx, const IDXTYPE maxidx)
Randomly relabels count of the still-id_empty slots of _data (within [minidx,maxidx]) to id_fill; dra...
Definition data_splitter_randrand.hpp:169
void fill(const IDXTYPE n, const BINTYPE id_empty, const BINTYPE id_fill, const IDXTYPE minidx, const IDXTYPE maxidx)
Relabels every still-id_empty slot of _data (within [minidx,maxidx]) to id_fill, unconditionally (no ...
Definition data_splitter_randrand.hpp:194
const IDXTYPE perctrain
Definition data_splitter_randrand.hpp:160
Implements train/test data splitting: equal to Data_Splitter_RandomRandom except that the same test d...
Definition data_splitter_randfix.hpp:102
Data_Splitter_TrainRandom_TestFixed(const Data_Splitter_TrainRandom_TestFixed &dsp)
copies configuration
Definition data_splitter_randfix.hpp:107
Data_Splitter< INTERVALLER, IDXTYPE > TCC
Definition data_splitter_randfix.hpp:112
virtual void makeRandomSplit(const IDXTYPE n, const std::shared_ptr< INTERVALCONTAINER > list_train, const std::shared_ptr< INTERVALCONTAINER > list_test) override
Definition data_splitter_randfix.hpp:119
Data_Splitter_RandomRandom< INTERVALLER, IDXTYPE, BINTYPE > parent
Definition data_splitter_randfix.hpp:104
Clonable_Impl< Data_Splitter_TrainRandom_TestFixed, parent, CLONE_INHERIT, CLONE_INHERIT, CLONE_DEEP > parent_impl
Definition data_splitter_randfix.hpp:105
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the splitter and its configuration; base default writes not...
Definition data_splitter_randfix.hpp:110
Data_Splitter_TrainRandom_TestFixed(const IDXTYPE _splits, const IDXTYPE _perctrain, const IDXTYPE _perctest, const bool randomize=false)
same parameters as Data_Splitter_RandomRandom; the test part is always the fixed trailing _perctest %...
Definition data_splitter_randfix.hpp:106
Abstract class, defines interface for data splitting implementations (for use in data accessors).
Definition data_splitter.hpp:124
FST4 specific error passing class.
Definition error.hpp:61
Implements train/test data splitting: use randomly chosen x% of data samples for training and another...
FST4 specific error passing class.
Global definitions.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
Support structure to hold data interval (referring into data held by data accessor).
Definition data_intervaller.hpp:88
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.