{"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\ndf = pd.read_csv(\"../input/creditcards-fraud-detection/creditcard_train.csv\", index_col=0)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:27:07.643139Z","iopub.execute_input":"2021-05-20T13:27:07.643616Z","iopub.status.idle":"2021-05-20T13:27:11.050326Z","shell.execute_reply.started":"2021-05-20T13:27:07.643519Z","shell.execute_reply":"2021-05-20T13:27:11.049279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.options.display.max_columns=35\ndf.describe()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:27:19.803246Z","iopub.execute_input":"2021-05-20T13:27:19.803652Z","iopub.status.idle":"2021-05-20T13:27:20.139492Z","shell.execute_reply.started":"2021-05-20T13:27:19.803616Z","shell.execute_reply":"2021-05-20T13:27:20.138434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.loc[df['Amount'] > 5000]","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:27:24.508654Z","iopub.execute_input":"2021-05-20T13:27:24.509068Z","iopub.status.idle":"2021-05-20T13:27:24.612626Z","shell.execute_reply.started":"2021-05-20T13:27:24.509033Z","shell.execute_reply":"2021-05-20T13:27:24.611155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import preprocessing\nscaler = preprocessing. StandardScaler()\ndf['NormalizedAmount'] = scaler.fit_transform (df['Amount']. values.reshape (-1, 1))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:27:30.339294Z","iopub.execute_input":"2021-05-20T13:27:30.339940Z","iopub.status.idle":"2021-05-20T13:27:31.387396Z","shell.execute_reply.started":"2021-05-20T13:27:30.339891Z","shell.execute_reply":"2021-05-20T13:27:31.385966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:27:33.216889Z","iopub.execute_input":"2021-05-20T13:27:33.217329Z","iopub.status.idle":"2021-05-20T13:27:33.257042Z","shell.execute_reply.started":"2021-05-20T13:27:33.217293Z","shell.execute_reply":"2021-05-20T13:27:33.255823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop ('Amount', axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:27:34.450361Z","iopub.execute_input":"2021-05-20T13:27:34.450770Z","iopub.status.idle":"2021-05-20T13:27:34.512026Z","shell.execute_reply.started":"2021-05-20T13:27:34.450734Z","shell.execute_reply":"2021-05-20T13:27:34.511132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = df['Class']\nX = df.drop('Class', axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:27:37.285607Z","iopub.execute_input":"2021-05-20T13:27:37.286006Z","iopub.status.idle":"2021-05-20T13:27:37.309225Z","shell.execute_reply.started":"2021-05-20T13:27:37.285973Z","shell.execute_reply":"2021-05-20T13:27:37.308170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:27:40.223942Z","iopub.execute_input":"2021-05-20T13:27:40.224567Z","iopub.status.idle":"2021-05-20T13:27:40.368331Z","shell.execute_reply.started":"2021-05-20T13:27:40.224518Z","shell.execute_reply":"2021-05-20T13:27:40.367470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nmodel = keras.Sequential([\nlayers.Dense(input_dim = 29, units = 16, activation = 'relu'),\nlayers.Dense(units = 24, activation = 'relu'),\nlayers.Dropout(0.5),\nlayers.Dense(units = 20, activation = 'relu'),\nlayers.Dense(units = 24, activation = 'relu'),\nlayers.Dense(units =1, activation = 'sigmoid'),])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:27:43.576912Z","iopub.execute_input":"2021-05-20T13:27:43.577268Z","iopub.status.idle":"2021-05-20T13:27:49.329504Z","shell.execute_reply.started":"2021-05-20T13:27:43.577236Z","shell.execute_reply":"2021-05-20T13:27:49.328645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = 'binary_crossentropy', metrics = ['accuracy'])\nmodel.fit(X_train, y_train, batch_size = 15, epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:28:07.412776Z","iopub.execute_input":"2021-05-20T13:28:07.413214Z","iopub.status.idle":"2021-05-20T13:29:19.889253Z","shell.execute_reply.started":"2021-05-20T13:28:07.413155Z","shell.execute_reply":"2021-05-20T13:29:19.888381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.evaluate(X_test, y_test)\nprint(results)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:29:40.380188Z","iopub.execute_input":"2021-05-20T13:29:40.380659Z","iopub.status.idle":"2021-05-20T13:29:42.377834Z","shell.execute_reply.started":"2021-05-20T13:29:40.380615Z","shell.execute_reply":"2021-05-20T13:29:42.376434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling import SMOTE\nX_resample, y_resample = SMOTE().fit_resample(X, y)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:29:46.641855Z","iopub.execute_input":"2021-05-20T13:29:46.642266Z","iopub.status.idle":"2021-05-20T13:29:47.506481Z","shell.execute_reply.started":"2021-05-20T13:29:46.642233Z","shell.execute_reply":"2021-05-20T13:29:47.505159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X_resample, y_resample, test_size = 0.3, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:29:51.142234Z","iopub.execute_input":"2021-05-20T13:29:51.142793Z","iopub.status.idle":"2021-05-20T13:29:51.292429Z","shell.execute_reply.started":"2021-05-20T13:29:51.142749Z","shell.execute_reply":"2021-05-20T13:29:51.291053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = 'binary_crossentropy', metrics = ['accuracy'])\nmodel.fit(X_train, y_train, batch_size = 15, epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:29:52.582418Z","iopub.execute_input":"2021-05-20T13:29:52.582793Z","iopub.status.idle":"2021-05-20T13:32:16.420306Z","shell.execute_reply.started":"2021-05-20T13:29:52.582760Z","shell.execute_reply":"2021-05-20T13:32:16.418835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.evaluate(X_test, y_test)\nprint(results)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:32:22.892214Z","iopub.execute_input":"2021-05-20T13:32:22.892580Z","iopub.status.idle":"2021-05-20T13:32:26.542313Z","shell.execute_reply.started":"2021-05-20T13:32:22.892550Z","shell.execute_reply":"2021-05-20T13:32:26.541231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = 'binary_crossentropy', metrics = ['accuracy'])\nmodel.fit(X_resample, y_resample, batch_size = 15, epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:32:28.214810Z","iopub.execute_input":"2021-05-20T13:32:28.215168Z","iopub.status.idle":"2021-05-20T13:35:52.215879Z","shell.execute_reply.started":"2021-05-20T13:32:28.215134Z","shell.execute_reply":"2021-05-20T13:35:52.214613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/creditcards-fraud-detection/creditcard_test.csv\", index_col=0)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:36:07.172541Z","iopub.execute_input":"2021-05-20T13:36:07.172940Z","iopub.status.idle":"2021-05-20T13:36:08.441329Z","shell.execute_reply.started":"2021-05-20T13:36:07.172909Z","shell.execute_reply":"2021-05-20T13:36:08.440530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['NormalizedAmount'] = scaler.fit_transform (df['Amount']. values.reshape (-1, 1))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:36:10.204573Z","iopub.execute_input":"2021-05-20T13:36:10.205210Z","iopub.status.idle":"2021-05-20T13:36:10.213831Z","shell.execute_reply.started":"2021-05-20T13:36:10.205173Z","shell.execute_reply":"2021-05-20T13:36:10.212605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop ('Amount', axis = 1) ","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:36:11.304958Z","iopub.execute_input":"2021-05-20T13:36:11.305373Z","iopub.status.idle":"2021-05-20T13:36:11.336035Z","shell.execute_reply.started":"2021-05-20T13:36:11.305335Z","shell.execute_reply":"2021-05-20T13:36:11.334705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y=df\nY.describe()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:36:12.698850Z","iopub.execute_input":"2021-05-20T13:36:12.699333Z","iopub.status.idle":"2021-05-20T13:36:12.904627Z","shell.execute_reply.started":"2021-05-20T13:36:12.699280Z","shell.execute_reply":"2021-05-20T13:36:12.903326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted = model.predict(Y)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:36:15.902941Z","iopub.execute_input":"2021-05-20T13:36:15.903566Z","iopub.status.idle":"2021-05-20T13:36:17.626039Z","shell.execute_reply.started":"2021-05-20T13:36:15.903511Z","shell.execute_reply":"2021-05-20T13:36:17.624840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:36:18.717916Z","iopub.execute_input":"2021-05-20T13:36:18.718274Z","iopub.status.idle":"2021-05-20T13:36:18.725658Z","shell.execute_reply.started":"2021-05-20T13:36:18.718237Z","shell.execute_reply":"2021-05-20T13:36:18.724393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame({'Predicted': predicted[0]}).to_csv('solution.csv',index_label='Id')","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:36:20.191035Z","iopub.execute_input":"2021-05-20T13:36:20.191383Z","iopub.status.idle":"2021-05-20T13:36:20.200700Z","shell.execute_reply.started":"2021-05-20T13:36:20.191354Z","shell.execute_reply":"2021-05-20T13:36:20.199116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = 'binary_crossentropy', metrics = ['accuracy'])\nmodel.fit(X, y, batch_size = 15, epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:36:22.959322Z","iopub.execute_input":"2021-05-20T13:36:22.959662Z","iopub.status.idle":"2021-05-20T13:38:05.373904Z","shell.execute_reply.started":"2021-05-20T13:36:22.959634Z","shell.execute_reply":"2021-05-20T13:38:05.372107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/creditcards-fraud-detection/creditcard_test.csv\", index_col=0)\ndf['NormalizedAmount'] = scaler.fit_transform (df['Amount']. values.reshape (-1, 1))\ndf = df.drop ('Amount', axis = 1) \nY=df\nY.describe()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:38:27.947566Z","iopub.execute_input":"2021-05-20T13:38:27.947955Z","iopub.status.idle":"2021-05-20T13:38:28.965280Z","shell.execute_reply.started":"2021-05-20T13:38:27.947920Z","shell.execute_reply":"2021-05-20T13:38:28.963815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted = model.predict(Y)\npredicted=predicted.T","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:38:31.883236Z","iopub.execute_input":"2021-05-20T13:38:31.883640Z","iopub.status.idle":"2021-05-20T13:38:33.570477Z","shell.execute_reply.started":"2021-05-20T13:38:31.883609Z","shell.execute_reply":"2021-05-20T13:38:33.569573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predicted[0])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:38:35.944580Z","iopub.execute_input":"2021-05-20T13:38:35.945231Z","iopub.status.idle":"2021-05-20T13:38:35.952359Z","shell.execute_reply.started":"2021-05-20T13:38:35.945175Z","shell.execute_reply":"2021-05-20T13:38:35.951004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame({'Predicted': predicted[0]}).to_csv('solution1.csv',index_label='Id')","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:38:37.008160Z","iopub.execute_input":"2021-05-20T13:38:37.008564Z","iopub.status.idle":"2021-05-20T13:38:37.311379Z","shell.execute_reply.started":"2021-05-20T13:38:37.008530Z","shell.execute_reply":"2021-05-20T13:38:37.310430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_resample.head","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:38:38.321630Z","iopub.execute_input":"2021-05-20T13:38:38.322017Z","iopub.status.idle":"2021-05-20T13:38:38.328846Z","shell.execute_reply.started":"2021-05-20T13:38:38.321983Z","shell.execute_reply":"2021-05-20T13:38:38.327928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_1, X_2, y_1, y_2 = train_test_split(X_resample, y_resample, test_size = 0.0001, random_state = 0)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:38:41.877728Z","iopub.execute_input":"2021-05-20T13:38:41.878127Z","iopub.status.idle":"2021-05-20T13:38:42.042030Z","shell.execute_reply.started":"2021-05-20T13:38:41.878091Z","shell.execute_reply":"2021-05-20T13:38:42.041035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_1.head","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:38:43.523349Z","iopub.execute_input":"2021-05-20T13:38:43.523756Z","iopub.status.idle":"2021-05-20T13:38:43.530925Z","shell.execute_reply.started":"2021-05-20T13:38:43.523719Z","shell.execute_reply":"2021-05-20T13:38:43.529629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = 'binary_crossentropy', metrics = ['accuracy'])\nmodel.fit(X_1, y_1, batch_size = 15, epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:38:47.606434Z","iopub.execute_input":"2021-05-20T13:38:47.606902Z","iopub.status.idle":"2021-05-20T13:42:07.157033Z","shell.execute_reply.started":"2021-05-20T13:38:47.606861Z","shell.execute_reply":"2021-05-20T13:42:07.156049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_resample1, y_resample1 = SMOTE(sampling_strategy =0.5).fit_resample(X, y)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:12.435152Z","iopub.execute_input":"2021-05-20T13:42:12.435576Z","iopub.status.idle":"2021-05-20T13:42:12.948319Z","shell.execute_reply.started":"2021-05-20T13:42:12.435541Z","shell.execute_reply":"2021-05-20T13:42:12.947052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = 'binary_crossentropy', metrics = ['accuracy'])\nmodel.fit(X_resample1, y_resample1, batch_size = 20, epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:42:16.300781Z","iopub.execute_input":"2021-05-20T13:42:16.301196Z","iopub.status.idle":"2021-05-20T13:44:09.090104Z","shell.execute_reply.started":"2021-05-20T13:42:16.301162Z","shell.execute_reply":"2021-05-20T13:44:09.089048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\nlayers.Dense(input_dim = 29, units = 15, activation = 'relu'),\nlayers.Dense(units = 20, activation = 'relu'),\nlayers.Dropout(0.5),\nlayers.Dense(units = 15, activation = 'relu'),\nlayers.Dense(units =20, activation = 'relu'),\nlayers.Dense(units =15, activation = 'relu'),\nlayers.Dense(units =1, activation = 'sigmoid'),])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:45:50.532576Z","iopub.execute_input":"2021-05-20T13:45:50.533083Z","iopub.status.idle":"2021-05-20T13:45:50.596233Z","shell.execute_reply.started":"2021-05-20T13:45:50.533041Z","shell.execute_reply":"2021-05-20T13:45:50.595116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = 'binary_crossentropy', metrics = ['accuracy'])\nmodel.fit(X_resample1, y_resample1, batch_size = 20, epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:45:52.100232Z","iopub.execute_input":"2021-05-20T13:45:52.100751Z","iopub.status.idle":"2021-05-20T13:47:49.477158Z","shell.execute_reply.started":"2021-05-20T13:45:52.100718Z","shell.execute_reply":"2021-05-20T13:47:49.476222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\nlayers.Dense(input_dim = 29, units = 30, activation = 'relu'),\nlayers.Dense(units = 22, activation = 'relu'),\nlayers.Dropout(0.5),\nlayers.Dense(units = 18, activation = 'relu'),\nlayers.Dense(units = 22, activation = 'relu'),\nlayers.Dense(units =1, activation = 'sigmoid'),])","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:48:50.558972Z","iopub.execute_input":"2021-05-20T13:48:50.559566Z","iopub.status.idle":"2021-05-20T13:48:50.614783Z","shell.execute_reply.started":"2021-05-20T13:48:50.559513Z","shell.execute_reply":"2021-05-20T13:48:50.613833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',loss = 'binary_crossentropy', metrics = ['accuracy'])\nmodel.fit(X_resample1, y_resample1, batch_size = 20, epochs = 8)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T13:48:51.724404Z","iopub.execute_input":"2021-05-20T13:48:51.724837Z","iopub.status.idle":"2021-05-20T13:51:53.014538Z","shell.execute_reply.started":"2021-05-20T13:48:51.724799Z","shell.execute_reply":"2021-05-20T13:51:53.013725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}