{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using Decision Trees to Predict Lara's Next Battle Result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# To support both python 2 and python 3\n",
    "from __future__ import division, print_function, unicode_literals\n",
    "\n",
    "# Common imports\n",
    "import numpy as np\n",
    "import os\n",
    "# to make this notebook's output stable across runs\n",
    "np.random.seed(42)\n",
    "\n",
    "# To plot pretty figures\n",
    "%matplotlib inline\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams['axes.labelsize'] = 14\n",
    "plt.rcParams['xtick.labelsize'] = 12\n",
    "plt.rcParams['ytick.labelsize'] = 12\n",
    "\n",
    "# Where to save the figures\n",
    "PROJECT_ROOT_DIR = \".\"\n",
    "CLASSIFIER = \"decision_trees\"\n",
    "\n",
    "def image_path(fig_id):\n",
    "    return os.path.join(PROJECT_ROOT_DIR, \"images\", CLASSIFIER, fig_id)\n",
    "\n",
    "def save_fig(fig_id, tight_layout=True):\n",
    "    print(\"Saving figure\", fig_id)\n",
    "    if tight_layout:\n",
    "        plt.tight_layout()\n",
    "    plt.savefig(image_path(fig_id) + \".png\", format='png', dpi=300)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Training and visualizing\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['CharacterType', 'OpponentFitness', 'OpponentSkill', 'OpponentStrength']\n",
      "[[1 3 2 1]\n",
      " [1 3 2 2]\n",
      " [1 2 2 1]\n",
      " [1 1 2 1]\n",
      " [2 1 1 1]\n",
      " [2 1 1 2]\n",
      " [1 2 1 2]\n",
      " [3 3 2 1]\n",
      " [3 3 1 1]\n",
      " [2 1 1 1]\n",
      " [3 3 1 2]\n",
      " [2 2 2 2]\n",
      " [1 2 1 1]\n",
      " [2 1 2 2]]\n",
      "[0 0 1 1 1 0 1 0 1 1 1 1 1 0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "\n",
    "df=pd.read_csv('laras_battle_stats.csv',delimiter=',') #read labelled training data set\n",
    "mtarget =  df[\"Win\"].values #extract labelled results  -- 14 battles\n",
    "mtarget = np.array(mtarget, dtype='int')  #array of class targets results win=1, lose=0\n",
    "values = np.array(df.values,dtype='int') #array of attribute values for training data\n",
    "values = values[:,1:5] #slice-> cols 1,2,3 and 4 kept.  cols 0 and 5 excluded.\n",
    "targetNames=['lose','win']\n",
    "feature_names=list(df)\n",
    "tree_clf = DecisionTreeClassifier(max_depth=4, random_state=42)\n",
    "tree_clf.fit(values, mtarget)\n",
    "\n",
    "feature_names=feature_names[1:5]   #slice-> cols 1,2,3 and 4 kept.  cols 0 and 5 excluded.\n",
    "print(feature_names)\n",
    "print(values)\n",
    "print(mtarget)\n",
    "\n",
    "y_pred2=tree_clf.predict(values)\n",
    "y_pred2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.tree import export_graphviz\n",
    "\n",
    "export_graphviz(\n",
    "      tree_clf,\n",
    "      out_file=\"lara.dot\",\n",
    "      feature_names=feature_names,\n",
    "      class_names=targetNames,\n",
    "      rounded=True,\n",
    "      filled=True\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pydot\n",
    "\n",
    "(graph,) = pydot.graph_from_dot_file('lara.dot')\n",
    "graph.write_png('lara1.png')\n",
    "#from subprocess import check_call\n",
    "#check_call(['dot','-Tpng','lara.dot','-o','lara1.png'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
