|
|
typedef Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > | parent |
| | the immediate abstract base
|
|
typedef Clonable_Impl< Classifier_kNN, parent > | parent_impl |
| | implements clone(); sharing/stateless_clone throw in Classifier
|
|
typedef std::shared_ptr< DATAACCESSOR > | PDataAccessor |
| | shared pointer to the data accessor supplying train/test blocks
|
|
typedef std::shared_ptr< SUBSET > | PSubset |
| | shared pointer to the feature subset being trained/evaluated on
|
|
typedef DATAACCESSOR::PPattern | PPattern |
| | one data sample as exposed by DATAACCESSOR (a raw pointer to its feature vector)
|
|
typedef std::shared_ptr< DATAACCESSOR > | PDataAccessor |
| | shared pointer to the data accessor supplying train/test blocks
|
|
typedef std::shared_ptr< SUBSET > const | PSubset |
| | shared pointer to the (fixed, non-reassignable) feature subset being evaluated
|
|
typedef DATAACCESSOR::PPattern | PPattern |
| | one data sample as exposed by DATAACCESSOR (a raw pointer to its feature vector)
|
|
|
void | set_k (const DIMTYPE k) |
| | sets the number of nearest neighbours collected and voted over; k must be > 0
|
|
DIMTYPE | get_k () const |
| | the k set by set_k() (default 1)
|
| virtual bool | classify (DIMTYPE &cls, const PPattern &pattern) override |
| virtual bool | classify_scores (std::vector< RETURNTYPE > &scores, const PPattern &pattern) override |
| virtual bool | train (const PDataAccessor da, const PSubset sub) override |
| virtual std::ostream & | print (std::ostream &os) const override |
| | writes a human-readable identification of the object; base default writes nothing
|
| Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > * | clone () const override |
| | dispatches to the DEEP_KIND mechanism (default CLONE_DEEP: Derived's copy-constructor)
|
| Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > * | sharing_clone () const override |
| | dispatches to the SHARING_KIND mechanism (default CLONE_INHERIT: defers to Base, which throws by family convention)
|
| Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > * | stateless_clone () const override |
| | dispatches to the STATELESS_KIND mechanism (default CLONE_INHERIT: defers to Base, which throws by family convention)
|
| virtual bool | train (const PDataAccessor da, const PSubset sub)=0 |
| | learns from designated training part of data
|
| virtual bool | test (RETURNTYPE &result, const PDataAccessor da) |
| virtual bool | test_confusion (std::vector< RETURNTYPE > &confusion, DIMTYPE &classes, const PDataAccessor da) |
| virtual bool | test_scores (std::vector< std::pair< RETURNTYPE, DIMTYPE > > &scores, const PDataAccessor da) |
| virtual bool | test_scores_matrix (std::vector< RETURNTYPE > &scores, std::vector< DIMTYPE > &labels, DIMTYPE &classes, const PDataAccessor da) |
| virtual bool | test_metric (RETURNTYPE &result, const PDataAccessor da, const Metric_Spec< RETURNTYPE > &spec) |
| bool | is_clone () const |
|
bool | is_sharing () const |
| | true if this instance is a sharing_clone() result (referencing, not owning, the source's structures)
|
|
bool | is_sharing_clone () const |
| | true if this instance is BOTH a clone and sharing – i.e. specifically a sharing_clone() result, not a clone()/stateless_clone() one
|
|
|
DIMTYPE | _k |
| | number of nearest neighbours voted over; set by set_k()
|
|
DIMTYPE | _nns_max_size |
| | cap on _nns's size while collecting neighbours: k, the number voted over
|
|
std::list< Neighbour >::iterator | iter |
| | scratch iterator used internally by sort_in() to walk _nns during insertion
|
|
std::list< Neighbour > | _nns |
| | implemented descending ... the closest neighbour is the last
|
|
std::vector< RETURNTYPE > | _cs_sc |
| | classify() score scratch (member to avoid per-pattern allocation)
|
|
std::unique_ptr< DISTANCE > | _distance |
| | distance metric narrowed to the trained subspace; allocated/renarrowed by train()
|
|
PDataAccessor | _da_train |
| | training data accessor cached by train() (kNN has no real training step); enumerated as neighbours by collect_neighbours()
|
template<class RETURNTYPE, typename DATATYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR, class DISTANCE>
class FST::Classifier_kNN< RETURNTYPE, DATATYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR, DISTANCE >
Implements k-Nearest Neighbor classifier.
The decision rule: collect the k training patterns nearest to the classified pattern under the supplied DISTANCE, count the class votes among them, and take the argmax; the first maximum wins, i.e. ties are resolved in favour of the lowest class index. classify() and classify_scores() implement the same rule - classify() is the argmax of the vote fractions classify_scores() returns - so accuracy, the confusion matrix and the ranking metrics (AUC, ROC) all describe one and the same classifier.
- Examples
- demo10.cpp, demo52t.cpp, and demo55.cpp.
template<class RETURNTYPE, typename DATATYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR, class DISTANCE>
| bool FST::Classifier_kNN< RETURNTYPE, DATATYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR, DISTANCE >::classify |
( |
DIMTYPE & | cls, |
|
|
const PPattern & | pattern ) |
|
overridevirtual |
Classifies one pattern; on success cls receives the predicted class index (0-based, in [0,classes)). Returns false when no decision can be made at all - the model has not been trained, or the implementation hit an unrecoverable condition; false is not a "reject" option and aborts the enclosing test loop.
Implements FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR >.
template<class RETURNTYPE, typename DATATYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR, class DISTANCE>
| bool FST::Classifier_kNN< RETURNTYPE, DATATYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR, DISTANCE >::classify_scores |
( |
std::vector< RETURNTYPE > & | scores, |
|
|
const PPattern & | pattern ) |
|
overridevirtual |
template<class RETURNTYPE, typename DATATYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR, class DISTANCE>
| std::ostream & FST::Classifier_kNN< RETURNTYPE, DATATYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR, DISTANCE >::print |
( |
std::ostream & | os | ) |
const |
|
overridevirtual |
writes a human-readable identification of the object; base default writes nothing
Reimplemented from FST::Clonable.
template<class RETURNTYPE, typename DATATYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR, class DISTANCE>
| void FST::Classifier_kNN< RETURNTYPE, DATATYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR, DISTANCE >::sort_in |
( |
const RETURNTYPE | value, |
|
|
const DIMTYPE | cls ) |
|
protected |
Inserts a candidate neighbour (value, cls) into the descending-sorted _nns, capped at _nns_max_size (the farthest entry is dropped once the cap is exceeded); used by collect_neighbours().
template<class RETURNTYPE, typename DATATYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR, class DISTANCE>
With kNN there is actually no training: this just narrows/allocates the distance metric to sub's subspace and stores da, to be enumerated as training neighbours from classify()/test().