STK++ 0.9.13
STK::PartitionHandler Class Reference

CvHanler is an utility function for building the submatrix/subvectors needed when creating learning and test data sets. More...

#include <STK_PartitionHandler.h>

Inheritance diagram for STK::PartitionHandler:
Inheritance graph

Public Member Functions

 PartitionHandler (Range const &rangeData, Real prop)
 Default constructor.
 
virtual ~PartitionHandler ()
 destructor
 
Real constproportion () const
 
Range constrangeData () const
 
CVectorXi constpartitions () const
 
virtual bool run ()
 run the computations.
 
void setData (Range const &rangeData, Real prop)
 
template<class Data >
bool getPartitions (Data const &x, Data &xLearn, Data &xTest)
 get the data set when setting out fold k and test data set

 
template<class xData , class yData >
bool getPartitions (xData const &x, xData &xLearn, xData &xTest, yData const &y, yData &yLearn, 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
 
- 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)
 
Real prop_
 proportion
 
int sizeTest_
 size of the test (should

 
CVectorXi partitions_
 repartition of the sample into k-folds
 

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 creating learning and test data sets.

Definition at line 18 of file STK_PartitionHandler.h.

Constructor & Destructor Documentation

◆ PartitionHandler()

STK::PartitionHandler::PartitionHandler ( Range const rangeData,
Real  prop 
)
inline

Default constructor.

Parameters
rangeDataprop range of the data to partition
propto set in test data

Definition at line 74 of file STK_PartitionHandler.h.

75 : IRunnerBase()
76 , rangeData_(rangeData), prop_(prop)
78 , partitions_()
79{
80 // check proportion
81 if (prop_>1)
83 if (prop_<0)
85}
#define STKRUNTIME_ERROR_1ARG(Where, Arg, Error)
Definition STK_Macros.h:129
IRunnerBase()
default constructor
Definition STK_IRunner.h:68
PartitionHandler(Range const &rangeData, Real prop)
Default constructor.
CVectorXi partitions_
repartition of the sample into k-folds
Range rangeData_
Range of the data set (number of rows)
Range const & rangeData() const
int sizeTest_
size of the test (should
int size() const
get the size of the TRange (the number of elements).
Definition STK_Range.h:303

References PartitionHandler(), prop_, and STKRUNTIME_ERROR_1ARG.

Referenced by PartitionHandler().

◆ ~PartitionHandler()

virtual STK::PartitionHandler::~PartitionHandler ( )
inlinevirtual

destructor

Definition at line 27 of file STK_PartitionHandler.h.

27{}

Member Function Documentation

◆ getPartitions() [1/2]

template<class Data >
bool STK::PartitionHandler::getPartitions ( Data const x,
Data &  xLearn,
Data &  xTest 
)

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

Definition at line 88 of file STK_PartitionHandler.h.

89{
90 // check if partitions are determined
91 if (!hasRun_)
93 return false;
94 }
95 // check dimensions
96 if (x.rows() != rangeData_)
97 { msg_error_ = STKERROR_NO_ARG(PartitionHandler::getKLearn,x.rows()!=rangeData_);
98 return false;
99 }
100 // prepare containers
101 Range xLearnRows = x.rows();
102 xLearnRows.decLast(sizeTest_);
103 xLearn.resize(xLearnRows, x.cols());
104 xTest.resize(sizeTest_, x.cols());
105 // copy data
106 int iLearnRow = xLearn.beginRows(), iTestRow = xTest.beginRows();
107 for (int i = partitions_.begin(); i < partitions_.end(); ++i)
108 {
109 if (partitions_[i] == 1)
110 {
111 xTest.row(iTestRow) = x.row(i);
112 ++iTestRow;
113 }
114 else
115 {
116 xLearn.row(iLearnRow) = x.row(i);
117 ++iLearnRow;
118 }
119 }
120 return true;
121}
#define STKERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:49
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
virtual bool run()
run the computations.
bool getPartitions(Data const &x, Data &xLearn, Data &xTest)
get the data set when setting out fold k and test data set
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 getPartitions(), STK::IRunnerBase::hasRun_, STK::IRunnerBase::msg_error_, partitions_, rangeData_, run(), sizeTest_, and STKERROR_NO_ARG.

Referenced by getPartitions().

◆ getPartitions() [2/2]

template<class xData , class yData >
bool STK::PartitionHandler::getPartitions ( xData const x,
xData xLearn,
xData xTest,
yData const y,
yData yLearn,
yData yTest 
)

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

Definition at line 124 of file STK_PartitionHandler.h.

126{
127 // check if partitions are determined
128 if (!hasRun_)
129 { msg_error_ = STKERROR_NO_ARG(PartitionHandler::getKLearn,PartitionHandler has to run);
130 return false;
131 }
132 // check dimensions
133 if (x.rows() != rangeData_)
134 { msg_error_ = STKERROR_NO_ARG(PartitionHandler::getKLearn,x.rows()!=rangeData_);
135 return false;
136 }
137 if (y.rows() != rangeData_)
138 { msg_error_ = STKERROR_NO_ARG(PartitionHandler::getKLearn,y.rows()!=rangeData_);
139 return false;
140 }
141 // prepare constainers
142 Range xLearnRows = x.rows();
143 xLearnRows.decLast(sizeTest_);
144 xLearn.resize(xLearnRows, x.cols());
145 xTest.resize(sizeTest_, x.cols());
146 yLearn.resize(xLearnRows, y.cols());
147 yTest.resize(sizeTest_, y.cols());
148 // copy data
149 int iLearnRow = xLearn.beginRows(), iTestRow = xTest.beginRows();
150 for (int i = partitions_.begin(); i < partitions_.end(); ++i)
151 {
152 if (partitions_[i] == 1)
153 {
154 xTest.row(iTestRow) = x.row(i);
155 yTest.row(iTestRow) = y.row(i);
156 ++iTestRow;
157 }
158 else
159 {
160 xLearn.row(iLearnRow) = x.row(i);
161 yLearn.row(iLearnRow) = y.row(i);
162 ++iLearnRow;
163 }
164 }
165 return true;
166}

References STK::IRunnerBase::hasRun_, STK::IRunnerBase::msg_error_, partitions_, rangeData_, run(), sizeTest_, and STKERROR_NO_ARG.

◆ partition()

void STK::PartitionHandler::partition ( )
inlineprotected

create a random partition

Definition at line 169 of file STK_PartitionHandler.h.

170{
172 int endTest = partitions_.begin()+sizeTest_;
173 //fill the container with the index of the partition (1 test, 0 learn)
174 for(int i = partitions_.begin() ; i< endTest ;i++) { partitions_[i] = 1;}
175 for(int i = endTest ; i< partitions_.end() ;i++) { partitions_[i] = 0;}
176 //make a random rearrangement
177 int begin = partitions_.begin();
178 for (int i=partitions_.end()-2; i>begin; --i)
179 { std::swap(partitions_[i], partitions_[Law::UniformDiscrete::rand(begin, i+1)]);}
180}
Derived & resize(Range const &I, Range const &J)
resize the Array.
virtual int rand() const
Generate a pseudo Uniform random variate.

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

Referenced by run().

◆ partitions()

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

Definition at line 34 of file STK_PartitionHandler.h.

34{ return partitions_;}

References partitions_.

◆ proportion()

Real const & STK::PartitionHandler::proportion ( ) const
inline
Returns
the number of folds

Definition at line 30 of file STK_PartitionHandler.h.

30{ return prop_;}

References prop_.

◆ rangeData()

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

Definition at line 32 of file STK_PartitionHandler.h.

32{ return rangeData_;}

References rangeData_.

Referenced by setData().

◆ run()

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

run the computations.

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

Implements STK::IRunnerBase.

Definition at line 36 of file STK_PartitionHandler.h.

37 { partition(); hasRun_ = true; return true;}
void partition()
create a random partition

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

Referenced by getPartitions(), and getPartitions().

◆ setData()

void STK::PartitionHandler::setData ( Range const rangeData,
Real  prop 
)
inline

Member Data Documentation

◆ partitions_

CVectorXi STK::PartitionHandler::partitions_
private

repartition of the sample into k-folds

Definition at line 67 of file STK_PartitionHandler.h.

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

◆ prop_

Real STK::PartitionHandler::prop_
private

proportion

Definition at line 63 of file STK_PartitionHandler.h.

Referenced by PartitionHandler(), proportion(), and setData().

◆ rangeData_

Range STK::PartitionHandler::rangeData_
private

Range of the data set (number of rows)

Definition at line 61 of file STK_PartitionHandler.h.

Referenced by getPartitions(), getPartitions(), partition(), rangeData(), and setData().

◆ sizeTest_

int STK::PartitionHandler::sizeTest_
private

size of the test (should

Definition at line 65 of file STK_PartitionHandler.h.

Referenced by getPartitions(), getPartitions(), partition(), and setData().


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