{"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 libraries \nimport pandas as pd\n\ndf_train = pd.read_csv('/kaggle/input/anyas-gojo-revival-bondman/trainData.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-07T12:01:09.379466Z","iopub.execute_input":"2023-11-07T12:01:09.379933Z","iopub.status.idle":"2023-11-07T12:01:14.612599Z","shell.execute_reply.started":"2023-11-07T12:01:09.379897Z","shell.execute_reply":"2023-11-07T12:01:14.611416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# usefull features in df_train\ncol_features = [\n    'trans_date_trans_time', \n    'category', \n    'amt',  \n    'gender', \n    'state', \n    'lat', \n    'long', \n    'city_pop', \n    'dob',  \n    'job',  \n    'merch_lat', \n    'merch_long',\n    'is_fraud']\n\ndf_train = df_train[col_features]","metadata":{"execution":{"iopub.status.busy":"2023-11-07T12:01:14.614740Z","iopub.execute_input":"2023-11-07T12:01:14.615166Z","iopub.status.idle":"2023-11-07T12:01:14.726955Z","shell.execute_reply.started":"2023-11-07T12:01:14.615134Z","shell.execute_reply":"2023-11-07T12:01:14.725406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['trans_date_trans_time'][0]","metadata":{"execution":{"iopub.status.busy":"2023-11-07T12:06:06.096828Z","iopub.execute_input":"2023-11-07T12:06:06.097259Z","iopub.status.idle":"2023-11-07T12:06:06.105352Z","shell.execute_reply.started":"2023-11-07T12:06:06.097224Z","shell.execute_reply":"2023-11-07T12:06:06.104306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# First Looking at train dataset, visualization","metadata":{}},{"cell_type":"markdown","source":"**TIME:** ","metadata":{}},{"cell_type":"code","source":"df_train['trans_date_trans_time'] = pd.to_datetime(df_train['trans_date_trans_time'])\n\n# take only usefull time\ndef take_hour(x): \n     return x.hour\n\ndf_train['trans_time'] = df_train['trans_date_trans_time'].apply(take_hour)\n\ndf_train = df_train.drop(['trans_date_trans_time'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T12:12:06.751037Z","iopub.execute_input":"2023-11-07T12:12:06.751566Z","iopub.status.idle":"2023-11-07T12:12:08.797163Z","shell.execute_reply.started":"2023-11-07T12:12:06.751522Z","shell.execute_reply":"2023-11-07T12:12:08.795851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we can see that most part of fraud happen at the night\ndf_train.groupby(['trans_time']).agg({'is_fraud': 'mean'}).plot()","metadata":{"execution":{"iopub.status.busy":"2023-11-07T12:18:31.150579Z","iopub.execute_input":"2023-11-07T12:18:31.150955Z","iopub.status.idle":"2023-11-07T12:18:31.408718Z","shell.execute_reply.started":"2023-11-07T12:18:31.150926Z","shell.execute_reply":"2023-11-07T12:18:31.407726Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**AMOUT:** ","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt \n\n# Histogram for 'amt'\nplt.figure(figsize=(7, 5))\nplt.hist(df_train['amt'], bins=30, color='skyblue', edgecolor='black')\nplt.title('Distribution of Transaction Amounts')\nplt.xlabel('Amount in American Dollars')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-07T12:28:36.733680Z","iopub.execute_input":"2023-11-07T12:28:36.734094Z","iopub.status.idle":"2023-11-07T12:28:37.054642Z","shell.execute_reply.started":"2023-11-07T12:28:36.734062Z","shell.execute_reply":"2023-11-07T12:28:37.053454Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**GENDER**","metadata":{}},{"cell_type":"code","source":"# Stacked bar plot for 'gender' by 'is_fraud'\ngender_is_fraud = df_train.groupby(['gender', 'is_fraud']).size().unstack()\ngender_is_fraud.plot(kind='bar', stacked=True, color=['skyblue', 'pink'])\nplt.title('Gender Distribution by Fraud Status')\nplt.xlabel('Gender')\nplt.ylabel('Count')\nplt.xticks(rotation=0)\nplt.legend(title='Fraud Status', labels=['Not Fraud', 'Fraud'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-07T12:33:29.584035Z","iopub.execute_input":"2023-11-07T12:33:29.584522Z","iopub.status.idle":"2023-11-07T12:33:29.919259Z","shell.execute_reply.started":"2023-11-07T12:33:29.584465Z","shell.execute_reply":"2023-11-07T12:33:29.918009Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**LATITUDE AND LONGTITUDE:**","metadata":{}},{"cell_type":"code","source":"# Scatter plot for 'lat' and 'long'\nplt.figure(figsize=(10, 5))\nplt.scatter(df_train['lat'], df_train['long'], c=df_train['is_fraud'], cmap='coolwarm')\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-07T12:36:48.441863Z","iopub.execute_input":"2023-11-07T12:36:48.442266Z","iopub.status.idle":"2023-11-07T12:36:59.535566Z","shell.execute_reply.started":"2023-11-07T12:36:48.442236Z","shell.execute_reply":"2023-11-07T12:36:59.534338Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**AMT BY IS_FRAUD**","metadata":{}},{"cell_type":"code","source":"# Box plot for 'amt' by 'is_fraud'\nplt.figure(figsize=(5, 5))\ndf_train.boxplot(column='amt', by='is_fraud')\nplt.title('Boxplot of Transaction Amounts by Fraud Status')\nplt.suptitle('')\nplt.xlabel('Fraud Status')\nplt.ylabel('Amount in American Dollars')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-07T12:38:44.923991Z","iopub.execute_input":"2023-11-07T12:38:44.924425Z","iopub.status.idle":"2023-11-07T12:38:45.943779Z","shell.execute_reply.started":"2023-11-07T12:38:44.924394Z","shell.execute_reply":"2023-11-07T12:38:45.942375Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**STATE BY IS_FRAUD**","metadata":{}},{"cell_type":"code","source":"# Bar plot for 'state' by 'is_fraud'\nfraud_by_state = df_train[df_train['is_fraud'] == 1]['state'].value_counts()\nplt.figure(figsize=(10, 5))\nplt.bar(fraud_by_state.index, fraud_by_state.values, color='skyblue')\nplt.title('Fraudulent Transactions by State')\nplt.xlabel('State')\nplt.ylabel('Count')\nplt.xticks(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-07T12:41:06.386828Z","iopub.execute_input":"2023-11-07T12:41:06.387401Z","iopub.status.idle":"2023-11-07T12:41:06.963654Z","shell.execute_reply.started":"2023-11-07T12:41:06.387352Z","shell.execute_reply":"2023-11-07T12:41:06.961939Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]}]}