Fuzzy_CNN.ipynb 9.05 KB
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import numpy as np\n",
    "import os\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def Preprocess(dir_) :\n",
    "    f1 = open(dir_, 'r')\n",
    "    fr = csv.reader(f1)\n",
    "    d = [] \n",
    "    for line in fr :\n",
    "        l = []\n",
    "        l.append(line[0])\n",
    "        l.append(line[1])\n",
    "        l.extend(line[2:11])\n",
    "        \n",
    "        d.append(l)\n",
    "    return d\n",
    "\n",
    "data = Preprocess('Fuzzy.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = data[:3838857]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1349', '8', '216', '0', '0', '138', '0', '0', '0', '0', '1']\n"
     ]
    }
   ],
   "source": [
    "print(data[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "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",
    "            #t = np.array(t, dtype=np.float32)\n",
    "            #t = tf.convert_to_tensor(t)\n",
    "            d.append(t)\n",
    "        arr.append(d)\n",
    "    return arr\n",
    "\n",
    "resdata = createsequence(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2582020\n",
      "1256826\n"
     ]
    }
   ],
   "source": [
    "cnt = 0\n",
    "for e in checkblock :\n",
    "    if e == 1 :\n",
    "        cnt += 1\n",
    "\n",
    "print(cnt)\n",
    "print(len(checkblock)-cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "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": 32,
   "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": 33,
   "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": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1151654, 11, 11)\n"
     ]
    }
   ],
   "source": [
    "print(x_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "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 tensorflow.keras.utils import plot_model\n",
    "from keras.layers.normalization import BatchNormalization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "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",
      "('Failed to import pydot. You must `pip install pydot` and install graphviz (https://graphviz.gitlab.io/download/), ', 'for `pydotprint` to work.')\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()\n",
    "\n",
    "plot_model(model, to_file='CNNmodel.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "26872/26872 [==============================] - 31s 1ms/step - loss: 0.1271 - accuracy: 0.9865 - val_loss: 0.0066 - val_accuracy: 0.9983\n",
      "Epoch 2/10\n",
      "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0129 - accuracy: 0.9969 - val_loss: 0.0060 - val_accuracy: 0.9986\n",
      "Epoch 3/10\n",
      "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0107 - accuracy: 0.9975 - val_loss: 0.0046 - val_accuracy: 0.9990\n",
      "Epoch 4/10\n",
      "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0093 - accuracy: 0.9979 - val_loss: 0.0037 - val_accuracy: 0.9993\n",
      "Epoch 5/10\n",
      "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0081 - accuracy: 0.9982 - val_loss: 0.0034 - val_accuracy: 0.9993\n",
      "Epoch 6/10\n",
      "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0074 - accuracy: 0.9984 - val_loss: 0.0042 - val_accuracy: 0.9991\n",
      "Epoch 7/10\n",
      "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0070 - accuracy: 0.9985 - val_loss: 0.0039 - val_accuracy: 0.9992\n",
      "Epoch 8/10\n",
      "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0065 - accuracy: 0.9987 - val_loss: 0.0033 - val_accuracy: 0.9994\n",
      "Epoch 9/10\n",
      "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0063 - accuracy: 0.9987 - val_loss: 0.0032 - val_accuracy: 0.9994\n",
      "Epoch 10/10\n",
      "26872/26872 [==============================] - 31s 1ms/step - loss: 0.0058 - accuracy: 0.9988 - val_loss: 0.0031 - val_accuracy: 0.9994\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": 38,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "35990/35990 [==============================] - 13s 361us/step - loss: 0.0031 - accuracy: 0.9994\n",
      "\n",
      " Test accuracy : 0.9994\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
}