{"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":"!pip install deepctr_torch","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:18:14.558809Z","iopub.execute_input":"2022-06-11T14:18:14.559264Z","iopub.status.idle":"2022-06-11T14:18:52.555420Z","shell.execute_reply.started":"2022-06-11T14:18:14.559177Z","shell.execute_reply":"2022-06-11T14:18:52.554084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport sys\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\nfrom plotly.offline import init_notebook_mode\nimport cudf\nimport cupy\nimport gc\n\n\nimport torch\nfrom deepctr_torch.inputs import SparseFeat, DenseFeat, get_feature_names\nfrom deepctr_torch.models import *\nfrom tqdm import tqdm\nfrom sklearn.model_selection import GridSearchCV\nimport math\nplt.style.use('ggplot')\nimport warnings as w\nw.filterwarnings(action='ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-11T14:18:52.558156Z","iopub.execute_input":"2022-06-11T14:18:52.559765Z","iopub.status.idle":"2022-06-11T14:19:02.687221Z","shell.execute_reply.started":"2022-06-11T14:18:52.559726Z","shell.execute_reply":"2022-06-11T14:19:02.685670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns',None)","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:19:02.690800Z","iopub.execute_input":"2022-06-11T14:19:02.691274Z","iopub.status.idle":"2022-06-11T14:19:02.696043Z","shell.execute_reply.started":"2022-06-11T14:19:02.691245Z","shell.execute_reply":"2022-06-11T14:19:02.695033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_feather('../input/amexfeather/train_data.ftr')\ntrain = train.groupby('customer_ID').tail(1).set_index('customer_ID')\nprint(\"The training data begins on {} and ends on {}.\".format(train['S_2'].min().strftime('%m-%d-%Y'),train['S_2'].max().strftime('%m-%d-%Y')))\nprint(\"There are {:,.0f} customers in the training set and {} features.\".format(train.shape[0],train.shape[1]))\n\ntest = pd.read_feather('../input/amexfeather/test_data.ftr')\ntest = test.groupby('customer_ID').tail(1).set_index('customer_ID')\nprint(\"\\nThe test data begins on {} and ends on {}.\".format(test['S_2'].min().strftime('%m-%d-%Y'),test['S_2'].max().strftime('%m-%d-%Y')))\nprint(\"There are {:,.0f} customers in the test set and {} features.\".format(test.shape[0],test.shape[1]))\n\ncategorical_feature = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_68', 'D_64', 'D_66']\ndel test['S_2']\ndel train['S_2']\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:19:02.698950Z","iopub.execute_input":"2022-06-11T14:19:02.699863Z","iopub.status.idle":"2022-06-11T14:20:13.938974Z","shell.execute_reply.started":"2022-06-11T14:19:02.699820Z","shell.execute_reply":"2022-06-11T14:20:13.937940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:13.940981Z","iopub.execute_input":"2022-06-11T14:20:13.941630Z","iopub.status.idle":"2022-06-11T14:20:14.003579Z","shell.execute_reply.started":"2022-06-11T14:20:13.941573Z","shell.execute_reply":"2022-06-11T14:20:14.001334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:14.005411Z","iopub.execute_input":"2022-06-11T14:20:14.006541Z","iopub.status.idle":"2022-06-11T14:20:14.390045Z","shell.execute_reply.started":"2022-06-11T14:20:14.006494Z","shell.execute_reply":"2022-06-11T14:20:14.386920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Feature Explain\n 1. D_* = Delinquency Variable (criminal?)\n 2. S_* = Spend Varibale \n 3. P_* = Payment Variable\n 4. B_* = Balance Variable\n 5. R_* = Risk variable\n \n### Categorical Variable\n   * 'B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68'","metadata":{}},{"cell_type":"markdown","source":"# EDA skip in this notebook\n#### Link: https://www.kaggle.com/code/leejunseok97/amex-default-eda-prediction","metadata":{}},{"cell_type":"markdown","source":"### Labeling","metadata":{}},{"cell_type":"code","source":"numeric_feature = [cols for cols in train.columns if cols not in categorical_feature]\nfor feature in numeric_feature:\n    if train[feature][0].dtype == np.float16:\n        train[feature].fillna(-99.0,inplace=True)\n        test[feature].fillna(-99.0,inplace=True)\n    else:\n        pass\ntrain.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:14.394787Z","iopub.execute_input":"2022-06-11T14:20:14.395858Z","iopub.status.idle":"2022-06-11T14:20:16.259531Z","shell.execute_reply.started":"2022-06-11T14:20:14.395812Z","shell.execute_reply":"2022-06-11T14:20:16.258331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nencoder = LabelEncoder()\ncategorical_feature = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_68', 'D_64', 'D_66']\ncolumns = train.columns.values\nfor feature in categorical_feature:\n    if feature in columns:\n        train[feature] = encoder.fit_transform(train[feature])\n        test[feature] = encoder.fit_transform(test[feature])\n    else:\n        pass","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:16.261022Z","iopub.execute_input":"2022-06-11T14:20:16.262084Z","iopub.status.idle":"2022-06-11T14:20:17.931014Z","shell.execute_reply.started":"2022-06-11T14:20:16.262041Z","shell.execute_reply":"2022-06-11T14:20:17.929881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dense_feature = [cols for cols in train.columns if cols not in categorical_feature]\ndense_feature.remove('target')\nsparse_feature = categorical_feature\ntest_dense_feature = [cols for cols in test.columns if cols not in categorical_feature]\ntest_sparse_feature = categorical_feature\ntarget = ['target']\nprint('Dense Feature:',dense_feature)\nprint('-'*58)\nprint('sparse_feature:',sparse_feature)\nprint('-'*58)\nprint('Target:',target)","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:17.932653Z","iopub.execute_input":"2022-06-11T14:20:17.933143Z","iopub.status.idle":"2022-06-11T14:20:17.944960Z","shell.execute_reply.started":"2022-06-11T14:20:17.933098Z","shell.execute_reply":"2022-06-11T14:20:17.943264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dense_feature in sparse_feature","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:17.951279Z","iopub.execute_input":"2022-06-11T14:20:17.952256Z","iopub.status.idle":"2022-06-11T14:20:17.961510Z","shell.execute_reply.started":"2022-06-11T14:20:17.952209Z","shell.execute_reply":"2022-06-11T14:20:17.960200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fixlen_feature_columns = [SparseFeat(feat,train[feat].nunique())\n                          for feat in sparse_feature] + [DenseFeat(feat,1) for feat in dense_feature]\nfixlen_feature_columns","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:17.963824Z","iopub.execute_input":"2022-06-11T14:20:17.964752Z","iopub.status.idle":"2022-06-11T14:20:18.017483Z","shell.execute_reply.started":"2022-06-11T14:20:17.964704Z","shell.execute_reply":"2022-06-11T14:20:18.016245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dnn_feature_columns = fixlen_feature_columns\nlinear_feature_columns = fixlen_feature_columns","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:18.019459Z","iopub.execute_input":"2022-06-11T14:20:18.020095Z","iopub.status.idle":"2022-06-11T14:20:18.026851Z","shell.execute_reply.started":"2022-06-11T14:20:18.020054Z","shell.execute_reply":"2022-06-11T14:20:18.025476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_names = get_feature_names(linear_feature_columns + dnn_feature_columns)","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:18.029004Z","iopub.execute_input":"2022-06-11T14:20:18.029744Z","iopub.status.idle":"2022-06-11T14:20:18.038384Z","shell.execute_reply.started":"2022-06-11T14:20:18.029702Z","shell.execute_reply":"2022-06-11T14:20:18.037045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_model_input = {name:train[name] for name in feature_names}\ntest_model_input = {name:test[name] for name in feature_names}","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:18.040656Z","iopub.execute_input":"2022-06-11T14:20:18.041635Z","iopub.status.idle":"2022-06-11T14:20:18.071776Z","shell.execute_reply.started":"2022-06-11T14:20:18.041587Z","shell.execute_reply":"2022-06-11T14:20:18.070285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = ('cuda' if torch.cuda.is_available() else 'cpu')\ndevice","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:18.074244Z","iopub.execute_input":"2022-06-11T14:20:18.075054Z","iopub.status.idle":"2022-06-11T14:20:18.084068Z","shell.execute_reply.started":"2022-06-11T14:20:18.074981Z","shell.execute_reply":"2022-06-11T14:20:18.082439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xDeepFM(linear_feature_columns,dnn_feature_columns,task='binary',\n                device=device)\nmodel","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:18.087273Z","iopub.execute_input":"2022-06-11T14:20:18.088096Z","iopub.status.idle":"2022-06-11T14:20:25.561030Z","shell.execute_reply.started":"2022-06-11T14:20:18.087997Z","shell.execute_reply":"2022-06-11T14:20:25.560043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer=torch.optim.AdamW(model.parameters(),lr=3e-4),\n    loss='binary_crossentropy',\n    metrics=['binary_crossentropy','auc']\n)\nhistory = model.fit(train_model_input, train[target].values, batch_size=1024,\n                    epochs=400,verbose=1,validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:20:25.562600Z","iopub.execute_input":"2022-06-11T14:20:25.563383Z","iopub.status.idle":"2022-06-11T14:27:03.073600Z","shell.execute_reply.started":"2022-06-11T14:20:25.563326Z","shell.execute_reply":"2022-06-11T14:27:03.070785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_model_input,1024)","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:27:03.075032Z","iopub.status.idle":"2022-06-11T14:27:03.075942Z","shell.execute_reply.started":"2022-06-11T14:27:03.075601Z","shell.execute_reply":"2022-06-11T14:27:03.075637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/amex-default-prediction/sample_submission.csv')\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:27:03.077428Z","iopub.status.idle":"2022-06-11T14:27:03.078270Z","shell.execute_reply.started":"2022-06-11T14:27:03.077967Z","shell.execute_reply":"2022-06-11T14:27:03.077997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['prediction'] = pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:27:03.079695Z","iopub.status.idle":"2022-06-11T14:27:03.080743Z","shell.execute_reply.started":"2022-06-11T14:27:03.080338Z","shell.execute_reply":"2022-06-11T14:27:03.080394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-11T14:27:03.082132Z","iopub.status.idle":"2022-06-11T14:27:03.082896Z","shell.execute_reply.started":"2022-06-11T14:27:03.082616Z","shell.execute_reply":"2022-06-11T14:27:03.082645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}