Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
ApplicationClassificationKeras.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_tmva_keras
3## \notebook -nodraw
4## This tutorial shows how to apply a trained model to new data.
5##
6## \macro_code
7##
8## \date 2017
9## \author TMVA Team
10
11from ROOT import TMVA, TFile, TString, gROOT
12from array import array
13from subprocess import call
14from os.path import isfile
15
16# Setup TMVA
19reader = TMVA.Reader("Color:!Silent")
20
21# Load data
22data = TFile.Open(str(gROOT.GetTutorialDir()) + "/machine_learning/data/tmva_class_example.root")
23signal = data.Get('TreeS')
24background = data.Get('TreeB')
25
26branches = {}
27for branch in signal.GetListOfBranches():
28 branchName = branch.GetName()
29 branches[branchName] = array('f', [-999])
30 reader.AddVariable(branchName, branches[branchName])
31 signal.SetBranchAddress(branchName, branches[branchName])
32 background.SetBranchAddress(branchName, branches[branchName])
33
34# Book methods
35reader.BookMVA('PyKeras', TString('dataset/weights/TMVAClassification_PyKeras.weights.xml'))
36
37# Print some example classifications
38print('Some signal example classifications:')
39for i in range(20):
40 signal.GetEntry(i)
41 print(reader.EvaluateMVA('PyKeras'))
42print('')
43
44print('Some background example classifications:')
45for i in range(20):
46 background.GetEntry(i)
47 print(reader.EvaluateMVA('PyKeras'))
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:3787
static void PyInitialize()
Initialize Python interpreter.
The Reader class serves to use the MVAs in a specific analysis context.
Definition Reader.h:64
static Tools & Instance()
Definition Tools.cxx:72
Basic string class.
Definition TString.h:138