9#include <tracking/trackFindingVXD/filterTools/FBDTClassifier.h>
10#include <framework/logging/Logger.h>
12#include <FastBDT_IO.h>
20 B2DEBUG(20,
"Reading the FeatureBinnings");
22 B2DEBUG(20,
"Reading the Forest");
23 m_forest = FastBDT::readForestFromStream<unsigned int>(is);
24 B2DEBUG(20,
"Reading the DecorrelationMatrix");
25 if (!m_decorrMat.readFromStream(is)) {
26 B2ERROR(
"Reading in the decorrelation matrix did not work! The decorrelation matrix of this classifier will be set to identity!");
34 B2DEBUG(20,
"Reading the FeatureBinnings");
35 os << m_featBins << std::endl;
36 B2DEBUG(20,
"Reading the Forest");
37 os << m_forest << std::endl;
38 B2DEBUG(20,
"Reading the DecorrelationMatrix");
39 os << m_decorrMat.print() << std::endl;
44 int nTrees,
int depth,
double shrinkage,
double ratio)
46 if (samples.empty()) {
47 B2ERROR(
"No samples passed for training a FBDTClassifier.");
51 unsigned int nBinCuts = 8;
52 size_t nSamples = samples.size();
53 B2DEBUG(20,
"Using for training: nBinCuts: " << nBinCuts <<
", with " << Ndims <<
" features and " << nSamples <<
" samples.");
55 B2DEBUG(20,
"FBDTClassifier::train(): Starting to restructure the data into the format better suited for later use");
56 std::array<std::vector<double>, Ndims> data;
57 for (
const auto& event : samples) {
58 for (
size_t iSP = 0; iSP < Ndims; ++iSP) {
59 data[iSP].push_back(event.hits[iSP]);
63 B2DEBUG(20,
"FBDTClassifier::train(): Calculating the decorrelation transformation.");
64 m_decorrMat.calculateDecorrMatrix(data,
false);
65 B2DEBUG(20,
"FBDTClassifier::train(): Applying decorrelation transformation");
66 data = m_decorrMat.decorrelate(data);
68 B2DEBUG(20,
"FBDTClassifier::train(): Determining the FeatureBinnings");
69 std::vector<unsigned int> nBinningLevels;
71 for (
auto featureVec : data) {
72 m_featBins.push_back(FastBDT::FeatureBinning<double>(nBinCuts, featureVec));
73 nBinningLevels.push_back(nBinCuts);
77 B2DEBUG(20,
"FBDTClassifier::train(): Creating the EventSamples");
78 FastBDT::EventSample eventSample(nSamples, Ndims, 0, nBinningLevels);
79 for (
size_t iS = 0; iS < nSamples; ++iS) {
80 std::vector<unsigned> bins(Ndims);
81 for (
size_t iF = 0; iF < Ndims; ++iF) {
82 bins[iF] = m_featBins[iF].ValueToBin(data[iF][iS]);
84 eventSample.AddEvent(bins, 1.0, samples[iS].signal);
87 B2DEBUG(20,
"FBDTClassifier::train(): Training the FastBDT");
88 FastBDT::ForestBuilder fbdt(eventSample, nTrees, shrinkage, ratio, depth);
90 B2DEBUG(20,
"FBDTClassifier::train(): getting FastBDT to internal member");
91 FBDTForest forest(fbdt.GetF0(), fbdt.GetShrinkage(),
true);
92 for (
const auto& tree : fbdt.GetForest()) {
Class holding a Matrix that can be used to decorrelate input data to Machine Learning classifiers.
FastBDT as RelationsObject to make it storable and accessible on/via the DataStore.
void readFromStream(std::istream &is)
read all the necessary data from stream and fill the Forest and the FeatureBinnings NOTE: uses FastBD...
void writeToStream(std::ostream &os) const
write out the data from the Forest and the FeatureBinnings to a stream NOTE: uses FastBDTs IO stuff.
void train(const std::vector< Belle2::FBDTTrainSample< Ndims > > &samples, int nTree, int depth, double shrinkage=0.15, double ratio=0.5)
train the BDT NOTE overwrites a currently existing classifier internally TODO does not work at the mo...
Abstract base class for different kinds of events.
bundle together the classifier input and the target value into one struct for easier passing around.