|
PLearn 0.1
|
#include <TrainTestSplitter.h>


Public Member Functions | |
| TrainTestSplitter (real the_test_fraction=0.0) | |
| TrainTestSplitter (int the_test_fraction) | |
| virtual void | build () |
| Post-constructor. | |
| virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
| Transforms a shallow copy into a deep copy. | |
| virtual string | classname () const |
| virtual OptionList & | getOptionList () const |
| virtual OptionMap & | getOptionMap () const |
| virtual RemoteMethodMap & | getRemoteMethodMap () const |
| virtual TrainTestSplitter * | deepCopy (CopiesMap &copies) const |
| virtual int | nsplits () const |
| Returns the number of available different "splits". | |
| virtual int | nSetsPerSplit () const |
| Returns the number of sets per split. | |
| virtual TVec< VMat > | getSplit (int i=0) |
| Returns split number i. | |
Static Public Member Functions | |
| static string | _classname_ () |
| Declares various object methods. | |
| static OptionList & | _getOptionList_ () |
| static RemoteMethodMap & | _getRemoteMethodMap_ () |
| static Object * | _new_instance_for_typemap_ () |
| static bool | _isa_ (const Object *o) |
| static void | _static_initialize_ () |
| static const PPath & | declaringFile () |
Public Attributes | |
| bool | append_train |
| real | test_fraction |
| bool | calc_with_pct |
| int | test_fraction_abs |
| int32_t | shuffle_seed |
Static Public Attributes | |
| static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
| void | getRandomSubsets (int train_length, int test_length) |
Static Protected Member Functions | |
| static void | declareOptions (OptionList &ol) |
| Declares this class' options. | |
Protected Attributes | |
| TVec< int > | train_indices |
| TVec< int > | test_indices |
Private Types | |
| typedef Splitter | inherited |
Private Member Functions | |
| void | build_ () |
| This does the actual building. | |
Definition at line 50 of file TrainTestSplitter.h.
typedef Splitter PLearn::TrainTestSplitter::inherited [private] |
Reimplemented from PLearn::Splitter.
Definition at line 52 of file TrainTestSplitter.h.
| PLearn::TrainTestSplitter::TrainTestSplitter | ( | real | the_test_fraction = 0.0 | ) |
Definition at line 55 of file TrainTestSplitter.cc.
: append_train(false), test_fraction(the_test_fraction), calc_with_pct(true), test_fraction_abs(0), shuffle_seed(-1) { }
| PLearn::TrainTestSplitter::TrainTestSplitter | ( | int | the_test_fraction | ) |
Definition at line 63 of file TrainTestSplitter.cc.
: append_train(false), test_fraction(0.0), calc_with_pct(false), test_fraction_abs(the_test_fraction_abs), shuffle_seed(-1) { }
| string PLearn::TrainTestSplitter::_classname_ | ( | ) | [static] |
Declares various object methods.
Reimplemented from PLearn::Splitter.
Definition at line 77 of file TrainTestSplitter.cc.
| OptionList & PLearn::TrainTestSplitter::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::Splitter.
Definition at line 77 of file TrainTestSplitter.cc.
| RemoteMethodMap & PLearn::TrainTestSplitter::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::Splitter.
Definition at line 77 of file TrainTestSplitter.cc.
Reimplemented from PLearn::Splitter.
Definition at line 77 of file TrainTestSplitter.cc.
| Object * PLearn::TrainTestSplitter::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 77 of file TrainTestSplitter.cc.
| StaticInitializer TrainTestSplitter::_static_initializer_ & PLearn::TrainTestSplitter::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::Splitter.
Definition at line 77 of file TrainTestSplitter.cc.
| void PLearn::TrainTestSplitter::build | ( | ) | [virtual] |
Post-constructor.
The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.
Reimplemented from PLearn::Object.
Definition at line 116 of file TrainTestSplitter.cc.
References PLearn::Object::build(), and build_().
{
inherited::build();
build_();
}

| void PLearn::TrainTestSplitter::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::Object.
Definition at line 107 of file TrainTestSplitter.cc.
References calc_with_pct, PLERROR, and test_fraction.
Referenced by build().
{
if(calc_with_pct && (test_fraction < 0.0 || test_fraction > 1.0))
PLERROR("TrainTestSplitter: test_fraction must be between 0 and 1; "
"%f is not a valid value.", test_fraction);
}

| string PLearn::TrainTestSplitter::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 77 of file TrainTestSplitter.cc.
| void PLearn::TrainTestSplitter::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::Splitter.
Definition at line 79 of file TrainTestSplitter.cc.
References append_train, PLearn::OptionBase::buildoption, calc_with_pct, PLearn::declareOption(), PLearn::Splitter::declareOptions(), shuffle_seed, test_fraction, and test_fraction_abs.
{
declareOption(
ol, "append_train", &TrainTestSplitter::append_train, OptionBase::buildoption,
"if set to 1, the trainset will be appended after the test set "
"(thus each split will contain three sets)");
declareOption(
ol, "calc_with_pct", &TrainTestSplitter::calc_with_pct, OptionBase::buildoption,
"Boolean value : if it's true it will compute the examples in the test "
"set with the test_fraction value");
declareOption(
ol, "test_fraction", &TrainTestSplitter::test_fraction, OptionBase::buildoption,
"the fraction of the dataset reserved to the test set");
declareOption(
ol, "test_fraction_abs", &TrainTestSplitter::test_fraction_abs, OptionBase::buildoption,
"the number of example of the dataset reserved to the test set");
declareOption(
ol, "shuffle_seed", &TrainTestSplitter::shuffle_seed, OptionBase::buildoption,
"if seed is >0, the vmat should be shuffled before being split (using this seed)\n"
"NOTE: the records in each subset remain in the original order");
inherited::declareOptions(ol);
}

| static const PPath& PLearn::TrainTestSplitter::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::Splitter.
Definition at line 108 of file TrainTestSplitter.h.
| TrainTestSplitter * PLearn::TrainTestSplitter::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::Splitter.
Definition at line 77 of file TrainTestSplitter.cc.
| OptionList & PLearn::TrainTestSplitter::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 77 of file TrainTestSplitter.cc.
| OptionMap & PLearn::TrainTestSplitter::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 77 of file TrainTestSplitter.cc.
Definition at line 177 of file TrainTestSplitter.cc.
References PLearn::endl(), i, n, shuffle_seed, PLearn::PRandom::shuffleElements(), PLearn::sortElements(), PLearn::TVec< T >::subVec(), test_indices, and train_indices.
Referenced by getSplit().
{
int n= train_length+test_length;
TVec<int> v(n);
for(int i= 0; i < n; ++i)
v[i]= i;
PRandom(shuffle_seed).shuffleElements(v);
train_indices= v.subVec(0, train_length);
test_indices= v.subVec(train_length, test_length);
sortElements(train_indices);
sortElements(test_indices);
MODULE_LOG
<< "Shuffling train-test elements yields:\n"
<< "Train indices: " << train_indices << '\n'
<< "Test indices: " << test_indices
<< endl;
}


| RemoteMethodMap & PLearn::TrainTestSplitter::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 77 of file TrainTestSplitter.cc.
Returns split number i.
Implements PLearn::Splitter.
Definition at line 135 of file TrainTestSplitter.cc.
References append_train, calc_with_pct, PLearn::Splitter::dataset, getRandomSubsets(), PLearn::VMat::length(), PLERROR, PLearn::TVec< T >::resize(), shuffle_seed, PLearn::TVec< T >::size(), PLearn::VMat::subMatRows(), test_fraction, test_fraction_abs, test_indices, and train_indices.
{
if (k)
PLERROR("TrainTestSplitter::getSplit() - k cannot be greater than 0");
TVec<VMat> split_(2);
int l = dataset->length();
int test_length = calc_with_pct ? int(test_fraction*l) : test_fraction_abs;
int train_length = l - test_length;
// Generate the shuffled elements if required, but don't do it more than
// once (would be wasteful for a splitter used inside an hyperoptimizer,
// for instance)
if(0 < shuffle_seed && (train_indices.size() == 0 || test_indices.size() == 0))
getRandomSubsets(train_length, test_length);
if(train_length == l)
split_[0] = dataset;//to get the right metadatadir when its the same matrix
else
split_[0] = ( 0<shuffle_seed?
new SelectRowsVMatrix(dataset, train_indices)
: dataset.subMatRows(0, train_length) );
if(test_length == l)
split_[1] = dataset;//to get the right metadatadir when its the same matrix
else
split_[1] = ( 0<shuffle_seed?
new SelectRowsVMatrix(dataset, test_indices)
: dataset.subMatRows(train_length, test_length) );
if (append_train) {
split_.resize(3);
split_[2] = split_[0];
}
return split_;
}

| void PLearn::TrainTestSplitter::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::Splitter.
Definition at line 202 of file TrainTestSplitter.cc.
References PLearn::deepCopyField(), PLearn::Splitter::makeDeepCopyFromShallowCopy(), test_indices, and train_indices.
{
inherited::makeDeepCopyFromShallowCopy(copies);
deepCopyField(train_indices, copies);
deepCopyField(test_indices, copies);
}

| int PLearn::TrainTestSplitter::nSetsPerSplit | ( | ) | const [virtual] |
Returns the number of sets per split.
Implements PLearn::Splitter.
Definition at line 127 of file TrainTestSplitter.cc.
References append_train.
{
if (append_train)
return 3;
else
return 2;
}
| int PLearn::TrainTestSplitter::nsplits | ( | ) | const [virtual] |
Returns the number of available different "splits".
Implements PLearn::Splitter.
Definition at line 122 of file TrainTestSplitter.cc.
{
return 1; // only one split
}
Reimplemented from PLearn::Splitter.
Definition at line 108 of file TrainTestSplitter.h.
Definition at line 69 of file TrainTestSplitter.h.
Referenced by declareOptions(), getSplit(), and nSetsPerSplit().
Definition at line 71 of file TrainTestSplitter.h.
Referenced by build_(), declareOptions(), and getSplit().
Definition at line 73 of file TrainTestSplitter.h.
Referenced by declareOptions(), getRandomSubsets(), and getSplit().
Definition at line 70 of file TrainTestSplitter.h.
Referenced by build_(), declareOptions(), and getSplit().
Definition at line 72 of file TrainTestSplitter.h.
Referenced by declareOptions(), and getSplit().
TVec<int> PLearn::TrainTestSplitter::test_indices [protected] |
Definition at line 61 of file TrainTestSplitter.h.
Referenced by getRandomSubsets(), getSplit(), and makeDeepCopyFromShallowCopy().
TVec<int> PLearn::TrainTestSplitter::train_indices [protected] |
Definition at line 61 of file TrainTestSplitter.h.
Referenced by getRandomSubsets(), getSplit(), and makeDeepCopyFromShallowCopy().
1.7.4