{"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 os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-04T12:09:06.363900Z","iopub.execute_input":"2022-06-04T12:09:06.364385Z","iopub.status.idle":"2022-06-04T12:09:06.391553Z","shell.execute_reply.started":"2022-06-04T12:09:06.364299Z","shell.execute_reply":"2022-06-04T12:09:06.390695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.graph_objects as go\n\n\nimport warnings\nwarnings.filterwarnings('ignore')\nimport gc\n\nfrom sklearn.preprocessing import OrdinalEncoder\nfrom sklearn.model_selection import train_test_split\nfrom lightgbm import  LGBMClassifier,early_stopping,log_evaluation\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:06.393359Z","iopub.execute_input":"2022-06-04T12:09:06.393959Z","iopub.status.idle":"2022-06-04T12:09:10.528569Z","shell.execute_reply.started":"2022-06-04T12:09:06.393921Z","shell.execute_reply":"2022-06-04T12:09:10.527683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_feather('/kaggle/input/amexfeather/train_data.ftr')\n\n#train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:10.534027Z","iopub.execute_input":"2022-06-04T12:09:10.534953Z","iopub.status.idle":"2022-06-04T12:09:27.237377Z","shell.execute_reply.started":"2022-06-04T12:09:10.534907Z","shell.execute_reply":"2022-06-04T12:09:27.236512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:27.239400Z","iopub.execute_input":"2022-06-04T12:09:27.239965Z","iopub.status.idle":"2022-06-04T12:09:27.246337Z","shell.execute_reply.started":"2022-06-04T12:09:27.239926Z","shell.execute_reply":"2022-06-04T12:09:27.245304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_data.describe(include='all',datetime_is_numeric=True).T","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:27.247978Z","iopub.execute_input":"2022-06-04T12:09:27.248469Z","iopub.status.idle":"2022-06-04T12:09:27.255058Z","shell.execute_reply.started":"2022-06-04T12:09:27.248428Z","shell.execute_reply":"2022-06-04T12:09:27.254146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(train_data['customer_ID'].nunique())\n#print(train_data['S_2'].min())\n#print(train_data['S_2'].max())","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:27.256500Z","iopub.execute_input":"2022-06-04T12:09:27.257503Z","iopub.status.idle":"2022-06-04T12:09:27.263133Z","shell.execute_reply.started":"2022-06-04T12:09:27.257464Z","shell.execute_reply":"2022-06-04T12:09:27.261682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"No. of features for each category:\")\n\nfor perf in ['D_','S_','P_','B_','R_']:\n    print(f\"{perf}: {len([i for i in train_data.columns if i.startswith(perf)])}\")","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:27.264789Z","iopub.execute_input":"2022-06-04T12:09:27.265259Z","iopub.status.idle":"2022-06-04T12:09:27.272568Z","shell.execute_reply.started":"2022-06-04T12:09:27.265217Z","shell.execute_reply":"2022-06-04T12:09:27.271614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_data.isnull().sum()     ","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:27.274113Z","iopub.execute_input":"2022-06-04T12:09:27.274519Z","iopub.status.idle":"2022-06-04T12:09:27.280339Z","shell.execute_reply.started":"2022-06-04T12:09:27.274481Z","shell.execute_reply":"2022-06-04T12:09:27.279326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#sns.set()\n#sns.countplot(x=train_data['target'])","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:27.285346Z","iopub.execute_input":"2022-06-04T12:09:27.285942Z","iopub.status.idle":"2022-06-04T12:09:27.289710Z","shell.execute_reply.started":"2022-06-04T12:09:27.285903Z","shell.execute_reply":"2022-06-04T12:09:27.288683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_temp = pd.DataFrame(train_data.target.value_counts() * 100 / train_data.shape[0])\ndf_temp\n\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:27.294271Z","iopub.execute_input":"2022-06-04T12:09:27.294727Z","iopub.status.idle":"2022-06-04T12:09:27.336447Z","shell.execute_reply.started":"2022-06-04T12:09:27.294688Z","shell.execute_reply":"2022-06-04T12:09:27.335465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#values = df_temp['target'].values\n#labels = ['Default','Non Default']\n\n#layout = go.Layout(barmode = 'group') \n#fig = go.Figure(data=[go.Bar(y = values,x=labels)],layout=layout)\n#fig.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:27.337895Z","iopub.execute_input":"2022-06-04T12:09:27.338447Z","iopub.status.idle":"2022-06-04T12:09:27.343569Z","shell.execute_reply.started":"2022-06-04T12:09:27.338402Z","shell.execute_reply":"2022-06-04T12:09:27.342508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#percentage of missing values for each feature\n\n#missing_per = train_data.isnull().sum()*100/len(train_data)\n#df_missing = pd.DataFrame({'Column': train_data.columns,'percent of missing':missing_per}).sort_values('percent of missing',ascending=False)\n#df_missing","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:27.345700Z","iopub.execute_input":"2022-06-04T12:09:27.346584Z","iopub.status.idle":"2022-06-04T12:09:27.352167Z","shell.execute_reply.started":"2022-06-04T12:09:27.346544Z","shell.execute_reply":"2022-06-04T12:09:27.351076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Keep the last statement month per customer\n# https://www.kaggle.com/competitions/amex-default-prediction/discussion/327094\ntrain_data =  (train_data\n            .groupby('customer_ID')\n            .tail(1)\n            .set_index('customer_ID', drop=True)\n            .sort_index()\n            .drop(['S_2'], axis='columns'))","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:27.353436Z","iopub.execute_input":"2022-06-04T12:09:27.354146Z","iopub.status.idle":"2022-06-04T12:09:30.212677Z","shell.execute_reply.started":"2022-06-04T12:09:27.354102Z","shell.execute_reply":"2022-06-04T12:09:30.211766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = gc.collect","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:30.215676Z","iopub.execute_input":"2022-06-04T12:09:30.215997Z","iopub.status.idle":"2022-06-04T12:09:30.219974Z","shell.execute_reply.started":"2022-06-04T12:09:30.215969Z","shell.execute_reply":"2022-06-04T12:09:30.218791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns_all = train_data.columns.to_list()\ncolumns_cat = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:30.221776Z","iopub.execute_input":"2022-06-04T12:09:30.222167Z","iopub.status.idle":"2022-06-04T12:09:30.228865Z","shell.execute_reply.started":"2022-06-04T12:09:30.222130Z","shell.execute_reply":"2022-06-04T12:09:30.227826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns_num = [column for column in columns_all if column not in columns_cat + ['target']]","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:30.230489Z","iopub.execute_input":"2022-06-04T12:09:30.231056Z","iopub.status.idle":"2022-06-04T12:09:30.238018Z","shell.execute_reply.started":"2022-06-04T12:09:30.231021Z","shell.execute_reply":"2022-06-04T12:09:30.236767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_data[columns_cat + columns_num]\ny= train_data['target']","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:30.239766Z","iopub.execute_input":"2022-06-04T12:09:30.240652Z","iopub.status.idle":"2022-06-04T12:09:30.507261Z","shell.execute_reply.started":"2022-06-04T12:09:30.240590Z","shell.execute_reply":"2022-06-04T12:09:30.506459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#X.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:30.508786Z","iopub.execute_input":"2022-06-04T12:09:30.509150Z","iopub.status.idle":"2022-06-04T12:09:30.513542Z","shell.execute_reply.started":"2022-06-04T12:09:30.509113Z","shell.execute_reply":"2022-06-04T12:09:30.512605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = OrdinalEncoder()\nX[columns_cat] = encoder.fit_transform(X[columns_cat])","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:30.515194Z","iopub.execute_input":"2022-06-04T12:09:30.515616Z","iopub.status.idle":"2022-06-04T12:09:31.407104Z","shell.execute_reply.started":"2022-06-04T12:09:30.515579Z","shell.execute_reply":"2022-06-04T12:09:31.406269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%time\nX_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,stratify=y)\n\n#stratify parameter makes a split so that the proportion of values in the sample produced will be the same as the proportion of values provided to parameter stratify.\n#For example, if variable y is a binary categorical variable with values 0 and 1 and there are 25% of zeros and 75% of ones, \n#stratify=y will make sure that your random split has 25% of 0's and 75% of 1's.","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:31.408270Z","iopub.execute_input":"2022-06-04T12:09:31.408619Z","iopub.status.idle":"2022-06-04T12:09:32.771489Z","shell.execute_reply.started":"2022-06-04T12:09:31.408586Z","shell.execute_reply":"2022-06-04T12:09:32.770678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%time\nmodel = LGBMClassifier(n_estimators=50000,device='gpu',random_state=69420,extra_trees=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:32.772864Z","iopub.execute_input":"2022-06-04T12:09:32.773251Z","iopub.status.idle":"2022-06-04T12:09:32.778540Z","shell.execute_reply.started":"2022-06-04T12:09:32.773209Z","shell.execute_reply":"2022-06-04T12:09:32.777807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n            X_train,\n            y_train,\n            eval_set=[(X_test,y_test)],\n            callbacks=[early_stopping(25),log_evaluation(0)]\n        )","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:09:32.780210Z","iopub.execute_input":"2022-06-04T12:09:32.780810Z","iopub.status.idle":"2022-06-04T12:10:01.758055Z","shell.execute_reply.started":"2022-06-04T12:09:32.780773Z","shell.execute_reply":"2022-06-04T12:10:01.757432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict = pd.DataFrame(y_test.copy(deep=True))\ny_predict=y_predict.rename(columns={'target':'prediction'})\ny_predict","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:10:01.761073Z","iopub.execute_input":"2022-06-04T12:10:01.762554Z","iopub.status.idle":"2022-06-04T12:10:01.777170Z","shell.execute_reply.started":"2022-06-04T12:10:01.762509Z","shell.execute_reply":"2022-06-04T12:10:01.776394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict[\"prediction\"] = model.predict_proba(X_test)[:,1]\ny_predict","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:10:01.778500Z","iopub.execute_input":"2022-06-04T12:10:01.779103Z","iopub.status.idle":"2022-06-04T12:10:04.483137Z","shell.execute_reply.started":"2022-06-04T12:10:01.779064Z","shell.execute_reply":"2022-06-04T12:10:04.482378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:10:04.486890Z","iopub.execute_input":"2022-06-04T12:10:04.488833Z","iopub.status.idle":"2022-06-04T12:10:04.628257Z","shell.execute_reply.started":"2022-06-04T12:10:04.488801Z","shell.execute_reply":"2022-06-04T12:10:04.627304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_feather('../input/amexfeather/test_data.ftr')\ntest_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:10:04.630001Z","iopub.execute_input":"2022-06-04T12:10:04.630470Z","iopub.status.idle":"2022-06-04T12:10:38.937266Z","shell.execute_reply.started":"2022-06-04T12:10:04.630427Z","shell.execute_reply":"2022-06-04T12:10:38.936486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = (test_data.groupby('customer_ID').tail(1)).drop(['S_2'],axis='columns')\ntest_data","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:10:38.938542Z","iopub.execute_input":"2022-06-04T12:10:38.939106Z","iopub.status.idle":"2022-06-04T12:10:44.375425Z","shell.execute_reply.started":"2022-06-04T12:10:38.939069Z","shell.execute_reply":"2022-06-04T12:10:44.374674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[columns_cat] = encoder.transform(test_data[columns_cat])\ntest_data","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:10:44.379089Z","iopub.execute_input":"2022-06-04T12:10:44.379719Z","iopub.status.idle":"2022-06-04T12:10:45.727129Z","shell.execute_reply.started":"2022-06-04T12:10:44.379691Z","shell.execute_reply":"2022-06-04T12:10:45.726345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['prediction'] = model.predict_proba(test_data[columns_cat + columns_num])[:,1]\ntest_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:10:45.728466Z","iopub.execute_input":"2022-06-04T12:10:45.729001Z","iopub.status.idle":"2022-06-04T12:11:06.365399Z","shell.execute_reply.started":"2022-06-04T12:10:45.728962Z","shell.execute_reply":"2022-06-04T12:11:06.364367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['prediction'].to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:11:06.366789Z","iopub.execute_input":"2022-06-04T12:11:06.367250Z","iopub.status.idle":"2022-06-04T12:11:08.911534Z","shell.execute_reply.started":"2022-06-04T12:11:06.367210Z","shell.execute_reply":"2022-06-04T12:11:08.910676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#kaggle competitions submit -c amex-default-prediction -f submission.csv -m \"Message\"","metadata":{"execution":{"iopub.status.busy":"2022-06-04T12:11:08.912663Z","iopub.execute_input":"2022-06-04T12:11:08.913750Z","iopub.status.idle":"2022-06-04T12:11:08.918278Z","shell.execute_reply.started":"2022-06-04T12:11:08.913708Z","shell.execute_reply":"2022-06-04T12:11:08.917524Z"},"trusted":true},"execution_count":null,"outputs":[]}]}