Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
data_splitter_leave1out.hpp
Go to the documentation of this file.
1#ifndef FSTDATASPLITTERLEAVE1OUT_H
2#define FSTDATASPLITTERLEAVE1OUT_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 <iostream>
51#include <cstdlib>
52#include <ctime>
53#include <vector>
54#include "error.hpp"
55#include "global.hpp"
56#include "data_splitter.hpp"
57
58/*============== Template parameter type naming conventions ==============
59--------- Numeric types: -------------------------------------------------
60DATATYPE - data sample values - usually real numbers (but may be integers
61 in text processing etc.)
62REALTYPE - must be real numbers - for representing intermediate results of
63 calculations like mean, covariance etc.
64IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
65 extent depends on numbers of samples in data
66DIMTYPE - index values for enumeration of features (dimensions), or classes (not
67 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
68 but be aware of expressions like _classes*_features*_features !
69 in linearized representations of feature matrices for all classes
70BINTYPE - feature selection marker type - represents ca. <10 different feature
71 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
72RETURNTYPE - criterion value: real value, but may be extended in future to support
73 multiple values
74--------- Class types: ---------------------------------------------------
75SUBSET - class of class type Subset
76CLASSIFIER - class implementing interface defined in abstract class Classifier
77EVALUATOR - class implementing interface defined in abstract class Sequential_Step
78DISTANCE - class implementing interface defined in abstract class Distance
79DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
80INTERVALCONTAINER - class of class type Data_Intervaller
81CONTAINER - STL container of class type Data_Interval
82========================================================================== */
83
84namespace FST {
85
86// LEAVE-ONE-OUT
87
99template<class INTERVALCONTAINER, typename IDXTYPE>
100class Data_Splitter_Leave1Out : public Clonable_Impl<Data_Splitter_Leave1Out<INTERVALCONTAINER,IDXTYPE>,Data_Splitter<INTERVALCONTAINER,IDXTYPE>,CLONE_INHERIT,CLONE_INHERIT,CLONE_DEEP> {
101public:
103 Data_Splitter_Leave1Out() : parent_impl() {notify("Data_Splitter_Leave1Out (empty) constructor.");}
107 Data_Splitter_Leave1Out(const Data_Splitter_Leave1Out& dsp) : parent_impl(dsp), pivot(0) {notify("Data_Splitter_Leave1Out copy constructor.");}
108 virtual ~Data_Splitter_Leave1Out() {notify("Data_Splitter_Leave1Out destructor.");}
109 virtual IDXTYPE getNoOfSplits() const override;
110
111 virtual std::ostream& print(std::ostream& os) const override {os << "Data_Splitter_Leave1Out()"; return os;}
112protected:
114 // Template-method hooks: makeFirstSplit()/makeNextSplit() skeletons live in Data_Splitter
115 virtual const char* splitter_name() const override {return "Data_Splitter_Leave1Out";}
116 virtual bool prepareFirstSplit() override {
117 pivotclass=TCC::csplit.begin();
118 while(pivotclass!=TCC::csplit.end() && (*pivotclass)->n==0) pivotclass++;
119 if(pivotclass==TCC::csplit.end()) return false;
120 pivot=0;
121 return true;
122 }
123 virtual bool prepareNextSplit() override {
124 if(pivotclass==TCC::csplit.end()) return false;
125 if(pivot>=(*pivotclass)->n) {
126 pivot=0;
127 pivotclass++;
128 while(pivotclass!=TCC::csplit.end() && (*pivotclass)->n==0) pivotclass++;
129 if(pivotclass==TCC::csplit.end()) return false;
130 }
131 return true;
132 }
133 virtual void splitClassFirst(typename TCC::ClassIterator c, const unsigned int) override {
134 if(c==pivotclass) {
135 Data_Interval<IDXTYPE> tri={pivot+1,(*c)->n-1}; (*c)->train->clear(); (*c)->train->push_back(tri);
136 Data_Interval<IDXTYPE> tei={pivot,1}; (*c)->test->clear(); (*c)->test->push_back(tei);
137 } else {
138 Data_Interval<IDXTYPE> tri={0,(*c)->n}; (*c)->train->clear(); (*c)->train->push_back(tri); // all
139 (*c)->test->clear();
140 }
141 }
142 virtual void splitClassNext(typename TCC::ClassIterator c, const unsigned int) override {
143 if(c==pivotclass) {
144 (*c)->train->clear();
145 if(pivot>0) {Data_Interval<IDXTYPE> tri={0,pivot}; (*c)->train->push_back(tri);}
146 if(pivot<(*pivotclass)->n-1) {Data_Interval<IDXTYPE> tri2={pivot+1,(*c)->n-(pivot+1)}; (*c)->train->push_back(tri2);}
147 Data_Interval<IDXTYPE> tei={pivot,1}; (*c)->test->clear(); (*c)->test->push_back(tei);
148 } else {
149 Data_Interval<IDXTYPE> tri={0,(*c)->n}; (*c)->train->clear(); (*c)->train->push_back(tri); // all
150 (*c)->test->clear();
151 }
152 }
153 virtual void finishSplit() override {++pivot;}
154private:
155 IDXTYPE pivot;
157};
158
159template<class INTERVALCONTAINER, typename IDXTYPE>
161{
162 IDXTYPE sum=0;
163 for(typename TCC::ClassIterator c=TCC::csplit.begin();c!=TCC::csplit.end();c++) sum+=(*c)->n;
164 return sum;
165}
166
167
168} // namespace
169#endif // FSTDATASPLITTERLEAVE1OUT_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Implements train/test data splitting: by means of leave-one-out.
Definition data_splitter_leave1out.hpp:100
virtual const char * splitter_name() const override
class name, used in error messages
Definition data_splitter_leave1out.hpp:115
virtual IDXTYPE getNoOfSplits() const override
Definition data_splitter_leave1out.hpp:160
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_leave1out.hpp:111
Data_Splitter< INTERVALLER, IDXTYPE > TCC
Definition data_splitter_leave1out.hpp:113
TCC::ClassIterator pivotclass
Definition data_splitter_leave1out.hpp:156
virtual void splitClassFirst(typename TCC::ClassIterator c, const unsigned int) override
write first-split train/test intervals of class cidx (c is that class's entry in csplit)
Definition data_splitter_leave1out.hpp:133
IDXTYPE pivot
Definition data_splitter_leave1out.hpp:155
virtual void splitClassNext(typename TCC::ClassIterator c, const unsigned int) override
write next-split train/test intervals of class cidx (a single-split splitter never gets here,...
Definition data_splitter_leave1out.hpp:142
virtual bool prepareFirstSplit() override
set up split-sequence state; false = no split can be made
Definition data_splitter_leave1out.hpp:116
Clonable_Impl< Data_Splitter_Leave1Out, Data_Splitter< INTERVALLER, IDXTYPE >, CLONE_INHERIT, CLONE_INHERIT, CLONE_DEEP > parent_impl
Definition data_splitter_leave1out.hpp:102
Data_Splitter_Leave1Out(const Data_Splitter_Leave1Out &dsp)
Definition data_splitter_leave1out.hpp:107
virtual void finishSplit() override
post-loop state update shared by first/next split
Definition data_splitter_leave1out.hpp:153
virtual bool prepareNextSplit() override
advance split-sequence state; false = no more splits
Definition data_splitter_leave1out.hpp:123
Abstract class, defines interface for data splitting implementations (for use in data accessors).
Definition data_splitter.hpp:124
std::vector< PClassSplitter > csplit
one TClassSplitter per data class, indexed by class index; grown on demand by assign()/set_n()
Definition data_splitter.hpp:173
std::vector< PClassSplitter >::const_iterator ClassIterator
iterator over csplit, as passed to the per-class splitClassFirst()/splitClassNext() hooks
Definition data_splitter.hpp:174
Defines interface for data splitting implementations (for use in data accessors).
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
@ CLONE_DEEP
new Derived(*this), then set_cloned()
Definition clonable.hpp:149
@ CLONE_INHERIT
do not add behavior; defer to the Base class body (typically a family-level throwing default)
Definition clonable.hpp:147
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.