{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Confusion Matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook illustrates how to create a confusion matrix plot directly from a dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import seaborn as sns\n", "\n", "from freeforestml import Variable, Process, Cut, confusion_matrix, HistogramFactory\n", "from freeforestml import toydata, example_style\n", "example_style()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = toydata.get()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p_sig = Process(r\"Signal\", range=(1, 1))\n", "p_ztt = Process(r\"$Z\\rightarrow\\tau\\tau$\", range=(0, 0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c_low = Cut(lambda d: d.m_jj < 350, label=\"Low $m^{jj}$\")\n", "c_mid = Cut(lambda d: (d.m_jj >= 350) & (d.m_jj < 600), label=\"Mid $m^{jj}$\")\n", "c_high = Cut(lambda d: d.m_jj > 600, label=\"High $m^{jj}$\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Normalized columns" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "confusion_matrix(df, [p_sig, p_ztt], [c_low, c_mid, c_high],\n", " y_label=\"Region\", x_label=\"Truth Signal\", annot=True, weight=\"weight\")\n", "None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Normalized rows" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "confusion_matrix(df, [p_sig, p_ztt], [c_low, c_mid, c_high], normalize_rows=True,\n", " y_label=\"Region\", x_label=\"Truth Signal\", annot=True, weight=\"weight\")\n", "None" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 2 }