{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "0f935bddcc5ca02702578e712bf7f396ba7154ef", "execution": { "iopub.execute_input": "2022-04-01T19:45:49.419624Z", "iopub.status.busy": "2022-04-01T19:45:49.41933Z", "iopub.status.idle": "2022-04-01T19:45:51.246319Z", "shell.execute_reply": "2022-04-01T19:45:51.245615Z", "shell.execute_reply.started": "2022-04-01T19:45:49.419574Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import confusion_matrix\n", "\n", "from keras.models import Sequential\n", "from keras.layers import Conv2D, Lambda, MaxPooling2D # convolution layers\n", "from keras.layers import Dense, Dropout, Flatten # core layers\n", "\n", "from keras.layers.normalization import BatchNormalization\n", "\n", "from keras.preprocessing.image import ImageDataGenerator\n", "\n", "from keras.utils.np_utils import to_categorical\n", "\n", "#from keras.datasets import mnist\n", "import numpy as np # linear algebra\n", "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt\n", "# import warnings\n", "import warnings\n", "# filter warnings\n", "warnings.filterwarnings('ignore')\n", "\n", "# Input data files are available in the \"../input/\" directory.\n", "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n", "\n", "import os\n", "print(os.listdir(\"../input\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:51.24967Z", "iopub.status.busy": "2022-04-01T19:45:51.249411Z", "iopub.status.idle": "2022-04-01T19:45:51.254148Z", "shell.execute_reply": "2022-04-01T19:45:51.25338Z", "shell.execute_reply.started": "2022-04-01T19:45:51.249623Z" } }, "outputs": [], "source": [ "import os\n", "print(os.listdir('../input'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:51.256377Z", "iopub.status.busy": "2022-04-01T19:45:51.255712Z", "iopub.status.idle": "2022-04-01T19:45:51.44123Z", "shell.execute_reply": "2022-04-01T19:45:51.440366Z", "shell.execute_reply.started": "2022-04-01T19:45:51.256253Z" } }, "outputs": [], "source": [ "data = pd.read_csv(\"../input/500lukveri/data.csv\") \n", "print(data.shape)\n", "data.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:51.442591Z", "iopub.status.busy": "2022-04-01T19:45:51.442308Z", "iopub.status.idle": "2022-04-01T19:45:51.450013Z", "shell.execute_reply": "2022-04-01T19:45:51.449155Z", "shell.execute_reply.started": "2022-04-01T19:45:51.442546Z" } }, "outputs": [], "source": [ "X=data.iloc[0:, 1:-1]\n", "y=data.iloc[:,-1]\n", "print(X.shape)\n", "print(y.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:51.504748Z", "iopub.status.busy": "2022-04-01T19:45:51.504248Z", "iopub.status.idle": "2022-04-01T19:45:51.511105Z", "shell.execute_reply": "2022-04-01T19:45:51.510065Z", "shell.execute_reply.started": "2022-04-01T19:45:51.504573Z" } }, "outputs": [], "source": [ "X = X.values.reshape(500,467,1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:51.521344Z", "iopub.status.busy": "2022-04-01T19:45:51.520787Z", "iopub.status.idle": "2022-04-01T19:45:51.530732Z", "shell.execute_reply": "2022-04-01T19:45:51.53006Z", "shell.execute_reply.started": "2022-04-01T19:45:51.521294Z" } }, "outputs": [], "source": [ "mean = np.mean(X)\n", "std = np.std(X)\n", "\n", "def standardize(x):\n", " return (x-mean)/std" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:51.533871Z", "iopub.status.busy": "2022-04-01T19:45:51.533628Z", "iopub.status.idle": "2022-04-01T19:45:51.543723Z", "shell.execute_reply": "2022-04-01T19:45:51.542907Z", "shell.execute_reply.started": "2022-04-01T19:45:51.533829Z" } }, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "X_train, X_val, Y_train, Y_val = train_test_split(X, y, test_size = 0.15, random_state=2)\n", "print(\"x_train shape\",X_train.shape)\n", "print(\"x_test shape\",X_val.shape)\n", "print(\"y_train shape\",Y_train.shape)\n", "print(\"y_test shape\",Y_val.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:51.553792Z", "iopub.status.busy": "2022-04-01T19:45:51.553179Z", "iopub.status.idle": "2022-04-01T19:45:51.561178Z", "shell.execute_reply": "2022-04-01T19:45:51.560327Z", "shell.execute_reply.started": "2022-04-01T19:45:51.553741Z" } }, "outputs": [], "source": [ "from keras.utils.np_utils import to_categorical # convert to one-hot-encoding\n", "Y_trainn = to_categorical(Y_train, num_classes = 2)\n", "Y_vall = to_categorical(Y_val, num_classes = 2)\n", "print(Y_trainn.shape)\n", "print(Y_vall.shape)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:51.572996Z", "iopub.status.busy": "2022-04-01T19:45:51.572522Z", "iopub.status.idle": "2022-04-01T19:45:51.722294Z", "shell.execute_reply": "2022-04-01T19:45:51.721621Z", "shell.execute_reply.started": "2022-04-01T19:45:51.572933Z" } }, "outputs": [], "source": [ "from sklearn.metrics import confusion_matrix\n", "import itertools\n", "from keras.layers import LeakyReLU\n", "from keras.utils.np_utils import to_categorical # convert to one-hot-encoding\n", "from keras.models import Sequential, Model\n", "from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool1D,Conv1D\n", "from keras.optimizers import RMSprop,Adam,Nadam\n", "from keras.preprocessing.image import ImageDataGenerator\n", "from keras.callbacks import ReduceLROnPlateau\n", "import tensorflow as tf\n", "keras = tf.keras \n", "\n", "from tensorflow.keras import layers\n", "from tensorflow.keras import initializers\n", "\n", "initializer = tf.keras.initializers.RandomUniform(minval=0., maxval=1.)\n", "\n", "model = Sequential()\n", "\n", "model.add(Conv1D(filters = 3, kernel_size =7,kernel_initializer=initializer, padding = 'Same',activation=\"relu\", \n", " input_shape = (467,1)))\n", "\n", "\n", "model.add(MaxPool1D(pool_size=2))\n", "model.add(Dropout(0.25))\n", "#\n", "model.add(Conv1D(filters = 1, kernel_size =5,kernel_initializer=initializer,padding = 'Same',activation=\"relu\"))\n", "\n", "\n", "model.add(MaxPool1D(pool_size=2, strides=2))\n", "model.add(Dropout(0.25))\n", "\n", "model.add(Flatten())\n", "model.add(Dense(8, activation=\"relu\"))\n", "\n", "model.add(Dropout(0.5))\n", "model.add(Dense(2, activation = \"sigmoid\"))\n", "model.summary()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "d00d9b96668d11a33c272b2b64cbb4397dcbba89", "execution": { "iopub.execute_input": "2022-04-01T19:45:51.724158Z", "iopub.status.busy": "2022-04-01T19:45:51.72387Z", "iopub.status.idle": "2022-04-01T19:45:51.745249Z", "shell.execute_reply": "2022-04-01T19:45:51.74452Z", "shell.execute_reply.started": "2022-04-01T19:45:51.724112Z" }, "scrolled": true }, "outputs": [], "source": [ "optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:51.767248Z", "iopub.status.busy": "2022-04-01T19:45:51.767022Z", "iopub.status.idle": "2022-04-01T19:45:51.793763Z", "shell.execute_reply": "2022-04-01T19:45:51.793182Z", "shell.execute_reply.started": "2022-04-01T19:45:51.767205Z" } }, "outputs": [], "source": [ "model.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:51.868278Z", "iopub.status.busy": "2022-04-01T19:45:51.868048Z", "iopub.status.idle": "2022-04-01T19:45:51.873976Z", "shell.execute_reply": "2022-04-01T19:45:51.873294Z", "shell.execute_reply.started": "2022-04-01T19:45:51.868236Z" } }, "outputs": [], "source": [ "epochs = 1 # for better result increase the epochs\n", "batch_size = 32" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:52.084503Z", "iopub.status.busy": "2022-04-01T19:45:52.084247Z", "iopub.status.idle": "2022-04-01T19:45:54.788016Z", "shell.execute_reply": "2022-04-01T19:45:54.78732Z", "shell.execute_reply.started": "2022-04-01T19:45:52.084457Z" } }, "outputs": [], "source": [ "history = model.fit(X_train,Y_trainn,epochs = epochs, validation_data = (X_val,Y_vall),batch_size=32)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "_uuid": "82bddf135bfad1d3f91b52cd0cadb62e55db964b", "execution": { "iopub.execute_input": "2022-04-01T19:45:54.790267Z", "iopub.status.busy": "2022-04-01T19:45:54.789977Z", "iopub.status.idle": "2022-04-01T19:45:54.930069Z", "shell.execute_reply": "2022-04-01T19:45:54.929008Z", "shell.execute_reply.started": "2022-04-01T19:45:54.790218Z" } }, "outputs": [], "source": [ "from keras.utils.vis_utils import plot_model\n", "plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:54.932875Z", "iopub.status.busy": "2022-04-01T19:45:54.932314Z", "iopub.status.idle": "2022-04-01T19:45:55.452768Z", "shell.execute_reply": "2022-04-01T19:45:55.451994Z", "shell.execute_reply.started": "2022-04-01T19:45:54.932822Z" } }, "outputs": [], "source": [ "fig, ax = plt.subplots(2,1, figsize=(18, 10))\n", "ax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\n", "ax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\n", "legend = ax[0].legend(loc='best', shadow=True)\n", "\n", "ax[1].plot(history.history['acc'], color='b', label=\"Training accuracy\")\n", "ax[1].plot(history.history['val_acc'], color='r',label=\"Validation accuracy\")\n", "legend = ax[1].legend(loc='best', shadow=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:55.454835Z", "iopub.status.busy": "2022-04-01T19:45:55.454309Z", "iopub.status.idle": "2022-04-01T19:45:55.727878Z", "shell.execute_reply": "2022-04-01T19:45:55.72707Z", "shell.execute_reply.started": "2022-04-01T19:45:55.454784Z" } }, "outputs": [], "source": [ "fig = plt.figure(figsize=(10, 10)) # Set Figure\n", "\n", "yy_pred = model.predict(X_val) # Predict encoded label as 2 => [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]\n", "\n", "YY_pred = np.argmax(yy_pred, 1) # Decode Predicted labels\n", "Y_valc = np.argmax(Y_vall, 1) # Decode labels\n", "\n", "mat = confusion_matrix(Y_valc, YY_pred) # Confusion matrix\n", "\n", "# Plot Confusion matrix\n", "sns.heatmap(mat.T, square=True, annot=True, cbar=False, cmap=plt.cm.Blues)\n", "plt.xlabel('Predicted Values')\n", "plt.ylabel('True Values');\n", "plt.show();" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:55.731467Z", "iopub.status.busy": "2022-04-01T19:45:55.730859Z", "iopub.status.idle": "2022-04-01T19:45:55.844571Z", "shell.execute_reply": "2022-04-01T19:45:55.842932Z", "shell.execute_reply.started": "2022-04-01T19:45:55.731417Z" } }, "outputs": [], "source": [ "model_feat = Model(inputs=model.input,outputs=model.get_layer('dense_1').output)\n", "\n", "feat_trainf = model_feat.predict(X_train)\n", "print(feat_trainf.shape)\n", "\n", "feat_valf = model_feat.predict(X_val)\n", "print(feat_valf.shape)\n", "\n", "print(feat_valf)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-04-01T19:45:55.936629Z", "iopub.status.busy": "2022-04-01T19:45:55.936354Z", "iopub.status.idle": "2022-04-01T19:45:56.086744Z", "shell.execute_reply": "2022-04-01T19:45:56.086084Z", "shell.execute_reply.started": "2022-04-01T19:45:55.936583Z" } }, "outputs": [], "source": [ "featval=pd.DataFrame(feat_vald)\n", "featval.to_excel('X_val.xlsx')\n", "featvall=pd.DataFrame(feat_traind)\n", "featvall.to_excel('X_train.xlsx')\n", "featvalll=pd.DataFrame(Y_train)\n", "featvalll.to_excel('Y_train.xlsx')\n", "featvallll=pd.DataFrame(Y_val)\n", "featvallll.to_excel('Y_val.xlsx')" ] } ], "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 }