3 #ifndef FAIF_K_NEAREST_NEIGHBOR_CLASSIFIER_HPP 4 #define FAIF_K_NEAREST_NEIGHBOR_CLASSIFIER_HPP 9 #include <boost/bind.hpp> 11 #include "Classifier.hpp" 13 #include <boost/serialization/split_member.hpp> 14 #include <boost/serialization/base_object.hpp> 15 #include <boost/serialization/nvp.hpp> 16 #include <boost/serialization/vector.hpp> 36 static double distance(
const ExampleTest& a,
const ExampleTest& b) {
37 double distance = 0.0;
38 typename ExampleTest::const_iterator i = a.begin(), j = b.begin();
39 for(; i != a.end() && j != b.end(); ++i, ++j) {
41 if(*i == AttrDomain::getUnknownId() || *j == AttrDomain::getUnknownId() )
47 for(; i != a.end(); ++i) { distance += 1.0; }
48 for(; j != b.end(); ++j) { distance += 1.0; }
58 template<
typename Val,
74 KNearestNeighbor(
const Domains& attr_domains,
const AttrDomain& category_domain);
83 virtual void train(
const ExamplesTrain& e);
86 virtual AttrIdd
getCategory(
const ExampleTest& e)
const {
return getCategoryK(e, defaultK_); }
89 AttrIdd getCategoryK(
const ExampleTest& e,
int K)
const;
96 virtual Beliefs
getCategories(
const ExampleTest& e)
const {
return getCategoriesK(e, defaultK_); }
102 Beliefs getCategoriesK(
const ExampleTest& e,
int K)
const;
105 virtual void write(std::ostream& os)
const;
118 ExamplesTrain memory_;
122 friend class boost::serialization::access;
124 template<
class Archive>
125 void serialize( Archive &ar,
const unsigned int ) {
126 ar & boost::serialization::make_nvp(
"KNNBase", boost::serialization::base_object<
Classifier<Val> >(*
this) );
127 ar & boost::serialization::make_nvp(
"memory", memory_ );
128 ar & boost::serialization::make_nvp(
"defaultK", defaultK_ );
137 template<
typename Val,
template <
typename>
class Distance>
141 template<
typename Val,
template <
typename>
class Distance>
143 :
Classifier<Val>(attr_domains, category_domain), memory_(), defaultK_(3)
147 template<
typename Val,
template <
typename>
class Distance>
153 template<
typename Val,
template <
typename>
class Distance>
159 template<
typename Val,
template <
typename>
class Distance>
160 typename KNearestNeighbor<Val, Distance>::AttrIdd
164 return AttrDomain::getUnknownId();
166 return bel.front().getValue();
170 template<
typename Val,
template <
typename>
class Distance>
171 typename KNearestNeighbor<Val, Distance>::Beliefs
174 typedef std::pair<typename ExamplesTrain::const_iterator, double> DistanceDescr;
175 typedef std::vector<DistanceDescr> DistanceDescrVec;
178 DistanceDescrVec distances;
179 distances.reserve( memory_.size() );
180 for(
typename ExamplesTrain::const_iterator ii = memory_.begin(); ii != memory_.end(); ++ii) {
182 distances.push_back( DistanceDescr(ii, Distance<Val>::distance( *ii, e ) ) );
184 typename DistanceDescrVec::iterator middle = distances.end();
185 if( distances.end() - distances.begin() > K) {
186 middle = distances.begin() + K;
188 std::partial_sort( distances.begin(), middle, distances.end(),
189 boost::bind( std::less<double>(), boost::bind(&DistanceDescr::second, _1), boost::bind(&DistanceDescr::second, _2) ) );
192 for(
typename DistanceDescrVec::const_iterator jj = distances.begin(); jj != middle; ++jj) {
193 const ExampleTrain& ex = *(jj->first);
196 return counters.getHistogram();
200 template<
typename Val,
template <
typename>
class Distance>
202 os <<
"KNN classifier, defaultK=" << defaultK_ <<
", memSize=" << memory_.size() <<
":" << std::endl;
203 std::copy(memory_.begin(), memory_.end(), std::ostream_iterator<ExampleTrain>(os,
";") );
210 #endif //FAIF_K_NEAREST_NEIGHBOR_CLASSIFIER_HPP Val::DomainType::ValueId AttrIdd
attribute id representation in learning
Definition: Classifier.hpp:55
k Nearest Neighbor classifier
Definition: KNearestNeighbor.hpp:61
virtual Beliefs getCategories(const ExampleTest &e) const
classify and return all classes with belief that the example is from given class
Definition: KNearestNeighbor.hpp:96
Val::Value AttrValue
attribute value representation in learning
Definition: Classifier.hpp:49
virtual void write(std::ostream &os) const
Definition: KNearestNeighbor.hpp:201
int getDefaultK() const
Definition: KNearestNeighbor.hpp:108
void setDefaultK(int k)
Definition: KNearestNeighbor.hpp:111
AttrIdd getCategoryK(const ExampleTest &e, int K) const
Definition: KNearestNeighbor.hpp:161
Definition: Classifier.hpp:233
point and some feature
Definition: Point.hpp:58
Belief< Val >::Beliefs Beliefs
collection of pair (AttrIdd, Probability)
Definition: Classifier.hpp:64
inner class - examples train collection
Definition: Classifier.hpp:82
Beliefs getCategoriesK(const ExampleTest &e, int K) const
classify and return all classes with belief that the example is from given class
Definition: KNearestNeighbor.hpp:172
virtual void reset()
reset - clear the memory
Definition: KNearestNeighbor.hpp:148
Point in n-space, each component of the same type.
Definition: Point.hpp:22
the value concept
Definition: Value.hpp:41
Val::DomainType::ValueIdSerialize AttrIddSerialize
for serialization the const interferes
Definition: Classifier.hpp:58
virtual void train(const ExamplesTrain &e)
learn classifier (on the collection of training examples), here store all train examples.
Definition: KNearestNeighbor.hpp:154
virtual AttrIdd getCategory(const ExampleTest &e) const
Definition: KNearestNeighbor.hpp:86
Val::DomainType AttrDomain
the attribute domain for learning
Definition: Classifier.hpp:52
Distance metrics for Nomianal Values Collection, used in K Nearest Neighbors classifier.
Definition: KNearestNeighbor.hpp:27
the clasiffier interface
Definition: Classifier.hpp:43