{ "cells": [ { "cell_type": "markdown", "id": "3655656e", "metadata": {}, "source": [ "# tmva004_RStandardScaler\n", "This tutorial illustrates the usage of the standard scaler as preprocessing\n", "method.\n", "\n", "\n", "\n", "\n", "**Author:** Stefan Wunsch \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:22 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "6445036f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:22:36.715756Z", "iopub.status.busy": "2026-05-19T20:22:36.715621Z", "iopub.status.idle": "2026-05-19T20:22:37.037861Z", "shell.execute_reply": "2026-05-19T20:22:37.037395Z" } }, "outputs": [], "source": [ "using namespace TMVA::Experimental;" ] }, { "cell_type": "markdown", "id": "edf6c959", "metadata": {}, "source": [ "Load data used to fit the parameters" ] }, { "cell_type": "code", "execution_count": 2, "id": "1269b412", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:22:37.048816Z", "iopub.status.busy": "2026-05-19T20:22:37.048676Z", "iopub.status.idle": "2026-05-19T20:22:38.317777Z", "shell.execute_reply": "2026-05-19T20:22:38.317107Z" } }, "outputs": [], "source": [ "std::string inputFile = std::string(gROOT->GetTutorialDir()) + \"/machine_learning/data/tmva_class_example.root\";\n", "ROOT::RDataFrame df(\"TreeS\", inputFile);\n", "auto x = AsTensor(df);" ] }, { "cell_type": "markdown", "id": "1431fd79", "metadata": {}, "source": [ "Create standard scaler and fit to data" ] }, { "cell_type": "code", "execution_count": 3, "id": "bad1b55a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:22:38.322404Z", "iopub.status.busy": "2026-05-19T20:22:38.322279Z", "iopub.status.idle": "2026-05-19T20:22:38.528197Z", "shell.execute_reply": "2026-05-19T20:22:38.527402Z" } }, "outputs": [], "source": [ "RStandardScaler scaler;\n", "scaler.Fit(x);" ] }, { "cell_type": "markdown", "id": "ba0c99a0", "metadata": {}, "source": [ "Compute transformation" ] }, { "cell_type": "code", "execution_count": 4, "id": "c5629a2e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:22:38.530251Z", "iopub.status.busy": "2026-05-19T20:22:38.530112Z", "iopub.status.idle": "2026-05-19T20:22:38.736123Z", "shell.execute_reply": "2026-05-19T20:22:38.735291Z" } }, "outputs": [], "source": [ "auto y = scaler.Compute(x);" ] }, { "cell_type": "markdown", "id": "beb633f3", "metadata": {}, "source": [ "Plot first variable scaled and unscaled" ] }, { "cell_type": "code", "execution_count": 5, "id": "ff0ed539", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:22:38.738101Z", "iopub.status.busy": "2026-05-19T20:22:38.737980Z", "iopub.status.idle": "2026-05-19T20:22:38.943685Z", "shell.execute_reply": "2026-05-19T20:22:38.943066Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TH1F h1(\"h1\", \";x_{4};N_{Events}\", 20, -4, 4);\n", "TH1F h2(\"h2\", \";x_{4};N_{Events}\", 20, -4, 4);\n", "for (std::size_t i = 0; i < x.GetShape()[0]; i++) {\n", " h1.Fill(x(i, 3));\n", " h2.Fill(y(i, 3));\n", "}\n", "h1.SetLineWidth(2);\n", "h1.SetLineColor(kRed);\n", "h2.SetLineWidth(2);\n", "h2.SetLineColor(kBlue);\n", "\n", "gStyle->SetOptStat(0);\n", "auto c = new TCanvas(\"\", \"\", 800, 800);\n", "h2.Draw(\"HIST\");\n", "h1.Draw(\"HIST SAME\");\n", "\n", "TLegend legend(0.7, 0.7, 0.89, 0.89);\n", "legend.SetBorderSize(0);\n", "legend.AddEntry(\"h1\", \"Unscaled\", \"l\");\n", "legend.AddEntry(\"h2\", \"Scaled\", \"l\");\n", "legend.Draw();\n", "\n", "c->DrawClone();" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }