Skip to content

Instantly share code, notes, and snippets.

@dizys
Last active December 14, 2022 18:20
Show Gist options
  • Save dizys/c427e9229866cffe249a698ea4147d16 to your computer and use it in GitHub Desktop.
Save dizys/c427e9229866cffe249a698ea4147d16 to your computer and use it in GitHub Desktop.
Plot a confusion matrix in Jupyter Notebook. A piece of utility code snippet from fastai library.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNn7oaMu+OmImZXuAPu5d2g",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/dizys/c427e9229866cffe249a698ea4147d16/confusion-matrix.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"source": [
" import matplotlib.pyplot as plt\n",
" import numpy as np"
],
"metadata": {
"id": "aYFJNLHWsMmo"
},
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"source": [
"vocab = [\"ai\", \"original\"]\n",
"TN = 3 # original images correctly detected.\n",
"TP = 17 # ai images correctly detected.\n",
"FP = 13 # original images falsly detected as ai ones.\n",
"FN = 283 # ai images falsly detected as original ones.\n",
"cm = np.array([\n",
" [TP, FN],\n",
" [FP, TN]\n",
"])"
],
"metadata": {
"id": "lU-RoAsgs651"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 311
},
"id": "YkJ8zXCBsHce",
"outputId": "4b13ff46-0750-4a26-e1d0-0f92074f8ff0"
},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
],
"image/png": "iVBORw0KGgoAAAANSUhEUgA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WbWnFSBXRMdLXUws1wzGxY9x3QzOzratqGZvW5mH5vZ42T4dSRJ03UsElR0ZHISMDaadQTQ2t3nm1lPYJm7tzOzmsBkM3sdOBxoBRwM7EXqPrD/3GK/ucBjQMdoXw3c/b9m9giwwt37Res9D/zd3d8xs6bAa8BPgV7AO+5+u5mdDFwc619ENadikVBqm1nxL9+aROoCsPbAv919fjT/18ChxeMnQDapT/12BAa7+3rgazN7q5T9HwVMLN5XObd1/CVwcOr+SgDUi37dRkfgjGjbMWb2XSVfp1SAikVCWe3ubUrOiH64V5acBVzp7q9tsV6XgDmygKPcfU0pWSRNNMYi6fQacLmZ7QpgZi2jD+RNBM6JxmD2ATqXsu1UoKOZ7R9tW3yD6h+APUqs9zpwZfGEmRWX3USgWzTvJFK3MZCYqFgknR4nNX7ynpl9BDxK6qh5ODAvWvYMMGXLDd19CdATeMnMPgCGRItGAacXD94CVwF50eDwbDa9O9WHVDF9TOqUqFrdgzbddEm/iASnIxYRCU7FIiLBqVhEJDgVi4gEp2IRkeBULCISnIpFRIL7f7PzZbms37M4AAAAAElFTkSuQmCC\n"
},
"metadata": {
"needs_background": "light"
}
}
],
"source": [
"\n",
"fig = plt.figure()\n",
"plt.imshow(cm, interpolation='nearest', cmap=\"Blues\")\n",
"plt.title(\"Confusion matrix\")\n",
"tick_marks = np.arange(len(vocab))\n",
"plt.xticks(tick_marks, vocab, rotation=90)\n",
"plt.yticks(tick_marks, vocab, rotation=0)\n",
"\n",
"thresh = cm.max() / 2.\n",
"for i in range(cm.shape[0]):\n",
" for j in range(cm.shape[1]):\n",
" coeff = f'{cm[i, j]}'\n",
" plt.text(j, i, coeff, horizontalalignment=\"center\", verticalalignment=\"center\", color=\"white\"\n",
" if cm[i, j] > thresh else \"black\")\n",
"\n",
"ax = fig.gca()\n",
"ax.set_ylim(len(vocab)-.5,-.5)\n",
"\n",
"plt.tight_layout()\n",
"plt.ylabel('Actual')\n",
"plt.xlabel('Predicted')\n",
"plt.grid(False)"
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "Ggmx0fDwvNhA"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment