1 #ifndef FAIF_VALIDATOR_H_ 2 #define FAIF_VALIDATOR_H_ 9 #include <boost/lambda/lambda.hpp> 11 #include <boost/noncopyable.hpp> 12 #include <boost/scoped_ptr.hpp> 14 #include "Classifier.hpp" 15 #include "../utils/Random.hpp" 22 template<
typename Val>
23 struct CheckExampleFunctor {
25 CheckExampleFunctor(Classifier<Val>& c) : cl_(c)
29 return cl_.getCategory(example) == example.getFeature();
32 CheckExampleFunctor& operator=(
const CheckExampleFunctor&);
43 template<
typename Val>
45 CheckExampleFunctor<Val> checkFunctor(classifier);
46 return static_cast<int>( std::count_if( test.begin(), test.end(), checkFunctor ) );
52 template<
typename Val>
58 typename std::vector<const typename Classifier<Val>::ExampleTrain* >::iterator it = tcollect.begin();
59 for(
int idx=0; it != tcollect.end(); ++it, ++idx ) {
61 train.push_back( **it );
62 else if( idx < end_idx )
63 test.push_back( **it );
65 train.push_back( **it );
69 classifier.
train(train);
76 struct ShuffleFunctor {
80 int operator()(
int){
return r_(); }
82 ShuffleFunctor& operator=(
const ShuffleFunctor&);
99 template<
typename Val>
104 int n = (int)examples.size();
109 typedef std::vector<const ExampleTrain*> TrainCollection;
110 TrainCollection tcollect( n );
111 std::transform( examples.begin(), examples.end(), tcollect.begin(), & boost::lambda::_1 );
113 ShuffleFunctor shuffleFunctor( gen );
114 std::random_shuffle( tcollect.begin(), tcollect.end(), shuffleFunctor );
121 for(
int i = 0; i < k; i++ ) {
123 end_idx = (n * (i + 1) )/k;
125 num_proper += testRange<Val>(tcollect, start_idx, end_idx, classifier );
128 int num_all =
static_cast<int>( tcollect.size() );
129 return num_proper /(double)num_all ;
135 #endif //FAIF_VALIDATOR_H_ PointAndFeature< Val, AttrIdd, InitValueId > ExampleTrain
the train example (test example and the category)
Definition: Classifier.hpp:78
virtual void train(const ExamplesTrain &)=0
learn classifier (on the collection of training examples)
int checkClassifier(const typename Classifier< Val >::ExamplesTrain &test, Classifier< Val > &classifier)
check the classifier
Definition: Validator.hpp:44
point and some feature
Definition: Point.hpp:58
inner class - examples train collection
Definition: Classifier.hpp:82
double checkCross(const typename Classifier< Val >::ExamplesTrain &examples, int k, Classifier< Val > &classifier)
Definition: Validator.hpp:100
the uniform distribution for int, in range <min,max>, uses RandomSingleton
Definition: Random.hpp:107
virtual void reset()=0
the clasiffier will have no knowledge
int testRange(std::vector< const typename Classifier< Val >::ExampleTrain * > &tcollect, int start_idx, int end_idx, Classifier< Val > &classifier)
Definition: Validator.hpp:53
the clasiffier interface
Definition: Classifier.hpp:43