{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dfae8849",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from keras.datasets import mnist\n",
    "\n",
    "# 손글씨 이미지 데이터셋 784 x 1짜리 벡터로 변환 = 28 x 28 pixel로 이루어진 손글씨 이미지\n",
    "((x_train, y_train), (x_test, y_test)) = mnist.load_data()\n",
    "\n",
    "X_train = x_train.reshape(-1,28*28)\n",
    "X_test = x_test.reshape(-1,28*28)\n",
    "\n",
    "N_train,D = X_train.shape\n",
    "N_test,D = X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7c23cfd0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train 0.9899, test 0.9792\n",
      "train 0.6691, test 0.6740\n"
     ]
    }
   ],
   "source": [
    "from sklearn import tree\n",
    "from sklearn import svm\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# svm = classification\n",
    "model_svm = svm.SVC()\n",
    "model_svm.fit(X_train,y_train)\n",
    "\n",
    "y_svm_train = model_svm.predict(X_train)\n",
    "y_svm_test = model_svm.predict(X_test)\n",
    "\n",
    "print( 'train %.4f, test %.4f' % (accuracy_score(y_svm_train, y_train), accuracy_score(y_svm_test, y_test)) )\n",
    "\n",
    "# decision tree = classification\n",
    "model_tree = tree.DecisionTreeClassifier(max_depth = 10, min_samples_split = 1000, min_samples_leaf = 1000)\n",
    "model_tree.fit(X_train,y_train)\n",
    "\n",
    "y_tree_train = model_tree.predict(X_train)\n",
    "y_tree_test = model_tree.predict(X_test)\n",
    "\n",
    "print( 'train %.4f, test %.4f' % (accuracy_score(y_tree_train, y_train), accuracy_score(y_tree_test, y_test)) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "744027d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1199364 characters]\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# decision tree를 시각화\n",
    "from sklearn.tree import export_graphviz\n",
    "import pydotplus\n",
    "from IPython.display import Image\n",
    "\n",
    "dot_data = export_graphviz(model_tree, out_file=None, filled=True, rounded=True, special_characters=True)\n",
    "graph = pydotplus.graph_from_dot_data(dot_data)\n",
    "Image(graph.create_png())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8c29b819",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ensemble 1: train 0.5501, test 0.5484\n",
      "ensemble 2: train 0.6408, test 0.6538\n",
      "ensemble 5: train 0.7357, test 0.7333\n",
      "ensemble 10: train 0.8064, test 0.8159\n",
      "ensemble 20: train 0.8186, test 0.8218\n",
      "ensemble 50: train 0.8394, test 0.8453\n",
      "ensemble 100: train 0.8484, test 0.8512\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1f9f154dbb0>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 24324 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# random forest = classification\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "numlist_ensemble = np.array([1, 2, 5, 10, 20, 50, 100])\n",
    "accuracy_train = []\n",
    "accuracy_test = []\n",
    "\n",
    "for num_ensemble in numlist_ensemble:\n",
    "    model_rdf = RandomForestClassifier(n_estimators = num_ensemble, max_depth = 10, min_samples_split = 1000, min_samples_leaf = 1000)\n",
    "    model_rdf.fit(X_train,y_train)\n",
    "\n",
    "    y_rdf_train = model_rdf.predict(X_train)\n",
    "    y_rdf_test = model_rdf.predict(X_test)\n",
    "\n",
    "    print( 'ensemble %d: train %.4f, test %.4f' % (num_ensemble, accuracy_score(y_rdf_train, y_train), accuracy_score(y_rdf_test, y_test)) )\n",
    "    \n",
    "    accuracy_train.append(accuracy_score(y_rdf_train, y_train))\n",
    "    accuracy_test.append(accuracy_score(y_rdf_test, y_test))\n",
    "    \n",
    "plt.figure()\n",
    "plt.plot(numlist_ensemble, accuracy_train, 'o-')\n",
    "plt.plot(numlist_ensemble ,accuracy_test, 'o-')"
   ]
  }
 ],
 "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.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
