Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
FST::Classifier_Random_Forest< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR > Class Template Reference

Dependency-free Random Forest classifier (CART trees, Gini impurity, bagging, per-node feature subsampling), deterministic by design. More...

#include <classifier_rf.hpp>

Inheritance diagram for FST::Classifier_Random_Forest< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >:
FST::Clonable_Impl< Classifier_Random_Forest< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >, Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > > FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > FST::Clonable

Classes

struct  Node

Public Types

typedef Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > parent
 the abstract base type
typedef Clonable_Impl< Classifier_Random_Forest, parent > parent_impl
 implements clone() via the (implicit, member-wise) copy-constructor - all members are value-semantic
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/classified on
typedef DATAACCESSOR::PPattern PPattern
 one data sample as exposed by DATAACCESSOR (a raw pointer to its feature vector)
Public Types inherited from FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR >
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)

Public Member Functions

void set_tree_count (const unsigned int t)
 number of trees to grow (must be >0); default 100
void set_mtry (const DIMTYPE m)
 features tried per node; 0 = floor(sqrt(subset size))
void set_max_depth (const unsigned int dp)
 0 = unlimited
void set_min_leaf (const IDXTYPE ml)
 minimum samples in each child after a split (must be >0); default 1
void set_seed_salt (const unsigned int s)
 salts the subset-content hash the training RNG is seeded from; default 1 (the same value the unseeded library PRNG uses), so leaving it alone still gives a fixed, reproducible forest
void initialize (const PDataAccessor da)
 records data shape, allocates the index buffer
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
 Grows the forest on the designated training part of da, restricted to sub's selected features. Buffers the raw (unstandardized – CART is scale-invariant) selected feature values and labels, seeds the local xorshift64* RNG (see class doc) from an FNV-1a hash of (subset content, --seed salt, current split index), derives mtry (default floor(sqrt(subset size))), then grows _tree_count trees from independent bootstrap resamples (rng_below(N) draws with replacement, fixed order).
virtual std::ostream & print (std::ostream &os) const override
 writes a human-readable identification of the object; base default writes nothing
Public Member Functions inherited from FST::Clonable_Impl< Classifier_Random_Forest< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >, Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR > >
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)
Public Member Functions inherited from FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR >
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)
Public Member Functions inherited from FST::Clonable
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

Protected Member Functions

std::uint64_t rng_next ()
 advances the xorshift64* generator, returns its next 64-bit output
IDXTYPE rng_below (const IDXTYPE n)
 a value in [0,n) via modulo of rng_next(). The modulo bias is of order n/2^64 - unmeasurable for any sample or feature count - and only determinism matters here, not statistical perfection
void grow_tree (std::vector< Node > &tree, std::vector< IDXTYPE > &samples, const DIMTYPE mtry)
 Grows one CART tree (Gini-impurity best split, mtry random candidate features per node) into tree, over the bootstrap sample indices in samples (indices into _X/_y). Splits are found by a total order on (feature value, sample index) so ties are resolved deterministically; equal-gain candidates keep the first one encountered; a node becomes a leaf when pure, when either child would fall below _min_leaf, or at _max_depth; the leaf class is the majority vote with lowest-class-index tie-break.
DIMTYPE tree_vote (const std::vector< Node > &tree, const PPattern &pattern) const
 Walks tree from its root to a leaf for pattern (via _index to map each node's subset-position feature to the pattern's raw feature index) and returns the leaf's class.
Protected Member Functions inherited from FST::Clonable
void set_cloned (const bool cloned=true)
 marks/unmarks this instance as a clone; call from clone()/stateless_clone() overrides (Clonable_Impl does this automatically)
void set_sharing (const bool sharing=true)
 marks/unmarks this instance as sharing (see is_sharing())
void set_sharing_cloned (const bool cloned=true, const bool sharing=true)
 sets both flags at once; call from sharing_clone() overrides (Clonable_Impl does this automatically)

Protected Attributes

unsigned int _tree_count
 number of trees to grow; set by set_tree_count()
DIMTYPE _mtry
 features tried per split node; 0 = auto (floor(sqrt(subset size))); set by set_mtry()
unsigned int _max_depth
 tree depth cap; 0 = unlimited; set by set_max_depth()
IDXTYPE _min_leaf
 minimum samples in each child after a split; set by set_min_leaf()
unsigned int _seed_salt
 salts the per-evaluation subset-content hash; set by set_seed_salt(), default 1
DIMTYPE _classes
 number of classes, as recorded by initialize()
DIMTYPE _features
 raw (full) feature count, as recorded by initialize()
DIMTYPE _subfeatures
 d = current subset size
std::vector< DIMTYPE > _index
 subset position -> raw feature index (first d entries valid)
std::vector< IDXTYPE > _votes
 classify() vote scratch (member to avoid per-pattern allocation)
std::vector< std::vector< Node > > _forest
 the trained forest; outer index = tree, inner = that tree's node vector (root at index 0)
std::vector< REALTYPE > _X
 N x d row-major selected training values.
std::vector< DIMTYPE > _y
 N training labels.
std::uint64_t _rng_state
 xorshift64* generator state; seeded per train() call (see train())

Detailed Description

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
class FST::Classifier_Random_Forest< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >

Dependency-free Random Forest classifier (CART trees, Gini impurity, bagging, per-node feature subsampling), deterministic by design.

Randomness design: RF training draws randomness inside worker-side criterion clones, which must not touch the thread_local fst_rand() stream (the reproducibility of the threaded evaluators depends on the master owning it). Instead the training RNG is a LOCAL xorshift64* generator seeded by an FNV-1a hash of the candidate subset content (the ascending selected feature indices), salted with the process seed and the data accessor's current split index. Every evaluation is therefore a pure function of (subset, data, seed): threading-invariant and evaluation-order-invariant by construction, at any thread count, under any evaluator.

Determinism details: bootstrap draws and per-node feature subsampling come from the hash-seeded stream in fixed order; candidate-value sorting uses a total order (value, then sample index); equal-gain splits keep the first candidate encountered; vote ties resolve to the lowest class index. No criterion estimation uses out-of-bag machinery - subset quality comes from the FST4 splitters (CV/holdout) as with every other wrapper.

Trees are per-train() state; the wrapper retrains per candidate subset. classify_scores() returns per-class tree-vote fractions, which is what makes ranking metrics such as AUC available for this classifier.

Member Function Documentation

◆ classify()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
bool FST::Classifier_Random_Forest< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::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 >.

◆ classify_scores()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
bool FST::Classifier_Random_Forest< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::classify_scores ( std::vector< RETURNTYPE > & scores,
const PPattern & pattern )
overridevirtual

per-class real scores for one pattern (any monotone transform of the class posteriors); enables ranking-based metrics (AUC). Default: not supported.

Reimplemented from FST::Classifier< RETURNTYPE, DIMTYPE, SUBSET, DATAACCESSOR >.

◆ print()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
virtual std::ostream & FST::Classifier_Random_Forest< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::print ( std::ostream & os) const
inlineoverridevirtual

writes a human-readable identification of the object; base default writes nothing

Reimplemented from FST::Clonable.

◆ train()

template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
bool FST::Classifier_Random_Forest< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR >::train ( const PDataAccessor da,
const PSubset sub )
overridevirtual

Grows the forest on the designated training part of da, restricted to sub's selected features. Buffers the raw (unstandardized – CART is scale-invariant) selected feature values and labels, seeds the local xorshift64* RNG (see class doc) from an FNV-1a hash of (subset content, --seed salt, current split index), derives mtry (default floor(sqrt(subset size))), then grows _tree_count trees from independent bootstrap resamples (rng_below(N) draws with replacement, fixed order).

Returns
false if the designated training part is empty; true otherwise.

The documentation for this class was generated from the following file:
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.