1#ifndef FSTREGRESSORWEIGHTEDNEIGHBOURS_H
2#define FSTREGRESSORWEIGHTEDNEIGHBOURS_H
54#define M_PI 3.14159265358979323846
117template<
class RETURNTYPE,
typename DATATYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR,
class DISTANCE>
118class Regressor_Weighted_Neighbours :
public Clonable_Impl<Regressor_Weighted_Neighbours<RETURNTYPE,DATATYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR,DISTANCE>,Regressor<RETURNTYPE,DIMTYPE,SUBSET,DATAACCESSOR> > {
148 virtual std::ostream&
print(std::ostream& os)
const override;
162template<
class RETURNTYPE,
typename DATATYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR,
class DISTANCE>
163Regressor_Weighted_Neighbours<RETURNTYPE,DATATYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR,DISTANCE>::Regressor_Weighted_Neighbours(
const Regressor_Weighted_Neighbours& rwn) :
169 notify(
"Regressor_Weighted_Neighbours copy-constructor.");
176template<
class RETURNTYPE,
typename DATATYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR,
class DISTANCE>
190 PSubset dsub(
new SUBSET(*sub));
200 typename DATAACCESSOR::PPattern p0,p1;
201 IDXTYPE s0,i0, s1,i1;
202 DIMTYPE _feats=
_da_train->getNoOfFeatures();
206 IDXTYPE _dist_count = 0;
207 typename std::list<RETURNTYPE> _dists;
208 typename std::list<RETURNTYPE>::const_iterator diter;
211 for(DIMTYPE c_train0=0;c_train0<
_da_train->getNoOfClasses();c_train0++)
214 for(
const auto &blk0 :
_da_train->blocks(
TRAIN))
for(i0=0,p0=blk0.data,s0=blk0.size;i0<s0;i0++)
217 for(DIMTYPE c_train1=0;c_train1<
_da_train->getNoOfClasses();c_train1++)
220 for(
const auto &blk1 :
_da_train->blocks(
TRAIN))
for(i1=0,p1=blk1.data,s1=blk1.size;i1<s1;i1++)
223 dist=
_distance->distance(&p0[i0*_feats],&p1[i1*_feats]);
229 _dists.push_back(dist);
242 assert(_dist_count > 0);
243 assert(_dist_count == _dists.size());
246 for(diter = _dists.begin(); diter != _dists.end(); diter++) {
253template<
class RETURNTYPE,
typename DATATYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR,
class DISTANCE>
261 return exp( -(dist*dist)/(2.0*stddev*stddev) ) / (stddev * sqrt(2.0*
M_PI));
264template<
class RETURNTYPE,
typename DATATYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR,
class DISTANCE>
273 typename DATAACCESSOR::PPattern p2;
275 const DIMTYPE _feats=
_da_train->getNoOfFeatures();
279 RETURNTYPE predval = 0;
280 RETURNTYPE weightsum = 0;
285 for(DIMTYPE c_train=0;c_train<
_da_train->getNoOfClasses();c_train++)
288 for(
const auto &blk :
_da_train->blocks(
TRAIN))
for(i2=0,p2=blk.data,s2=blk.size;i2<s2;i2++)
297 dist=
_distance->distance(pattern,&p2[i2*_feats]);
300 predval += weight * depval;
307 assert(weightsum != 0);
308 depvar = predval / weightsum;
312template<
class RETURNTYPE,
typename DATATYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR,
class DISTANCE>
319 assert(da->getNoOfFeatures()==
_da_train->getNoOfFeatures());
321 typename DATAACCESSOR::PPattern p1;
324 RETURNTYPE errsum = 0;
325 DIMTYPE _feats=da->getNoOfFeatures();
330 for(DIMTYPE c_test=0;c_test<da->getNoOfClasses();c_test++)
332 da->setClass(c_test);
333 for(
const auto &blk : da->blocks(
TEST)) { p1=blk.data; s1=blk.size;
346 if(!
predict(predval, &p1[i1*_feats]))
return false;
349 if( std::abs( depval - predval ) < 0.5 ) errsum += 1;
351 errsum += - std::abs( depval - predval );
355 da->setClass(c_test);
358 if(count==0)
return false;
359 result = (RETURNTYPE)errsum/(RETURNTYPE)count;
363template<
class RETURNTYPE,
typename DATATYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR,
class DISTANCE>
374 typename DATAACCESSOR::PPattern p1;
376 DIMTYPE _feats=da->getNoOfFeatures();
378 std::vector<std::pair<RETURNTYPE,RETURNTYPE>> pairs;
380 for(DIMTYPE c_test=0;c_test<da->getNoOfClasses();c_test++)
382 da->setClass(c_test);
383 for(
const auto &blk : da->blocks(
TEST)) { p1=blk.data; s1=blk.size;
389 if(!
predict(predval, &p1[i1*_feats]))
return false;
390 pairs.push_back(std::make_pair(depval,predval));
392 da->setClass(c_test);
398template<
class RETURNTYPE,
typename DATATYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR,
class DISTANCE>
401 os <<
"Regressor_Weighted_Neighbours()";
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Implements experimental regressor that produces weighted average of the predicted variable over neigh...
Definition regressor_weighted_neighbours.hpp:118
virtual bool test(RETURNTYPE &result, const PDataAccessor da) override
Definition regressor_weighted_neighbours.hpp:313
RETURNTYPE _kernel_multiplier
Definition regressor_weighted_neighbours.hpp:158
Regressor< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > parent
Definition regressor_weighted_neighbours.hpp:120
RETURNTYPE get_kernel_size() const
the effective kernel width (training-set pairwise-distance stddev times the multiplier set by set_ker...
Definition regressor_weighted_neighbours.hpp:129
void set_dependent_index(const IDXTYPE depvaridx)
data column to predict; the (IDXTYPE)-1 default/sentinel selects the class label instead ("regression...
Definition regressor_weighted_neighbours.hpp:130
void set_kernel_multiplier(const RETURNTYPE m)
scales the Gaussian kernel width used by get_weight(); width = distance stddev * m (must be > 0)
Definition regressor_weighted_neighbours.hpp:128
DATAACCESSOR::PPattern PPattern
Definition regressor_weighted_neighbours.hpp:124
DIMTYPE _dep_var_idx
Definition regressor_weighted_neighbours.hpp:154
Clonable_Impl< Regressor_Weighted_Neighbours, parent > parent_impl
Definition regressor_weighted_neighbours.hpp:121
PDataAccessor _da_train
Definition regressor_weighted_neighbours.hpp:159
std::shared_ptr< DATAACCESSOR > PDataAccessor
Definition regressor_weighted_neighbours.hpp:122
std::unique_ptr< DIST_L2 > _distance
Definition regressor_weighted_neighbours.hpp:155
virtual bool train(const PDataAccessor da, const PSubset sub) override
Definition regressor_weighted_neighbours.hpp:177
RETURNTYPE _dist_mean
Definition regressor_weighted_neighbours.hpp:156
virtual bool predict(RETURNTYPE &depvar, const PPattern &pattern) override
Predicts the dependent variable of one pattern into depvar (see the dependent-variable convention in ...
Definition regressor_weighted_neighbours.hpp:265
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition regressor_weighted_neighbours.hpp:399
RETURNTYPE _dist_stddev
Definition regressor_weighted_neighbours.hpp:157
virtual bool test_metric(RETURNTYPE &result, const PDataAccessor da, const Metric_Spec< RETURNTYPE > &spec) override
Definition regressor_weighted_neighbours.hpp:364
virtual RETURNTYPE get_weight(RETURNTYPE dist)
the (unnormalized) Gaussian kernel weight for a neighbour at distance dist, using get_kernel_size() a...
Definition regressor_weighted_neighbours.hpp:254
Regressor_Weighted_Neighbours(const Regressor_Weighted_Neighbours &cknn)
copy-constructor (clones _distance if allocated; scalar members are value-copied)
Definition regressor_weighted_neighbours.hpp:163
std::shared_ptr< SUBSET > PSubset
Definition regressor_weighted_neighbours.hpp:123
abstract class, defines interface for Regressor implementations (mainly to be used in wrappers)
Definition regressor.hpp:102
#define M_PI
value of pi; POSIX <cmath> extension, provided here as a fallback since strict C++17 does not guarant...
Definition classifier_normal_bayes.hpp:55
FST4 specific error passing class.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
@ TEST
the split's test part
Definition data_accessor.hpp:86
@ TRAIN
the split's training part
Definition data_accessor.hpp:85
std::unique_ptr< T > clone_unique(const T &src)
Definition clonable.hpp:234
bool metric_from_predictions(RETURNTYPE &result, const Wrapper_Metric metric, const std::vector< std::pair< RETURNTYPE, RETURNTYPE > > &pairs)
Definition wrapper_metrics.hpp:245
Defines interface for regressor implementations.
configuration of the wrapper metric; immutable during search, copied by criterion clones (thread-safe...
Definition wrapper_metrics.hpp:73
bool is_regression_metric() const
true for the three regression metrics (r2/neg_rmse/neg_mae), false for the classification metrics
Definition wrapper_metrics.hpp:85
Wrapper_Metric metric
which metric to compute; accuracy = the default path served by Classifier::test()
Definition wrapper_metrics.hpp:74
bool is_default() const
true iff this spec is the plain unweighted accuracy path (f1_positive is irrelevant unless metric==f1...
Definition wrapper_metrics.hpp:84