{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU",
    "gpuClass": "standard"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Applying LSTM Models on Raw Data"
      ],
      "metadata": {
        "id": "jUkGXVGfU1xN"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "7USnX2QTSuKt"
      },
      "outputs": [],
      "source": [
        "# Importing Libraries\n",
        "\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "\n",
        "# Import Keras\n",
        "from keras import backend as K\n",
        "from keras.models import Sequential\n",
        "from keras.layers import LSTM\n",
        "from keras.layers.core import Dense, Dropout\n",
        "from keras.layers import BatchNormalization\n",
        "from keras.regularizers import L1L2"
      ]
    },
    {
      "cell_type": "code",
      "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",
        "}"
      ],
      "metadata": {
        "id": "UklmP7-eU9Wm"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "\n",
        "# function to print the confusion matrix\n",
        "\n",
        "def confusion_matrix(Y_true, Y_pred):\n",
        "    \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'])\n",
        "\n",
        "    \n",
        "    result = confusion_matrix(Y_true, Y_pred)\n",
        "\n",
        "    plt.figure(figsize=(10, 8))\n",
        "    sns.heatmap(result, \n",
        "                xticklabels= list(ACTIVITIES.values()), \n",
        "                yticklabels=list(ACTIVITIES.values()), \n",
        "                annot=True, fmt=\"d\");\n",
        "    plt.title(\"Confusion matrix\")\n",
        "    plt.ylabel('True label')\n",
        "    plt.xlabel('Predicted label')\n",
        "    plt.show() "
      ],
      "metadata": {
        "id": "cG79tQGXVASE"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Loading Data"
      ],
      "metadata": {
        "id": "dfbEhMvGVG4K"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Data directory\n",
        "DATADIR = 'UCI_HAR_Dataset'\n",
        "\n",
        "# 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",
        "    ]"
      ],
      "metadata": {
        "id": "W6E6gq2tVJjl"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# define a 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",
        "# function to load the load\n",
        "def load_signals(subset):\n",
        "    signals_data = []\n",
        "\n",
        "    for signal in SIGNALS:\n",
        "        filename = f'/content/drive/MyDrive/UCI_HAR_Dataset/{subset}/Inertial Signals/{signal}_{subset}.txt'\n",
        "        signals_data.append(\n",
        "            _read_csv(filename).to_numpy()\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))"
      ],
      "metadata": {
        "id": "Gbp0kyOLVO13"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "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'/content/drive/MyDrive/UCI_HAR_Dataset/{subset}/y_{subset}.txt'\n",
        "    y = _read_csv(filename)[0]\n",
        "\n",
        "    return pd.get_dummies(y).to_numpy()"
      ],
      "metadata": {
        "id": "MOXrPORRVRcJ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "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"
      ],
      "metadata": {
        "id": "MEf5hg9lVTun"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Importing tensorflow\n",
        "np.random.seed(42)\n",
        "import tensorflow as tf\n",
        "tf.random.set_seed(42)"
      ],
      "metadata": {
        "id": "Edexn_grVWug"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Initializing parameters\n",
        "epochs = 30\n",
        "batch_size = 16\n",
        "n_hidden = 32"
      ],
      "metadata": {
        "id": "B4dT-A4bVapl"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#function to count the number of classes\n",
        "def _count_classes(y):\n",
        "    return len(set([tuple(category) for category in y]))"
      ],
      "metadata": {
        "id": "5D6pcPuVVbyl"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Loading the train and test data\n",
        "X_train, X_test, y_train, y_test = load_data()"
      ],
      "metadata": {
        "id": "VUoSMvSfVga3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "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))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MXby1ubyVjBV",
        "outputId": "8f44a692-57c1-4df3-b11f-251c4979e19e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "128\n",
            "9\n",
            "7352\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        " 1. Defining the Architecture of 1-Layer of LSTM"
      ],
      "metadata": {
        "id": "m182sLnyVl0K"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Initiliazing the sequential model\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()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BGD2Lt3MVnK5",
        "outputId": "f7ece6dd-85c8-4d41-a713-540bea33ddf0"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Model: \"sequential_3\"\n",
            "_________________________________________________________________\n",
            " Layer (type)                Output Shape              Param #   \n",
            "=================================================================\n",
            " lstm_5 (LSTM)               (None, 32)                5376      \n",
            "                                                                 \n",
            " dropout_5 (Dropout)         (None, 32)                0         \n",
            "                                                                 \n",
            " dense_3 (Dense)             (None, 6)                 198       \n",
            "                                                                 \n",
            "=================================================================\n",
            "Total params: 5,574\n",
            "Trainable params: 5,574\n",
            "Non-trainable params: 0\n",
            "_________________________________________________________________\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Compiling the model\n",
        "model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])"
      ],
      "metadata": {
        "id": "MIX8hyRoVrSs"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Training the model\n",
        "model.fit(X_train, y_train, batch_size=batch_size, validation_data=(X_test, y_test),epochs=epochs)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RxoV_8fdVt7u",
        "outputId": "fc88555b-3bcc-47b6-f10e-1aa6d752887e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Epoch 1/30\n",
            "460/460 [==============================] - 8s 11ms/step - loss: 1.0781 - accuracy: 0.5423 - val_loss: 0.9016 - val_accuracy: 0.6213\n",
            "Epoch 2/30\n",
            "460/460 [==============================] - 4s 10ms/step - loss: 0.7431 - accuracy: 0.6632 - val_loss: 0.6547 - val_accuracy: 0.7255\n",
            "Epoch 3/30\n",
            "460/460 [==============================] - 5s 12ms/step - loss: 0.5874 - accuracy: 0.7629 - val_loss: 0.5316 - val_accuracy: 0.7906\n",
            "Epoch 4/30\n",
            "460/460 [==============================] - 4s 8ms/step - loss: 0.4666 - accuracy: 0.8320 - val_loss: 1.4815 - val_accuracy: 0.6155\n",
            "Epoch 5/30\n",
            "460/460 [==============================] - 4s 8ms/step - loss: 0.3551 - accuracy: 0.8863 - val_loss: 0.4364 - val_accuracy: 0.8483\n",
            "Epoch 6/30\n",
            "460/460 [==============================] - 6s 12ms/step - loss: 0.2936 - accuracy: 0.9067 - val_loss: 0.3727 - val_accuracy: 0.8765\n",
            "Epoch 7/30\n",
            "460/460 [==============================] - 4s 8ms/step - loss: 0.2671 - accuracy: 0.9174 - val_loss: 0.4289 - val_accuracy: 0.8639\n",
            "Epoch 8/30\n",
            "460/460 [==============================] - 4s 8ms/step - loss: 0.2423 - accuracy: 0.9266 - val_loss: 0.3437 - val_accuracy: 0.8711\n",
            "Epoch 9/30\n",
            "460/460 [==============================] - 5s 11ms/step - loss: 0.2158 - accuracy: 0.9293 - val_loss: 0.3924 - val_accuracy: 0.8826\n",
            "Epoch 10/30\n",
            "460/460 [==============================] - 4s 10ms/step - loss: 0.2087 - accuracy: 0.9343 - val_loss: 0.3207 - val_accuracy: 0.8856\n",
            "Epoch 11/30\n",
            "460/460 [==============================] - 4s 10ms/step - loss: 0.1990 - accuracy: 0.9344 - val_loss: 0.3968 - val_accuracy: 0.8704\n",
            "Epoch 12/30\n",
            "460/460 [==============================] - 5s 11ms/step - loss: 0.1726 - accuracy: 0.9376 - val_loss: 0.2942 - val_accuracy: 0.8931\n",
            "Epoch 13/30\n",
            "460/460 [==============================] - 4s 9ms/step - loss: 0.1755 - accuracy: 0.9414 - val_loss: 0.2492 - val_accuracy: 0.9002\n",
            "Epoch 14/30\n",
            "460/460 [==============================] - 4s 10ms/step - loss: 0.1585 - accuracy: 0.9465 - val_loss: 0.3617 - val_accuracy: 0.8904\n",
            "Epoch 15/30\n",
            "460/460 [==============================] - 5s 11ms/step - loss: 0.1629 - accuracy: 0.9468 - val_loss: 0.5414 - val_accuracy: 0.8724\n",
            "Epoch 16/30\n",
            "460/460 [==============================] - 4s 8ms/step - loss: 0.1647 - accuracy: 0.9479 - val_loss: 0.3329 - val_accuracy: 0.8979\n",
            "Epoch 17/30\n",
            "460/460 [==============================] - 4s 8ms/step - loss: 0.1531 - accuracy: 0.9486 - val_loss: 0.3276 - val_accuracy: 0.9043\n",
            "Epoch 18/30\n",
            "460/460 [==============================] - 4s 10ms/step - loss: 0.1623 - accuracy: 0.9459 - val_loss: 0.2336 - val_accuracy: 0.9125\n",
            "Epoch 19/30\n",
            "460/460 [==============================] - 4s 9ms/step - loss: 0.1494 - accuracy: 0.9489 - val_loss: 0.3656 - val_accuracy: 0.9013\n",
            "Epoch 20/30\n",
            "460/460 [==============================] - 4s 8ms/step - loss: 0.1543 - accuracy: 0.9480 - val_loss: 0.3468 - val_accuracy: 0.9016\n",
            "Epoch 21/30\n",
            "460/460 [==============================] - 4s 9ms/step - loss: 0.1398 - accuracy: 0.9490 - val_loss: 0.3211 - val_accuracy: 0.9057\n",
            "Epoch 22/30\n",
            "460/460 [==============================] - 5s 10ms/step - loss: 0.1520 - accuracy: 0.9479 - val_loss: 0.2789 - val_accuracy: 0.9067\n",
            "Epoch 23/30\n",
            "460/460 [==============================] - 4s 8ms/step - loss: 0.1479 - accuracy: 0.9475 - val_loss: 0.2733 - val_accuracy: 0.9074\n",
            "Epoch 24/30\n",
            "460/460 [==============================] - 4s 10ms/step - loss: 0.1425 - accuracy: 0.9493 - val_loss: 0.3381 - val_accuracy: 0.9036\n",
            "Epoch 25/30\n",
            "460/460 [==============================] - 5s 12ms/step - loss: 0.1437 - accuracy: 0.9504 - val_loss: 0.3459 - val_accuracy: 0.8921\n",
            "Epoch 26/30\n",
            "460/460 [==============================] - 4s 8ms/step - loss: 0.1390 - accuracy: 0.9497 - val_loss: 0.3192 - val_accuracy: 0.9074\n",
            "Epoch 27/30\n",
            "460/460 [==============================] - 4s 10ms/step - loss: 0.1361 - accuracy: 0.9520 - val_loss: 0.3324 - val_accuracy: 0.9189\n",
            "Epoch 28/30\n",
            "460/460 [==============================] - 6s 12ms/step - loss: 0.1469 - accuracy: 0.9509 - val_loss: 0.3501 - val_accuracy: 0.9097\n",
            "Epoch 29/30\n",
            "460/460 [==============================] - 5s 10ms/step - loss: 0.1439 - accuracy: 0.9438 - val_loss: 0.2556 - val_accuracy: 0.9308\n",
            "Epoch 30/30\n",
            "460/460 [==============================] - 4s 10ms/step - loss: 0.1434 - accuracy: 0.9486 - val_loss: 0.3141 - val_accuracy: 0.9165\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<keras.callbacks.History at 0x7f78fa564e80>"
            ]
          },
          "metadata": {},
          "execution_count": 52
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Confusion Matrix\n",
        "confusion_matrix(y_test, model.predict(X_test))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 286
        },
        "id": "Pl6Y6R3zVwzE",
        "outputId": "d50b38f5-1af7-4cd8-c5f5-771190191675"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "93/93 [==============================] - 1s 4ms/step\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Pred                LAYING  SITTING  STANDING  WALKING  WALKING_DOWNSTAIRS  \\\n",
              "True                                                                         \n",
              "LAYING                 511        0        26        0                   0   \n",
              "SITTING                  1      409        76        4                   0   \n",
              "STANDING                 0       95       433        4                   0   \n",
              "WALKING                  0        0         0      480                  15   \n",
              "WALKING_DOWNSTAIRS       0        0         0        0                 419   \n",
              "WALKING_UPSTAIRS         0        1         0       12                   9   \n",
              "\n",
              "Pred                WALKING_UPSTAIRS  \n",
              "True                                  \n",
              "LAYING                             0  \n",
              "SITTING                            1  \n",
              "STANDING                           0  \n",
              "WALKING                            1  \n",
              "WALKING_DOWNSTAIRS                 1  \n",
              "WALKING_UPSTAIRS                 449  "
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-ebce1837-967c-4ffc-872b-b24d45f84c89\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th>Pred</th>\n",
              "      <th>LAYING</th>\n",
              "      <th>SITTING</th>\n",
              "      <th>STANDING</th>\n",
              "      <th>WALKING</th>\n",
              "      <th>WALKING_DOWNSTAIRS</th>\n",
              "      <th>WALKING_UPSTAIRS</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>True</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>LAYING</th>\n",
              "      <td>511</td>\n",
              "      <td>0</td>\n",
              "      <td>26</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>SITTING</th>\n",
              "      <td>1</td>\n",
              "      <td>409</td>\n",
              "      <td>76</td>\n",
              "      <td>4</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>STANDING</th>\n",
              "      <td>0</td>\n",
              "      <td>95</td>\n",
              "      <td>433</td>\n",
              "      <td>4</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>WALKING</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>480</td>\n",
              "      <td>15</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>WALKING_DOWNSTAIRS</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>419</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>WALKING_UPSTAIRS</th>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>0</td>\n",
              "      <td>12</td>\n",
              "      <td>9</td>\n",
              "      <td>449</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-ebce1837-967c-4ffc-872b-b24d45f84c89')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-ebce1837-967c-4ffc-872b-b24d45f84c89 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-ebce1837-967c-4ffc-872b-b24d45f84c89');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 53
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "score = model.evaluate(X_test, y_test)\n",
        "\n",
        "print(\"\\n   categorical_crossentropy  ||   accuracy \")\n",
        "print(\"  ____________________________________\")\n",
        "print(score)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "hwFZ64M7V0dN",
        "outputId": "7d42242a-fe62-4172-ca6b-f328c8e9412a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "93/93 [==============================] - 0s 5ms/step - loss: 0.3141 - accuracy: 0.9165\n",
            "\n",
            "   categorical_crossentropy  ||   accuracy \n",
            "  ____________________________________\n",
            "[0.3141055107116699, 0.9165253043174744]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "   2. Defining the Architecture of 2-Layer of LSTM with more hyperparameter tunning"
      ],
      "metadata": {
        "id": "EFsp0FcXV9HG"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### 2.1 First Model for 2-Layer of LSTM with more hyperparameter tunning"
      ],
      "metadata": {
        "id": "8p4j41aSV_Uv"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Initializing parameters\n",
        "n_epochs = 30\n",
        "n_batch = 16\n",
        "n_classes = _count_classes(y_train)\n",
        "\n",
        "# Bias regularizer value - we will use elasticnet\n",
        "reg = L1L2(0.01, 0.01)"
      ],
      "metadata": {
        "id": "O594yNV2WSRj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Model execution\n",
        "model = Sequential()\n",
        "model.add(LSTM(48, input_shape=(timesteps, input_dim), return_sequences=True,bias_regularizer=reg ))\n",
        "model.add(BatchNormalization())\n",
        "model.add(Dropout(0.50))\n",
        "model.add(LSTM(32))\n",
        "model.add(Dropout(0.50))\n",
        "model.add(Dense(n_classes, activation='sigmoid'))\n",
        "print(\"Model Summary: \")\n",
        "model.summary()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "17yKQHdAWVWs",
        "outputId": "48627f4b-09b1-465c-d1ab-cd2142af8642"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Model Summary: \n",
            "Model: \"sequential_4\"\n",
            "_________________________________________________________________\n",
            " Layer (type)                Output Shape              Param #   \n",
            "=================================================================\n",
            " lstm_6 (LSTM)               (None, 128, 48)           11136     \n",
            "                                                                 \n",
            " batch_normalization_2 (Batc  (None, 128, 48)          192       \n",
            " hNormalization)                                                 \n",
            "                                                                 \n",
            " dropout_6 (Dropout)         (None, 128, 48)           0         \n",
            "                                                                 \n",
            " lstm_7 (LSTM)               (None, 32)                10368     \n",
            "                                                                 \n",
            " dropout_7 (Dropout)         (None, 32)                0         \n",
            "                                                                 \n",
            " dense_4 (Dense)             (None, 6)                 198       \n",
            "                                                                 \n",
            "=================================================================\n",
            "Total params: 21,894\n",
            "Trainable params: 21,798\n",
            "Non-trainable params: 96\n",
            "_________________________________________________________________\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])"
      ],
      "metadata": {
        "id": "HfXE9HTbWYMc"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Training the model\n",
        "model.fit(X_train, y_train, batch_size=n_batch, validation_data=(X_test, y_test), epochs=n_epochs)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TQxn8R_VWbJA",
        "outputId": "b9186f7f-87a8-4e68-f35e-63fd06ca7efb"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Epoch 1/30\n",
            "460/460 [==============================] - 14s 17ms/step - loss: 1.5191 - accuracy: 0.6938 - val_loss: 0.9805 - val_accuracy: 0.8310\n",
            "Epoch 2/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.7182 - accuracy: 0.8690 - val_loss: 0.5508 - val_accuracy: 0.8904\n",
            "Epoch 3/30\n",
            "460/460 [==============================] - 6s 13ms/step - loss: 0.3774 - accuracy: 0.9128 - val_loss: 0.3188 - val_accuracy: 0.8965\n",
            "Epoch 4/30\n",
            "460/460 [==============================] - 8s 17ms/step - loss: 0.2595 - accuracy: 0.9225 - val_loss: 0.4659 - val_accuracy: 0.8405\n",
            "Epoch 5/30\n",
            "460/460 [==============================] - 6s 14ms/step - loss: 0.2368 - accuracy: 0.9187 - val_loss: 0.4207 - val_accuracy: 0.8565\n",
            "Epoch 6/30\n",
            "460/460 [==============================] - 8s 17ms/step - loss: 0.2269 - accuracy: 0.9234 - val_loss: 0.2667 - val_accuracy: 0.9036\n",
            "Epoch 7/30\n",
            "460/460 [==============================] - 6s 14ms/step - loss: 0.1687 - accuracy: 0.9374 - val_loss: 0.2469 - val_accuracy: 0.9125\n",
            "Epoch 8/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1691 - accuracy: 0.9387 - val_loss: 0.3808 - val_accuracy: 0.8884\n",
            "Epoch 9/30\n",
            "460/460 [==============================] - 7s 14ms/step - loss: 0.1535 - accuracy: 0.9422 - val_loss: 0.2922 - val_accuracy: 0.9060\n",
            "Epoch 10/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1839 - accuracy: 0.9350 - val_loss: 0.3028 - val_accuracy: 0.8935\n",
            "Epoch 11/30\n",
            "460/460 [==============================] - 7s 15ms/step - loss: 0.1648 - accuracy: 0.9408 - val_loss: 0.3396 - val_accuracy: 0.8860\n",
            "Epoch 12/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1487 - accuracy: 0.9442 - val_loss: 0.2619 - val_accuracy: 0.9148\n",
            "Epoch 13/30\n",
            "460/460 [==============================] - 8s 18ms/step - loss: 0.1564 - accuracy: 0.9393 - val_loss: 0.2611 - val_accuracy: 0.9131\n",
            "Epoch 14/30\n",
            "460/460 [==============================] - 7s 14ms/step - loss: 0.1492 - accuracy: 0.9418 - val_loss: 0.3017 - val_accuracy: 0.9155\n",
            "Epoch 15/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1759 - accuracy: 0.9370 - val_loss: 0.3169 - val_accuracy: 0.9182\n",
            "Epoch 16/30\n",
            "460/460 [==============================] - 7s 14ms/step - loss: 0.1700 - accuracy: 0.9396 - val_loss: 0.3099 - val_accuracy: 0.9030\n",
            "Epoch 17/30\n",
            "460/460 [==============================] - 8s 17ms/step - loss: 0.1506 - accuracy: 0.9403 - val_loss: 0.3593 - val_accuracy: 0.8965\n",
            "Epoch 18/30\n",
            "460/460 [==============================] - 6s 13ms/step - loss: 0.1462 - accuracy: 0.9461 - val_loss: 0.3433 - val_accuracy: 0.9074\n",
            "Epoch 19/30\n",
            "460/460 [==============================] - 8s 17ms/step - loss: 0.1372 - accuracy: 0.9459 - val_loss: 0.2816 - val_accuracy: 0.9206\n",
            "Epoch 20/30\n",
            "460/460 [==============================] - 6s 13ms/step - loss: 0.1436 - accuracy: 0.9429 - val_loss: 0.2907 - val_accuracy: 0.9196\n",
            "Epoch 21/30\n",
            "460/460 [==============================] - 8s 17ms/step - loss: 0.1311 - accuracy: 0.9480 - val_loss: 0.2638 - val_accuracy: 0.9223\n",
            "Epoch 22/30\n",
            "460/460 [==============================] - 6s 13ms/step - loss: 0.1498 - accuracy: 0.9410 - val_loss: 0.3071 - val_accuracy: 0.9030\n",
            "Epoch 23/30\n",
            "460/460 [==============================] - 8s 16ms/step - loss: 0.1399 - accuracy: 0.9472 - val_loss: 0.3322 - val_accuracy: 0.9060\n",
            "Epoch 24/30\n",
            "460/460 [==============================] - 6s 14ms/step - loss: 0.1619 - accuracy: 0.9397 - val_loss: 0.2797 - val_accuracy: 0.9179\n",
            "Epoch 25/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1452 - accuracy: 0.9446 - val_loss: 0.2839 - val_accuracy: 0.9148\n",
            "Epoch 26/30\n",
            "460/460 [==============================] - 7s 14ms/step - loss: 0.1296 - accuracy: 0.9494 - val_loss: 0.3043 - val_accuracy: 0.8982\n",
            "Epoch 27/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1334 - accuracy: 0.9460 - val_loss: 0.2795 - val_accuracy: 0.9080\n",
            "Epoch 28/30\n",
            "460/460 [==============================] - 7s 14ms/step - loss: 0.1310 - accuracy: 0.9463 - val_loss: 0.2821 - val_accuracy: 0.8945\n",
            "Epoch 29/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1253 - accuracy: 0.9489 - val_loss: 0.2743 - val_accuracy: 0.9165\n",
            "Epoch 30/30\n",
            "460/460 [==============================] - 7s 14ms/step - loss: 0.1407 - accuracy: 0.9406 - val_loss: 0.2640 - val_accuracy: 0.9131\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<keras.callbacks.History at 0x7f78fa3f00a0>"
            ]
          },
          "metadata": {},
          "execution_count": 58
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Confusion Matrix\n",
        "confusion_matrix(y_test, model.predict(X_test))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 286
        },
        "id": "abLtI6JsWeM1",
        "outputId": "1e5b55ea-0793-463a-df25-7596e18e36c8"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "93/93 [==============================] - 2s 7ms/step\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Pred                LAYING  SITTING  STANDING  WALKING  WALKING_DOWNSTAIRS  \\\n",
              "True                                                                         \n",
              "LAYING                 537        0         0        0                   0   \n",
              "SITTING                  2      419        67        0                   0   \n",
              "STANDING                 0      128       404        0                   0   \n",
              "WALKING                  0        0         0      472                  19   \n",
              "WALKING_DOWNSTAIRS       0        0         0        2                 415   \n",
              "WALKING_UPSTAIRS         0        0         0       11                  16   \n",
              "\n",
              "Pred                WALKING_UPSTAIRS  \n",
              "True                                  \n",
              "LAYING                             0  \n",
              "SITTING                            3  \n",
              "STANDING                           0  \n",
              "WALKING                            5  \n",
              "WALKING_DOWNSTAIRS                 3  \n",
              "WALKING_UPSTAIRS                 444  "
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-03249097-2495-4177-b033-477f5b7ebb57\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th>Pred</th>\n",
              "      <th>LAYING</th>\n",
              "      <th>SITTING</th>\n",
              "      <th>STANDING</th>\n",
              "      <th>WALKING</th>\n",
              "      <th>WALKING_DOWNSTAIRS</th>\n",
              "      <th>WALKING_UPSTAIRS</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>True</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>LAYING</th>\n",
              "      <td>537</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>SITTING</th>\n",
              "      <td>2</td>\n",
              "      <td>419</td>\n",
              "      <td>67</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>STANDING</th>\n",
              "      <td>0</td>\n",
              "      <td>128</td>\n",
              "      <td>404</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>WALKING</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>472</td>\n",
              "      <td>19</td>\n",
              "      <td>5</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>WALKING_DOWNSTAIRS</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>415</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>WALKING_UPSTAIRS</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>11</td>\n",
              "      <td>16</td>\n",
              "      <td>444</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-03249097-2495-4177-b033-477f5b7ebb57')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-03249097-2495-4177-b033-477f5b7ebb57 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-03249097-2495-4177-b033-477f5b7ebb57');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 59
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "score = model.evaluate(X_test, y_test)\n",
        "\n",
        "print(\"\\n   categorica_crossentropy  ||   accuracy \")\n",
        "print(\"  ____________________________________\")\n",
        "print(score)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sYNMpcu_WjB-",
        "outputId": "e43835c8-4b1b-4994-9e9e-67cf6f208d59"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "93/93 [==============================] - 1s 6ms/step - loss: 0.2640 - accuracy: 0.9131\n",
            "\n",
            "   categorica_crossentropy  ||   accuracy \n",
            "  ____________________________________\n",
            "[0.2640216052532196, 0.9131320118904114]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### 2.2 Second Model for 2-Layer of LSTM with more hyperparameter tunning"
      ],
      "metadata": {
        "id": "gPw5BDcNWocp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Model execution\n",
        "model = Sequential()\n",
        "model.add(LSTM(64, input_shape=(timesteps, input_dim), return_sequences=True, bias_regularizer=reg))\n",
        "model.add(BatchNormalization())\n",
        "model.add(Dropout(0.50))\n",
        "model.add(LSTM(48))\n",
        "model.add(Dropout(0.50))\n",
        "model.add(Dense(n_classes, activation='sigmoid'))\n",
        "print(\"Model Summary: \")\n",
        "model.summary()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "KgIyrMM0WsEY",
        "outputId": "414d300c-10de-40e0-b5e5-73de5ff3e97f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Model Summary: \n",
            "Model: \"sequential_5\"\n",
            "_________________________________________________________________\n",
            " Layer (type)                Output Shape              Param #   \n",
            "=================================================================\n",
            " lstm_8 (LSTM)               (None, 128, 64)           18944     \n",
            "                                                                 \n",
            " batch_normalization_3 (Batc  (None, 128, 64)          256       \n",
            " hNormalization)                                                 \n",
            "                                                                 \n",
            " dropout_8 (Dropout)         (None, 128, 64)           0         \n",
            "                                                                 \n",
            " lstm_9 (LSTM)               (None, 48)                21696     \n",
            "                                                                 \n",
            " dropout_9 (Dropout)         (None, 48)                0         \n",
            "                                                                 \n",
            " dense_5 (Dense)             (None, 6)                 294       \n",
            "                                                                 \n",
            "=================================================================\n",
            "Total params: 41,190\n",
            "Trainable params: 41,062\n",
            "Non-trainable params: 128\n",
            "_________________________________________________________________\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])"
      ],
      "metadata": {
        "id": "cJ4wvkh5WycW"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Training the model\n",
        "model.fit(X_train, y_train, batch_size=n_batch, validation_data=(X_test, y_test), epochs=n_epochs)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VKrGoJiuW3tK",
        "outputId": "86011b01-e928-434b-a373-102ff9c27ec2"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Epoch 1/30\n",
            "460/460 [==============================] - 13s 19ms/step - loss: 1.6734 - accuracy: 0.7078 - val_loss: 1.5870 - val_accuracy: 0.5945\n",
            "Epoch 2/30\n",
            "460/460 [==============================] - 6s 14ms/step - loss: 0.7557 - accuracy: 0.8898 - val_loss: 0.4793 - val_accuracy: 0.9179\n",
            "Epoch 3/30\n",
            "460/460 [==============================] - 8s 16ms/step - loss: 0.4011 - accuracy: 0.9135 - val_loss: 0.3697 - val_accuracy: 0.8761\n",
            "Epoch 4/30\n",
            "460/460 [==============================] - 6s 14ms/step - loss: 0.2216 - accuracy: 0.9287 - val_loss: 0.3722 - val_accuracy: 0.8829\n",
            "Epoch 5/30\n",
            "460/460 [==============================] - 8s 17ms/step - loss: 0.1946 - accuracy: 0.9334 - val_loss: 0.4488 - val_accuracy: 0.8446\n",
            "Epoch 6/30\n",
            "460/460 [==============================] - 6s 13ms/step - loss: 0.1719 - accuracy: 0.9381 - val_loss: 0.2355 - val_accuracy: 0.9128\n",
            "Epoch 7/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1631 - accuracy: 0.9399 - val_loss: 0.2094 - val_accuracy: 0.9325\n",
            "Epoch 8/30\n",
            "460/460 [==============================] - 6s 13ms/step - loss: 0.1798 - accuracy: 0.9317 - val_loss: 0.2030 - val_accuracy: 0.9189\n",
            "Epoch 9/30\n",
            "460/460 [==============================] - 8s 17ms/step - loss: 0.1874 - accuracy: 0.9312 - val_loss: 0.2831 - val_accuracy: 0.9165\n",
            "Epoch 10/30\n",
            "460/460 [==============================] - 6s 14ms/step - loss: 0.1565 - accuracy: 0.9387 - val_loss: 0.2430 - val_accuracy: 0.9192\n",
            "Epoch 11/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1611 - accuracy: 0.9384 - val_loss: 0.2457 - val_accuracy: 0.9080\n",
            "Epoch 12/30\n",
            "460/460 [==============================] - 6s 14ms/step - loss: 0.1424 - accuracy: 0.9445 - val_loss: 0.3006 - val_accuracy: 0.8992\n",
            "Epoch 13/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1442 - accuracy: 0.9440 - val_loss: 0.2308 - val_accuracy: 0.9162\n",
            "Epoch 14/30\n",
            "460/460 [==============================] - 6s 13ms/step - loss: 0.1882 - accuracy: 0.9353 - val_loss: 0.2404 - val_accuracy: 0.9199\n",
            "Epoch 15/30\n",
            "460/460 [==============================] - 8s 16ms/step - loss: 0.1385 - accuracy: 0.9448 - val_loss: 0.2316 - val_accuracy: 0.9094\n",
            "Epoch 16/30\n",
            "460/460 [==============================] - 6s 14ms/step - loss: 0.1704 - accuracy: 0.9393 - val_loss: 0.2102 - val_accuracy: 0.9257\n",
            "Epoch 17/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1349 - accuracy: 0.9465 - val_loss: 0.2214 - val_accuracy: 0.9165\n",
            "Epoch 18/30\n",
            "460/460 [==============================] - 6s 13ms/step - loss: 0.1502 - accuracy: 0.9400 - val_loss: 0.2715 - val_accuracy: 0.9101\n",
            "Epoch 19/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1281 - accuracy: 0.9472 - val_loss: 0.2358 - val_accuracy: 0.9179\n",
            "Epoch 20/30\n",
            "460/460 [==============================] - 6s 14ms/step - loss: 0.1380 - accuracy: 0.9448 - val_loss: 0.2803 - val_accuracy: 0.9080\n",
            "Epoch 21/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1440 - accuracy: 0.9429 - val_loss: 0.2399 - val_accuracy: 0.9253\n",
            "Epoch 22/30\n",
            "460/460 [==============================] - 7s 14ms/step - loss: 0.1608 - accuracy: 0.9416 - val_loss: 0.2226 - val_accuracy: 0.9216\n",
            "Epoch 23/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1291 - accuracy: 0.9489 - val_loss: 0.2334 - val_accuracy: 0.9257\n",
            "Epoch 24/30\n",
            "460/460 [==============================] - 7s 14ms/step - loss: 0.1311 - accuracy: 0.9471 - val_loss: 0.2140 - val_accuracy: 0.9267\n",
            "Epoch 25/30\n",
            "460/460 [==============================] - 8s 16ms/step - loss: 0.1324 - accuracy: 0.9475 - val_loss: 0.2815 - val_accuracy: 0.9165\n",
            "Epoch 26/30\n",
            "460/460 [==============================] - 6s 13ms/step - loss: 0.1284 - accuracy: 0.9484 - val_loss: 0.2534 - val_accuracy: 0.9325\n",
            "Epoch 27/30\n",
            "460/460 [==============================] - 7s 16ms/step - loss: 0.1268 - accuracy: 0.9486 - val_loss: 0.2600 - val_accuracy: 0.9220\n",
            "Epoch 28/30\n",
            "460/460 [==============================] - 6s 13ms/step - loss: 0.1290 - accuracy: 0.9501 - val_loss: 0.2439 - val_accuracy: 0.9192\n",
            "Epoch 29/30\n",
            "460/460 [==============================] - 8s 17ms/step - loss: 0.1354 - accuracy: 0.9486 - val_loss: 0.5618 - val_accuracy: 0.8320\n",
            "Epoch 30/30\n",
            "460/460 [==============================] - 7s 14ms/step - loss: 0.1541 - accuracy: 0.9411 - val_loss: 0.3802 - val_accuracy: 0.9125\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<keras.callbacks.History at 0x7f78f3474a30>"
            ]
          },
          "metadata": {},
          "execution_count": 63
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Confusion Matrix\n",
        "confusion_matrix(y_test, model.predict(X_test))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 286
        },
        "id": "65v6IGSdW66a",
        "outputId": "e9876301-35b6-4a9e-f364-4580cd009f5d"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "93/93 [==============================] - 2s 7ms/step\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Pred                LAYING  SITTING  STANDING  WALKING  WALKING_DOWNSTAIRS  \\\n",
              "True                                                                         \n",
              "LAYING                 537        0         0        0                   0   \n",
              "SITTING                  0      407        82        0                   0   \n",
              "STANDING                 0       94       438        0                   0   \n",
              "WALKING                  0        0         0      447                  46   \n",
              "WALKING_DOWNSTAIRS       0        0         0        1                 419   \n",
              "WALKING_UPSTAIRS         0        2         0        1                  27   \n",
              "\n",
              "Pred                WALKING_UPSTAIRS  \n",
              "True                                  \n",
              "LAYING                             0  \n",
              "SITTING                            2  \n",
              "STANDING                           0  \n",
              "WALKING                            3  \n",
              "WALKING_DOWNSTAIRS                 0  \n",
              "WALKING_UPSTAIRS                 441  "
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-af1f7d68-479a-4380-870e-0e82389a4389\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th>Pred</th>\n",
              "      <th>LAYING</th>\n",
              "      <th>SITTING</th>\n",
              "      <th>STANDING</th>\n",
              "      <th>WALKING</th>\n",
              "      <th>WALKING_DOWNSTAIRS</th>\n",
              "      <th>WALKING_UPSTAIRS</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>True</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>LAYING</th>\n",
              "      <td>537</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>SITTING</th>\n",
              "      <td>0</td>\n",
              "      <td>407</td>\n",
              "      <td>82</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>STANDING</th>\n",
              "      <td>0</td>\n",
              "      <td>94</td>\n",
              "      <td>438</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>WALKING</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>447</td>\n",
              "      <td>46</td>\n",
              "      <td>3</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>WALKING_DOWNSTAIRS</th>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>419</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>WALKING_UPSTAIRS</th>\n",
              "      <td>0</td>\n",
              "      <td>2</td>\n",
              "      <td>0</td>\n",
              "      <td>1</td>\n",
              "      <td>27</td>\n",
              "      <td>441</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-af1f7d68-479a-4380-870e-0e82389a4389')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-af1f7d68-479a-4380-870e-0e82389a4389 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-af1f7d68-479a-4380-870e-0e82389a4389');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 64
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "score = model.evaluate(X_test, y_test)\n",
        "\n",
        "print(\"\\n   categorical_crossentropy  ||   accuracy \")\n",
        "print(\"  ____________________________________\")\n",
        "print(score)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "N7i84QfGW9Er",
        "outputId": "18a8a70f-2896-45a2-a22b-cef28557cfe3"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "93/93 [==============================] - 1s 9ms/step - loss: 0.3802 - accuracy: 0.9125\n",
            "\n",
            "   categorical_crossentropy  ||   accuracy \n",
            "  ____________________________________\n",
            "[0.38021206855773926, 0.9124533534049988]\n"
          ]
        }
      ]
    }
  ]
}