{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d17abaa2",
   "metadata": {},
   "source": [
    "# ApplicationRegressionKeras\n",
    "This tutorial shows how to apply a trained model to new data (regression).\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** TMVA Team  \n",
    "<i><small>This notebook tutorial was automatically generated with <a href= \"https://github.com/root-project/root/blob/master/documentation/doxygen/converttonotebook.py\">ROOTBOOK-izer</a> from the macro found in the ROOT repository  on Tuesday, May 19, 2026 at 08:20 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea3cf48f",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from ROOT import TMVA, TFile, TString, gROOT\n",
    "from array import array\n",
    "from subprocess import call\n",
    "from os.path import isfile"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96a975a3",
   "metadata": {},
   "source": [
    "Setup TMVA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c020c990",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "TMVA.Tools.Instance()\n",
    "TMVA.PyMethodBase.PyInitialize()\n",
    "reader = TMVA.Reader(\"Color:!Silent\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3820c41f",
   "metadata": {},
   "source": [
    "Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "053fea9b",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = TFile.Open(str(gROOT.GetTutorialDir()) + '/machine_learning/data/tmva_reg_example.root')\n",
    "tree = data.Get('TreeR')\n",
    "\n",
    "branches = {}\n",
    "for branch in tree.GetListOfBranches():\n",
    "    branchName = branch.GetName()\n",
    "    branches[branchName] = array('f', [-999])\n",
    "    tree.SetBranchAddress(branchName, branches[branchName])\n",
    "    if branchName != 'fvalue':\n",
    "        reader.AddVariable(branchName, branches[branchName])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a7bac68",
   "metadata": {},
   "source": [
    "Book methods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c71869f0",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "reader.BookMVA('PyKeras', TString('dataset/weights/TMVARegression_PyKeras.weights.xml'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee0076b0",
   "metadata": {},
   "source": [
    "Print some example regressions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "442326c6",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print('Some example regressions:')\n",
    "for i in range(20):\n",
    "    tree.GetEntry(i)\n",
    "    print('True/MVA value: {}/{}'.format(branches['fvalue'][0],reader.EvaluateMVA('PyKeras')))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}