Commit ffc9edee authored by Mauroof N A's avatar Mauroof N A

Delete Boundary_Attack_CNN.ipynb

parent b33be518
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "BCLDh2jyRoqr"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"from matplotlib import pyplot as plt\n",
"import numpy as np\n",
"%matplotlib inline\n",
"\n",
"import tensorflow as tf\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense\n",
"from tensorflow.keras.layers import Flatten\n",
"from sklearn.model_selection import train_test_split\n",
"import time\n",
"\n",
"from imblearn.under_sampling import NearMiss\n",
"from imblearn.over_sampling import SMOTE\n",
"from imblearn.over_sampling import ADASYN\n",
"from imblearn.combine import SMOTEENN\n",
"\n",
"from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix, roc_curve, auc"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "KT9gMT7nRtlO",
"outputId": "b6efed91-afa7-4eec-8266-bec3dbefe5ab"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /content/drive\n"
]
}
],
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 461
},
"id": "y5OgkAr7RuRe",
"outputId": "81137ebe-a4a5-466c-d20e-77eb89bee5ef"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" gender SeniorCitizen Partner Dependents PhoneService MultipleLines \\\n",
"0 1 0 1 0 0 0 \n",
"1 0 0 0 0 1 0 \n",
"2 0 0 0 0 1 0 \n",
"3 0 0 0 0 0 0 \n",
"4 1 0 0 0 1 0 \n",
"... ... ... ... ... ... ... \n",
"7038 0 0 1 1 1 1 \n",
"7039 1 0 1 1 1 1 \n",
"7040 1 0 1 1 0 0 \n",
"7041 0 1 1 0 1 1 \n",
"7042 0 0 0 0 1 0 \n",
"\n",
" InternetService OnlineSecurity OnlineBackup DeviceProtection ... \\\n",
"0 1 0 1 0 ... \n",
"1 1 1 0 1 ... \n",
"2 1 1 1 0 ... \n",
"3 1 1 0 1 ... \n",
"4 2 0 0 0 ... \n",
"... ... ... ... ... ... \n",
"7038 1 1 0 1 ... \n",
"7039 2 0 1 1 ... \n",
"7040 1 1 0 0 ... \n",
"7041 2 0 0 0 ... \n",
"7042 2 1 0 1 ... \n",
"\n",
" Contract PaperlessBilling MonthlyCharges TotalCharges Churn \\\n",
"0 2 1 29.85 29.85 0 \n",
"1 1 0 56.95 1889.50 0 \n",
"2 2 1 53.85 108.15 1 \n",
"3 1 0 42.30 1840.75 0 \n",
"4 2 1 70.70 151.65 1 \n",
"... ... ... ... ... ... \n",
"7038 1 1 84.80 1990.50 0 \n",
"7039 1 1 103.20 7362.90 0 \n",
"7040 2 1 29.60 346.45 0 \n",
"7041 2 1 74.40 306.60 1 \n",
"7042 0 1 105.65 6844.50 0 \n",
"\n",
" tenureBand PaymentMethod_Bank transfer PaymentMethod_Credit card \\\n",
"0 2 0 0 \n",
"1 1 0 0 \n",
"2 2 0 0 \n",
"3 1 1 0 \n",
"4 2 0 0 \n",
"... ... ... ... \n",
"7038 2 0 0 \n",
"7039 0 0 1 \n",
"7040 2 0 0 \n",
"7041 2 0 0 \n",
"7042 0 1 0 \n",
"\n",
" PaymentMethod_Electronic check PaymentMethod_Mailed check \n",
"0 1 0 \n",
"1 0 1 \n",
"2 0 1 \n",
"3 0 0 \n",
"4 1 0 \n",
"... ... ... \n",
"7038 0 1 \n",
"7039 0 0 \n",
"7040 1 0 \n",
"7041 0 1 \n",
"7042 0 0 \n",
"\n",
"[7043 rows x 23 columns]"
],
"text/html": [
"\n",
" <div id=\"df-48fa6037-b95a-44b2-9e31-bd94ff743e3f\" 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></th>\n",
" <th>gender</th>\n",
" <th>SeniorCitizen</th>\n",
" <th>Partner</th>\n",
" <th>Dependents</th>\n",
" <th>PhoneService</th>\n",
" <th>MultipleLines</th>\n",
" <th>InternetService</th>\n",
" <th>OnlineSecurity</th>\n",
" <th>OnlineBackup</th>\n",
" <th>DeviceProtection</th>\n",
" <th>...</th>\n",
" <th>Contract</th>\n",
" <th>PaperlessBilling</th>\n",
" <th>MonthlyCharges</th>\n",
" <th>TotalCharges</th>\n",
" <th>Churn</th>\n",
" <th>tenureBand</th>\n",
" <th>PaymentMethod_Bank transfer</th>\n",
" <th>PaymentMethod_Credit card</th>\n",
" <th>PaymentMethod_Electronic check</th>\n",
" <th>PaymentMethod_Mailed check</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>...</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>29.85</td>\n",
" <td>29.85</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>56.95</td>\n",
" <td>1889.50</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>...</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>53.85</td>\n",
" <td>108.15</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>42.30</td>\n",
" <td>1840.75</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>...</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>70.70</td>\n",
" <td>151.65</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7038</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>84.80</td>\n",
" <td>1990.50</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7039</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>103.20</td>\n",
" <td>7362.90</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7040</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>...</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>29.60</td>\n",
" <td>346.45</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7041</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>...</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>74.40</td>\n",
" <td>306.60</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7042</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>105.65</td>\n",
" <td>6844.50</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>7043 rows × 23 columns</p>\n",
"</div>\n",
" <div class=\"colab-df-buttons\">\n",
"\n",
" <div class=\"colab-df-container\">\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-48fa6037-b95a-44b2-9e31-bd94ff743e3f')\"\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 -960 960 960\">\n",
" <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
" </svg>\n",
" </button>\n",
"\n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\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",
" .colab-df-buttons div {\n",
" margin-bottom: 4px;\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-48fa6037-b95a-44b2-9e31-bd94ff743e3f 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-48fa6037-b95a-44b2-9e31-bd94ff743e3f');\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",
"\n",
"\n",
"<div id=\"df-e0028020-1a13-4eb5-9856-a0ba56c831f7\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-e0028020-1a13-4eb5-9856-a0ba56c831f7')\"\n",
" title=\"Suggest charts.\"\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",
" <g>\n",
" <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
" </g>\n",
"</svg>\n",
" </button>\n",
"\n",
"<style>\n",
" .colab-df-quickchart {\n",
" --bg-color: #E8F0FE;\n",
" --fill-color: #1967D2;\n",
" --hover-bg-color: #E2EBFA;\n",
" --hover-fill-color: #174EA6;\n",
" --disabled-fill-color: #AAA;\n",
" --disabled-bg-color: #DDD;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-quickchart {\n",
" --bg-color: #3B4455;\n",
" --fill-color: #D2E3FC;\n",
" --hover-bg-color: #434B5C;\n",
" --hover-fill-color: #FFFFFF;\n",
" --disabled-bg-color: #3B4455;\n",
" --disabled-fill-color: #666;\n",
" }\n",
"\n",
" .colab-df-quickchart {\n",
" background-color: var(--bg-color);\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: var(--fill-color);\n",
" height: 32px;\n",
" padding: 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-quickchart:hover {\n",
" background-color: var(--hover-bg-color);\n",
" box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: var(--button-hover-fill-color);\n",
" }\n",
"\n",
" .colab-df-quickchart-complete:disabled,\n",
" .colab-df-quickchart-complete:disabled:hover {\n",
" background-color: var(--disabled-bg-color);\n",
" fill: var(--disabled-fill-color);\n",
" box-shadow: none;\n",
" }\n",
"\n",
" .colab-df-spinner {\n",
" border: 2px solid var(--fill-color);\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" animation:\n",
" spin 1s steps(1) infinite;\n",
" }\n",
"\n",
" @keyframes spin {\n",
" 0% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" border-left-color: var(--fill-color);\n",
" }\n",
" 20% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 30% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 40% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 60% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 80% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" 90% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" }\n",
"</style>\n",
"\n",
" <script>\n",
" async function quickchart(key) {\n",
" const quickchartButtonEl =\n",
" document.querySelector('#' + key + ' button');\n",
" quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
" quickchartButtonEl.classList.add('colab-df-spinner');\n",
" try {\n",
" const charts = await google.colab.kernel.invokeFunction(\n",
" 'suggestCharts', [key], {});\n",
" } catch (error) {\n",
" console.error('Error during call to suggestCharts:', error);\n",
" }\n",
" quickchartButtonEl.classList.remove('colab-df-spinner');\n",
" quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
" }\n",
" (() => {\n",
" let quickchartButtonEl =\n",
" document.querySelector('#df-e0028020-1a13-4eb5-9856-a0ba56c831f7 button');\n",
" quickchartButtonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
" })();\n",
" </script>\n",
"</div>\n",
" </div>\n",
" </div>\n"
]
},
"metadata": {},
"execution_count": 5
}
],
"source": [
"data = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/Churn_preprocess.csv')\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7D_z6tHmRuUn"
},
"outputs": [],
"source": [
"# define target variable and features\n",
"\n",
"y='Churn'\n",
"x = [x for x in list(data.columns) if x != y]\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(data [x],\n",
" data [y],\n",
" test_size=0.2,\n",
" random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "G-FJIe7I5l_y"
},
"outputs": [],
"source": [
"#apply standardization\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"# Create a StandardScaler instance\n",
"scaler = StandardScaler()\n",
"\n",
"\n",
"# Fit the scaler on the training data and transform it\n",
"X_train_scaled = scaler.fit_transform(X_train)\n",
"\n",
"# Use the same scaler to transform the test data\n",
"X_test_scaled = scaler.transform(X_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3QovU1aQKtSK"
},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"from imblearn.over_sampling import SMOTE\n",
"from imblearn.under_sampling import RandomUnderSampler\n",
"from imblearn.combine import SMOTEENN # A hybrid method\n",
"\n",
"\n",
"# Apply SMOTE to oversample the minority class\n",
"smote = SMOTE(sampling_strategy='auto', random_state=23)\n",
"X_train_smote, y_train_smote = smote.fit_resample(X_train_scaled, y_train)\n",
"\n",
"# Apply undersampling to the majority class\n",
"under_sampler = RandomUnderSampler(sampling_strategy='auto', random_state=23)\n",
"X_train_combined, y_train_combined = under_sampler.fit_resample(X_train_smote, y_train_smote)\n",
"\n",
"# Train and evaluate your machine learning model using X_train_combined and y_train_combined\n",
"# Evaluate the model on X_test_scaled and y_test\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Y3zpltoiRuZf",
"outputId": "52c8cbe1-0677-458c-8439-229f75cd9102"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"0 4138\n",
"1 1496\n",
"Name: Churn, dtype: int64\n"
]
}
],
"source": [
"# Print the count of each class in the resampled data\n",
"print(y_train.value_counts())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "OvL3oikyRubx"
},
"outputs": [],
"source": [
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Conv1D, MaxPooling1D, Flatten, Dense, Dropout, BatchNormalization\n",
"from tensorflow.keras.optimizers import Adam\n",
"from tensorflow.keras.losses import BinaryCrossentropy\n",
"\n",
"input_shape = (X_train_combined.shape[1], 1)\n",
"model = Sequential()\n",
"\n",
"# Add Convolutional and Pooling layers\n",
"model.add(Conv1D(filters=128, kernel_size=3, activation='relu', input_shape=input_shape))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"model.add(Conv1D(filters=256, kernel_size=3, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(MaxPooling1D(pool_size=2))\n",
"\n",
"model.add(Flatten())\n",
"\n",
"# Add Dense layers\n",
"model.add(Dense(units=512, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=256, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=128, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=64, activation='relu'))\n",
"model.add(BatchNormalization()) # Add batch normalization\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(units=1, activation='sigmoid'))\n",
"\n",
"# Compile the model\n",
"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])\n",
"#model.summary()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rQ-CLmPXRuei",
"outputId": "f282b810-e9de-4c9e-c79f-d3e266ccc299"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Epoch 1/50\n",
"207/207 - 23s - loss: 0.9346 - accuracy: 0.5953 - val_loss: 0.9881 - val_accuracy: 0.0175 - 23s/epoch - 113ms/step\n",
"Epoch 2/50\n",
"207/207 - 9s - loss: 0.7704 - accuracy: 0.6651 - val_loss: 0.5959 - val_accuracy: 0.6818 - 9s/epoch - 44ms/step\n",
"Epoch 3/50\n",
"207/207 - 6s - loss: 0.7259 - accuracy: 0.6819 - val_loss: 0.6197 - val_accuracy: 0.6775 - 6s/epoch - 29ms/step\n",
"Epoch 4/50\n",
"207/207 - 8s - loss: 0.6682 - accuracy: 0.7023 - val_loss: 0.7392 - val_accuracy: 0.6359 - 8s/epoch - 37ms/step\n",
"Epoch 5/50\n",
"207/207 - 6s - loss: 0.6619 - accuracy: 0.7053 - val_loss: 0.6975 - val_accuracy: 0.6534 - 6s/epoch - 31ms/step\n",
"Epoch 6/50\n",
"207/207 - 11s - loss: 0.6255 - accuracy: 0.7115 - val_loss: 0.7327 - val_accuracy: 0.6292 - 11s/epoch - 53ms/step\n",
"Epoch 7/50\n",
"207/207 - 10s - loss: 0.6255 - accuracy: 0.7231 - val_loss: 0.7228 - val_accuracy: 0.6377 - 10s/epoch - 49ms/step\n",
"Epoch 8/50\n",
"207/207 - 7s - loss: 0.5987 - accuracy: 0.7308 - val_loss: 0.7631 - val_accuracy: 0.6087 - 7s/epoch - 33ms/step\n",
"Epoch 9/50\n",
"207/207 - 8s - loss: 0.5881 - accuracy: 0.7261 - val_loss: 0.7450 - val_accuracy: 0.6159 - 8s/epoch - 39ms/step\n",
"Epoch 10/50\n",
"207/207 - 6s - loss: 0.5690 - accuracy: 0.7393 - val_loss: 0.7004 - val_accuracy: 0.6226 - 6s/epoch - 28ms/step\n",
"Epoch 11/50\n",
"207/207 - 7s - loss: 0.5640 - accuracy: 0.7363 - val_loss: 0.6844 - val_accuracy: 0.6329 - 7s/epoch - 34ms/step\n",
"Epoch 12/50\n",
"207/207 - 6s - loss: 0.5630 - accuracy: 0.7370 - val_loss: 0.6931 - val_accuracy: 0.6347 - 6s/epoch - 30ms/step\n",
"Epoch 13/50\n",
"207/207 - 7s - loss: 0.5465 - accuracy: 0.7464 - val_loss: 0.6858 - val_accuracy: 0.6498 - 7s/epoch - 34ms/step\n",
"Epoch 14/50\n",
"207/207 - 6s - loss: 0.5338 - accuracy: 0.7485 - val_loss: 0.6808 - val_accuracy: 0.6546 - 6s/epoch - 30ms/step\n",
"Epoch 15/50\n",
"207/207 - 10s - loss: 0.5212 - accuracy: 0.7541 - val_loss: 0.6619 - val_accuracy: 0.6564 - 10s/epoch - 47ms/step\n",
"Epoch 16/50\n",
"207/207 - 7s - loss: 0.5093 - accuracy: 0.7642 - val_loss: 0.6243 - val_accuracy: 0.6745 - 7s/epoch - 33ms/step\n",
"Epoch 17/50\n",
"207/207 - 7s - loss: 0.5075 - accuracy: 0.7633 - val_loss: 0.6680 - val_accuracy: 0.6419 - 7s/epoch - 36ms/step\n",
"Epoch 18/50\n",
"207/207 - 6s - loss: 0.4967 - accuracy: 0.7692 - val_loss: 0.6463 - val_accuracy: 0.6594 - 6s/epoch - 31ms/step\n",
"Epoch 19/50\n",
"207/207 - 7s - loss: 0.4882 - accuracy: 0.7660 - val_loss: 0.6170 - val_accuracy: 0.6733 - 7s/epoch - 36ms/step\n",
"Epoch 20/50\n",
"207/207 - 7s - loss: 0.4887 - accuracy: 0.7689 - val_loss: 0.6570 - val_accuracy: 0.6492 - 7s/epoch - 33ms/step\n",
"Epoch 21/50\n",
"207/207 - 7s - loss: 0.4764 - accuracy: 0.7689 - val_loss: 0.6374 - val_accuracy: 0.6685 - 7s/epoch - 35ms/step\n",
"Epoch 22/50\n",
"207/207 - 6s - loss: 0.4575 - accuracy: 0.7867 - val_loss: 0.6370 - val_accuracy: 0.6594 - 6s/epoch - 31ms/step\n",
"Epoch 23/50\n",
"207/207 - 7s - loss: 0.4501 - accuracy: 0.7863 - val_loss: 0.6717 - val_accuracy: 0.6401 - 7s/epoch - 33ms/step\n",
"Epoch 24/50\n",
"207/207 - 7s - loss: 0.4569 - accuracy: 0.7890 - val_loss: 0.6037 - val_accuracy: 0.6812 - 7s/epoch - 35ms/step\n",
"Epoch 25/50\n",
"207/207 - 7s - loss: 0.4424 - accuracy: 0.7947 - val_loss: 0.6320 - val_accuracy: 0.6600 - 7s/epoch - 32ms/step\n",
"Epoch 26/50\n",
"207/207 - 7s - loss: 0.4406 - accuracy: 0.7958 - val_loss: 0.5989 - val_accuracy: 0.6848 - 7s/epoch - 36ms/step\n",
"Epoch 27/50\n",
"207/207 - 6s - loss: 0.4366 - accuracy: 0.8039 - val_loss: 0.5870 - val_accuracy: 0.7005 - 6s/epoch - 31ms/step\n",
"Epoch 28/50\n",
"207/207 - 7s - loss: 0.4286 - accuracy: 0.8035 - val_loss: 0.6480 - val_accuracy: 0.6504 - 7s/epoch - 34ms/step\n",
"Epoch 29/50\n",
"207/207 - 6s - loss: 0.4183 - accuracy: 0.8122 - val_loss: 0.5804 - val_accuracy: 0.6969 - 6s/epoch - 30ms/step\n",
"Epoch 30/50\n",
"207/207 - 7s - loss: 0.4345 - accuracy: 0.7998 - val_loss: 0.6189 - val_accuracy: 0.6733 - 7s/epoch - 36ms/step\n",
"Epoch 31/50\n",
"207/207 - 7s - loss: 0.4214 - accuracy: 0.8094 - val_loss: 0.6892 - val_accuracy: 0.6316 - 7s/epoch - 33ms/step\n",
"Epoch 32/50\n",
"207/207 - 7s - loss: 0.4069 - accuracy: 0.8136 - val_loss: 0.6283 - val_accuracy: 0.6727 - 7s/epoch - 33ms/step\n",
"Epoch 33/50\n",
"207/207 - 6s - loss: 0.4138 - accuracy: 0.8086 - val_loss: 0.6545 - val_accuracy: 0.6473 - 6s/epoch - 30ms/step\n",
"Epoch 34/50\n",
"207/207 - 7s - loss: 0.3980 - accuracy: 0.8145 - val_loss: 0.5803 - val_accuracy: 0.7005 - 7s/epoch - 35ms/step\n",
"Epoch 35/50\n",
"207/207 - 6s - loss: 0.3980 - accuracy: 0.8180 - val_loss: 0.5666 - val_accuracy: 0.7174 - 6s/epoch - 30ms/step\n",
"Epoch 36/50\n",
"207/207 - 7s - loss: 0.3974 - accuracy: 0.8279 - val_loss: 0.5356 - val_accuracy: 0.7162 - 7s/epoch - 36ms/step\n",
"Epoch 37/50\n",
"207/207 - 7s - loss: 0.3830 - accuracy: 0.8248 - val_loss: 0.6666 - val_accuracy: 0.6492 - 7s/epoch - 33ms/step\n",
"Epoch 38/50\n",
"207/207 - 7s - loss: 0.3867 - accuracy: 0.8276 - val_loss: 0.5013 - val_accuracy: 0.7397 - 7s/epoch - 36ms/step\n",
"Epoch 39/50\n",
"207/207 - 6s - loss: 0.3862 - accuracy: 0.8237 - val_loss: 0.5407 - val_accuracy: 0.6981 - 6s/epoch - 28ms/step\n",
"Epoch 40/50\n",
"207/207 - 8s - loss: 0.3766 - accuracy: 0.8258 - val_loss: 0.5544 - val_accuracy: 0.7180 - 8s/epoch - 37ms/step\n",
"Epoch 41/50\n",
"207/207 - 6s - loss: 0.3723 - accuracy: 0.8320 - val_loss: 0.5233 - val_accuracy: 0.7343 - 6s/epoch - 31ms/step\n",
"Epoch 42/50\n",
"207/207 - 8s - loss: 0.3736 - accuracy: 0.8299 - val_loss: 0.5076 - val_accuracy: 0.7470 - 8s/epoch - 39ms/step\n",
"Epoch 43/50\n",
"207/207 - 6s - loss: 0.3675 - accuracy: 0.8349 - val_loss: 0.5012 - val_accuracy: 0.7470 - 6s/epoch - 30ms/step\n",
"Epoch 44/50\n",
"207/207 - 7s - loss: 0.3681 - accuracy: 0.8310 - val_loss: 0.4906 - val_accuracy: 0.7536 - 7s/epoch - 35ms/step\n",
"Epoch 45/50\n",
"207/207 - 6s - loss: 0.3672 - accuracy: 0.8384 - val_loss: 0.4517 - val_accuracy: 0.7772 - 6s/epoch - 29ms/step\n",
"Epoch 46/50\n",
"207/207 - 8s - loss: 0.3668 - accuracy: 0.8301 - val_loss: 0.5894 - val_accuracy: 0.7035 - 8s/epoch - 38ms/step\n",
"Epoch 47/50\n",
"207/207 - 7s - loss: 0.3630 - accuracy: 0.8331 - val_loss: 0.4551 - val_accuracy: 0.7838 - 7s/epoch - 32ms/step\n",
"Epoch 48/50\n",
"207/207 - 8s - loss: 0.3580 - accuracy: 0.8403 - val_loss: 0.5125 - val_accuracy: 0.7458 - 8s/epoch - 37ms/step\n",
"Epoch 49/50\n",
"207/207 - 7s - loss: 0.3591 - accuracy: 0.8358 - val_loss: 0.5191 - val_accuracy: 0.7488 - 7s/epoch - 31ms/step\n",
"Epoch 50/50\n",
"207/207 - 8s - loss: 0.3521 - accuracy: 0.8394 - val_loss: 0.5111 - val_accuracy: 0.7488 - 8s/epoch - 38ms/step\n",
"Execution time: 393.3023612499237 seconds\n"
]
}
],
"source": [
"start_time = time.time()\n",
"history = model.fit(X_train_combined, y_train_combined, epochs=50, validation_split=0.2, verbose=2)\n",
"end_time = time.time()\n",
"execution_time = end_time - start_time\n",
"print(\"Execution time:\", execution_time, \"seconds\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uHghgdzqPqes",
"outputId": "ad258e5e-c892-4187-9e16-fcc86da46f81"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"45/45 [==============================] - 0s 8ms/step - loss: 0.4506 - accuracy: 0.7857\n",
"Original Model Accuracy: 0.7856636047363281\n"
]
}
],
"source": [
"original_model_accuracy = model.evaluate(X_test_scaled, y_test)[1]\n",
"print(\"Original Model Accuracy:\", original_model_accuracy)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "IqfYDHNXP2fN"
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ezYMBcQPRuoP",
"outputId": "7493a7e2-5af9-44d1-a4f4-5978facdea66"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"45/45 [==============================] - 1s 8ms/step\n",
"[[926 110]\n",
" [192 181]]\n",
" precision recall f1-score support\n",
"\n",
" 0 0.83 0.89 0.86 1036\n",
" 1 0.62 0.49 0.55 373\n",
"\n",
" accuracy 0.79 1409\n",
" macro avg 0.73 0.69 0.70 1409\n",
"weighted avg 0.77 0.79 0.78 1409\n",
"\n"
]
}
],
"source": [
"from sklearn.metrics import confusion_matrix , classification_report\n",
"y_pred = model.predict(X_test_scaled) > 0.5\n",
"print(confusion_matrix(y_test, y_pred))\n",
"print(classification_report(y_test, y_pred))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XemPQMXeGRIM",
"outputId": "88608f3a-277f-45bf-b05a-c302853d1a91"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"259/259 [==============================] - 2s 6ms/step\n",
"45/45 [==============================] - 0s 8ms/step\n",
"259/259 [==============================] - 3s 11ms/step\n",
"45/45 [==============================] - 0s 8ms/step\n"
]
}
],
"source": [
"train_set_preds = [round (x[0]) for x in model.predict(X_train_combined)]\n",
"test_set_preds = [round (x[0]) for x in model.predict(X_test_scaled)]\n",
"train_preds = [x[0] for x in model.predict(X_train_combined)]\n",
"test_preds = [x[0] for x in model.predict(X_test_scaled)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "sSptsawURuqy",
"outputId": "76145816-85b9-44b2-8666-c61a9527c621"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Accuracy for test set: 0.7857\n",
"Accuracy for train set: 0.8502\n",
"\n",
"\n",
"Precision for test set: 0.6220\n",
"Precision for train set: 0.9089\n",
"\n",
"\n",
"Recall for test set: 0.4853\n",
"Recall for train set: 0.7784\n"
]
}
],
"source": [
"print('Accuracy for test set: %0.4f' % accuracy_score (y_test, test_set_preds))\n",
"print('Accuracy for train set: %0.4f' % accuracy_score (y_train_combined, train_set_preds))\n",
"\n",
"print('\\n')\n",
"\n",
"print('Precision for test set: %0.4f' % precision_score (y_test, test_set_preds))\n",
"print('Precision for train set: %0.4f' % precision_score (y_train_combined, train_set_preds))\n",
"\n",
"print('\\n')\n",
"\n",
"print('Recall for test set: %0.4f' % recall_score (y_test, test_set_preds))\n",
"print('Recall for train set: %0.4f' % recall_score (y_train_combined, train_set_preds))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ABbju20HRuv8"
},
"outputs": [],
"source": [
"def boundary_attack(model, x, y, max_iterations=20, step_size=0.001, distance_metric='l2'):\n",
" original_preds = model.predict(x)\n",
" original_preds = np.round(original_preds).flatten() #Convert to 1d array to fit input shape of y\n",
"\n",
" # Initialize the adversarial examples with the original inputs\n",
" x_adv = np.copy(x)\n",
"\n",
" for i in range(len(x)):\n",
" x_curr = tf.convert_to_tensor(x_adv[i:i+1]) # get x[i] and Convert to tf.Tensor\n",
"\n",
" for _ in range(max_iterations):\n",
" # Compute the gradient of the loss w.r.t. the input\n",
" with tf.GradientTape() as tape: #Create context tape.Record operations on x_curr for gradient computationss\n",
" tape.watch(x_curr) #tracks operations when x_curr forward passes\n",
" preds = model(x_curr, training=False) #prediction for x_curr\n",
" loss = tf.keras.losses.BinaryCrossentropy()(y[i:i+1, np.newaxis], preds) #Calculate loss.New axis-To match shape of preds\n",
"\n",
" gradients = tape.gradient(loss, x_curr) #magnitude of steepest ascent/descent\n",
"\n",
" # Compute the normalized gradient direction\n",
" if distance_metric == 'l2': #Euclidean distance\n",
" gradient_direction = gradients / (tf.norm(gradients) + 1e-10)\n",
" elif distance_metric == 'l1': #Manhatten distance\n",
" gradient_direction = tf.sign(gradients)\n",
" else:\n",
" raise ValueError(\"Invalid distance metric. Choose 'l1' or 'l2'.\")\n",
"\n",
" # Update the adversarial example by taking a step towards the gradient direction\n",
" x_curr = x_curr + step_size * gradient_direction\n",
"\n",
" x_curr = tf.clip_by_value(x_curr, 0, 1)\n",
"\n",
" # Check if the adversarial example has caused a misclassification\n",
" adv_preds = model(x_curr, training=False)\n",
" adv_preds = np.round(adv_preds).flatten()\n",
"\n",
" if adv_preds != original_preds[i]:\n",
" # The adversarial example successfully caused a misclassification\n",
" x_adv[i] = x_curr.numpy()[0]\n",
" break\n",
"\n",
" return x_adv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dc4SEg9JRuyh",
"outputId": "a74682cd-5056-4e5c-9667-529f3a772485"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"45/45 [==============================] - 1s 6ms/step\n"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-16-86250228f126>:16: FutureWarning: Support for multi-dimensional indexing (e.g. `obj[:, None]`) is deprecated and will be removed in a future version. Convert to a numpy array before indexing instead.\n",
" loss = tf.keras.losses.BinaryCrossentropy()(y[i:i+1, np.newaxis], preds) #Calculate loss.New axis-To match shape of preds\n"
]
}
],
"source": [
"# Generate the adversarial samples using the boundary attack\n",
"X_test_adv= boundary_attack(model, X_test, y_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YZtyaOZJLCbN",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "093d5752-ed31-4d52-9f8a-e4ac96afe3d4"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"45/45 [==============================] - 0s 8ms/step - loss: 26.4888 - accuracy: 0.2647\n"
]
}
],
"source": [
"# Calculate the accuracy of the model on the adversarial examples\n",
"adv_accuracy = model.evaluate(X_test_adv, y_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YnN1iGFkRu5v",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "4c630c23-bfcd-4cc0-ed3b-4975d68e1854"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"45/45 [==============================] - 1s 9ms/step - loss: 699.5123 - accuracy: 0.7353\n",
"Comparing Accuracy Pre and Post Boundary Attack\n",
"Pre Attack: 0.7856636047363281\n",
"Post Attack: 0.26472675800323486\n"
]
}
],
"source": [
"#comparing accuracies pre and post attack\n",
"org_accuracy=model.evaluate(X_test,y_test)\n",
"\n",
"print(\"Comparing Accuracy Pre and Post Boundary Attack\")\n",
"print(\"Pre Attack: \" ,original_model_accuracy)\n",
"print(\"Post Attack: \",adv_accuracy[1])\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XsKYx3gmoiFC"
},
"source": []
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment