Spoofing_CNN.ipynb 8.55 KB
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import numpy as np\n",
    "import os\n",
    "import pandas as pd\n",
    "import import_ipynb\n",
    "import DoS_CNN as dos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dos.transferData('gear_dataset.csv', 'Spoofing.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = dos.Preprocess('Spoofing.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['320', '8', '0', '0', '0', '0', '16', '41', '42', '36', '1']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def createsequence(data) :\n",
    "    arr = []\n",
    "    for i in range(0, len(data)-11): \n",
    "        d = []\n",
    "        t = []\n",
    "        for x in range(i, i+11):\n",
    "            t = list(map(float, data[x]))\n",
    "            d.append(t)\n",
    "        arr.append(d)\n",
    "    return arr\n",
    "\n",
    "resdata = createsequence(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def check(data):\n",
    "    check = []\n",
    "    \n",
    "    for e in data :\n",
    "        flag = False\n",
    "        for i in e :\n",
    "            if i[-1] == 0 :\n",
    "                check.append(0)\n",
    "                flag = True\n",
    "                break\n",
    "        if flag == False :\n",
    "            check.append(1)\n",
    "            \n",
    "    return check\n",
    "\n",
    "checkblock = check(resdata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2580434\n",
      "1862697\n"
     ]
    }
   ],
   "source": [
    "cnt = 0 # 1\n",
    "for i in checkblock :\n",
    "    if i  == 1 :\n",
    "        cnt += 1\n",
    "print(cnt) # normal message\n",
    "print(len(checkblock)-cnt) #abnormal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "import tensorflow as tf\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(resdata)\n",
    "n = len(df)\n",
    "\n",
    "X_train = df.iloc[0:n, 0:11].values.tolist()\n",
    "Y_train = checkblock\n",
    "\n",
    "X_train = np.asarray(X_train)\n",
    "Y_train = np.asarray(Y_train)\n",
    "\n",
    "x_train, x_test, y_train, y_test = train_test_split(X_train, Y_train, test_size=0.3, random_state=321)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in x_train:\n",
    "    for j in i:\n",
    "        if len(j) != 11:\n",
    "            print(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1332940, 11, 11)\n"
     ]
    }
   ],
   "source": [
    "print(x_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Model\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, LSTM, Conv1D, Dropout, GlobalAveragePooling1D, MaxPooling1D\n",
    "from keras.utils import np_utils\n",
    "from keras.layers.normalization import BatchNormalization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv1d (Conv1D)              (None, 9, 64)             2176      \n",
      "_________________________________________________________________\n",
      "global_average_pooling1d (Gl (None, 64)                0         \n",
      "_________________________________________________________________\n",
      "dropout (Dropout)            (None, 64)                0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 1)                 65        \n",
      "=================================================================\n",
      "Total params: 2,241\n",
      "Trainable params: 2,241\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model = Sequential()\n",
    "model.add(Conv1D(64, 3, input_shape=(11, 11), activation='relu'))\n",
    "model.add(GlobalAveragePooling1D())\n",
    "model.add(Dropout(0.5))\n",
    "model.add(Dense(1, activation='sigmoid'))\n",
    "model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0739 - accuracy: 0.9938 - val_loss: 6.8605e-04 - val_accuracy: 1.0000\n",
      "Epoch 2/10\n",
      "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0029 - accuracy: 0.9995 - val_loss: 2.1258e-04 - val_accuracy: 1.0000\n",
      "Epoch 3/10\n",
      "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0030 - accuracy: 0.9995 - val_loss: 2.5308e-04 - val_accuracy: 1.0000\n",
      "Epoch 4/10\n",
      "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0025 - accuracy: 0.9996 - val_loss: 9.4737e-05 - val_accuracy: 1.0000\n",
      "Epoch 5/10\n",
      "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0031 - accuracy: 0.9995 - val_loss: 4.3025e-04 - val_accuracy: 1.0000\n",
      "Epoch 6/10\n",
      "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0022 - accuracy: 0.9997 - val_loss: 6.6521e-05 - val_accuracy: 1.0000\n",
      "Epoch 7/10\n",
      "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0020 - accuracy: 0.9997 - val_loss: 7.7669e-05 - val_accuracy: 1.0000\n",
      "Epoch 8/10\n",
      "31102/31102 [==============================] - 36s 1ms/step - loss: 0.0019 - accuracy: 0.9997 - val_loss: 1.3602e-04 - val_accuracy: 1.0000\n",
      "Epoch 9/10\n",
      "31102/31102 [==============================] - 35s 1ms/step - loss: 0.0018 - accuracy: 0.9997 - val_loss: 3.1278e-05 - val_accuracy: 1.0000\n",
      "Epoch 10/10\n",
      "31102/31102 [==============================] - 35s 1ms/step - loss: 0.0017 - accuracy: 0.9998 - val_loss: 3.1277e-05 - val_accuracy: 1.0000\n"
     ]
    }
   ],
   "source": [
    "history = model.fit(x_train, y_train, batch_size=100, epochs=10,validation_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "41655/41655 [==============================] - 15s 352us/step - loss: 3.1277e-05 - accuracy: 1.0000\n",
      "\n",
      " Test accuracy : 1.0000\n"
     ]
    }
   ],
   "source": [
    "print(\"\\n Test accuracy : %.4f\"%(model.evaluate(x_test, y_test)[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}