[731764]: / HAR_LSTM.ipynb

Download this file

760 lines (759 with data), 25.5 kB

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Importing Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Activities are the class labels\n",
    "# It is a 6 class classification\n",
    "ACTIVITIES = {\n",
    "    0: 'WALKING',\n",
    "    1: 'WALKING_UPSTAIRS',\n",
    "    2: 'WALKING_DOWNSTAIRS',\n",
    "    3: 'SITTING',\n",
    "    4: 'STANDING',\n",
    "    5: 'LAYING',\n",
    "}\n",
    "\n",
    "# Utility function to print the confusion matrix\n",
    "def confusion_matrix(Y_true, Y_pred):\n",
    "    Y_true = pd.Series([ACTIVITIES[y] for y in np.argmax(Y_true, axis=1)])\n",
    "    Y_pred = pd.Series([ACTIVITIES[y] for y in np.argmax(Y_pred, axis=1)])\n",
    "\n",
    "    return pd.crosstab(Y_true, Y_pred, rownames=['True'], colnames=['Pred'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Data directory\n",
    "DATADIR = 'UCI_HAR_Dataset'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Raw data signals\n",
    "# Signals are from Accelerometer and Gyroscope\n",
    "# The signals are in x,y,z directions\n",
    "# Sensor signals are filtered to have only body acceleration\n",
    "# excluding the acceleration due to gravity\n",
    "# Triaxial acceleration from the accelerometer is total acceleration\n",
    "SIGNALS = [\n",
    "    \"body_acc_x\",\n",
    "    \"body_acc_y\",\n",
    "    \"body_acc_z\",\n",
    "    \"body_gyro_x\",\n",
    "    \"body_gyro_y\",\n",
    "    \"body_gyro_z\",\n",
    "    \"total_acc_x\",\n",
    "    \"total_acc_y\",\n",
    "    \"total_acc_z\"\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Utility function to read the data from csv file\n",
    "def _read_csv(filename):\n",
    "    return pd.read_csv(filename, delim_whitespace=True, header=None)\n",
    "\n",
    "# Utility function to load the load\n",
    "def load_signals(subset):\n",
    "    signals_data = []\n",
    "\n",
    "    for signal in SIGNALS:\n",
    "        filename = f'UCI_HAR_Dataset/{subset}/Inertial Signals/{signal}_{subset}.txt'\n",
    "        signals_data.append(\n",
    "            _read_csv(filename).as_matrix()\n",
    "        ) \n",
    "\n",
    "    # Transpose is used to change the dimensionality of the output,\n",
    "    # aggregating the signals by combination of sample/timestep.\n",
    "    # Resultant shape is (7352 train/2947 test samples, 128 timesteps, 9 signals)\n",
    "    return np.transpose(signals_data, (1, 2, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def load_y(subset):\n",
    "    \"\"\"\n",
    "    The objective that we are trying to predict is a integer, from 1 to 6,\n",
    "    that represents a human activity. We return a binary representation of \n",
    "    every sample objective as a 6 bits vector using One Hot Encoding\n",
    "    (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html)\n",
    "    \"\"\"\n",
    "    filename = f'UCI_HAR_Dataset/{subset}/y_{subset}.txt'\n",
    "    y = _read_csv(filename)[0]\n",
    "\n",
    "    return pd.get_dummies(y).as_matrix()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data():\n",
    "    \"\"\"\n",
    "    Obtain the dataset from multiple files.\n",
    "    Returns: X_train, X_test, y_train, y_test\n",
    "    \"\"\"\n",
    "    X_train, X_test = load_signals('train'), load_signals('test')\n",
    "    y_train, y_test = load_y('train'), load_y('test')\n",
    "\n",
    "    return X_train, X_test, y_train, y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Importing tensorflow\n",
    "np.random.seed(42)\n",
    "import tensorflow as tf\n",
    "tf.set_random_seed(42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Configuring a session\n",
    "session_conf = tf.ConfigProto(\n",
    "    intra_op_parallelism_threads=1,\n",
    "    inter_op_parallelism_threads=1\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/prajin/Downloads/ENTER/envs/py36/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n",
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "# Import Keras\n",
    "from keras import backend as K\n",
    "sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)\n",
    "K.set_session(sess)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Importing libraries\n",
    "from keras.models import Sequential\n",
    "from keras.layers import LSTM\n",
    "from keras.layers.core import Dense, Dropout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initializing parameters\n",
    "epochs = 30\n",
    "batch_size = 16\n",
    "n_hidden = 32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Utility function to count the number of classes\n",
    "def _count_classes(y):\n",
    "    return len(set([tuple(category) for category in y]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/prajin/Downloads/ENTER/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:12: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.\n",
      "  if sys.path[0] == '':\n"
     ]
    }
   ],
   "source": [
    "# Loading the train and test data\n",
    "X_train, X_test, Y_train, Y_test = load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "128\n",
      "9\n",
      "7352\n"
     ]
    }
   ],
   "source": [
    "timesteps = len(X_train[0])\n",
    "input_dim = len(X_train[0][0])\n",
    "n_classes = _count_classes(Y_train)\n",
    "\n",
    "print(timesteps)\n",
    "print(input_dim)\n",
    "print(len(X_train))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7352, 128, 9)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7352, 6)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2947, 128, 9)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1.165315e-02, -2.939904e-02,  1.068262e-01, ...,\n",
       "          1.041216e+00, -2.697959e-01,  2.377977e-02],\n",
       "        [ 1.310909e-02, -3.972867e-02,  1.524549e-01, ...,\n",
       "          1.041803e+00, -2.800250e-01,  7.629271e-02],\n",
       "        [ 1.126885e-02, -5.240586e-02,  2.168462e-01, ...,\n",
       "          1.039086e+00, -2.926631e-01,  1.474754e-01],\n",
       "        ...,\n",
       "        [ 1.291511e-03,  1.173502e-02,  3.665587e-03, ...,\n",
       "          9.930164e-01, -2.599865e-01,  1.443951e-01],\n",
       "        [ 1.469997e-03,  9.517414e-03,  4.041945e-03, ...,\n",
       "          9.932414e-01, -2.620643e-01,  1.447033e-01],\n",
       "        [ 2.573841e-03,  7.305069e-03,  4.888436e-03, ...,\n",
       "          9.943906e-01, -2.641348e-01,  1.454939e-01]],\n",
       "\n",
       "       [[ 9.279629e-03,  6.650520e-03, -2.631933e-02, ...,\n",
       "          9.991921e-01, -2.649349e-01,  1.256164e-01],\n",
       "        [ 4.929711e-03,  1.864973e-02, -2.688753e-02, ...,\n",
       "          9.946787e-01, -2.532142e-01,  1.256249e-01],\n",
       "        [ 3.953596e-03,  1.553950e-02, -3.663861e-02, ...,\n",
       "          9.935518e-01, -2.565887e-01,  1.163814e-01],\n",
       "        ...,\n",
       "        [ 7.787600e-03,  4.730625e-03,  1.412899e-02, ...,\n",
       "          1.001861e+00, -2.619359e-01,  1.527878e-01],\n",
       "        [ 3.433489e-03, -4.619849e-03,  1.338054e-03, ...,\n",
       "          9.975208e-01, -2.713225e-01,  1.398428e-01],\n",
       "        [-1.238678e-03, -1.322889e-02, -1.703861e-02, ...,\n",
       "          9.928615e-01, -2.799715e-01,  1.213135e-01]],\n",
       "\n",
       "       [[ 5.731945e-03,  7.304842e-03,  1.021286e-02, ...,\n",
       "          9.975931e-01, -2.639912e-01,  1.507741e-01],\n",
       "        [ 7.065650e-03,  7.330912e-03,  1.341419e-02, ...,\n",
       "          9.989703e-01, -2.638194e-01,  1.539427e-01],\n",
       "        [ 5.109758e-03,  7.153458e-03,  3.646559e-03, ...,\n",
       "          9.970574e-01, -2.638495e-01,  1.441536e-01],\n",
       "        ...,\n",
       "        [-7.428461e-04, -9.629137e-03, -2.500924e-03, ...,\n",
       "          9.918802e-01, -2.836712e-01,  1.326780e-01],\n",
       "        [-1.923356e-03, -6.425974e-03, -2.524952e-03, ...,\n",
       "          9.906626e-01, -2.805970e-01,  1.326941e-01],\n",
       "        [-4.304617e-03, -7.932046e-03, -3.140111e-03, ...,\n",
       "          9.882446e-01, -2.822329e-01,  1.321175e-01]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[-1.476465e-01,  5.519791e-03,  1.025031e-02, ...,\n",
       "          8.213505e-01, -2.484623e-01, -2.216934e-01],\n",
       "        [-1.699026e-01,  3.235187e-02,  2.632373e-02, ...,\n",
       "          7.991996e-01, -2.232599e-01, -2.045561e-01],\n",
       "        [-1.686980e-01,  7.826144e-02, -2.703439e-02, ...,\n",
       "          8.004623e-01, -1.790170e-01, -2.568719e-01],\n",
       "        ...,\n",
       "        [ 4.978930e-01, -3.158365e-01, -2.321939e-02, ...,\n",
       "          1.463170e+00, -5.515283e-01, -2.723974e-01],\n",
       "        [ 2.141275e-01, -3.121422e-01,  1.814949e-01, ...,\n",
       "          1.179223e+00, -5.472997e-01, -6.773376e-02],\n",
       "        [-1.145089e-01, -2.553472e-01,  3.870347e-01, ...,\n",
       "          8.504963e-01, -4.900368e-01,  1.378256e-01]],\n",
       "\n",
       "       [[ 7.122683e-02, -1.498122e-01, -1.659306e-01, ...,\n",
       "          1.037668e+00, -3.971532e-01, -3.940817e-01],\n",
       "        [-8.866530e-02, -3.755543e-02, -8.708159e-02, ...,\n",
       "          8.780725e-01, -2.848634e-01, -3.151097e-01],\n",
       "        [-7.067473e-02, -1.615178e-02,  1.401189e-02, ...,\n",
       "          8.963897e-01, -2.635297e-01, -2.139040e-01],\n",
       "        ...,\n",
       "        [ 1.859878e-01,  7.344366e-03,  2.383924e-01, ...,\n",
       "          1.156389e+00, -2.283478e-01, -3.512052e-03],\n",
       "        [ 2.737114e-01, -2.279012e-02,  1.302276e-01, ...,\n",
       "          1.243857e+00, -2.583220e-01, -1.117857e-01],\n",
       "        [ 3.536738e-01, -1.118625e-01, -3.402252e-02, ...,\n",
       "          1.323546e+00, -3.472416e-01, -2.760682e-01]],\n",
       "\n",
       "       [[-1.936425e-01, -1.907511e-01,  1.958357e-01, ...,\n",
       "          7.713622e-01, -4.250499e-01, -5.327655e-02],\n",
       "        [-6.498738e-02, -2.035990e-01, -1.531400e-01, ...,\n",
       "          9.000949e-01, -4.375916e-01, -4.020727e-01],\n",
       "        [-9.712210e-02, -2.083832e-01, -2.710627e-01, ...,\n",
       "          8.681034e-01, -4.421595e-01, -5.197379e-01],\n",
       "        ...,\n",
       "        [-5.075521e-02, -1.047171e-01,  1.732707e-01, ...,\n",
       "          9.188616e-01, -3.516799e-01, -7.253919e-02],\n",
       "        [-1.980675e-02, -2.076396e-02,  1.956384e-01, ...,\n",
       "          9.494752e-01, -2.675260e-01, -5.097549e-02],\n",
       "        [-1.104015e-02,  5.243883e-02,  2.184321e-01, ...,\n",
       "          9.578348e-01, -1.941603e-01, -2.892477e-02]]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2947, 6)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y_test.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Defining the Architecture of LSTM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "lstm_2 (LSTM)                (None, 32)                5376      \n",
      "_________________________________________________________________\n",
      "dropout_2 (Dropout)          (None, 32)                0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 6)                 198       \n",
      "=================================================================\n",
      "Total params: 5,574\n",
      "Trainable params: 5,574\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# Initiliazing the sequential model\n",
    "\n",
    "model = Sequential()\n",
    "# Configuring the parameters\n",
    "model.add(LSTM(n_hidden, input_shape=(timesteps, input_dim)))\n",
    "# Adding a dropout layer\n",
    "model.add(Dropout(0.5))\n",
    "# Adding a dense output layer with sigmoid activation\n",
    "model.add(Dense(n_classes, activation='sigmoid'))\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compiling the model\n",
    "model.compile(loss='categorical_crossentropy',\n",
    "              optimizer='rmsprop',\n",
    "              metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 7352 samples, validate on 2947 samples\n",
      "Epoch 1/30\n",
      "7352/7352 [==============================] - 30s 4ms/step - loss: 1.3992 - acc: 0.3528 - val_loss: 1.3149 - val_acc: 0.3485\n",
      "Epoch 2/30\n",
      "7352/7352 [==============================] - 29s 4ms/step - loss: 1.1923 - acc: 0.4475 - val_loss: 1.1875 - val_acc: 0.4523\n",
      "Epoch 3/30\n",
      "7352/7352 [==============================] - 27s 4ms/step - loss: 1.0586 - acc: 0.4977 - val_loss: 1.1083 - val_acc: 0.5124\n",
      "Epoch 4/30\n",
      "7352/7352 [==============================] - 27s 4ms/step - loss: 0.9001 - acc: 0.6019 - val_loss: 0.9712 - val_acc: 0.5898\n",
      "Epoch 5/30\n",
      "7352/7352 [==============================] - 27s 4ms/step - loss: 0.8077 - acc: 0.6205 - val_loss: 0.8670 - val_acc: 0.5769\n",
      "Epoch 6/30\n",
      "7352/7352 [==============================] - 29s 4ms/step - loss: 0.7221 - acc: 0.6443 - val_loss: 0.7999 - val_acc: 0.6108\n",
      "Epoch 7/30\n",
      "7352/7352 [==============================] - 34s 5ms/step - loss: 0.7032 - acc: 0.6481 - val_loss: 0.8130 - val_acc: 0.6067\n",
      "Epoch 8/30\n",
      "7352/7352 [==============================] - 37s 5ms/step - loss: 0.6789 - acc: 0.6590 - val_loss: 0.7781 - val_acc: 0.6118\n",
      "Epoch 9/30\n",
      "7352/7352 [==============================] - 38s 5ms/step - loss: 0.6733 - acc: 0.6549 - val_loss: 0.8595 - val_acc: 0.6033\n",
      "Epoch 10/30\n",
      "7352/7352 [==============================] - 38s 5ms/step - loss: 0.6385 - acc: 0.6714 - val_loss: 0.8202 - val_acc: 0.6043\n",
      "Epoch 11/30\n",
      "7352/7352 [==============================] - 37s 5ms/step - loss: 0.5983 - acc: 0.6918 - val_loss: 0.7822 - val_acc: 0.6586\n",
      "Epoch 12/30\n",
      "7352/7352 [==============================] - 28s 4ms/step - loss: 0.5781 - acc: 0.7304 - val_loss: 0.7093 - val_acc: 0.7503\n",
      "Epoch 13/30\n",
      "7352/7352 [==============================] - 23s 3ms/step - loss: 0.5395 - acc: 0.7752 - val_loss: 0.6877 - val_acc: 0.7503\n",
      "Epoch 14/30\n",
      "7352/7352 [==============================] - 22s 3ms/step - loss: 0.5074 - acc: 0.7888 - val_loss: 0.5969 - val_acc: 0.7621\n",
      "Epoch 15/30\n",
      "7352/7352 [==============================] - 24s 3ms/step - loss: 0.4639 - acc: 0.7983 - val_loss: 0.6399 - val_acc: 0.7574\n",
      "Epoch 16/30\n",
      "7352/7352 [==============================] - 27s 4ms/step - loss: 0.4533 - acc: 0.8041 - val_loss: 0.5525 - val_acc: 0.7625\n",
      "Epoch 17/30\n",
      "7352/7352 [==============================] - 25s 3ms/step - loss: 0.4612 - acc: 0.8166 - val_loss: 0.5325 - val_acc: 0.7679\n",
      "Epoch 18/30\n",
      "7352/7352 [==============================] - 27s 4ms/step - loss: 0.3810 - acc: 0.8595 - val_loss: 0.5302 - val_acc: 0.8385\n",
      "Epoch 19/30\n",
      "7352/7352 [==============================] - 24s 3ms/step - loss: 0.3549 - acc: 0.8924 - val_loss: 0.7042 - val_acc: 0.8246\n",
      "Epoch 20/30\n",
      "7352/7352 [==============================] - 25s 3ms/step - loss: 0.3123 - acc: 0.9124 - val_loss: 0.5711 - val_acc: 0.8456\n",
      "Epoch 21/30\n",
      "7352/7352 [==============================] - 35s 5ms/step - loss: 0.2819 - acc: 0.9136 - val_loss: 0.5149 - val_acc: 0.8636\n",
      "Epoch 22/30\n",
      "7352/7352 [==============================] - 55s 7ms/step - loss: 0.2355 - acc: 0.9249 - val_loss: 0.5110 - val_acc: 0.8646\n",
      "Epoch 23/30\n",
      "7352/7352 [==============================] - 43s 6ms/step - loss: 0.2248 - acc: 0.9290 - val_loss: 0.6960 - val_acc: 0.8524\n",
      "Epoch 24/30\n",
      "7352/7352 [==============================] - 32s 4ms/step - loss: 0.2245 - acc: 0.9314 - val_loss: 0.6003 - val_acc: 0.8687\n",
      "Epoch 25/30\n",
      "7352/7352 [==============================] - 31s 4ms/step - loss: 0.2142 - acc: 0.9312 - val_loss: 0.4520 - val_acc: 0.8809\n",
      "Epoch 26/30\n",
      "7352/7352 [==============================] - 63s 9ms/step - loss: 0.2139 - acc: 0.9340 - val_loss: 0.4768 - val_acc: 0.8643\n",
      "Epoch 27/30\n",
      "7352/7352 [==============================] - 38s 5ms/step - loss: 0.2048 - acc: 0.9316 - val_loss: 0.4726 - val_acc: 0.8795\n",
      "Epoch 28/30\n",
      "7352/7352 [==============================] - 34s 5ms/step - loss: 0.1946 - acc: 0.9369 - val_loss: 0.4605 - val_acc: 0.8765\n",
      "Epoch 29/30\n",
      "7352/7352 [==============================] - 63s 9ms/step - loss: 0.2185 - acc: 0.9327 - val_loss: 0.4615 - val_acc: 0.8768\n",
      "Epoch 30/30\n",
      "7352/7352 [==============================] - 76s 10ms/step - loss: 0.1809 - acc: 0.9374 - val_loss: 0.4475 - val_acc: 0.8843\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7fd101658ef0>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Training the model\n",
    "model.fit(X_train,\n",
    "          Y_train,\n",
    "          batch_size=batch_size,\n",
    "          validation_data=(X_test, Y_test),\n",
    "          epochs=epochs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2947/2947 [==============================] - 2s 821us/step\n"
     ]
    }
   ],
   "source": [
    "score = model.evaluate(X_test, Y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6.73119284e-05, 1.16691635e-05, 6.29200213e-06, 3.27186123e-03,\n",
       "        3.09266567e-01, 6.14459668e-06],\n",
       "       [6.55044132e-05, 2.05861852e-05, 1.43757034e-05, 6.77454285e-03,\n",
       "        4.01470065e-01, 6.11893711e-06],\n",
       "       [6.84985353e-05, 1.96996807e-05, 1.35612627e-05, 6.61419239e-03,\n",
       "        4.27904457e-01, 6.24169252e-06],\n",
       "       ...,\n",
       "       [1.55011995e-03, 7.93270528e-01, 3.01599008e-04, 2.30963960e-05,\n",
       "        5.54955914e-05, 1.02767759e-08],\n",
       "       [4.90763341e-04, 3.85723859e-01, 1.03853172e-05, 6.35696642e-06,\n",
       "        2.14066167e-05, 1.14835030e-08],\n",
       "       [7.23787583e-04, 6.95120990e-01, 2.18840923e-05, 8.08145796e-06,\n",
       "        6.29514252e-05, 5.91321019e-08]], dtype=float32)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " model.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pred                LAYING  SITTING  STANDING  WALKING  WALKING_DOWNSTAIRS  \\\n",
      "True                                                                         \n",
      "LAYING                 510        0        27        0                   0   \n",
      "SITTING                  0      375       110        3                   0   \n",
      "STANDING                 0       80       446        2                   0   \n",
      "WALKING                  0        0         0      410                  27   \n",
      "WALKING_DOWNSTAIRS       0        0         0        2                 407   \n",
      "WALKING_UPSTAIRS         0        0         0        3                  10   \n",
      "\n",
      "Pred                WALKING_UPSTAIRS  \n",
      "True                                  \n",
      "LAYING                             0  \n",
      "SITTING                            3  \n",
      "STANDING                           4  \n",
      "WALKING                           59  \n",
      "WALKING_DOWNSTAIRS                11  \n",
      "WALKING_UPSTAIRS                 458  \n"
     ]
    }
   ],
   "source": [
    "# Confusion Matrix\n",
    "print(confusion_matrix(Y_test, model.predict(X_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.44746464555687265, 0.8842891075670173]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- With a simple 2 layer architecture we got 90.09% accuracy and a loss of 0.30\n",
    "- We can further imporve the performace with Hyperparameter tuning"
   ]
  }
 ],
 "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.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}