{"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 pathlib import Path\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.linear_model import LogisticRegression, LinearRegression\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.model_selection import train_test_split\n%matplotlib inline\nimport gc\nfrom sklearn.linear_model import Lasso\n\ninput_path = Path('/kaggle/input/amex-default-prediction/')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-14T15:27:32.978533Z","iopub.execute_input":"2022-10-14T15:27:32.979137Z","iopub.status.idle":"2022-10-14T15:27:32.987673Z","shell.execute_reply.started":"2022-10-14T15:27:32.979091Z","shell.execute_reply":"2022-10-14T15:27:32.986764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def amex_metric(y_true: pd.Series, y_pred: pd.Series) -> float:\n\n    def top_four_percent_captured(df) -> float:\n        \n        df['weight'] = df['target'].apply(lambda x: 20 if x==0 else 1)\n        four_pct_cutoff = int(0.04 * df['weight'].sum())\n        df['weight_cumsum'] = df['weight'].cumsum()\n        df_cutoff = df.loc[df['weight_cumsum'] <= four_pct_cutoff]\n        return (df_cutoff['target'] == 1).sum() / (df['target'] == 1).sum()\n        \n    def weighted_gini(df) -> float:\n        \n        df['weight'] = df['target'].apply(lambda x: 20 if x==0 else 1)\n        df['random'] = (df['weight'] / df['weight'].sum()).cumsum()\n        total_pos = (df['target'] * df['weight']).sum()\n        df['cum_pos_found'] = (df['target'] * df['weight']).cumsum()\n        df['lorentz'] = df['cum_pos_found'] / total_pos\n        df['gini'] = (df['lorentz'] - df['random']) * df['weight']\n        return df['gini'].sum()\n\n    def normalized_weighted_gini(y_true, df) -> float:\n        y_true_pred = y_true.rename('prediction')\n        true_df = pd.concat([y_true, y_true_pred], axis='columns').sort_values('prediction', ascending=False)\n        return weighted_gini(df) / weighted_gini(true_df)\n\n    df = pd.DataFrame({'target': y_true, 'prediction': y_pred}).sort_values('prediction', ascending=False)\n    g = normalized_weighted_gini(y_true, df.copy())\n    d = top_four_percent_captured(df.copy())\n\n    return 0.5 * (g + d)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:27:32.989390Z","iopub.execute_input":"2022-10-14T15:27:32.990112Z","iopub.status.idle":"2022-10-14T15:27:33.004296Z","shell.execute_reply.started":"2022-10-14T15:27:32.990072Z","shell.execute_reply":"2022-10-14T15:27:33.003055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\n    input_path / 'train_data.csv',\n    index_col='customer_ID',\n    nrows=1_000_000)\n\ntrain_labels = pd.read_csv(input_path / 'train_labels.csv', index_col='customer_ID', nrows=1_000_000)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:27:33.006230Z","iopub.execute_input":"2022-10-14T15:27:33.006874Z","iopub.status.idle":"2022-10-14T15:28:34.123644Z","shell.execute_reply.started":"2022-10-14T15:27:33.006817Z","shell.execute_reply":"2022-10-14T15:28:34.121246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train_data.head())\nprint('\\n')\nprint('*'*50)\nprint('\\n')\ndisplay(train_labels.head())","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:28:34.126020Z","iopub.execute_input":"2022-10-14T15:28:34.126412Z","iopub.status.idle":"2022-10-14T15:28:34.158298Z","shell.execute_reply.started":"2022-10-14T15:28:34.126369Z","shell.execute_reply":"2022-10-14T15:28:34.157319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fetching labales of certain customers whos id is present in the main training dataframe\ntrain_labels = train_labels[train_labels.index.isin(train_data.index)]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:28:34.160868Z","iopub.execute_input":"2022-10-14T15:28:34.161314Z","iopub.status.idle":"2022-10-14T15:28:34.227429Z","shell.execute_reply.started":"2022-10-14T15:28:34.161277Z","shell.execute_reply":"2022-10-14T15:28:34.226678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_last_month = train_data.groupby('customer_ID').tail(1)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:28:34.228933Z","iopub.execute_input":"2022-10-14T15:28:34.229540Z","iopub.status.idle":"2022-10-14T15:28:34.591598Z","shell.execute_reply.started":"2022-10-14T15:28:34.229501Z","shell.execute_reply":"2022-10-14T15:28:34.590689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Last Month Data of each customer')\ndisplay(df_last_month.head())","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:28:34.594714Z","iopub.execute_input":"2022-10-14T15:28:34.595146Z","iopub.status.idle":"2022-10-14T15:28:34.621291Z","shell.execute_reply.started":"2022-10-14T15:28:34.595115Z","shell.execute_reply":"2022-10-14T15:28:34.620456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_data = df_last_month.merge(train_labels, how = 'inner', on = 'customer_ID', validate = 'one_to_one')\nprint('Final Data after mergingx')\ndisplay(final_data.head())","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:28:34.622831Z","iopub.execute_input":"2022-10-14T15:28:34.623479Z","iopub.status.idle":"2022-10-14T15:28:35.058333Z","shell.execute_reply.started":"2022-10-14T15:28:34.623441Z","shell.execute_reply":"2022-10-14T15:28:35.057367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(final_data.corr()['target'].abs().sort_values(ascending = False).head(10))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:28:35.059979Z","iopub.execute_input":"2022-10-14T15:28:35.060697Z","iopub.status.idle":"2022-10-14T15:28:41.326898Z","shell.execute_reply.started":"2022-10-14T15:28:35.060652Z","shell.execute_reply":"2022-10-14T15:28:41.325921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.kdeplot(final_data.loc[final_data['target'].eq(0), 'P_2'])\nsns.kdeplot(final_data.loc[final_data['target'].eq(1), 'P_2']) ","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:28:41.328293Z","iopub.execute_input":"2022-10-14T15:28:41.328754Z","iopub.status.idle":"2022-10-14T15:28:41.965540Z","shell.execute_reply.started":"2022-10-14T15:28:41.328714Z","shell.execute_reply":"2022-10-14T15:28:41.964718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.kdeplot(final_data.loc[final_data['target'].eq(0), 'D_48'])\nsns.kdeplot(final_data.loc[final_data['target'].eq(1), 'D_48'])","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:28:41.968559Z","iopub.execute_input":"2022-10-14T15:28:41.968923Z","iopub.status.idle":"2022-10-14T15:28:42.566080Z","shell.execute_reply.started":"2022-10-14T15:28:41.968871Z","shell.execute_reply":"2022-10-14T15:28:42.565330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"use_cols = ['P_2', 'D_48', 'B_2', 'D_61', 'B_18', 'D_55', 'B_9', 'D_44', 'B_33', 'customer_ID']\ntrain_data = pd.read_csv(input_path / 'train_data.csv', usecols=use_cols)\ntrain_labels = pd.read_csv(input_path / 'train_labels.csv', index_col='customer_ID')","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:28:42.567540Z","iopub.execute_input":"2022-10-14T15:28:42.567927Z","iopub.status.idle":"2022-10-14T15:33:10.002514Z","shell.execute_reply.started":"2022-10-14T15:28:42.567873Z","shell.execute_reply":"2022-10-14T15:33:10.001562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:33:10.003917Z","iopub.execute_input":"2022-10-14T15:33:10.004294Z","iopub.status.idle":"2022-10-14T15:33:10.024124Z","shell.execute_reply.started":"2022-10-14T15:33:10.004255Z","shell.execute_reply":"2022-10-14T15:33:10.023324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_df = train_data.groupby('customer_ID').tail(1).fillna(-999)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:33:10.025661Z","iopub.execute_input":"2022-10-14T15:33:10.026305Z","iopub.status.idle":"2022-10-14T15:33:11.331914Z","shell.execute_reply.started":"2022-10-14T15:33:10.026267Z","shell.execute_reply":"2022-10-14T15:33:11.331037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_train_df = last_month_df.merge(train_labels, how = 'inner', on = 'customer_ID')","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:33:11.333451Z","iopub.execute_input":"2022-10-14T15:33:11.333845Z","iopub.status.idle":"2022-10-14T15:33:11.663001Z","shell.execute_reply.started":"2022-10-14T15:33:11.333807Z","shell.execute_reply":"2022-10-14T15:33:11.662138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lasso = Lasso()\ntraincols = ['P_2', 'D_48', 'B_2', 'D_61', 'B_18', 'D_55', 'B_9', 'D_44', 'B_33']","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:33:11.664240Z","iopub.execute_input":"2022-10-14T15:33:11.664711Z","iopub.status.idle":"2022-10-14T15:33:11.669847Z","shell.execute_reply.started":"2022-10-14T15:33:11.664673Z","shell.execute_reply":"2022-10-14T15:33:11.669143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lasso.fit(last_month_train_df[traincols], last_month_train_df['target'])","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:33:11.671067Z","iopub.execute_input":"2022-10-14T15:33:11.671978Z","iopub.status.idle":"2022-10-14T15:33:11.811314Z","shell.execute_reply.started":"2022-10-14T15:33:11.671942Z","shell.execute_reply":"2022-10-14T15:33:11.810492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv(input_path / 'test_data.csv', usecols = use_cols)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:33:11.812494Z","iopub.execute_input":"2022-10-14T15:33:11.813086Z","iopub.status.idle":"2022-10-14T15:42:49.035637Z","shell.execute_reply.started":"2022-10-14T15:33:11.813048Z","shell.execute_reply":"2022-10-14T15:42:49.034571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_test_df = test_data.groupby('customer_ID').tail(1)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:42:49.036933Z","iopub.execute_input":"2022-10-14T15:42:49.037308Z","iopub.status.idle":"2022-10-14T15:42:51.959726Z","shell.execute_reply.started":"2022-10-14T15:42:49.037264Z","shell.execute_reply":"2022-10-14T15:42:51.958822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = lasso.predict(last_month_test_df[traincols].fillna(-999))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:42:51.960985Z","iopub.execute_input":"2022-10-14T15:42:51.961392Z","iopub.status.idle":"2022-10-14T15:42:52.047214Z","shell.execute_reply.started":"2022-10-14T15:42:51.961352Z","shell.execute_reply":"2022-10-14T15:42:52.046059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_test_df['prediction'] = y_pred","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:42:52.048634Z","iopub.execute_input":"2022-10-14T15:42:52.049193Z","iopub.status.idle":"2022-10-14T15:42:52.055992Z","shell.execute_reply.started":"2022-10-14T15:42:52.049153Z","shell.execute_reply":"2022-10-14T15:42:52.055192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_test_df['prediction'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:42:52.057222Z","iopub.execute_input":"2022-10-14T15:42:52.057746Z","iopub.status.idle":"2022-10-14T15:42:52.072446Z","shell.execute_reply.started":"2022-10-14T15:42:52.057702Z","shell.execute_reply":"2022-10-14T15:42:52.070538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_test_df[['customer_ID', 'prediction']].to_csv('submission.csv', index=False)\nlast_month_test_df[['customer_ID', 'prediction']].head(10)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:42:52.073699Z","iopub.execute_input":"2022-10-14T15:42:52.074145Z","iopub.status.idle":"2022-10-14T15:42:55.020903Z","shell.execute_reply.started":"2022-10-14T15:42:52.074109Z","shell.execute_reply":"2022-10-14T15:42:55.019808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nlogistic = LogisticRegression()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:42:55.022291Z","iopub.execute_input":"2022-10-14T15:42:55.022826Z","iopub.status.idle":"2022-10-14T15:42:55.027691Z","shell.execute_reply.started":"2022-10-14T15:42:55.022771Z","shell.execute_reply":"2022-10-14T15:42:55.026796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logistic.fit(last_month_train_df[traincols], last_month_train_df['target'])","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:42:55.029260Z","iopub.execute_input":"2022-10-14T15:42:55.030163Z","iopub.status.idle":"2022-10-14T15:43:03.118362Z","shell.execute_reply.started":"2022-10-14T15:42:55.030124Z","shell.execute_reply":"2022-10-14T15:43:03.116809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_log = logistic.predict(last_month_test_df[traincols].fillna(-999))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:43:03.119739Z","iopub.execute_input":"2022-10-14T15:43:03.120693Z","iopub.status.idle":"2022-10-14T15:43:03.241315Z","shell.execute_reply.started":"2022-10-14T15:43:03.120655Z","shell.execute_reply":"2022-10-14T15:43:03.239970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_test_df['prediction'] = y_pred_log","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:43:03.242912Z","iopub.execute_input":"2022-10-14T15:43:03.243587Z","iopub.status.idle":"2022-10-14T15:43:03.276171Z","shell.execute_reply.started":"2022-10-14T15:43:03.243544Z","shell.execute_reply":"2022-10-14T15:43:03.275365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_test_df['prediction'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:43:03.277826Z","iopub.execute_input":"2022-10-14T15:43:03.278640Z","iopub.status.idle":"2022-10-14T15:43:03.294631Z","shell.execute_reply.started":"2022-10-14T15:43:03.278592Z","shell.execute_reply":"2022-10-14T15:43:03.293181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_test_df[['customer_ID', 'prediction']].to_csv('submission.csv', index=False)\nlast_month_test_df[['customer_ID', 'prediction']].head(10)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:43:03.299833Z","iopub.execute_input":"2022-10-14T15:43:03.300694Z","iopub.status.idle":"2022-10-14T15:43:05.127119Z","shell.execute_reply.started":"2022-10-14T15:43:03.300654Z","shell.execute_reply":"2022-10-14T15:43:05.126348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nrfc = RandomForestClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:43:05.128587Z","iopub.execute_input":"2022-10-14T15:43:05.128970Z","iopub.status.idle":"2022-10-14T15:43:05.133175Z","shell.execute_reply.started":"2022-10-14T15:43:05.128932Z","shell.execute_reply":"2022-10-14T15:43:05.132431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfc.fit(last_month_train_df[traincols], last_month_train_df['target'])","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:43:05.134681Z","iopub.execute_input":"2022-10-14T15:43:05.135364Z","iopub.status.idle":"2022-10-14T15:47:16.825109Z","shell.execute_reply.started":"2022-10-14T15:43:05.135328Z","shell.execute_reply":"2022-10-14T15:47:16.824086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_rfc = rfc.predict_proba(last_month_test_df[traincols].fillna(-999))[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:47:16.826771Z","iopub.execute_input":"2022-10-14T15:47:16.827211Z","iopub.status.idle":"2022-10-14T15:47:42.606432Z","shell.execute_reply.started":"2022-10-14T15:47:16.827161Z","shell.execute_reply":"2022-10-14T15:47:42.605305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_test_df['prediction'] = y_pred_rfc","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:47:42.608343Z","iopub.execute_input":"2022-10-14T15:47:42.608741Z","iopub.status.idle":"2022-10-14T15:47:42.615532Z","shell.execute_reply.started":"2022-10-14T15:47:42.608703Z","shell.execute_reply":"2022-10-14T15:47:42.614631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_test_df['prediction'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T15:47:42.617080Z","iopub.execute_input":"2022-10-14T15:47:42.617515Z","iopub.status.idle":"2022-10-14T15:47:42.631832Z","shell.execute_reply.started":"2022-10-14T15:47:42.617480Z","shell.execute_reply":"2022-10-14T15:47:42.631089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_month_test_df[['customer_ID', 'prediction']].to_csv('submission1.csv', index=False)\nlast_month_test_df[['customer_ID', 'prediction']].head(10)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T16:08:31.307644Z","iopub.execute_input":"2022-10-14T16:08:31.308062Z","iopub.status.idle":"2022-10-14T16:08:33.332711Z","shell.execute_reply.started":"2022-10-14T16:08:31.308024Z","shell.execute_reply":"2022-10-14T16:08:33.331920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}