23#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
24#include <numpy/arrayobject.h>
67 fBaseEstimator(
"None"),
70 fAlgorithm(
"SAMME.R"),
79 fBaseEstimator(
"None"),
82 fAlgorithm(
"SAMME.R"),
106 The base estimator from which the boosted ensemble is built.\
107 Support for sample weighting is required, as well as proper `classes_`\
108 and `n_classes_` attributes.");
111 The maximum number of estimators at which boosting is terminated.\
112 In case of perfect fit, the learning procedure is stopped early.");
115 Learning rate shrinks the contribution of each classifier by\
116 ``learning_rate``. There is a trade-off between ``learning_rate`` and\
120 If 'SAMME.R' then use the SAMME.R real boosting algorithm.\
121 ``base_estimator`` must support calculation of class probabilities.\
122 If 'SAMME' then use the SAMME discrete boosting algorithm.\
123 The SAMME.R algorithm typically converges faster than SAMME,\
124 achieving a lower test error with fewer boosting iterations.");
127 If int, random_state is the seed used by the random number generator;\
128 If RandomState instance, random_state is the random number generator;\
129 If None, the random number generator is the RandomState instance used\
133 "Store trained classifier in this file");
143 <<
" The options are Object or None." <<
Endl;
148 Log() << kFATAL <<
"NEstimators <=0 ... that does not work!" <<
Endl;
154 Log() << kFATAL <<
"LearningRate <=0 ... that does not work!" <<
Endl;
161 <<
" The options are SAMME of SAMME.R." <<
Endl;
169 <<
"If int, random_state is the seed used by the random number generator;"
170 <<
"If RandomState instance, random_state is the random number generator;"
171 <<
"If None, the random number generator is the RandomState instance used by `np.random`." <<
Endl;
203 npy_intp dimsData[2];
204 dimsData[0] = fNrowsTraining;
206 PyArrayObject * fTrainData = (PyArrayObject *)PyArray_SimpleNew(2, dimsData, NPY_FLOAT);
208 float *TrainData = (
float *)(PyArray_DATA(fTrainData));
210 npy_intp dimsClasses = (npy_intp) fNrowsTraining;
211 PyArrayObject * fTrainDataClasses = (PyArrayObject *)PyArray_SimpleNew(1, &dimsClasses, NPY_FLOAT);
212 PyDict_SetItemString(
fLocalNS,
"trainDataClasses", (
PyObject*)fTrainDataClasses);
213 float *TrainDataClasses = (
float *)(PyArray_DATA(fTrainDataClasses));
215 PyArrayObject * fTrainDataWeights = (PyArrayObject *)PyArray_SimpleNew(1, &dimsClasses, NPY_FLOAT);
216 PyDict_SetItemString(
fLocalNS,
"trainDataWeights", (
PyObject*)fTrainDataWeights);
217 float *TrainDataWeights = (
float *)(PyArray_DATA(fTrainDataWeights));
219 for (
int i = 0; i < fNrowsTraining; i++) {
227 TrainDataClasses[i] =
e->GetClass();
230 TrainDataWeights[i] =
e->GetWeight();
234 PyRunString(
"classifier = sklearn.ensemble.AdaBoostClassifier(base_estimator=baseEstimator, n_estimators=nEstimators, learning_rate=learningRate, algorithm=algorithm, random_state=randomState)",
235 "Failed to setup classifier");
239 PyRunString(
"dump = classifier.fit(trainData, trainDataClasses, trainDataWeights)",
"Failed to train classifier");
244 Log() << kFATAL <<
"Can't create classifier object from AdaBoostClassifier" <<
Endl;
270 if (firstEvt > lastEvt || lastEvt > nEvents) lastEvt = nEvents;
271 if (firstEvt < 0) firstEvt = 0;
272 nEvents = lastEvt-firstEvt;
281 <<
" sample (" << nEvents <<
" events)" <<
Endl;
287 PyArrayObject *pEvent= (PyArrayObject *)PyArray_SimpleNew(2, dims, NPY_FLOAT);
288 float *pValue = (
float *)(PyArray_DATA(pEvent));
290 for (
Int_t ievt=0; ievt<nEvents; ievt++) {
299 PyArrayObject *result = (PyArrayObject *)PyObject_CallMethod(
fClassifier,
const_cast<char *
>(
"predict_proba"),
const_cast<char *
>(
"(O)"), pEvent);
300 double *proba = (
double *)(PyArray_DATA(result));
304 for (
int i = 0; i < nEvents; ++i) {
313 <<
"Elapsed time for evaluation of " << nEvents <<
" events: "
334 PyArrayObject *pEvent= (PyArrayObject *)PyArray_SimpleNew(2, dims, NPY_FLOAT);
335 float *pValue = (
float *)(PyArray_DATA(pEvent));
336 for (
UInt_t i = 0; i <
fNvars; i++) pValue[i] =
e->GetValue(i);
339 PyArrayObject *result = (PyArrayObject *)PyObject_CallMethod(
fClassifier,
const_cast<char *
>(
"predict_proba"),
const_cast<char *
>(
"(O)"), pEvent);
340 double *proba = (
double *)(PyArray_DATA(result));
363 PyArrayObject *pEvent= (PyArrayObject *)PyArray_SimpleNew(2, dims, NPY_FLOAT);
364 float *pValue = (
float *)(PyArray_DATA(pEvent));
365 for (
UInt_t i = 0; i <
fNvars; i++) pValue[i] =
e->GetValue(i);
368 PyArrayObject *result = (PyArrayObject *)PyObject_CallMethod(
fClassifier,
const_cast<char *
>(
"predict_proba"),
const_cast<char *
>(
"(O)"), pEvent);
369 double *proba = (
double *)(PyArray_DATA(result));
410 PyArrayObject* pRanking = (PyArrayObject*) PyObject_GetAttrString(
fClassifier,
"feature_importances_");
413 if(pRanking == 0)
return NULL;
432 Log() <<
"An AdaBoost classifier is a meta-estimator that begins by fitting" <<
Endl;
433 Log() <<
"a classifier on the original dataset and then fits additional copies" <<
Endl;
434 Log() <<
"of the classifier on the same dataset but where the weights of incorrectly" <<
Endl;
435 Log() <<
"classified instances are adjusted such that subsequent classifiers focus" <<
Endl;
436 Log() <<
"more on difficult cases." <<
Endl;
438 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
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
PyGILState_STATE m_GILState
virtual void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility they are hence without any...
const char * GetName() const
Bool_t IsModelPersistence() const
const TString & GetWeightFileDir() const
const TString & GetMethodName() const
DataSetInfo & DataInfo() const
virtual void TestClassification()
initialization
UInt_t GetNVariables() const
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.
PyObject * Eval(TString code)
Evaluate Python code.
static void PyInitialize()
Initialize Python interpreter.
static void Serialize(TString file, PyObject *classifier)
Serialize Python object.
static Int_t UnSerialize(TString file, PyObject **obj)
Unserialize Python object.
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)