Belle II Software  release-08-01-10
PyEstimator Class Reference

Class to invoke a pretrained python estimator that follows the sklearn interface. More...

#include <PyEstimator.h>

Collaboration diagram for PyEstimator:

Public Member Functions

 PyEstimator (const std::string &pickleFileName)
 Construct the Estimator. More...
 
double predict (const std::vector< double > &inputVariables)
 Call the predict method of the estimator.
 
double predict (const std::vector< NamedFloatTuple * > &floatTuples)
 Call the predict method of the estimator.
 
double predict (boost::python::object &array)
 Call the predict method of the estimator.
 

Private Member Functions

void unpickleEstimator (const std::string &pickleFileName)
 Load the estimator object from the pickled file.
 
void expand (size_t nVars)
 Reserves space for at least n variable in the input array.
 

Private Attributes

std::string m_pickleFileName
 File name of the pickle file that contains the trained estimator.
 
boost::python::object m_estimator
 Retrained python estimator object.
 
boost::python::object m_predict
 Python bound prediction method - cached to avoid repeated lookup.
 
boost::python::object m_array
 Array to be served to the estimator.
 
size_t m_nCurrent = 0
 Cache for the current length of the input array.
 
bool m_is_binary_classification = false
 Internal flag to keep track whether a binary classification with predict_proba is evaluated.
 

Detailed Description

Class to invoke a pretrained python estimator that follows the sklearn interface.

Definition at line 23 of file PyEstimator.h.

Constructor & Destructor Documentation

◆ PyEstimator()

PyEstimator ( const std::string &  pickleFileName)
explicit

Construct the Estimator.

Parameters
pickleFileNameName of the file that contains the pickled estimator object eg from sklearn

Definition at line 22 of file PyEstimator.cc.

23  : m_pickleFileName(pickleFileName)
24 {
25  try {
26  // Construct an array with one entry
27  // Expand it once the number of variables is known.
28  boost::python::object numpy = boost::python::import("numpy");
29  boost::python::list initValues;
30  initValues.append(0.0);
31  m_array = numpy.attr("array")(initValues);
32  m_nCurrent = boost::python::len(m_array);
33  // boost::python::object array = boost::python::import("array");
34  // m_array = array.attr("array")("d");
35  unpickleEstimator(pickleFileName);
36  } catch (const boost::python::error_already_set&) {
37  PyErr_Print();
38  B2ERROR("Could not construct PyEstimator from " << pickleFileName);
39  }
40 }
void unpickleEstimator(const std::string &pickleFileName)
Load the estimator object from the pickled file.
Definition: PyEstimator.cc:92
std::string m_pickleFileName
File name of the pickle file that contains the trained estimator.
Definition: PyEstimator.h:48
size_t m_nCurrent
Cache for the current length of the input array.
Definition: PyEstimator.h:60
boost::python::object m_array
Array to be served to the estimator.
Definition: PyEstimator.h:57

The documentation for this class was generated from the following files: