{"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":"markdown","source":"# ****Importing packages and modules****","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.compose import ColumnTransformer\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.preprocessing import OrdinalEncoder\nfrom catboost import CatBoostClassifier\nfrom sklearn.model_selection import GridSearchCV\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Dropout\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics import roc_curve, roc_auc_score\nfrom sklearn.model_selection import train_test_split\nfrom sklearn .metrics import roc_auc_score\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-12T14:03:22.630003Z","iopub.execute_input":"2023-11-12T14:03:22.630374Z","iopub.status.idle":"2023-11-12T14:03:22.638344Z","shell.execute_reply.started":"2023-11-12T14:03:22.630348Z","shell.execute_reply":"2023-11-12T14:03:22.636002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Preprocessing Train data****","metadata":{}},{"cell_type":"code","source":"train_data=pd.read_csv('/kaggle/input/anyas-gojo-revival-bondman/trainData.csv')\ntrain_fraud=train_data['is_fraud']\nnumerical_cols=[i for i in train_data.columns if train_data[i].dtype in ['int64', 'float64']]\ncategorical_cols=[i for i in train_data.columns if train_data[i].dtype==\"object\" ]\nprint(categorical_cols)\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:22.641188Z","iopub.execute_input":"2023-11-12T14:03:22.641623Z","iopub.status.idle":"2023-11-12T14:03:24.660189Z","shell.execute_reply.started":"2023-11-12T14:03:22.641589Z","shell.execute_reply":"2023-11-12T14:03:24.658549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:24.664107Z","iopub.execute_input":"2023-11-12T14:03:24.664528Z","iopub.status.idle":"2023-11-12T14:03:24.859976Z","shell.execute_reply.started":"2023-11-12T14:03:24.664494Z","shell.execute_reply":"2023-11-12T14:03:24.858769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:24.863272Z","iopub.execute_input":"2023-11-12T14:03:24.863651Z","iopub.status.idle":"2023-11-12T14:03:25.077533Z","shell.execute_reply.started":"2023-11-12T14:03:24.863619Z","shell.execute_reply":"2023-11-12T14:03:25.076060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.drop(columns=['id','trans_date_trans_time','cc_num','first','last','street','dob','trans_num'],inplace=True)\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:25.080318Z","iopub.execute_input":"2023-11-12T14:03:25.080620Z","iopub.status.idle":"2023-11-12T14:03:25.125075Z","shell.execute_reply.started":"2023-11-12T14:03:25.080596Z","shell.execute_reply":"2023-11-12T14:03:25.124239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=[i for i in train_data.columns if train_data[i].dtype==\"object\" ]\nencoder = OrdinalEncoder(\n        handle_unknown='use_encoded_value', \n        unknown_value=-1\n    ).fit(train_data[features])\ntrain_data[features]=encoder.transform(train_data[features])\ntrain_data.head()\ntrain_y=train_data['is_fraud']\ntrain_data.pop('is_fraud')","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:25.126514Z","iopub.execute_input":"2023-11-12T14:03:25.126819Z","iopub.status.idle":"2023-11-12T14:03:25.962051Z","shell.execute_reply.started":"2023-11-12T14:03:25.126792Z","shell.execute_reply":"2023-11-12T14:03:25.960955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Training Data Visualization****","metadata":{}},{"cell_type":"code","source":"visual_data=pd.read_csv('/kaggle/input/anyas-gojo-revival-bondman/trainData.csv')\nplt.figure(figsize=(8, 6))\nsns.countplot(x='gender', data=visual_data, palette='viridis')\nplt.title('Frequency Bar Chart for Categorical Column')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:25.963727Z","iopub.execute_input":"2023-11-12T14:03:25.964739Z","iopub.status.idle":"2023-11-12T14:03:28.588367Z","shell.execute_reply.started":"2023-11-12T14:03:25.964696Z","shell.execute_reply":"2023-11-12T14:03:28.587080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nplt.pie(visual_data['category'].value_counts().values, labels=visual_data['category'].value_counts().index, autopct='%1.1f%%', startangle=90, colors=sns.color_palette('viridis'))\nplt.title('Pie Chart with Legends for Categorical Column')\nplt.legend(title='Categories', labels=visual_data['category'].value_counts().index, loc='best')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:28.590066Z","iopub.execute_input":"2023-11-12T14:03:28.590864Z","iopub.status.idle":"2023-11-12T14:03:29.058796Z","shell.execute_reply.started":"2023-11-12T14:03:28.590825Z","shell.execute_reply":"2023-11-12T14:03:29.057139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.plot(visual_data['id'],visual_data['amt'])\nplt.title('Line Plot using Seaborn')\nplt.xlabel('id')\nplt.ylabel('Amount')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:29.061357Z","iopub.execute_input":"2023-11-12T14:03:29.061765Z","iopub.status.idle":"2023-11-12T14:03:29.350935Z","shell.execute_reply.started":"2023-11-12T14:03:29.061731Z","shell.execute_reply":"2023-11-12T14:03:29.349636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.scatter(visual_data['lat'], visual_data['long'], c=visual_data['is_fraud'])\nplt.title('Geographical Distribution of Transactions by people')\nplt.xlabel('Latitude')\nplt.ylabel('Longitude')\nplt.colorbar(label='Fraudulent')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:29.352368Z","iopub.execute_input":"2023-11-12T14:03:29.352702Z","iopub.status.idle":"2023-11-12T14:03:37.092158Z","shell.execute_reply.started":"2023-11-12T14:03:29.352674Z","shell.execute_reply":"2023-11-12T14:03:37.090797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.scatter(visual_data['merch_lat'], visual_data['merch_long'], c=visual_data['is_fraud'])\nplt.title('Geographical Distribution of Transactions by merchants')\nplt.xlabel('Latitude')\nplt.ylabel('Longitude')\nplt.colorbar(label='Fraudulent')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:37.096799Z","iopub.execute_input":"2023-11-12T14:03:37.097632Z","iopub.status.idle":"2023-11-12T14:03:44.750262Z","shell.execute_reply.started":"2023-11-12T14:03:37.097562Z","shell.execute_reply":"2023-11-12T14:03:44.749297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****PreProcessing Test Data****","metadata":{}},{"cell_type":"code","source":"test_data=pd.read_csv('/kaggle/input/anyas-gojo-revival-bondman/testData.csv')\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:44.751291Z","iopub.execute_input":"2023-11-12T14:03:44.751564Z","iopub.status.idle":"2023-11-12T14:03:47.002675Z","shell.execute_reply.started":"2023-11-12T14:03:44.751541Z","shell.execute_reply":"2023-11-12T14:03:47.001103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:47.004524Z","iopub.execute_input":"2023-11-12T14:03:47.005001Z","iopub.status.idle":"2023-11-12T14:03:47.206287Z","shell.execute_reply.started":"2023-11-12T14:03:47.004962Z","shell.execute_reply":"2023-11-12T14:03:47.204780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.drop(columns=['id','trans_date_trans_time','cc_num','first','last','street','dob','trans_num'],inplace=True)\nfeatures=[i for i in test_data.columns if test_data[i].dtype==\"object\" ]\nencoder = OrdinalEncoder(\n        handle_unknown='use_encoded_value', \n        unknown_value=-1\n    ).fit(test_data[features])\ntest_data[features]=encoder.transform(test_data[features])\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:47.207603Z","iopub.execute_input":"2023-11-12T14:03:47.207895Z","iopub.status.idle":"2023-11-12T14:03:48.134534Z","shell.execute_reply.started":"2023-11-12T14:03:47.207872Z","shell.execute_reply":"2023-11-12T14:03:48.133940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Data Visualization of Test Data****","metadata":{}},{"cell_type":"code","source":"test_visual_data=pd.read_csv('/kaggle/input/anyas-gojo-revival-bondman/testData.csv')\nplt.figure(figsize=(8, 6))\nsns.countplot(x='gender', data=test_visual_data, palette='viridis')\nplt.title('Frequency Bar Chart for Categorical Column')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:48.135412Z","iopub.execute_input":"2023-11-12T14:03:48.136701Z","iopub.status.idle":"2023-11-12T14:03:50.718608Z","shell.execute_reply.started":"2023-11-12T14:03:48.136655Z","shell.execute_reply":"2023-11-12T14:03:50.717542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nplt.pie(test_visual_data['category'].value_counts().values, labels=test_visual_data['category'].value_counts().index, autopct='%1.1f%%', startangle=90, colors=sns.color_palette('viridis'))\nplt.title('Pie Chart with Legends for Categorical Column')\nplt.legend(title='Categories', labels=test_visual_data['category'].value_counts().index, loc='best')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:50.720207Z","iopub.execute_input":"2023-11-12T14:03:50.720575Z","iopub.status.idle":"2023-11-12T14:03:51.156063Z","shell.execute_reply.started":"2023-11-12T14:03:50.720541Z","shell.execute_reply":"2023-11-12T14:03:51.154954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.plot(test_visual_data['id'],test_visual_data['amt'])\nplt.title('Line Plot using Seaborn')\nplt.xlabel('id')\nplt.ylabel('Amount')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:51.157411Z","iopub.execute_input":"2023-11-12T14:03:51.157686Z","iopub.status.idle":"2023-11-12T14:03:51.419581Z","shell.execute_reply.started":"2023-11-12T14:03:51.157661Z","shell.execute_reply":"2023-11-12T14:03:51.418235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Making of Model and training of model****","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(train_data, train_y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:51.421323Z","iopub.execute_input":"2023-11-12T14:03:51.421756Z","iopub.status.idle":"2023-11-12T14:03:51.585451Z","shell.execute_reply.started":"2023-11-12T14:03:51.421720Z","shell.execute_reply":"2023-11-12T14:03:51.584113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(256, activation='relu', input_dim=14))\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dense(16, activation='relu'))\nmodel.add(Dense(8, activation='relu'))\nmodel.add(Dense(4, activation='relu'))\nmodel.add(Dense(2, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid')) \nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) \nsscaler = StandardScaler()\nX_train = sscaler.fit_transform(X_train)\nmodel.fit(X_train, y_train, epochs=20, batch_size=50)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:03:51.587833Z","iopub.execute_input":"2023-11-12T14:03:51.588604Z","iopub.status.idle":"2023-11-12T14:14:13.927447Z","shell.execute_reply.started":"2023-11-12T14:03:51.588542Z","shell.execute_reply":"2023-11-12T14:14:13.926101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = sscaler.fit_transform( X_test)\narr=model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:14:13.928943Z","iopub.execute_input":"2023-11-12T14:14:13.929279Z","iopub.status.idle":"2023-11-12T14:14:19.186154Z","shell.execute_reply.started":"2023-11-12T14:14:13.929250Z","shell.execute_reply":"2023-11-12T14:14:19.184221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Model Analysis****","metadata":{}},{"cell_type":"code","source":"arrset=[]\nprint(arr)\nfor i in arr:\n    if i[0]>0.03:\n        arrset.append(1)\n    else:\n        arrset.append(0)\nroc_auc = roc_auc_score(y_test, arrset)\nprint(\"The value of roc_auc is \"+str(roc_auc))","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:14:19.189004Z","iopub.execute_input":"2023-11-12T14:14:19.189453Z","iopub.status.idle":"2023-11-12T14:14:19.291937Z","shell.execute_reply.started":"2023-11-12T14:14:19.189427Z","shell.execute_reply":"2023-11-12T14:14:19.290124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\nfpr, tpr, thresholds = roc_curve(y_test, arrset)\nroc_auc = auc(fpr, tpr)\nplt.figure(figsize=(8, 8))\nplt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (AUC = {roc_auc:.2f})')\nplt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--', label='Random')\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('Receiver Operating Characteristic (ROC) Curve')\nplt.legend(loc='lower right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:14:19.293513Z","iopub.execute_input":"2023-11-12T14:14:19.293901Z","iopub.status.idle":"2023-11-12T14:14:19.547089Z","shell.execute_reply.started":"2023-11-12T14:14:19.293869Z","shell.execute_reply":"2023-11-12T14:14:19.546002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Predictions on Test data****","metadata":{}},{"cell_type":"code","source":"test_data = sscaler.fit_transform(test_data)\narr1=model.predict(test_data)\nprint(arr1)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:14:19.548328Z","iopub.execute_input":"2023-11-12T14:14:19.548665Z","iopub.status.idle":"2023-11-12T14:14:48.320456Z","shell.execute_reply.started":"2023-11-12T14:14:19.548629Z","shell.execute_reply":"2023-11-12T14:14:48.319386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arrset1=[]\nroc=[]\nprint(len(arr1))\nfor i in arr1:\n    if i[0]>0.03:\n        arrset1.append(1)\n    else:\n        arrset1.append(0)\n    roc.append(i[0])\nprint(sum(arrset1))","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:15:29.033607Z","iopub.execute_input":"2023-11-12T14:15:29.034503Z","iopub.status.idle":"2023-11-12T14:15:29.974918Z","shell.execute_reply.started":"2023-11-12T14:15:29.034459Z","shell.execute_reply":"2023-11-12T14:15:29.973578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Result Visualzation****","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.scatter(test_visual_data['merch_lat'], test_visual_data['merch_long'], c=arrset1)\nplt.title('Geographical Distribution of Transactions by merchants')\nplt.xlabel('Latitude')\nplt.ylabel('Longitude')\nplt.colorbar(label='Fraudulent')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:15:32.719911Z","iopub.execute_input":"2023-11-12T14:15:32.720294Z","iopub.status.idle":"2023-11-12T14:15:40.809170Z","shell.execute_reply.started":"2023-11-12T14:15:32.720266Z","shell.execute_reply":"2023-11-12T14:15:40.807819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.scatter(test_visual_data['lat'], test_visual_data['long'], c=arrset1)\nplt.title('Geographical Distribution of Transactions')\nplt.xlabel('Latitude')\nplt.ylabel('Longitude')\nplt.colorbar(label='Fraudulent')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:15:50.312919Z","iopub.execute_input":"2023-11-12T14:15:50.313403Z","iopub.status.idle":"2023-11-12T14:15:58.441064Z","shell.execute_reply.started":"2023-11-12T14:15:50.313367Z","shell.execute_reply":"2023-11-12T14:15:58.440127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum(arr)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:15:05.613752Z","iopub.execute_input":"2023-11-12T14:15:05.614126Z","iopub.status.idle":"2023-11-12T14:15:05.653438Z","shell.execute_reply.started":"2023-11-12T14:15:05.614094Z","shell.execute_reply":"2023-11-12T14:15:05.652137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Downloading CSV file****","metadata":{}},{"cell_type":"code","source":"submission=pd.DataFrame({'id':range(1,len(arr1)+1),'id_fraud':arrset1})\nsubmission.to_csv('submission20.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-12T14:18:14.191415Z","iopub.execute_input":"2023-11-12T14:18:14.191894Z","iopub.status.idle":"2023-11-12T14:18:14.796447Z","shell.execute_reply.started":"2023-11-12T14:18:14.191834Z","shell.execute_reply":"2023-11-12T14:18:14.795629Z"},"trusted":true},"execution_count":null,"outputs":[]}]}