{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "48214590-414c-4dd0-9d1f-9446c1d751a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f0738b63-6ea5-4bb0-81e3-101ff9d67a63",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets, metrics, svm\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8a26a325-096c-45fd-b0ef-cb0166a4833d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 6144 characters]",
      "text/plain": [
       "<Figure size 1000x300 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### load dataset \n",
    "\n",
    "digits = datasets.load_digits()\n",
    "\n",
    "### show training data\n",
    "_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10,3))\n",
    "for ax, image, label in zip(axes, digits.images, digits.target):\n",
    "    ax.set_axis_off()\n",
    "    ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
    "    ax.set_title(\"Training: %i\" % label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8355cc2f-3128-43a3-8122-1517d11b911a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 6 1 6 5 7 1 5 3 7 4 1 2 5 4 9 8 2 6 6 0 5 4 9 7 4 8 7 5 6 5 3 8 9 9 6 1\n",
      " 1 0 6 8 4 8 8 8 1 2 7 1 2 0 1 8 8 9 6 4 6 7 0 4 1 0 5 4 1 7 2 0 6 7 9 1 5\n",
      " 5 4 0 6 6 2 5 4 1 1 0 9 5 6 5 0 5 1 5 1 3 0 2 5 8 0 1 9 1 1 7 0 6 4 9 8 7\n",
      " 3 7 4 4 2 7 3 6 2 6 1 9 7 4 8 5 5 1 3 9 4 3 7 8 0 5 8 9 5 5 0 0 5 0 6 1 8\n",
      " 8 4 0 4 5 0 1 7 6 9 7 9 5 7 5 6 9 5 0 6 9 6 7 6 2 0 3 3 8 4 9 7 1 2 9 5 5\n",
      " 7 0 9 1 4 6 1 4 0 5 4 9 6 7 1 6 8 1 7 0 7 0 1 5 7 1 6 1 5 1 6 6 7 3 2 7 3\n",
      " 6 4 2 5 1 2 4 6 4 8 1 2 2 5 6 2 4 8 5 6 3 1 6 8 2 8 3 9 7 1 5 7 3 4 8 4 5\n",
      " 1 5 4 3 9 0 1 9 5 4 1 4 3 2 2 4 5 7 6 2 0 9 2 6 4 6 8 4 4 5 8 0 4 1 1 3 4\n",
      " 8 3 3 5 6 3 3 6 7 0 8 3 4 2 8 5 1 0 8 4 7 1 6 3 6 2 7 0 5 8 2 0 0 0 1 3 0\n",
      " 7 8 8 5 1 0 6 8 2 0 0 5 9 7 5 4 5 7 6 3 5 3 9 3 5 5 3]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "# gini a criterion to minimize the probability of misclassification\n",
    "clf_dt = DecisionTreeClassifier(criterion = 'gini')\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.2, shuffle=True)\n",
    "\n",
    "\n",
    "clf_dt.fit(X_train, y_train)\n",
    "predicted = clf_dt.predict(X_test)\n",
    "print(predicted)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a4c53a12-f834-426d-8217-ea98962fda64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8805555555555555"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "accuracy_score(y_test , predicted)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e2b99748-73e4-4e6a-bd3f-4a095eec4c12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 7916 characters]",
      "text/plain": [
       "<Figure size 1000x300 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10,3))\n",
    "for ax, image, prediction, t in zip(axes, X_test, predicted, y_test):\n",
    "    ax.set_axis_off()\n",
    "    image = image.reshape(8,8)\n",
    "    ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
    "    ax.set_title(f\"Prediction: {prediction}, real: {t}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1d010c44-2967-44fa-996f-c1fd733d17f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Confusion Matrix:\n",
      "[[36  0  0  0  0  1  0  0  0  0]\n",
      " [ 0 37  1  1  0  0  0  0  2  1]\n",
      " [ 0  0 21  0  0  0  0  0  2  0]\n",
      " [ 0  0  4 26  0  2  1  0  1  1]\n",
      " [ 0  1  0  0 31  0  1  0  0  0]\n",
      " [ 0  0  0  0  2 43  0  1  0  0]\n",
      " [ 1  1  0  0  4  0 40  0  0  0]\n",
      " [ 0  0  0  0  1  1  0 34  1  0]\n",
      " [ 0  3  0  0  0  1  0  0 27  2]\n",
      " [ 0  2  0  1  1  2  0  0  0 22]]\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 36008 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### confusion matrix\n",
    "\n",
    "disp = metrics.ConfusionMatrixDisplay.from_predictions(y_test, predicted, cmap=\"summer\")\n",
    "disp.figure_.suptitle(\"Confusion Matrix\")\n",
    "\n",
    "print(f\"Confusion Matrix:\\n{disp.confusion_matrix}\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8608e6eb-6dd0-4947-9ee6-dc77f35cecc7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Classification report for classifier DecisionTreeClassifier():\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.97      0.97      0.97        37\n",
      "           1       0.84      0.88      0.86        42\n",
      "           2       0.81      0.91      0.86        23\n",
      "           3       0.93      0.74      0.83        35\n",
      "           4       0.79      0.94      0.86        33\n",
      "           5       0.86      0.93      0.90        46\n",
      "           6       0.95      0.87      0.91        46\n",
      "           7       0.97      0.92      0.94        37\n",
      "           8       0.82      0.82      0.82        33\n",
      "           9       0.85      0.79      0.81        28\n",
      "\n",
      "    accuracy                           0.88       360\n",
      "   macro avg       0.88      0.88      0.88       360\n",
      "weighted avg       0.89      0.88      0.88       360\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\n",
    "    f\"Classification report for classifier {clf_dt}:\\n\"\n",
    "    f\"{metrics.classification_report(y_test, predicted)}\\n\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42fdd6a5-af00-4155-bc42-44e037d13c2d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
