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

#include <KFoldSplitter.h>

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

List of all members.

Public Member Functions

 KFoldSplitter (int k=5)
 Default constructor.
virtual void build ()
 Post-constructor.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual KFoldSplitterdeepCopy (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 name and deepCopy 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_non_constant_test
bool append_train
pair< real, realcross_range
bool include_test_in_train
bool balance_classes
int K

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declares this class' options.

Private Types

typedef Splitter inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Definition at line 50 of file KFoldSplitter.h.


Member Typedef Documentation

Reimplemented from PLearn::Splitter.

Definition at line 52 of file KFoldSplitter.h.


Constructor & Destructor Documentation

PLearn::KFoldSplitter::KFoldSplitter ( int  k = 5)

Default constructor.

Definition at line 56 of file KFoldSplitter.cc.

References cross_range.

    : append_non_constant_test(false),
      append_train(false),
      include_test_in_train(false),
      balance_classes(false),
      K(k)
{
    // Default cross-validation range is the whole dataset.
    cross_range.first   = 0;
    cross_range.second  = 1;
}

Member Function Documentation

string PLearn::KFoldSplitter::_classname_ ( ) [static]

Declares name and deepCopy methods.

Reimplemented from PLearn::Splitter.

Definition at line 77 of file KFoldSplitter.cc.

OptionList & PLearn::KFoldSplitter::_getOptionList_ ( ) [static]

Reimplemented from PLearn::Splitter.

Definition at line 77 of file KFoldSplitter.cc.

RemoteMethodMap & PLearn::KFoldSplitter::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::Splitter.

Definition at line 77 of file KFoldSplitter.cc.

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

Reimplemented from PLearn::Splitter.

Definition at line 77 of file KFoldSplitter.cc.

Object * PLearn::KFoldSplitter::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 77 of file KFoldSplitter.cc.

StaticInitializer KFoldSplitter::_static_initializer_ & PLearn::KFoldSplitter::_static_initialize_ ( ) [static]

Reimplemented from PLearn::Splitter.

Definition at line 77 of file KFoldSplitter.cc.

void PLearn::KFoldSplitter::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 133 of file KFoldSplitter.cc.

References PLearn::Object::build(), and build_().

Here is the call graph for this function:

void PLearn::KFoldSplitter::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::Object.

Definition at line 125 of file KFoldSplitter.cc.

References K, and PLASSERT.

Referenced by build().

{
    PLASSERT( K > 0 || K == -1 );
}

Here is the caller graph for this function:

string PLearn::KFoldSplitter::classname ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 77 of file KFoldSplitter.cc.

void PLearn::KFoldSplitter::declareOptions ( OptionList ol) [static, protected]

Declares this class' options.

Reimplemented from PLearn::Splitter.

Definition at line 82 of file KFoldSplitter.cc.

References PLearn::OptionBase::advanced_level, append_non_constant_test, append_train, balance_classes, PLearn::OptionBase::buildoption, cross_range, PLearn::declareOption(), PLearn::Splitter::declareOptions(), include_test_in_train, and K.

{
    declareOption(ol, "K", &KFoldSplitter::K, OptionBase::buildoption,
                  "Split dataset in K parts (you can use K = -1 to perform leave-one-out CV).");

    declareOption(ol, "append_train", &KFoldSplitter::append_train, OptionBase::buildoption,
                  "If set to 1, the trainset will be appended after in the returned sets.");

    declareOption(ol, "append_non_constant_test",
                  &KFoldSplitter::append_non_constant_test,
                  OptionBase::buildoption,
        "If true, the non-constant part of the test set will be appended\n"
        "in the returned sets. This mostly makes sense when 'cross_range'\n"
        "is not (0:1).",
        OptionBase::advanced_level);

    declareOption(ol, "include_test_in_train", &KFoldSplitter::include_test_in_train, OptionBase::buildoption,
                  "If set to 1, the test set will be included in the train set.");

    declareOption(ol, "cross_range", &KFoldSplitter::cross_range,
                  OptionBase::buildoption,
        "The range on which cross-validation is applied (similar to the\n"
        "FractionSplitter ranges).",
        OptionBase::advanced_level);

    declareOption(ol, "balance_classes", &KFoldSplitter::balance_classes,
                  OptionBase::buildoption,
        "Should we balance classes inside the splits to obtain the same\n"
        "class frequencies. This corresponds to concatenating the results\n"
        "of a K-Fold performed on the subsets of examples from each class.\n"
        "Note that it currently does not support leave-one-out, and that\n"
        "you might obtain strange results if K > number of samples in one\n"
        "class.\n"
        "Note also that for this option to work, you have to label your\n"
        "classes from 0 to (n_classes-1), and all classes must be present\n"
        "in the source VMat.");

    inherited::declareOptions(ol);
}

Here is the call graph for this function:

static const PPath& PLearn::KFoldSplitter::declaringFile ( ) [inline, static]

Reimplemented from PLearn::Splitter.

Definition at line 98 of file KFoldSplitter.h.

KFoldSplitter * PLearn::KFoldSplitter::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::Splitter.

Definition at line 77 of file KFoldSplitter.cc.

OptionList & PLearn::KFoldSplitter::getOptionList ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 77 of file KFoldSplitter.cc.

OptionMap & PLearn::KFoldSplitter::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 77 of file KFoldSplitter.cc.

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

Reimplemented from PLearn::Object.

Definition at line 77 of file KFoldSplitter.cc.

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

Returns split number i.

Implements PLearn::Splitter.

Definition at line 165 of file KFoldSplitter.cc.

References PLearn::TVec< T >::append(), append_non_constant_test, append_train, balance_classes, PLearn::TVec< T >::copy(), cross_range, PLearn::Splitter::dataset, PLearn::fast_exact_is_equal(), include_test_in_train, K, PLearn::VMat::length(), nsplits(), PLASSERT, PLASSERT_MSG, PLCHECK_MSG, PLERROR, PLWARNING, PLearn::split(), PLearn::tostring(), and PLearn::VMat::width().

{
    if (k >= nsplits())
        PLERROR("KFoldSplitter::getSplit() - k (%d) cannot be greater than "
                " the number of splits (%d)", k, nsplits());

    real start = cross_range.first;
    real end   = cross_range.second;
    int n_data = dataset->length();
    PLASSERT_MSG(start >= 0 && end >= 0 && end > start && start < n_data && end < n_data,
                 string("start=")+tostring(start)+", end="+tostring(end)+", n_data="+tostring(n_data));
    int i_start = 0;
    if (start > 0)
        i_start = start >= 1 ? int(round(start)) : int(round(n_data * start));
    int i_end = n_data;
    if (!fast_exact_is_equal(end, 1))
        i_end   = end   >= 1 ? int(round(end))   : int(round(n_data * end));
    // The cross validation will be done only on examples i_start, ..., i_end - 1.
    int n_cross_data = i_end - i_start;
    bool do_partial_cross = (n_cross_data != n_data);
    if (K > 0 && K > n_data)
        PLERROR("In KFoldSplitter::getSplit - The number of splits (%d) cannot"
                " be greater than the number of samples in the dataset (%d). "
                "If you want to perform leave-one-out cross-validation, please"
                "set K = -1", K, n_data);
    real test_fraction = K > 0 ? (n_cross_data/(real)K) : 0;
    if ((int)(test_fraction) < 1)
        test_fraction = 1; // leave-one-out cross-validation

    TVec<VMat> split_(2);
    VMat non_constant_test;
    if (do_partial_cross) {
        if (balance_classes)
            PLERROR("balance_classes not implemented with partial_cross");
        VMat sub_data = new SubVMatrix(dataset, i_start, 0, n_cross_data, dataset->width());
        split(sub_data, test_fraction, split_[0], split_[1], k, true);
        non_constant_test = split_[1];
        if (i_start > 0) {
            VMat constant_train = new SubVMatrix(dataset, 0, 0, i_start, dataset->width());
            split_[0] = new ConcatRowsVMatrix(constant_train, split_[0]);
        }
        if (i_end < n_data) {
            VMat constant_test = new SubVMatrix(dataset, i_end, 0, n_data - i_end, dataset->width());
            split_[1] = new ConcatRowsVMatrix(split_[1], constant_test);
        }
    }
    else {
        if (balance_classes)
        {
            PLASSERT( dataset->targetsize() > 0 );
            TVec<VMat> tmp_split_(2);
            int i_class = 0;
            if (test_fraction > 1.0)
                test_fraction /= n_cross_data;
            else
                PLERROR("In KFoldSplitter::getSplit - Leave-one-out not implemented");
            while (true) { // break point below
                VMat dataset_class = new ClassSubsetVMatrix(dataset, i_class);
                int length = dataset_class->length();
                if (length == 0 ) break;
                if (length < K)
                    PLWARNING("In KFoldSplitter::getSplit - There are less "
                            "samples from class %d (N = %d) than splits "
                            "(K = %d): you may get weird results",
                            i_class, length, K);
                split(dataset_class, test_fraction, tmp_split_[0], tmp_split_[1], k, true);
                if (i_class == 0) {
                    split_ = tmp_split_.copy();
                } else {
                    split_[0] = new ConcatRowsVMatrix(split_[0], tmp_split_[0]);
                    split_[1] = new ConcatRowsVMatrix(split_[1], tmp_split_[1]);
                }
                i_class++;
            }
        }
        else
            split(dataset, test_fraction, split_[0], split_[1], k, true);
        non_constant_test = split_[1];
    }
    if (include_test_in_train)
        split_[0] = new ConcatRowsVMatrix(split_[0], split_[1]);
    if (append_train)
        split_.append(split_[0]);
    if (append_non_constant_test) {
        PLCHECK_MSG(!balance_classes, "Not implemented");
        split_.append(non_constant_test);
    }
    return split_;
}

Here is the call graph for this function:

int PLearn::KFoldSplitter::nSetsPerSplit ( ) const [virtual]

Returns the number of sets per split.

Implements PLearn::Splitter.

Definition at line 152 of file KFoldSplitter.cc.

References append_non_constant_test, and append_train.

{
    int nsets = 2;
    if (append_train)
        nsets++;
    if (append_non_constant_test)
        nsets++;
    return nsets;
}
int PLearn::KFoldSplitter::nsplits ( ) const [virtual]

Returns the number of available different "splits".

Implements PLearn::Splitter.

Definition at line 142 of file KFoldSplitter.cc.

References PLearn::Splitter::dataset, K, and PLearn::VMat::length().

Referenced by getSplit().

{
    return K > 0 ? K
                 : dataset ? dataset->length()
                           : -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Reimplemented from PLearn::Splitter.

Definition at line 98 of file KFoldSplitter.h.

Definition at line 67 of file KFoldSplitter.h.

Referenced by declareOptions(), getSplit(), and nSetsPerSplit().

Definition at line 68 of file KFoldSplitter.h.

Referenced by declareOptions(), getSplit(), and nSetsPerSplit().

Definition at line 71 of file KFoldSplitter.h.

Referenced by declareOptions(), and getSplit().

Definition at line 69 of file KFoldSplitter.h.

Referenced by declareOptions(), getSplit(), and KFoldSplitter().

Definition at line 70 of file KFoldSplitter.h.

Referenced by declareOptions(), and getSplit().

Definition at line 72 of file KFoldSplitter.h.

Referenced by build_(), declareOptions(), getSplit(), and nsplits().


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