{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom keras import Sequential\nfrom keras.layers import Dense","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-20T21:17:54.118083Z","iopub.execute_input":"2021-05-20T21:17:54.118475Z","iopub.status.idle":"2021-05-20T21:18:01.162122Z","shell.execute_reply.started":"2021-05-20T21:17:54.11845Z","shell.execute_reply":"2021-05-20T21:18:01.160927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.python.client import device_lib\nprint(device_lib.list_local_devices())","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:01.164441Z","iopub.execute_input":"2021-05-20T21:18:01.164966Z","iopub.status.idle":"2021-05-20T21:18:01.185573Z","shell.execute_reply.started":"2021-05-20T21:18:01.164924Z","shell.execute_reply":"2021-05-20T21:18:01.184794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = pd.read_csv('../input/creditcards-fraud-detection/creditcard_train.csv')\ndata_test = pd.read_csv('../input/creditcards-fraud-detection/creditcard_test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:01.186626Z","iopub.execute_input":"2021-05-20T21:18:01.18684Z","iopub.status.idle":"2021-05-20T21:18:06.071192Z","shell.execute_reply.started":"2021-05-20T21:18:01.186818Z","shell.execute_reply":"2021-05-20T21:18:06.06947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:06.073109Z","iopub.execute_input":"2021-05-20T21:18:06.073512Z","iopub.status.idle":"2021-05-20T21:18:06.122898Z","shell.execute_reply.started":"2021-05-20T21:18:06.07347Z","shell.execute_reply":"2021-05-20T21:18:06.121773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = data_train.sort_values('Time')\ndata_train","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:06.125799Z","iopub.execute_input":"2021-05-20T21:18:06.126198Z","iopub.status.idle":"2021-05-20T21:18:06.254684Z","shell.execute_reply.started":"2021-05-20T21:18:06.126155Z","shell.execute_reply":"2021-05-20T21:18:06.253074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:06.871215Z","iopub.execute_input":"2021-05-20T21:18:06.871566Z","iopub.status.idle":"2021-05-20T21:18:06.878456Z","shell.execute_reply.started":"2021-05-20T21:18:06.871537Z","shell.execute_reply":"2021-05-20T21:18:06.876982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.hist(data_train['Class'])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:12.737168Z","iopub.execute_input":"2021-05-20T21:18:12.737539Z","iopub.status.idle":"2021-05-20T21:18:12.937445Z","shell.execute_reply.started":"2021-05-20T21:18:12.737513Z","shell.execute_reply":"2021-05-20T21:18:12.93642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX = data_train.drop(columns=['Class', 'Time'])\ny = data_train['Class']\ntrain_size = int(199364 * 0.7)\nX_train = X[:train_size]\nX_test = X[train_size:]\ny_train = y[:train_size]\ny_test = y[train_size:]\n#X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=11)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:19.495641Z","iopub.execute_input":"2021-05-20T21:18:19.495962Z","iopub.status.idle":"2021-05-20T21:18:20.28882Z","shell.execute_reply.started":"2021-05-20T21:18:19.495935Z","shell.execute_reply":"2021-05-20T21:18:20.287469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:24.563394Z","iopub.execute_input":"2021-05-20T21:18:24.563688Z","iopub.status.idle":"2021-05-20T21:18:24.639169Z","shell.execute_reply.started":"2021-05-20T21:18:24.563663Z","shell.execute_reply":"2021-05-20T21:18:24.637838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(y_train)\nprint(\"1 is \" + str(sum(y_train) / len(y_train)) + \"%\")","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:30.080624Z","iopub.execute_input":"2021-05-20T21:18:30.081048Z","iopub.status.idle":"2021-05-20T21:18:30.215154Z","shell.execute_reply.started":"2021-05-20T21:18:30.081015Z","shell.execute_reply":"2021-05-20T21:18:30.21339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(y_test)\nprint(\"1 is \" + str(sum(y_test) / len(y_test)) + \"%\")","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:35.204929Z","iopub.execute_input":"2021-05-20T21:18:35.206141Z","iopub.status.idle":"2021-05-20T21:18:35.333586Z","shell.execute_reply.started":"2021-05-20T21:18:35.206102Z","shell.execute_reply":"2021-05-20T21:18:35.332456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier = Sequential()\nclassifier.add(Dense(11, activation='relu', kernel_initializer='random_normal', input_dim=29))\n#classifier.add(Dense(111, activation='relu', kernel_initializer='random_normal', input_dim=29))\nclassifier.add(Dense(33, activation='relu', kernel_initializer='random_normal'))\nclassifier.add(Dense(1, activation='sigmoid', kernel_initializer='random_normal'))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:18:53.172216Z","iopub.execute_input":"2021-05-20T21:18:53.172517Z","iopub.status.idle":"2021-05-20T21:18:53.26145Z","shell.execute_reply.started":"2021-05-20T21:18:53.172486Z","shell.execute_reply":"2021-05-20T21:18:53.260397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Compiling the neural network\nfrom keras import metrics\nclassifier.compile(optimizer ='adam',loss='binary_crossentropy', metrics =[metrics.AUC(name='auc')])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:19:00.110571Z","iopub.execute_input":"2021-05-20T21:19:00.110865Z","iopub.status.idle":"2021-05-20T21:19:00.130948Z","shell.execute_reply.started":"2021-05-20T21:19:00.110835Z","shell.execute_reply":"2021-05-20T21:19:00.129077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import class_weight\nclass_weights = class_weight.compute_class_weight('balanced',\n                                                 np.unique(y_train),\n                                                 y_train)\nclass_weights = dict(zip([0, 1], class_weights))\nlr_model_history = classifier.fit(X_train,y_train, batch_size=100000, epochs=111, class_weight=class_weights, validation_data=(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:19:05.344519Z","iopub.execute_input":"2021-05-20T21:19:05.344799Z","iopub.status.idle":"2021-05-20T21:19:42.366438Z","shell.execute_reply.started":"2021-05-20T21:19:05.344775Z","shell.execute_reply":"2021-05-20T21:19:42.365149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(10,6))\nax.plot(np.sqrt(lr_model_history.history['loss']), 'r', label='train')\nax.plot(np.sqrt(lr_model_history.history['val_loss']), 'b' ,label='val')\nax.set_xlabel(r'Epoch', fontsize=20)\nax.set_ylabel(r'Loss', fontsize=20)\nax.legend()\nax.tick_params(labelsize=20)\n\n# Plot the accuracy\nfig, ax = plt.subplots(1, 1, figsize=(10,6))\nax.plot(np.sqrt(lr_model_history.history['auc']), 'r', label='train')\nax.plot(np.sqrt(lr_model_history.history['val_auc']), 'b' ,label='val')\nax.set_xlabel(r'Epoch', fontsize=20)\nax.set_ylabel(r'AUC', fontsize=20)\nax.legend()\nax.tick_params(labelsize=20)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:19:42.368563Z","iopub.execute_input":"2021-05-20T21:19:42.368793Z","iopub.status.idle":"2021-05-20T21:19:42.661198Z","shell.execute_reply.started":"2021-05-20T21:19:42.36877Z","shell.execute_reply":"2021-05-20T21:19:42.660001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\nfrom sklearn.metrics import confusion_matrix\ny_pred = classifier.predict(X_test)\nprint(roc_auc_score(y_test, y_pred))#0.987228030433605\nprint(confusion_matrix(y_test, y_pred > 0.5))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:19:42.662939Z","iopub.execute_input":"2021-05-20T21:19:42.663275Z","iopub.status.idle":"2021-05-20T21:19:43.897962Z","shell.execute_reply.started":"2021-05-20T21:19:42.663244Z","shell.execute_reply":"2021-05-20T21:19:43.89624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_curve\nfpr, tpr, thresholds = roc_curve(y_test, y_pred)\nplt.plot([0,1], [0,1], linestyle='--', label='No Skill')\nplt.plot(fpr, tpr, marker='.')\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:19:43.900011Z","iopub.execute_input":"2021-05-20T21:19:43.900384Z","iopub.status.idle":"2021-05-20T21:19:44.027559Z","shell.execute_reply.started":"2021-05-20T21:19:43.900352Z","shell.execute_reply":"2021-05-20T21:19:44.026417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind = np.argmax(np.sqrt(tpr * (1 - fpr)))\nthresh = thresholds[ind]\nprint(\"Threshold: \" + str(thresh))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:19:47.349719Z","iopub.execute_input":"2021-05-20T21:19:47.350022Z","iopub.status.idle":"2021-05-20T21:19:47.356117Z","shell.execute_reply.started":"2021-05-20T21:19:47.349998Z","shell.execute_reply":"2021-05-20T21:19:47.354324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\nfrom sklearn.metrics import confusion_matrix\ny_pred = classifier.predict(X_test)\nprint(roc_auc_score(y_test, y_pred))#0.987228030433605\nprint(confusion_matrix(y_test, y_pred > thresh))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:19:53.184186Z","iopub.execute_input":"2021-05-20T21:19:53.184537Z","iopub.status.idle":"2021-05-20T21:19:54.322085Z","shell.execute_reply.started":"2021-05-20T21:19:53.184506Z","shell.execute_reply":"2021-05-20T21:19:54.320537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data_train.drop(columns=['Class', 'Time'])\nX = scaler.fit_transform(X)\nlr_model_history = classifier.fit(X,y, batch_size=10000, epochs=111, class_weight=class_weights)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:20:03.594629Z","iopub.execute_input":"2021-05-20T21:20:03.595113Z","iopub.status.idle":"2021-05-20T21:20:33.792657Z","shell.execute_reply.started":"2021-05-20T21:20:03.595086Z","shell.execute_reply":"2021-05-20T21:20:33.791001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(10,6))\nax.plot(np.sqrt(lr_model_history.history['loss']), 'r', label='train')\nax.set_xlabel(r'Epoch', fontsize=20)\nax.set_ylabel(r'Loss', fontsize=20)\nax.legend()\nax.tick_params(labelsize=20)\n\n# Plot the accuracy\nfig, ax = plt.subplots(1, 1, figsize=(10,6))\nax.plot(np.sqrt(lr_model_history.history['auc']), 'r', label='train')\nax.set_xlabel(r'Epoch', fontsize=20)\nax.set_ylabel(r'auc', fontsize=20)\nax.legend()\nax.tick_params(labelsize=20)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:20:33.794442Z","iopub.execute_input":"2021-05-20T21:20:33.794667Z","iopub.status.idle":"2021-05-20T21:20:34.053192Z","shell.execute_reply.started":"2021-05-20T21:20:33.794644Z","shell.execute_reply":"2021-05-20T21:20:34.051821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\nfrom sklearn.metrics import confusion_matrix\ny_pred = classifier.predict(X_test)\nprint(roc_auc_score(y_test, y_pred))#0.987228030433605\nprint(confusion_matrix(y_test, y_pred > 0.5))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:20:34.054454Z","iopub.execute_input":"2021-05-20T21:20:34.054737Z","iopub.status.idle":"2021-05-20T21:20:35.247745Z","shell.execute_reply.started":"2021-05-20T21:20:34.054708Z","shell.execute_reply":"2021-05-20T21:20:35.247027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test = pd.read_csv('../input/creditcards-fraud-detection/creditcard_test.csv')\ntest = data_test.drop(columns=['Time'])\ntest = scaler.transform(test)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:20:35.248881Z","iopub.execute_input":"2021-05-20T21:20:35.249359Z","iopub.status.idle":"2021-05-20T21:20:35.885866Z","shell.execute_reply.started":"2021-05-20T21:20:35.249326Z","shell.execute_reply":"2021-05-20T21:20:35.884582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm = pd.read_csv('../input/creditcards-fraud-detection/creditcard_sample_solution.csv')\npreds = classifier.predict(test)\nsubm['Predicted'] = list(map(int, preds > 0.5))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:20:37.596132Z","iopub.execute_input":"2021-05-20T21:20:37.596558Z","iopub.status.idle":"2021-05-20T21:20:39.234346Z","shell.execute_reply.started":"2021-05-20T21:20:37.596524Z","shell.execute_reply":"2021-05-20T21:20:39.233258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subm.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:20:44.413406Z","iopub.execute_input":"2021-05-20T21:20:44.413758Z","iopub.status.idle":"2021-05-20T21:20:44.569278Z","shell.execute_reply.started":"2021-05-20T21:20:44.413729Z","shell.execute_reply":"2021-05-20T21:20:44.568413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sum(subm['Predicted']) / len(subm))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T21:20:49.814326Z","iopub.execute_input":"2021-05-20T21:20:49.814803Z","iopub.status.idle":"2021-05-20T21:20:49.83409Z","shell.execute_reply.started":"2021-05-20T21:20:49.814772Z","shell.execute_reply":"2021-05-20T21:20:49.832335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}