{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nimport keras\nfrom sklearn.preprocessing import StandardScaler\n\nfrom sklearn.metrics import confusion_matrix, log_loss, auc, roc_curve, roc_auc_score, recall_score, precision_recall_curve","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-19T19:46:25.771660Z","iopub.execute_input":"2021-05-19T19:46:25.772034Z","iopub.status.idle":"2021-05-19T19:46:34.447581Z","shell.execute_reply.started":"2021-05-19T19:46:25.772003Z","shell.execute_reply":"2021-05-19T19:46:34.446389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling import RandomOverSampler\nfrom imblearn.under_sampling import RandomUnderSampler","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:35.950291Z","iopub.execute_input":"2021-05-19T19:46:35.950651Z","iopub.status.idle":"2021-05-19T19:46:36.767691Z","shell.execute_reply.started":"2021-05-19T19:46:35.950620Z","shell.execute_reply":"2021-05-19T19:46:36.766500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_over_sample(X, Y):\n    ros = RandomOverSampler(random_state=0)\n    X_over, Y_over = ros.fit_resample(X, Y)\n    return X_over, Y_over, ros\n\n\ndef make_under_sample(X, Y):\n    uos = RandomUnderSampler(random_state=0)\n    X_over, Y_over = uos.fit_resample(X, Y)\n    return X_over, Y_over, uos","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:37.159769Z","iopub.execute_input":"2021-05-19T19:46:37.160165Z","iopub.status.idle":"2021-05-19T19:46:37.167114Z","shell.execute_reply.started":"2021-05-19T19:46:37.160132Z","shell.execute_reply":"2021-05-19T19:46:37.165724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"../input/creditcards-fraud-detection/creditcard_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:38.143321Z","iopub.execute_input":"2021-05-19T19:46:38.143689Z","iopub.status.idle":"2021-05-19T19:46:41.794121Z","shell.execute_reply.started":"2021-05-19T19:46:38.143659Z","shell.execute_reply":"2021-05-19T19:46:41.793062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:41.795799Z","iopub.execute_input":"2021-05-19T19:46:41.796192Z","iopub.status.idle":"2021-05-19T19:46:41.881529Z","shell.execute_reply.started":"2021-05-19T19:46:41.796150Z","shell.execute_reply":"2021-05-19T19:46:41.880178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:44.740652Z","iopub.execute_input":"2021-05-19T19:46:44.740969Z","iopub.status.idle":"2021-05-19T19:46:44.758987Z","shell.execute_reply.started":"2021-05-19T19:46:44.740925Z","shell.execute_reply":"2021-05-19T19:46:44.758061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_data['Class']), train_data['Class'].sum())","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:48.832815Z","iopub.execute_input":"2021-05-19T19:46:48.833294Z","iopub.status.idle":"2021-05-19T19:46:48.842282Z","shell.execute_reply.started":"2021-05-19T19:46:48.833244Z","shell.execute_reply":"2021-05-19T19:46:48.840606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights = {0: 1 - train_data['Class'].sum() / len(train_data['Class']), 1: train_data['Class'].sum() / len(train_data['Class'])}","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:49.959352Z","iopub.execute_input":"2021-05-19T19:46:49.959658Z","iopub.status.idle":"2021-05-19T19:46:49.965506Z","shell.execute_reply.started":"2021-05-19T19:46:49.959632Z","shell.execute_reply":"2021-05-19T19:46:49.964152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = pd.DataFrame(train_data.copy()['Class'])\nX = train_data.copy()\nX.drop('Class', axis=1, inplace=True)\nX","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:50.925511Z","iopub.execute_input":"2021-05-19T19:46:50.925833Z","iopub.status.idle":"2021-05-19T19:46:50.995261Z","shell.execute_reply.started":"2021-05-19T19:46:50.925801Z","shell.execute_reply":"2021-05-19T19:46:50.993722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:52.489810Z","iopub.execute_input":"2021-05-19T19:46:52.490122Z","iopub.status.idle":"2021-05-19T19:46:52.500445Z","shell.execute_reply.started":"2021-05-19T19:46:52.490098Z","shell.execute_reply":"2021-05-19T19:46:52.499570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=227)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:52.959658Z","iopub.execute_input":"2021-05-19T19:46:52.960017Z","iopub.status.idle":"2021-05-19T19:46:53.046442Z","shell.execute_reply.started":"2021-05-19T19:46:52.959987Z","shell.execute_reply":"2021-05-19T19:46:53.045436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test, X_dev, Y_test, Y_dev = train_test_split(X_test,Y_test, test_size=.5, random_state=227)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:54.293456Z","iopub.execute_input":"2021-05-19T19:46:54.293922Z","iopub.status.idle":"2021-05-19T19:46:54.311799Z","shell.execute_reply.started":"2021-05-19T19:46:54.293891Z","shell.execute_reply":"2021-05-19T19:46:54.310929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(input_shape):\n    model = tf.keras.models.Sequential()\n    \n    model.add(tf.keras.layers.Dense(30, activation='relu', input_shape=(input_shape,)))\n    model.add(tf.keras.layers.Dense(64, activation='relu',))\n    #model.add(tf.keras.layers.Dense(128, activation='relu',))\n    model.add(tf.keras.layers.Dense(32, activation='relu',))\n    model.add(tf.keras.layers.Dense(32, activation='relu',))\n    model.add(tf.keras.layers.Dense(16, activation='relu',))\n    model.add(tf.keras.layers.Dense(16, activation='relu',))\n    model.add(tf.keras.layers.Dense(8, activation='relu',))\n    model.add(tf.keras.layers.Dense(1, activation='sigmoid',))\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:55:49.510630Z","iopub.execute_input":"2021-05-19T19:55:49.511003Z","iopub.status.idle":"2021-05-19T19:55:49.522097Z","shell.execute_reply.started":"2021-05-19T19:55:49.510969Z","shell.execute_reply":"2021-05-19T19:55:49.520260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.to_numpy()\nY_train = np.asarray(Y_train).astype('float32').reshape((-1,1))\nX_dev = X_dev.to_numpy()\nY_dev = np.asarray(Y_dev).astype('float32').reshape((-1,1))\nX_test = X_test.to_numpy()\nY_test = Y_test.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:57.172527Z","iopub.execute_input":"2021-05-19T19:46:57.172870Z","iopub.status.idle":"2021-05-19T19:46:57.180786Z","shell.execute_reply.started":"2021-05-19T19:46:57.172841Z","shell.execute_reply":"2021-05-19T19:46:57.179172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"No of training Examples : \"+str(X_train.shape[0]))  # 94% data \nprint(\"No of test Examples : \"+str(X_test.shape[0]))       # 3% data\nprint(\"No of dev Examples : \"+str(X_dev.shape[0]))         # 3% data\nprint(\"Shape of training data : \"+str(X_train.shape))\nprint(\"Shape of test data : \"+str(X_test.shape))\nprint(\"Shape of dev data : \"+str(X_dev.shape))\nprint(\"Shape of Y test data : \"+str(Y_test.shape))\nprint(\"Shape of Y dev data : \"+str(Y_dev.shape))","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:58.347336Z","iopub.execute_input":"2021-05-19T19:46:58.347793Z","iopub.status.idle":"2021-05-19T19:46:58.355860Z","shell.execute_reply.started":"2021-05-19T19:46:58.347764Z","shell.execute_reply":"2021-05-19T19:46:58.354998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:46:59.634913Z","iopub.execute_input":"2021-05-19T19:46:59.635301Z","iopub.status.idle":"2021-05-19T19:46:59.708983Z","shell.execute_reply.started":"2021-05-19T19:46:59.635271Z","shell.execute_reply":"2021-05-19T19:46:59.707753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_scaled = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:47:02.059173Z","iopub.execute_input":"2021-05-19T19:47:02.059525Z","iopub.status.idle":"2021-05-19T19:47:02.065234Z","shell.execute_reply.started":"2021-05-19T19:47:02.059495Z","shell.execute_reply":"2021-05-19T19:47:02.064343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_dev_scaled = scaler.transform(X_dev)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:47:02.479131Z","iopub.execute_input":"2021-05-19T19:47:02.479604Z","iopub.status.idle":"2021-05-19T19:47:02.486255Z","shell.execute_reply.started":"2021-05-19T19:47:02.479568Z","shell.execute_reply":"2021-05-19T19:47:02.485122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_0 = len(train_data[train_data['Class']==0])\nnum_1 = len(train_data[train_data['Class']==1])\nundersampled_data = pd.concat([ train_data[train_data['Class']==0].sample(num_1) , train_data[train_data['Class']==1] ])","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:47:03.947043Z","iopub.execute_input":"2021-05-19T19:47:03.947323Z","iopub.status.idle":"2021-05-19T19:47:04.041672Z","shell.execute_reply.started":"2021-05-19T19:47:03.947300Z","shell.execute_reply":"2021-05-19T19:47:04.040074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_us = pd.DataFrame(undersampled_data.copy()['Class'])\nX_us = undersampled_data.copy()\nX_us.drop('Class', axis=1, inplace=True)\nX_train_us, X_test_us, Y_train_us, Y_test_us = train_test_split(X_us, Y_us, test_size=0.2, random_state=227)\nX_test_us, X_dev_us, Y_test_us, Y_dev_us = train_test_split(X_test_us,Y_test_us, test_size=.5, random_state=227)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:47:05.253374Z","iopub.execute_input":"2021-05-19T19:47:05.253676Z","iopub.status.idle":"2021-05-19T19:47:05.267632Z","shell.execute_reply.started":"2021-05-19T19:47:05.253650Z","shell.execute_reply":"2021-05-19T19:47:05.265427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_us = X_train_us.to_numpy()\nY_train_us = np.asarray(Y_train_us).astype('float32').reshape((-1,1))\nX_dev_us = X_dev_us.to_numpy()\nY_dev_us = np.asarray(Y_dev_us).astype('float32').reshape((-1,1))\nX_test_us = X_test_us.to_numpy()\nY_test_us = Y_test_us.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:47:06.909457Z","iopub.execute_input":"2021-05-19T19:47:06.910069Z","iopub.status.idle":"2021-05-19T19:47:06.916327Z","shell.execute_reply.started":"2021-05-19T19:47:06.910031Z","shell.execute_reply":"2021-05-19T19:47:06.915515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape_us = X_train_us.shape[1]","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:47:08.063029Z","iopub.execute_input":"2021-05-19T19:47:08.063551Z","iopub.status.idle":"2021-05-19T19:47:08.068431Z","shell.execute_reply.started":"2021-05-19T19:47:08.063512Z","shell.execute_reply":"2021-05-19T19:47:08.067394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape=X_train.shape[1]","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:47:08.308268Z","iopub.execute_input":"2021-05-19T19:47:08.308679Z","iopub.status.idle":"2021-05-19T19:47:08.312422Z","shell.execute_reply.started":"2021-05-19T19:47:08.308653Z","shell.execute_reply":"2021-05-19T19:47:08.311445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.Adam(lr=1e-5)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:47:10.759562Z","iopub.execute_input":"2021-05-19T19:47:10.759985Z","iopub.status.idle":"2021-05-19T19:47:10.773999Z","shell.execute_reply.started":"2021-05-19T19:47:10.759958Z","shell.execute_reply":"2021-05-19T19:47:10.772076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model(input_shape_us)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:04.096803Z","iopub.execute_input":"2021-05-19T19:56:04.097198Z","iopub.status.idle":"2021-05-19T19:56:04.171568Z","shell.execute_reply.started":"2021-05-19T19:56:04.097167Z","shell.execute_reply":"2021-05-19T19:56:04.170011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:04.623370Z","iopub.execute_input":"2021-05-19T19:56:04.623737Z","iopub.status.idle":"2021-05-19T19:56:04.628510Z","shell.execute_reply.started":"2021-05-19T19:56:04.623706Z","shell.execute_reply":"2021-05-19T19:56:04.627231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer=optimizer,\n    loss='binary_crossentropy',\n    metrics=['accuracy'],\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:04.946889Z","iopub.execute_input":"2021-05-19T19:56:04.947420Z","iopub.status.idle":"2021-05-19T19:56:04.961462Z","shell.execute_reply.started":"2021-05-19T19:56:04.947382Z","shell.execute_reply":"2021-05-19T19:56:04.960251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_s, Y_s, ros = make_over_sample(X_train, Y_train)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:05.241471Z","iopub.execute_input":"2021-05-19T19:56:05.241866Z","iopub.status.idle":"2021-05-19T19:56:05.323524Z","shell.execute_reply.started":"2021-05-19T19:56:05.241831Z","shell.execute_reply":"2021-05-19T19:56:05.321998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler_s = StandardScaler()\nX_s_scaled = scaler_s.fit_transform(X_s)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:05.725847Z","iopub.execute_input":"2021-05-19T19:56:05.726158Z","iopub.status.idle":"2021-05-19T19:56:05.893990Z","shell.execute_reply.started":"2021-05-19T19:56:05.726134Z","shell.execute_reply":"2021-05-19T19:56:05.892647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_dev_scaled = scaler_s.transform(X_dev)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:06.032402Z","iopub.execute_input":"2021-05-19T19:56:06.032717Z","iopub.status.idle":"2021-05-19T19:56:06.038138Z","shell.execute_reply.started":"2021-05-19T19:56:06.032682Z","shell.execute_reply":"2021-05-19T19:56:06.037266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_scaled = scaler_s.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:06.280816Z","iopub.execute_input":"2021-05-19T19:56:06.281251Z","iopub.status.idle":"2021-05-19T19:56:06.286569Z","shell.execute_reply.started":"2021-05-19T19:56:06.281213Z","shell.execute_reply":"2021-05-19T19:56:06.286024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    X_s_scaled, Y_s,\n    batch_size=256,\n    epochs=12,\n    validation_data=(X_dev_scaled, Y_dev),\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:06.707919Z","iopub.execute_input":"2021-05-19T19:56:06.708279Z","iopub.status.idle":"2021-05-19T19:56:43.725594Z","shell.execute_reply.started":"2021-05-19T19:56:06.708250Z","shell.execute_reply":"2021-05-19T19:56:43.724459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(X_test_scaled, Y_test)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:46.662791Z","iopub.execute_input":"2021-05-19T19:56:46.663176Z","iopub.status.idle":"2021-05-19T19:56:47.756772Z","shell.execute_reply.started":"2021-05-19T19:56:46.663145Z","shell.execute_reply":"2021-05-19T19:56:47.755834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_us.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:50.150161Z","iopub.execute_input":"2021-05-19T19:56:50.150467Z","iopub.status.idle":"2021-05-19T19:56:50.156302Z","shell.execute_reply.started":"2021-05-19T19:56:50.150442Z","shell.execute_reply":"2021-05-19T19:56:50.154667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Accuracy on test dataset:', test_accuracy)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:50.663589Z","iopub.execute_input":"2021-05-19T19:56:50.664015Z","iopub.status.idle":"2021-05-19T19:56:50.670146Z","shell.execute_reply.started":"2021-05-19T19:56:50.663979Z","shell.execute_reply":"2021-05-19T19:56:50.668636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(X_test_scaled, Y_test)\nprint('Accuracy on test dataset:', test_accuracy)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:51.764617Z","iopub.execute_input":"2021-05-19T19:56:51.765030Z","iopub.status.idle":"2021-05-19T19:56:52.735086Z","shell.execute_reply.started":"2021-05-19T19:56:51.764997Z","shell.execute_reply":"2021-05-19T19:56:52.734029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_lst = []\nfor i in range(len(Y_test)):\n    test_lst.append(Y_test[i][0])\ntest_lst = np.array(test_lst)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:53.215252Z","iopub.execute_input":"2021-05-19T19:56:53.215547Z","iopub.status.idle":"2021-05-19T19:56:53.232229Z","shell.execute_reply.started":"2021-05-19T19:56:53.215523Z","shell.execute_reply":"2021-05-19T19:56:53.231340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test_scaled)\nlst = []\nfor i in range(len(predictions)):\n    lst.append(predictions[i][0])","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:53.401675Z","iopub.execute_input":"2021-05-19T19:56:53.402086Z","iopub.status.idle":"2021-05-19T19:56:53.877049Z","shell.execute_reply.started":"2021-05-19T19:56:53.402061Z","shell.execute_reply":"2021-05-19T19:56:53.875837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lst = np.array(lst)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:54.803156Z","iopub.execute_input":"2021-05-19T19:56:54.803545Z","iopub.status.idle":"2021-05-19T19:56:54.814086Z","shell.execute_reply.started":"2021-05-19T19:56:54.803510Z","shell.execute_reply":"2021-05-19T19:56:54.813228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"roc_auc_score(test_lst, lst)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:56:55.141127Z","iopub.execute_input":"2021-05-19T19:56:55.141594Z","iopub.status.idle":"2021-05-19T19:56:55.153718Z","shell.execute_reply.started":"2021-05-19T19:56:55.141564Z","shell.execute_reply":"2021-05-19T19:56:55.152581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(\"../input/creditcards-fraud-detection/creditcard_test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:57:08.543495Z","iopub.execute_input":"2021-05-19T19:57:08.543789Z","iopub.status.idle":"2021-05-19T19:57:09.817959Z","shell.execute_reply.started":"2021-05-19T19:57:08.543765Z","shell.execute_reply":"2021-05-19T19:57:09.816801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test_data.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:57:12.259821Z","iopub.execute_input":"2021-05-19T19:57:12.260232Z","iopub.status.idle":"2021-05-19T19:57:12.265343Z","shell.execute_reply.started":"2021-05-19T19:57:12.260200Z","shell.execute_reply":"2021-05-19T19:57:12.264137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = scaler_s.transform(test_data)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:57:13.397765Z","iopub.execute_input":"2021-05-19T19:57:13.398085Z","iopub.status.idle":"2021-05-19T19:57:13.412815Z","shell.execute_reply.started":"2021-05-19T19:57:13.398060Z","shell.execute_reply":"2021-05-19T19:57:13.411697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:57:14.810164Z","iopub.execute_input":"2021-05-19T19:57:14.810491Z","iopub.status.idle":"2021-05-19T19:57:17.239994Z","shell.execute_reply.started":"2021-05-19T19:57:14.810456Z","shell.execute_reply":"2021-05-19T19:57:17.238964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lst = []\nfor i in range(len(test_predictions)):\n    lst.append(test_predictions[i][0])","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:57:17.996140Z","iopub.execute_input":"2021-05-19T19:57:17.996490Z","iopub.status.idle":"2021-05-19T19:57:18.069325Z","shell.execute_reply.started":"2021-05-19T19:57:17.996453Z","shell.execute_reply":"2021-05-19T19:57:18.067684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indeces = np.arange(len(lst))","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:57:19.282224Z","iopub.execute_input":"2021-05-19T19:57:19.282559Z","iopub.status.idle":"2021-05-19T19:57:19.287018Z","shell.execute_reply.started":"2021-05-19T19:57:19.282530Z","shell.execute_reply":"2021-05-19T19:57:19.286262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_ = {'Id': indeces, 'Predicted': lst}","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:57:20.135359Z","iopub.execute_input":"2021-05-19T19:57:20.135838Z","iopub.status.idle":"2021-05-19T19:57:20.139556Z","shell.execute_reply.started":"2021-05-19T19:57:20.135808Z","shell.execute_reply":"2021-05-19T19:57:20.138746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame(data=data_)","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:57:20.877346Z","iopub.execute_input":"2021-05-19T19:57:20.877826Z","iopub.status.idle":"2021-05-19T19:57:20.906614Z","shell.execute_reply.started":"2021-05-19T19:57:20.877796Z","shell.execute_reply":"2021-05-19T19:57:20.905607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('Credit_card.csv', index=False)\nresult","metadata":{"execution":{"iopub.status.busy":"2021-05-19T19:57:22.283344Z","iopub.execute_input":"2021-05-19T19:57:22.283827Z","iopub.status.idle":"2021-05-19T19:57:22.584323Z","shell.execute_reply.started":"2021-05-19T19:57:22.283798Z","shell.execute_reply":"2021-05-19T19:57:22.583320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import HTML\nimport pandas as pd\nimport numpy as np\nimport base64","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_download_link(df, title = \"Download CSV file\", filename = \"data.csv\"):  \n    csv = df.to_csv(index=False)\n    b64 = base64.b64encode(csv.encode())\n    payload = b64.decode()\n    html = '<a download=\"{filename}\" href=\"data:text/csv;base64,{payload}\" target=\"_blank\">{title}</a>'\n    html = html.format(payload=payload,title=title,filename=filename)\n    return HTML(html)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_download_link(result)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}