23#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
24#include <numpy/arrayobject.h>
55 PyGILState_STATE m_GILState;
58 PyGILRAII() : m_GILState(PyGILState_Ensure()) {}
59 ~PyGILRAII() { PyGILState_Release(m_GILState); }
69MethodPyAdaBoost::MethodPyAdaBoost(
const TString &jobName,
74 fBaseEstimator(
"None"),
77 fAlgorithm(
"SAMME.R"),
86 fBaseEstimator(
"None"),
89 fAlgorithm(
"SAMME.R"),
113 The base estimator from which the boosted ensemble is built.\
114 Support for sample weighting is required, as well as proper `classes_`\
115 and `n_classes_` attributes.");
118 The maximum number of estimators at which boosting is terminated.\
119 In case of perfect fit, the learning procedure is stopped early.");
122 Learning rate shrinks the contribution of each classifier by\
123 ``learning_rate``. There is a trade-off between ``learning_rate`` and\
127 If 'SAMME.R' then use the SAMME.R real boosting algorithm.\
128 ``base_estimator`` must support calculation of class probabilities.\
129 If 'SAMME' then use the SAMME discrete boosting algorithm.\
130 The SAMME.R algorithm typically converges faster than SAMME,\
131 achieving a lower test error with fewer boosting iterations.");
134 If int, random_state is the seed used by the random number generator;\
135 If RandomState instance, random_state is the random number generator;\
136 If None, the random number generator is the RandomState instance used\
140 "Store trained classifier in this file");
150 <<
" The options are Object or None." <<
Endl;
155 Log() << kFATAL <<
"NEstimators <=0 ... that does not work!" <<
Endl;
161 Log() << kFATAL <<
"LearningRate <=0 ... that does not work!" <<
Endl;
168 <<
" The options are SAMME of SAMME.R." <<
Endl;
176 <<
"If int, random_state is the seed used by the random number generator;"
177 <<
"If RandomState instance, random_state is the random number generator;"
178 <<
"If None, the random number generator is the RandomState instance used by `np.random`." <<
Endl;
191 TMVA::Internal::PyGILRAII raii;
210 npy_intp dimsData[2];
211 dimsData[0] = fNrowsTraining;
213 fTrainData = (PyArrayObject *)PyArray_SimpleNew(2, dimsData, NPY_FLOAT);
215 float *TrainData = (
float *)(PyArray_DATA(
fTrainData));
217 npy_intp dimsClasses = (npy_intp) fNrowsTraining;
218 fTrainDataClasses = (PyArrayObject *)PyArray_SimpleNew(1, &dimsClasses, NPY_FLOAT);
222 fTrainDataWeights = (PyArrayObject *)PyArray_SimpleNew(1, &dimsClasses, NPY_FLOAT);
226 for (
int i = 0; i < fNrowsTraining; i++) {
230 TrainData[j + i *
fNvars] =
e->GetValue(j);
234 TrainDataClasses[i] =
e->GetClass();
237 TrainDataWeights[i] =
e->GetWeight();
241 PyRunString(
"classifier = sklearn.ensemble.AdaBoostClassifier(base_estimator=baseEstimator, n_estimators=nEstimators, learning_rate=learningRate, algorithm=algorithm, random_state=randomState)",
242 "Failed to setup classifier");
246 PyRunString(
"dump = classifier.fit(trainData, trainDataClasses, trainDataWeights)",
"Failed to train classifier");
251 Log() << kFATAL <<
"Can't create classifier object from AdaBoostClassifier" <<
Endl;
277 if (firstEvt > lastEvt || lastEvt > nEvents) lastEvt = nEvents;
278 if (firstEvt < 0) firstEvt = 0;
279 nEvents = lastEvt-firstEvt;
288 <<
" sample (" << nEvents <<
" events)" <<
Endl;
294 PyArrayObject *pEvent= (PyArrayObject *)PyArray_SimpleNew(2, dims, NPY_FLOAT);
295 float *pValue = (
float *)(PyArray_DATA(pEvent));
297 for (
Int_t ievt=0; ievt<nEvents; ievt++) {
301 pValue[ievt *
fNvars + i] =
e->GetValue(i);
306 PyArrayObject *result = (PyArrayObject *)PyObject_CallMethod(
fClassifier,
const_cast<char *
>(
"predict_proba"),
const_cast<char *
>(
"(O)"), pEvent);
307 double *proba = (
double *)(PyArray_DATA(result));
311 for (
int i = 0; i < nEvents; ++i) {
320 <<
"Elapsed time for evaluation of " << nEvents <<
" events: "
341 PyArrayObject *pEvent= (PyArrayObject *)PyArray_SimpleNew(2, dims, NPY_FLOAT);
342 float *pValue = (
float *)(PyArray_DATA(pEvent));
343 for (
UInt_t i = 0; i <
fNvars; i++) pValue[i] =
e->GetValue(i);
346 PyArrayObject *result = (PyArrayObject *)PyObject_CallMethod(
fClassifier,
const_cast<char *
>(
"predict_proba"),
const_cast<char *
>(
"(O)"), pEvent);
347 double *proba = (
double *)(PyArray_DATA(result));
370 PyArrayObject *pEvent= (PyArrayObject *)PyArray_SimpleNew(2, dims, NPY_FLOAT);
371 float *pValue = (
float *)(PyArray_DATA(pEvent));
372 for (
UInt_t i = 0; i <
fNvars; i++) pValue[i] =
e->GetValue(i);
375 PyArrayObject *result = (PyArrayObject *)PyObject_CallMethod(
fClassifier,
const_cast<char *
>(
"predict_proba"),
const_cast<char *
>(
"(O)"), pEvent);
376 double *proba = (
double *)(PyArray_DATA(result));
417 PyArrayObject* pRanking = (PyArrayObject*) PyObject_GetAttrString(
fClassifier,
"feature_importances_");
420 if(pRanking == 0)
return NULL;
439 Log() <<
"An AdaBoost classifier is a meta-estimator that begins by fitting" <<
Endl;
440 Log() <<
"a classifier on the original dataset and then fits additional copies" <<
Endl;
441 Log() <<
"of the classifier on the same dataset but where the weights of incorrectly" <<
Endl;
442 Log() <<
"classified instances are adjusted such that subsequent classifiers focus" <<
Endl;
443 Log() <<
"more on difficult cases." <<
Endl;
445 Log() <<
"Check out the scikit-learn documentation for more information." <<
Endl;
#define REGISTER_METHOD(CLASS)
for example
char * Form(const char *fmt,...)
OptionBase * DeclareOptionRef(T &ref, const TString &name, const TString &desc="")
Class that contains all the data information.
UInt_t GetNClasses() const
const Event * GetEvent() const
Types::ETreeType GetCurrentType() const
Long64_t GetNEvents(Types::ETreeType type=Types::kMaxTreeType) const
Long64_t GetNTrainingEvents() const
void SetCurrentEvent(Long64_t ievt) const
const Event * GetTrainingEvent(Long64_t ievt) const
virtual void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility they are hence without any...
const char * GetName() const
const TString & GetWeightFileDir() const
const TString & GetMethodName() const
DataSetInfo & DataInfo() const
virtual void TestClassification()
initialization
UInt_t GetNVariables() const
Bool_t IsModelPersistence()
void NoErrorCalc(Double_t *const err, Double_t *const errUpper)
const TString & GetInputLabel(Int_t i) const
std::vector< Double_t > GetMvaValues(Long64_t firstEvt=0, Long64_t lastEvt=-1, Bool_t logProgress=false)
get all the MVA values for the events of the current Data type
PyObject * pBaseEstimator
Double_t GetMvaValue(Double_t *errLower=0, Double_t *errUpper=0)
std::vector< Double_t > mvaValues
TString fFilenameClassifier
void GetHelpMessage() const
const Ranking * CreateRanking()
std::vector< Float_t > classValues
MethodPyAdaBoost(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="")
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)
virtual void TestClassification()
initialization
virtual void ReadModelFromFile()
std::vector< Float_t > & GetMulticlassValues()
static int PyIsInitialized()
Check Python interpreter initialization status.
PyArrayObject * fTrainData
PyObject * Eval(TString code)
Evaluate Python code.
static void PyInitialize()
Initialize Python interpreter.
static void Serialize(TString file, PyObject *classifier)
Serialize Python object.
PyArrayObject * fTrainDataWeights
static Int_t UnSerialize(TString file, PyObject **obj)
Unserialize Python object.
PyArrayObject * fTrainDataClasses
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=Py_single_input)
Execute Python code from string.
Ranking for variables in method (implementation)
virtual void AddRank(const Rank &rank)
Add a new rank take ownership of it.
Timing information for training and evaluation of MVA methods.
TString GetElapsedTime(Bool_t Scientific=kTRUE)
returns pretty string with elapsed time
Singleton class for Global types used by TMVA.
const char * Data() const
create variable transformations
MsgLogger & Endl(MsgLogger &ml)