PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Types | Private Member Functions
PLearn::TrainTestSplitter Class Reference

#include <TrainTestSplitter.h>

Inheritance diagram for PLearn::TrainTestSplitter:
Inheritance graph
[legend]
Collaboration diagram for PLearn::TrainTestSplitter:
Collaboration graph
[legend]

List of all members.

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 OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual TrainTestSplitterdeepCopy (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< VMatgetSplit (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 PPathdeclaringFile ()

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< inttrain_indices
TVec< inttest_indices

Private Types

typedef Splitter inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Definition at line 50 of file TrainTestSplitter.h.


Member Typedef Documentation

Reimplemented from PLearn::Splitter.

Definition at line 52 of file TrainTestSplitter.h.


Constructor & Destructor Documentation

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)
{ }

Member Function Documentation

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.

bool PLearn::TrainTestSplitter::_isa_ ( const Object o) [static]

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_().

Here is the call graph for this function:

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);

}

Here is the caller graph for this function:

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);
}

Here is the call graph for this function:

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.

void PLearn::TrainTestSplitter::getRandomSubsets ( int  train_length,
int  test_length 
) [protected]

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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

RemoteMethodMap & PLearn::TrainTestSplitter::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 77 of file TrainTestSplitter.cc.

TVec< VMat > PLearn::TrainTestSplitter::getSplit ( int  i = 0) [virtual]

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_;
}

Here is the call graph for this function:

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.

Here is the call graph for this function:

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
}

Member Data Documentation

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().

Definition at line 61 of file TrainTestSplitter.h.

Referenced by getRandomSubsets(), getSplit(), and makeDeepCopyFromShallowCopy().

Definition at line 61 of file TrainTestSplitter.h.

Referenced by getRandomSubsets(), getSplit(), and makeDeepCopyFromShallowCopy().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines