STK++ 0.9.13
STK::CvHandler Class Reference

CvHanler is an utility function for building the submatrix/subvectors needed when using k-folds cross-validation. More...

#include <STK_CvHandler.h>

Inheritance diagram for STK::CvHandler:
Inheritance graph

Public Member Functions

 CvHandler (Range const &rangeData, int nbFolds)
 Default constructor.
 
virtual ~CvHandler ()
 destructor
 
int nbFolds () const
 
Range constrangeData () const
 
CVectorXi constpartitions () const
 
CVectorXi constsizePartitions () const
 
virtual bool run ()
 run the computations.
 
void setData (Range const &rangeData, int nbFolds)
 
template<class Data >
bool getKFold (int k, Data const &x, Data &xFold, Data &xTest)
 get the data set when setting out fold k and test data set

 
template<class xData , class yData >
bool getKFold (int k, xData const &x, xData &xFold, xData &xTest, yData const &y, yData &yFold, yData &yTest)
 get the data set when setting out fold k and test data set

 
- Public Member Functions inherited from STK::IRunnerBase
String consterror () const
 get the last error message.
 

Protected Member Functions

void partition ()
 create a random partition in k folds
 
- Protected Member Functions inherited from STK::IRunnerBase
 IRunnerBase ()
 default constructor
 
 IRunnerBase (IRunnerBase const &runner)
 copy constructor
 
virtual ~IRunnerBase ()
 destructor
 
virtual void update ()
 update the runner.
 

Private Attributes

Range rangeData_
 Range of the data set (number of rows)
 
int nbFolds_
 Number of folds.
 
CVectorXi partitions_
 repartition of the sample into k-folds
 
CVectorXi sizePartitions_
 size of each fold
 

Additional Inherited Members

- Protected Attributes inherited from STK::IRunnerBase
String msg_error_
 String with the last error message.
 
bool hasRun_
 true if run has been used, false otherwise
 

Detailed Description

CvHanler is an utility function for building the submatrix/subvectors needed when using k-folds cross-validation.

Definition at line 52 of file STK_CvHandler.h.

Constructor & Destructor Documentation

◆ CvHandler()

STK::CvHandler::CvHandler ( Range const rangeData,
int  nbFolds 
)
inline

Default constructor.

Parameters
rangeData,nbFoldsrange of the data and number of folds

Definition at line 109 of file STK_CvHandler.h.

110 : IRunnerBase()
113{
114 // check nbFolds parameter
115 if (nbFolds_<1)
119}
#define STKRUNTIME_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:129
CVectorXi partitions_
repartition of the sample into k-folds
Range rangeData_
Range of the data set (number of rows)
CVectorXi sizePartitions_
size of each fold
int nbFolds_
Number of folds.
CvHandler(Range const &rangeData, int nbFolds)
Default constructor.
Range const & rangeData() const
int nbFolds() const
IRunnerBase()
default constructor
Definition STK_IRunner.h:68
int size() const
get the size of the TRange (the number of elements).
Definition STK_Range.h:303

References CvHandler(), nbFolds(), nbFolds_, rangeData_, STK::TRange< UnknownSize >::size(), and STKRUNTIME_ERROR_1ARG.

Referenced by CvHandler().

◆ ~CvHandler()

virtual STK::CvHandler::~CvHandler ( )
inlinevirtual

destructor

Definition at line 60 of file STK_CvHandler.h.

60{}

Member Function Documentation

◆ getKFold() [1/2]

template<class Data >
bool STK::CvHandler::getKFold ( int  k,
Data const x,
Data &  xFold,
Data &  xTest 
)

get the data set when setting out fold k and test data set

Definition at line 122 of file STK_CvHandler.h.

123{
124 // check if partitions are determined
125 if (!hasRun_)
127 return false;
128 }
129 // check dimensions
130 if (x.rows() != rangeData_)
132 return false;
133 }
134 if (sizePartitions_.begin() > k)
136 return false;
137 }
138 if (sizePartitions_.end() <= k)
140 return false;
141 }
142 // prepare containers
143 Range xFoldRows = x.rows();
144 xFoldRows.decLast(sizePartitions_[k]);
145 xFold.resize(xFoldRows, x.cols());
146 xTest.resize(sizePartitions_[k], x.cols());
147 // copy data
148 int iFoldRow = xFold.beginRows(), iTestRow = xTest.beginRows();
149 for (int i = partitions_.begin(); i < partitions_.end(); ++i)
150 {
151 if (partitions_[i] == k)
152 {
153 xTest.row(iTestRow) = x.row(i);
154 ++iTestRow;
155 }
156 else
157 {
158 xFold.row(iFoldRow) = x.row(i);
159 ++iFoldRow;
160 }
161 }
162 return true;
163}
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
#define STKERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:61
bool getKFold(int k, Data const &x, Data &xFold, Data &xTest)
get the data set when setting out fold k and test data set
virtual bool run()
run the computations.
String msg_error_
String with the last error message.
Definition STK_IRunner.h:96
bool hasRun_
true if run has been used, false otherwise
Definition STK_IRunner.h:98
TRange & decLast(int dec=1)
create the TRange [begin_, end_-dec)
Definition STK_Range.h:353
TRange< UnknownSize > Range
Definition STK_Range.h:59

References getKFold(), STK::IRunnerBase::hasRun_, STK::IRunnerBase::msg_error_, partitions_, rangeData_, run(), sizePartitions_, STKERROR_1ARG, and STKERROR_NO_ARG.

Referenced by getKFold(), and getKFold().

◆ getKFold() [2/2]

template<class xData , class yData >
bool STK::CvHandler::getKFold ( int  k,
xData const x,
xData xFold,
xData xTest,
yData const y,
yData yFold,
yData yTest 
)

get the data set when setting out fold k and test data set

Definition at line 166 of file STK_CvHandler.h.

168{
169 // check if partitions are determined
170 if (!hasRun_)
172 return false;
173 }
174 // check dimensions
175 if (x.rows() != rangeData_)
177 return false;
178 }
179 if (y.rows() != rangeData_)
181 return false;
182 }
183 if (sizePartitions_.begin() > k)
185 return false;
186 }
187 if (sizePartitions_.end() <= k)
189 return false;
190 }
191 // prepare constainers
192 Range xFoldRows = x.rows();
193 xFoldRows.decLast(sizePartitions_[k]);
194 xFold.resize(xFoldRows, x.cols());
195 xTest.resize(sizePartitions_[k], x.cols());
196 yFold.resize(xFoldRows, y.cols());
197 yTest.resize(sizePartitions_[k], y.cols());
198 // copy data
199 int iFoldRow = xFold.beginRows(), iTestRow = xTest.beginRows();
200 for (int i = partitions_.begin(); i < partitions_.end(); ++i)
201 {
202 if (partitions_[i] == k)
203 {
204 xTest.row(iTestRow) = x.row(i);
205 yTest.row(iTestRow) = y.row(i);
206 ++iTestRow;
207 }
208 else
209 {
210 xFold.row(iFoldRow) = x.row(i);
211 yFold.row(iFoldRow) = y.row(i);
212 ++iFoldRow;
213 }
214 }
215 return true;
216}

References getKFold(), STK::IRunnerBase::hasRun_, STK::IRunnerBase::msg_error_, partitions_, rangeData_, run(), sizePartitions_, STKERROR_1ARG, and STKERROR_NO_ARG.

◆ nbFolds()

int STK::CvHandler::nbFolds ( ) const
inline
Returns
the number of folds

Definition at line 63 of file STK_CvHandler.h.

63{ return nbFolds_;}

References nbFolds_.

Referenced by CvHandler(), and setData().

◆ partition()

void STK::CvHandler::partition ( )
inlineprotected

create a random partition in k folds

Definition at line 219 of file STK_CvHandler.h.

220{
223 //fill the container with the index of folds
224 for(int i = partitions_.begin() ; i< partitions_.end() ;i++)
225 {
226 partitions_[i] = i%nbFolds_;
228 }
229 //make a random rearrangement
230 int begin = partitions_.begin();
231 for (int i=partitions_.end()-2; i>begin; --i)
232 { std::swap(partitions_[i], partitions_[Law::UniformDiscrete::rand(begin, i+1)]);}
233}
Derived & resize(Range const &I, Range const &J)
resize the Array.
virtual int rand() const
Generate a pseudo Uniform random variate.

References nbFolds_, partitions_, STK::Law::UniformDiscrete::rand(), rangeData_, STK::ICArray< Derived >::resize(), and sizePartitions_.

Referenced by run().

◆ partitions()

CVectorXi const & STK::CvHandler::partitions ( ) const
inline
Returns
the partitions

Definition at line 67 of file STK_CvHandler.h.

67{ return partitions_;}

References partitions_.

◆ rangeData()

Range const & STK::CvHandler::rangeData ( ) const
inline
Returns
the range of the data

Definition at line 65 of file STK_CvHandler.h.

65{ return rangeData_;}

References rangeData_.

Referenced by setData().

◆ run()

virtual bool STK::CvHandler::run ( )
inlinevirtual

run the computations.

Returns
true if no error occur during the running process, false otherwise

Implements STK::IRunnerBase.

Definition at line 71 of file STK_CvHandler.h.

72 { partition(); hasRun_ = true; return true;}
void partition()
create a random partition in k folds

References STK::IRunnerBase::hasRun_, and partition().

Referenced by getKFold(), and getKFold().

◆ setData()

void STK::CvHandler::setData ( Range const rangeData,
int  nbFolds 
)
inline

Definition at line 74 of file STK_CvHandler.h.

75 {
80 hasRun_ = false;
81 }
void clear()
clear all allocated memory .

References STK::ICArray< Derived >::clear(), STK::IRunnerBase::hasRun_, nbFolds(), nbFolds_, partitions_, rangeData(), rangeData_, and sizePartitions_.

◆ sizePartitions()

CVectorXi const & STK::CvHandler::sizePartitions ( ) const
inline
Returns
the size of the partitions

Definition at line 69 of file STK_CvHandler.h.

69{ return sizePartitions_;}

References sizePartitions_.

Member Data Documentation

◆ nbFolds_

int STK::CvHandler::nbFolds_
private

Number of folds.

Definition at line 98 of file STK_CvHandler.h.

Referenced by CvHandler(), nbFolds(), partition(), and setData().

◆ partitions_

CVectorXi STK::CvHandler::partitions_
private

repartition of the sample into k-folds

Definition at line 100 of file STK_CvHandler.h.

Referenced by getKFold(), getKFold(), partition(), partitions(), and setData().

◆ rangeData_

Range STK::CvHandler::rangeData_
private

Range of the data set (number of rows)

Definition at line 96 of file STK_CvHandler.h.

Referenced by CvHandler(), getKFold(), getKFold(), partition(), rangeData(), and setData().

◆ sizePartitions_

CVectorXi STK::CvHandler::sizePartitions_
private

size of each fold

Definition at line 102 of file STK_CvHandler.h.

Referenced by getKFold(), getKFold(), partition(), setData(), and sizePartitions().


The documentation for this class was generated from the following file: