An example of embedding a scikit-learn classifier in a RooAbsReal by Kyle Cranmer
see: https://github.com/cranmer/parametrized-learning/tree/master/SciKitLearnWrapper for the c++ code, Makefile, and pkl file used here
see also a standalone version: https://github.com/cranmer/roofit-python-wrapper
In [1]:
from ROOT import TCanvas, gSystem, RooRealVar
from ROOT import RooFit
import ROOT
import numpy as np
from sklearn import svm
from sklearn.externals import joblib
In [2]:
def scikitlearnFunc(x=0.):
clf = joblib.load('../adaptive.pkl')
traindata = np.array((x,0.))
outputs=clf.predict(traindata)
return outputs[0]
def scikitlearnTest():
gSystem.Load( 'libSciKitLearnWrapper' )
x = RooRealVar('x','x',0.2,-5,5)
s = ROOT.SciKitLearnWrapper('s','s',x)
s.RegisterCallBack( scikitlearnFunc );
c1 = TCanvas('c1')
frame = x.frame()
s.plotOn(frame)
frame.Draw()
#c1.SaveAs('scikitlearn-wrapper-plot.pdf')
c1.Draw()
if __name__ == '__main__':
scikitlearnTest()
In [ ]: