PLearn 0.1
|
#include <NeighborhoodImputationVMatrix.h>
Public Member Functions | |
NeighborhoodImputationVMatrix () | |
virtual | ~NeighborhoodImputationVMatrix () |
virtual void | build () |
Simply calls inherited::build() then build_(). | |
virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be. | |
virtual void | getExample (int i, Vec &input, Vec &target, real &weight) |
Default version calls getSubRow based on inputsize_ targetsize_ weightsize_ But exotic subclasses may construct, input, target and weight however they please. | |
virtual real | get (int i, int j) const |
This method must be implemented in all subclasses. | |
virtual void | put (int i, int j, real value) |
This method must be implemented in all subclasses of writable matrices. | |
virtual void | getSubRow (int i, int j, Vec v) const |
It is suggested that this method be implemented in subclasses to speed up accesses (default version repeatedly calls get(i,j) which may have a significant overhead). | |
virtual void | putSubRow (int i, int j, Vec v) |
It is suggested that this method be implemented in subclasses of writable matrices to speed up accesses (default version repeatedly calls put(i,j,value) which may have a significant overhead) | |
virtual void | appendRow (Vec v) |
This method must be implemented for matrices that are allowed to grow. | |
virtual void | insertRow (int i, Vec v) |
This method must be implemented for matrices that are allowed to grow. | |
virtual void | getRow (int i, Vec v) const |
These methods do not usually need to be overridden in subclasses (default versions call getSubRow, which should do just fine) | |
virtual void | putRow (int i, Vec v) |
virtual void | getColumn (int i, Vec v) const |
Copies column i into v (which must have appropriate length equal to the VMat's length). | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual NeighborhoodImputationVMatrix * | deepCopy (CopiesMap &copies) const |
Static Public Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
static string | _classname_ () |
NeighborhoodImputationVMatrix. | |
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 | |
VMat | reference_index |
The set of pre-computed neighbors index. | |
VMat | reference_with_missing |
The reference set corresponding to the pre-computed index with missing values. | |
VMat | reference_with_covariance_preserved |
The reference set corresponding to the pre-computed index with the initial imputations. | |
int | number_of_neighbors |
This is usually called K, the number of neighbors to consider. | |
TVec< pair< string, int > > | imputation_spec |
A vector that give for each variable the number of neighbors to use If a variable is not in the spec, it will use number_of_neighbors. | |
int | count_missing_neighbors |
0: (default) We do not count a neighbour with a missing value in the number of neighbors 1: We count a neighbour with a missing value in the number of neighbors | |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Private Types | |
typedef ImputationVMatrix | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. | |
real | impute (int i, int j) const |
Private Attributes | |
int | src_length |
int | src_width |
int | ref_length |
Mat | ref_idx |
Mat | ref_mis |
Mat | ref_cov |
TVec< int > | nb_neighbors |
Definition at line 55 of file NeighborhoodImputationVMatrix.h.
typedef ImputationVMatrix PLearn::NeighborhoodImputationVMatrix::inherited [private] |
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 57 of file NeighborhoodImputationVMatrix.h.
PLearn::NeighborhoodImputationVMatrix::NeighborhoodImputationVMatrix | ( | ) |
Definition at line 57 of file NeighborhoodImputationVMatrix.cc.
: count_missing_neighbors(0) { }
PLearn::NeighborhoodImputationVMatrix::~NeighborhoodImputationVMatrix | ( | ) | [virtual] |
Definition at line 62 of file NeighborhoodImputationVMatrix.cc.
{ }
string PLearn::NeighborhoodImputationVMatrix::_classname_ | ( | ) | [static] |
NeighborhoodImputationVMatrix.
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
OptionList & PLearn::NeighborhoodImputationVMatrix::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
RemoteMethodMap & PLearn::NeighborhoodImputationVMatrix::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
Object * PLearn::NeighborhoodImputationVMatrix::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
StaticInitializer NeighborhoodImputationVMatrix::_static_initializer_ & PLearn::NeighborhoodImputationVMatrix::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
void PLearn::NeighborhoodImputationVMatrix::appendRow | ( | Vec | v | ) | [virtual] |
This method must be implemented for matrices that are allowed to grow.
Reimplemented from PLearn::VMatrix.
Definition at line 144 of file NeighborhoodImputationVMatrix.cc.
References PLERROR.
{ PLERROR("In NeighborhoodImputationVMatrix::appendRow not implemented"); }
void PLearn::NeighborhoodImputationVMatrix::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 93 of file NeighborhoodImputationVMatrix.cc.
References PLearn::ImputationVMatrix::build(), build_(), and PLearn::ImputationVMatrix::testResultantVMatrix().
{ inherited::build(); build_(); testResultantVMatrix(); }
void PLearn::NeighborhoodImputationVMatrix::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 173 of file NeighborhoodImputationVMatrix.cc.
References PLearn::VMatrix::declareFieldNames(), PLearn::VMatrix::fieldIndex(), PLearn::TVec< T >::fill(), imputation_spec, PLearn::VMatrix::inputsize_, PLearn::VMat::length(), PLearn::VMatrix::length_, nb_neighbors, number_of_neighbors, PLERROR, ref_cov, ref_idx, ref_length, ref_mis, reference_index, reference_with_covariance_preserved, reference_with_missing, PLearn::TVec< T >::resize(), PLearn::TMat< T >::resize(), PLearn::TVec< T >::size(), PLearn::ImputationVMatrix::source, src_length, src_width, PLearn::VMatrix::targetsize_, PLearn::VMat::toMat(), PLearn::VMatrix::weightsize_, PLearn::VMat::width(), and PLearn::VMatrix::width_.
Referenced by build().
{ if (!source) PLERROR("In NeighborhoodImputationVMatrix::source with missing set must be supplied"); if (!reference_index) PLERROR("In NeighborhoodImputationVMatrix::reference index set must be supplied"); if (!reference_with_missing) PLERROR("In NeighborhoodImputationVMatrix::reference with missing set must be supplied"); if (!reference_with_covariance_preserved) PLERROR("In NeighborhoodImputationVMatrix::reference with covariance preserved must be supplied"); src_length = source->length(); if (src_length != reference_index->length()) PLERROR("In NeighborhoodImputationVMatrix::length of the source and its index must agree, got: %i - %i", src_length, reference_index->length()); ref_length = reference_with_missing->length(); if (ref_length != reference_with_covariance_preserved->length()) PLERROR("In NeighborhoodImputationVMatrix::length of the reference set with missing and with covariance preserved must agree, got: %i - %i", ref_length, reference_with_covariance_preserved->length()); src_width = source->width(); if (src_width != reference_with_missing->width()) PLERROR("In NeighborhoodImputationVMatrix::width of the source and the reference with missing must agree, got: %i - %i", src_width, reference_with_missing->width()); if (src_width != reference_with_covariance_preserved->width()) PLERROR("In NeighborhoodImputationVMatrix::width of the source and the reference with covariance preserved must agree, got: %i - %i", src_width, reference_with_covariance_preserved->width()); if (number_of_neighbors < 1) PLERROR("In NeighborhoodImputationVMatrix::there must be at least 1 neighbors, got: %d",number_of_neighbors); if (number_of_neighbors > reference_index->width()) PLERROR("In NeighborhoodImputationVMatrix::the index must contains at least as many reference as the specified number of neighbors, got: %i - %i", number_of_neighbors, reference_index->width()); ref_idx.resize(reference_index->length(), reference_index->width()); ref_idx = reference_index->toMat(); ref_mis.resize(reference_with_missing->length(), reference_with_missing->width()); ref_mis = reference_with_missing->toMat(); ref_cov.resize(reference_with_covariance_preserved->length(), reference_with_covariance_preserved->width()); ref_cov = reference_with_covariance_preserved->toMat(); length_ = src_length; width_ = src_width; inputsize_ = source->inputsize(); targetsize_ = source->targetsize(); weightsize_ = source->weightsize(); declareFieldNames(source->fieldNames()); nb_neighbors.resize(source->inputsize()); nb_neighbors.fill(number_of_neighbors); for (int spec_col = 0; spec_col < imputation_spec.size(); spec_col++) { int source_col = fieldIndex(imputation_spec[spec_col].first); if (source_col < 0) PLERROR("In NeighborhoodImputationVMatrix::build_() no field with this name in source data set: %s", (imputation_spec[spec_col].first).c_str()); nb_neighbors[source_col] = imputation_spec[spec_col].second; } }
string PLearn::NeighborhoodImputationVMatrix::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
void PLearn::NeighborhoodImputationVMatrix::declareOptions | ( | OptionList & | ol | ) | [static] |
Declares this class' options.
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 66 of file NeighborhoodImputationVMatrix.cc.
References PLearn::OptionBase::buildoption, count_missing_neighbors, PLearn::declareOption(), PLearn::ImputationVMatrix::declareOptions(), imputation_spec, number_of_neighbors, reference_index, reference_with_covariance_preserved, and reference_with_missing.
{ declareOption(ol, "reference_index", &NeighborhoodImputationVMatrix::reference_index, OptionBase::buildoption, "The set of pre-computed neighbors index.\n" "This can be done with BallTreeNearestNeighbors.\n"); declareOption(ol, "reference_with_missing", &NeighborhoodImputationVMatrix::reference_with_missing, OptionBase::buildoption, "The reference set corresponding to the pre-computed index with missing values."); declareOption(ol, "reference_with_covariance_preserved", &NeighborhoodImputationVMatrix::reference_with_covariance_preserved, OptionBase::buildoption, "The reference set corresponding to the pre-computed index with the initial imputations."); declareOption(ol, "number_of_neighbors", &NeighborhoodImputationVMatrix::number_of_neighbors, OptionBase::buildoption, "This is usually called K, the number of neighbors to consider.\n" "It must be less or equal than the with of the reference index."); declareOption(ol, "count_missing_neighbors", &NeighborhoodImputationVMatrix::count_missing_neighbors, OptionBase::buildoption, "0: (default) We do not count a neighbour with a missing value in the number of neighbors.\n" "1: We count a neighbour with a missing value in the number of neighbors.\n"); declareOption(ol, "imputation_spec", &NeighborhoodImputationVMatrix::imputation_spec, OptionBase::buildoption, "A vector that give for each variable the number of neighbors to use.\n" "If a variable don't have a value, we use the value of the varialbe: number_of_neighbors.\n" " Ex: [ varname1 : nbneighbors1, varname2 : nbneighbors2 ]\n"); inherited::declareOptions(ol); }
static const PPath& PLearn::NeighborhoodImputationVMatrix::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 115 of file NeighborhoodImputationVMatrix.h.
NeighborhoodImputationVMatrix * PLearn::NeighborhoodImputationVMatrix::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
This method must be implemented in all subclasses.
Returns element (i,j).
Implements PLearn::VMatrix.
Definition at line 120 of file NeighborhoodImputationVMatrix.cc.
References impute(), PLearn::is_missing(), and PLearn::ImputationVMatrix::source.
{ real variable_value = source->get(i, j); if (!is_missing(variable_value)) return variable_value; return impute(i, j); }
Copies column i into v (which must have appropriate length equal to the VMat's length).
Reimplemented from PLearn::VMatrix.
Definition at line 166 of file NeighborhoodImputationVMatrix.cc.
References impute(), PLearn::is_missing(), PLearn::TVec< T >::length(), and PLearn::ImputationVMatrix::source.
{ source-> getColumn(i, v); for (int source_row = 0; source_row < v->length(); source_row++) if (is_missing(v[source_row])) v[source_row] = impute(source_row, i); }
void PLearn::NeighborhoodImputationVMatrix::getExample | ( | int | i, |
Vec & | input, | ||
Vec & | target, | ||
real & | weight | ||
) | [virtual] |
Default version calls getSubRow based on inputsize_ targetsize_ weightsize_ But exotic subclasses may construct, input, target and weight however they please.
If not a weighted matrix, weight should be set to default value 1.
Reimplemented from PLearn::VMatrix.
Definition at line 111 of file NeighborhoodImputationVMatrix.cc.
References PLearn::VMat::getExample(), impute(), PLearn::is_missing(), PLearn::TVec< T >::length(), and PLearn::ImputationVMatrix::source.
{ source->getExample(i, input, target, weight); for (int source_col = 0; source_col < input->length(); source_col++) { if (is_missing(input[source_col])) input[source_col] = impute(i, source_col); } }
OptionList & PLearn::NeighborhoodImputationVMatrix::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
OptionMap & PLearn::NeighborhoodImputationVMatrix::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
RemoteMethodMap & PLearn::NeighborhoodImputationVMatrix::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 55 of file NeighborhoodImputationVMatrix.cc.
These methods do not usually need to be overridden in subclasses (default versions call getSubRow, which should do just fine)
Copies row i into v (which must have appropriate length equal to the VMat's width).
Reimplemented from PLearn::VMatrix.
Definition at line 154 of file NeighborhoodImputationVMatrix.cc.
References impute(), PLearn::is_missing(), PLearn::TVec< T >::length(), and PLearn::ImputationVMatrix::source.
{ source-> getRow(i, v); for (int source_col = 0; source_col < v->length(); source_col++) if (is_missing(v[source_col])) v[source_col] = impute(i, source_col); }
It is suggested that this method be implemented in subclasses to speed up accesses (default version repeatedly calls get(i,j) which may have a significant overhead).
Fills v with the subrow i lying between columns j (inclusive) and j+v.length() (exclusive).
Reimplemented from PLearn::VMatrix.
Definition at line 132 of file NeighborhoodImputationVMatrix.cc.
References PLearn::VMat::getSubRow(), impute(), PLearn::is_missing(), PLearn::TVec< T >::length(), and PLearn::ImputationVMatrix::source.
{ source->getSubRow(i, j, v); for (int source_col = 0; source_col < v->length(); source_col++) if (is_missing(v[source_col])) v[source_col] = impute(i, source_col + j); }
Definition at line 222 of file NeighborhoodImputationVMatrix.cc.
References count_missing_neighbors, PLearn::is_missing(), j, nb_neighbors, number_of_neighbors, PLERROR, ref_cov, ref_idx, ref_length, ref_mis, and PLearn::TMat< T >::width().
Referenced by get(), getColumn(), getExample(), getRow(), and getSubRow().
{ int ref_row; real return_value = 0.0; int value_count = 0; int neighbors_count = 0; for (int ref_idx_col = 0; ref_idx_col < ref_idx.width() && neighbors_count < nb_neighbors[j]; ref_idx_col++) { ref_row = (int) ref_idx(i, ref_idx_col); if (ref_row < 0 || ref_row >= ref_length) PLERROR("In NeighborhoodImputationVMatrix::invalid index reference, got: %i for sample %i", ref_row, i); if (is_missing(ref_mis(ref_row, j))){ if(count_missing_neighbors) neighbors_count++; continue; } return_value += ref_mis(ref_row, j); value_count++; neighbors_count++; } if (value_count > 0) return return_value / value_count; //if all neighbors have missing value we use the inputed version //TODO put a warning return_value = 0.0; value_count = 0; for (int ref_idx_col = 0; ref_idx_col < number_of_neighbors; ref_idx_col++) { ref_row = (int) ref_idx(i, ref_idx_col); if (is_missing(ref_cov(ref_row, j))) PLERROR("In NeighborhoodImputationVMatrix::missing value found in the reference with covariance preserved at: %i , %i", ref_row, j); return_value += ref_cov(ref_row, j); value_count += 1; } return return_value / value_count; }
This method must be implemented for matrices that are allowed to grow.
Reimplemented from PLearn::VMatrix.
Definition at line 149 of file NeighborhoodImputationVMatrix.cc.
References PLERROR.
{ PLERROR("In NeighborhoodImputationVMatrix::insertRow not implemented"); }
void PLearn::NeighborhoodImputationVMatrix::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Does the necessary operations to transform a shallow copy (this) into a deep copy by deep-copying all the members that need to be.
This needs to be overridden by every class that adds "complex" data members to the class, such as Vec
, Mat
, PP<Something>
, etc. Typical implementation:
void CLASS_OF_THIS::makeDeepCopyFromShallowCopy(CopiesMap& copies) { inherited::makeDeepCopyFromShallowCopy(copies); deepCopyField(complex_data_member1, copies); deepCopyField(complex_data_member2, copies); ... }
copies | A map used by the deep-copy mechanism to keep track of already-copied objects. |
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 100 of file NeighborhoodImputationVMatrix.cc.
References count_missing_neighbors, PLearn::deepCopyField(), imputation_spec, PLearn::ImputationVMatrix::makeDeepCopyFromShallowCopy(), number_of_neighbors, reference_index, reference_with_covariance_preserved, and reference_with_missing.
{ deepCopyField(reference_index, copies); deepCopyField(reference_with_missing, copies); deepCopyField(reference_with_covariance_preserved, copies); deepCopyField(number_of_neighbors, copies); deepCopyField(count_missing_neighbors, copies); deepCopyField(imputation_spec, copies); inherited::makeDeepCopyFromShallowCopy(copies); }
This method must be implemented in all subclasses of writable matrices.
Sets element (i,j) to value.
Reimplemented from PLearn::VMatrix.
Definition at line 127 of file NeighborhoodImputationVMatrix.cc.
References PLERROR.
{ PLERROR("In NeighborhoodImputationVMatrix::put not implemented"); }
Reimplemented from PLearn::VMatrix.
Definition at line 161 of file NeighborhoodImputationVMatrix.cc.
References PLERROR.
{ PLERROR("In NeighborhoodImputationVMatrix::putRow not implemented"); }
It is suggested that this method be implemented in subclasses of writable matrices to speed up accesses (default version repeatedly calls put(i,j,value) which may have a significant overhead)
Reimplemented from PLearn::VMatrix.
Definition at line 139 of file NeighborhoodImputationVMatrix.cc.
References PLERROR.
{ PLERROR("In NeighborhoodImputationVMatrix::putSubRow not implemented"); }
Reimplemented from PLearn::ImputationVMatrix.
Definition at line 115 of file NeighborhoodImputationVMatrix.h.
0: (default) We do not count a neighbour with a missing value in the number of neighbors 1: We count a neighbour with a missing value in the number of neighbors
Definition at line 81 of file NeighborhoodImputationVMatrix.h.
Referenced by declareOptions(), impute(), and makeDeepCopyFromShallowCopy().
TVec< pair<string, int> > PLearn::NeighborhoodImputationVMatrix::imputation_spec |
A vector that give for each variable the number of neighbors to use If a variable is not in the spec, it will use number_of_neighbors.
Definition at line 77 of file NeighborhoodImputationVMatrix.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 110 of file NeighborhoodImputationVMatrix.h.
This is usually called K, the number of neighbors to consider.
It must be less or equal than the with of the reference index.
Definition at line 73 of file NeighborhoodImputationVMatrix.h.
Referenced by build_(), declareOptions(), impute(), and makeDeepCopyFromShallowCopy().
Definition at line 109 of file NeighborhoodImputationVMatrix.h.
Definition at line 107 of file NeighborhoodImputationVMatrix.h.
Definition at line 106 of file NeighborhoodImputationVMatrix.h.
Definition at line 108 of file NeighborhoodImputationVMatrix.h.
The set of pre-computed neighbors index.
This can be done with BallTreeNearestNeighbors.
Definition at line 63 of file NeighborhoodImputationVMatrix.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
The reference set corresponding to the pre-computed index with the initial imputations.
Definition at line 69 of file NeighborhoodImputationVMatrix.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
The reference set corresponding to the pre-computed index with missing values.
Definition at line 66 of file NeighborhoodImputationVMatrix.h.
Referenced by build_(), declareOptions(), and makeDeepCopyFromShallowCopy().
Definition at line 104 of file NeighborhoodImputationVMatrix.h.
Referenced by build_().
Definition at line 105 of file NeighborhoodImputationVMatrix.h.
Referenced by build_().