{"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\nimport warnings, gc\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-08T14:31:40.877721Z","iopub.execute_input":"2022-08-08T14:31:40.878602Z","iopub.status.idle":"2022-08-08T14:31:40.891251Z","shell.execute_reply.started":"2022-08-08T14:31:40.878529Z","shell.execute_reply":"2022-08-08T14:31:40.889926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain = pd.read_parquet(\"../input/amex-data-integer-dtypes-parquet-format/train.parquet\")\ntrain_labels = pd.read_csv(\"../input/amex-default-prediction/train_labels.csv\")\ntrain = train.merge(train_labels,how=\"inner\",on=\"customer_ID\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:31:47.316077Z","iopub.execute_input":"2022-08-08T14:31:47.317380Z","iopub.status.idle":"2022-08-08T14:34:31.298051Z","shell.execute_reply.started":"2022-08-08T14:31:47.317338Z","shell.execute_reply":"2022-08-08T14:34:31.296761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nlab = LabelEncoder()\ntrain['customer_ID']= lab.fit_transform(train['customer_ID'])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:34:35.430317Z","iopub.execute_input":"2022-08-08T14:34:35.430783Z","iopub.status.idle":"2022-08-08T14:34:38.993442Z","shell.execute_reply.started":"2022-08-08T14:34:35.430745Z","shell.execute_reply":"2022-08-08T14:34:38.992412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.groupby('customer_ID').tail(1).set_index('customer_ID')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:34:41.851122Z","iopub.execute_input":"2022-08-08T14:34:41.851888Z","iopub.status.idle":"2022-08-08T14:34:45.136103Z","shell.execute_reply.started":"2022-08-08T14:34:41.851849Z","shell.execute_reply":"2022-08-08T14:34:45.134837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train.target\nX = train.drop([\"target\",\"S_2\"],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:34:49.507997Z","iopub.execute_input":"2022-08-08T14:34:49.508486Z","iopub.status.idle":"2022-08-08T14:34:49.693889Z","shell.execute_reply.started":"2022-08-08T14:34:49.508444Z","shell.execute_reply":"2022-08-08T14:34:49.692791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape,y.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:34:54.578571Z","iopub.execute_input":"2022-08-08T14:34:54.579002Z","iopub.status.idle":"2022-08-08T14:34:54.589081Z","shell.execute_reply.started":"2022-08-08T14:34:54.578969Z","shell.execute_reply":"2022-08-08T14:34:54.587683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.fillna(-123)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:35:00.640942Z","iopub.execute_input":"2022-08-08T14:35:00.641379Z","iopub.status.idle":"2022-08-08T14:35:00.789787Z","shell.execute_reply.started":"2022-08-08T14:35:00.641344Z","shell.execute_reply":"2022-08-08T14:35:00.788496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_cols =['B_30', 'B_38', 'D_63', 'D_64', 'D_66', 'D_68', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126']\nnum_cols = [col for col in X.columns if col not in cat_cols ]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:35:04.635413Z","iopub.execute_input":"2022-08-08T14:35:04.635923Z","iopub.status.idle":"2022-08-08T14:35:04.641433Z","shell.execute_reply.started":"2022-08-08T14:35:04.635863Z","shell.execute_reply":"2022-08-08T14:35:04.640240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D_n_cols = [col for col in num_cols if col.startswith(\"D\")]\nS_n_cols = [col for col in num_cols if col.startswith(\"S\")]\nP_n_cols = [col for col in num_cols if col.startswith(\"P\")]\nB_n_cols = [col for col in num_cols if col.startswith(\"B\")]\nR_n_cols = [col for col in num_cols if col.startswith(\"R\")]\nD_c_cols = [col for col in cat_cols if col.startswith(\"D\")]\nB_c_cols = [col for col in cat_cols if col.startswith(\"B\")] ","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:35:07.216428Z","iopub.execute_input":"2022-08-08T14:35:07.216837Z","iopub.status.idle":"2022-08-08T14:35:07.224668Z","shell.execute_reply.started":"2022-08-08T14:35:07.216803Z","shell.execute_reply":"2022-08-08T14:35:07.223607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \nX_num_agg_D = X.groupby(\"customer_ID\")[D_n_cols].agg(['mean','min', 'last'])\nX_num_agg_D.columns = ['_'.join(x) for x in X_num_agg_D.columns]\n\nX_num_agg_S = X.groupby(\"customer_ID\")[S_n_cols].agg(['mean','min', 'last'])\nX_num_agg_S.columns = ['_'.join(x) for x in X_num_agg_S.columns]\n\nX_num_agg_P = X.groupby(\"customer_ID\")[P_n_cols].agg(['mean','min','max' ,'last'])\nX_num_agg_P.columns = ['_'.join(x) for x in X_num_agg_P.columns]\n\nX_num_agg_B = X.groupby(\"customer_ID\")[B_n_cols].agg(['mean','min', 'last'])\nX_num_agg_B.columns = ['_'.join(x) for x in X_num_agg_B.columns]\n\nX_num_agg_R = X.groupby(\"customer_ID\")[R_n_cols].agg(['mean','min','last'])\nX_num_agg_R.columns = ['_'.join(x) for x in X_num_agg_R.columns]\n\nX_cat_agg_D = X.groupby(\"customer_ID\")[D_c_cols].agg([ 'count','last','first','nunique'])\nX_cat_agg_D.columns = ['_'.join(x) for x in X_cat_agg_D.columns]\n\nX_cat_agg_B = X.groupby(\"customer_ID\")[B_c_cols].agg([ 'count','last','nunique'])\nX_cat_agg_B.columns = ['_'.join(x) for x in X_cat_agg_B.columns]\n\nX = pd.concat([X_num_agg_D, X_num_agg_S,X_num_agg_P,X_num_agg_B,X_num_agg_R,X_cat_agg_D,X_cat_agg_B], axis=1)\ndel X_num_agg_D, X_num_agg_S,X_num_agg_P,X_num_agg_B,X_num_agg_R,X_cat_agg_D,X_cat_agg_B\n_ = gc.collect()\n\nprint('X shape after engineering', X.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:35:10.680795Z","iopub.execute_input":"2022-08-08T14:35:10.681845Z","iopub.status.idle":"2022-08-08T14:35:19.686013Z","shell.execute_reply.started":"2022-08-08T14:35:10.681785Z","shell.execute_reply":"2022-08-08T14:35:19.684677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom keras import models\nfrom keras import layers\n\n\nfrom tensorflow.keras import layers,callbacks\n\nfrom tensorflow.keras import layers,callbacks\nfrom keras import models,layers\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:35:28.070801Z","iopub.execute_input":"2022-08-08T14:35:28.071500Z","iopub.status.idle":"2022-08-08T14:35:30.039402Z","shell.execute_reply.started":"2022-08-08T14:35:28.071462Z","shell.execute_reply":"2022-08-08T14:35:30.038260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = np.array(X)\ny = np.array(y)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:35:33.035678Z","iopub.execute_input":"2022-08-08T14:35:33.036738Z","iopub.status.idle":"2022-08-08T14:35:35.130496Z","shell.execute_reply.started":"2022-08-08T14:35:33.036695Z","shell.execute_reply":"2022-08-08T14:35:35.129385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X/255.","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:36:13.433391Z","iopub.execute_input":"2022-08-08T14:36:13.433818Z","iopub.status.idle":"2022-08-08T14:36:14.111308Z","shell.execute_reply.started":"2022-08-08T14:36:13.433784Z","shell.execute_reply":"2022-08-08T14:36:14.110321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.reshape(-1,24,24,1)\n\nprint(\"x_train shape: \",X.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:36:18.755723Z","iopub.execute_input":"2022-08-08T14:36:18.756166Z","iopub.status.idle":"2022-08-08T14:36:18.762818Z","shell.execute_reply.started":"2022-08-08T14:36:18.756131Z","shell.execute_reply":"2022-08-08T14:36:18.761536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import  matplotlib.pyplot as plt\n\nplt.imshow(X[5])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:36:23.813920Z","iopub.execute_input":"2022-08-08T14:36:23.814336Z","iopub.status.idle":"2022-08-08T14:36:24.017593Z","shell.execute_reply.started":"2022-08-08T14:36:23.814301Z","shell.execute_reply":"2022-08-08T14:36:24.016422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X, y, test_size = 0.25, random_state=2,stratify=y)\nprint(\"x_train shape\",X_train.shape)\nprint(\"x_val shape\",X_val.shape)\nprint(\"y_train shape\",Y_train.shape)\nprint(\"y_valid shape\",Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:36:31.232927Z","iopub.execute_input":"2022-08-08T14:36:31.233374Z","iopub.status.idle":"2022-08-08T14:36:36.497883Z","shell.execute_reply.started":"2022-08-08T14:36:31.233337Z","shell.execute_reply":"2022-08-08T14:36:36.496991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',\n                 activation ='swish', input_shape = (24,24,1)))\nmodel.add(MaxPooling2D(pool_size=(3,3),strides=(1, 1)))\nmodel.add(Dropout(0.50))\nmodel.add(BatchNormalization())\n\nmodel.add(Flatten())\nmodel.add(Dropout(0.50))\nmodel.add(BatchNormalization())\nmodel.add(Dense(196, activation = \"swish\"))\nmodel.add(Dropout(0.30))\nmodel.add(BatchNormalization())\nmodel.add(Dense(1, activation = \"sigmoid\"))\n","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:41:30.631204Z","iopub.execute_input":"2022-08-08T14:41:30.631694Z","iopub.status.idle":"2022-08-08T14:41:30.748557Z","shell.execute_reply.started":"2022-08-08T14:41:30.631621Z","shell.execute_reply":"2022-08-08T14:41:30.747518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer= tf.keras.optimizers.Adam(\n    learning_rate=0.0054166898758110146),\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:41:37.802825Z","iopub.execute_input":"2022-08-08T14:41:37.803246Z","iopub.status.idle":"2022-08-08T14:41:37.814421Z","shell.execute_reply.started":"2022-08-08T14:41:37.803213Z","shell.execute_reply":"2022-08-08T14:41:37.813457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = callbacks.EarlyStopping(\n                 min_delta=0.001,\n                 patience=20,\n                 restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:41:40.933645Z","iopub.execute_input":"2022-08-08T14:41:40.934108Z","iopub.status.idle":"2022-08-08T14:41:40.939907Z","shell.execute_reply.started":"2022-08-08T14:41:40.934072Z","shell.execute_reply":"2022-08-08T14:41:40.938337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nhistory = model.fit(X_train,Y_train,validation_data=(X_val,Y_val),batch_size=2000,\n                    epochs=20,callbacks=[early_stopping],workers=4,verbose=1)\n\nhistory_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot();\nprint(\"Minimum validation loss: {}\".format(history_df['val_loss'].min()))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:41:44.540485Z","iopub.execute_input":"2022-08-08T14:41:44.541730Z","iopub.status.idle":"2022-08-08T15:53:12.425759Z","shell.execute_reply.started":"2022-08-08T14:41:44.541655Z","shell.execute_reply":"2022-08-08T15:53:12.423681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df.loc[:, ['accuracy', 'val_accuracy']].plot();\nprint(\"Maximum accuracy : {}\".format(history_df['val_accuracy'].max()))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:53:20.759529Z","iopub.execute_input":"2022-08-08T15:53:20.759952Z","iopub.status.idle":"2022-08-08T15:53:20.997060Z","shell.execute_reply.started":"2022-08-08T15:53:20.759919Z","shell.execute_reply":"2022-08-08T15:53:20.995810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_val)\npred_te = predictions.round()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:53:26.393446Z","iopub.execute_input":"2022-08-08T15:53:26.394420Z","iopub.status.idle":"2022-08-08T15:53:52.218294Z","shell.execute_reply.started":"2022-08-08T15:53:26.394370Z","shell.execute_reply":"2022-08-08T15:53:52.217361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report,confusion_matrix\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\ncm = confusion_matrix(Y_val,pred_te)\n\nplt.figure(figsize=(10,7))\n\nsns.heatmap(cm,annot=True,fmt='d')\n\nplt.xlabel('Predicted')\nplt.ylabel('Truth')\n","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:53:58.298504Z","iopub.execute_input":"2022-08-08T15:53:58.298954Z","iopub.status.idle":"2022-08-08T15:53:58.784672Z","shell.execute_reply.started":"2022-08-08T15:53:58.298915Z","shell.execute_reply":"2022-08-08T15:53:58.783403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cr =classification_report(Y_val,pred_te)\n\nprint(cr)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:54:03.455273Z","iopub.execute_input":"2022-08-08T15:54:03.456058Z","iopub.status.idle":"2022-08-08T15:54:03.761321Z","shell.execute_reply.started":"2022-08-08T15:54:03.456003Z","shell.execute_reply":"2022-08-08T15:54:03.759701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest = pd.read_parquet(\"../input/amex-data-integer-dtypes-parquet-format/test.parquet\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T15:54:10.400304Z","iopub.execute_input":"2022-08-08T15:54:10.401015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['customer_ID']= lab.fit_transform(test['customer_ID'])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.groupby('customer_ID').tail(1).set_index('customer_ID')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.drop([\"S_2\"],axis=1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.fillna(-123)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \ntest_num_agg_D = test.groupby(\"customer_ID\")[D_n_cols].agg(['mean','min', 'last'])\ntest_num_agg_D.columns = ['_'.join(x) for x in test_num_agg_D.columns]\n\ntest_num_agg_S = test.groupby(\"customer_ID\")[S_n_cols].agg(['mean','min', 'last'])\ntest_num_agg_S.columns = ['_'.join(x) for x in test_num_agg_S.columns]\n\ntest_num_agg_P = test.groupby(\"customer_ID\")[P_n_cols].agg(['mean','min','max', 'last'])\ntest_num_agg_P.columns = ['_'.join(x) for x in test_num_agg_P.columns]\n\ntest_num_agg_B = test.groupby(\"customer_ID\")[B_n_cols].agg(['mean','min', 'last'])\ntest_num_agg_B.columns = ['_'.join(x) for x in test_num_agg_B.columns]\n\ntest_num_agg_R = test.groupby(\"customer_ID\")[R_n_cols].agg(['mean','min', 'last'])\ntest_num_agg_R.columns = ['_'.join(x) for x in test_num_agg_R.columns]\n\ntest_cat_agg_D = test.groupby(\"customer_ID\")[D_c_cols].agg(['count','first', 'last','nunique'])\ntest_cat_agg_D.columns = ['_'.join(x) for x in test_cat_agg_D.columns]\n\ntest_cat_agg_B = test.groupby(\"customer_ID\")[B_c_cols].agg([ 'count','last','nunique'])\ntest_cat_agg_B.columns = ['_'.join(x) for x in test_cat_agg_B.columns]\n\ntest = pd.concat([test_num_agg_D, test_num_agg_S,test_num_agg_P,test_num_agg_B,test_num_agg_R,test_cat_agg_D,test_cat_agg_B], axis=1)\ndel test_num_agg_D, test_num_agg_S,test_num_agg_P,test_num_agg_B,test_num_agg_R,test_cat_agg_D,test_cat_agg_B\n_ = gc.collect()\n\nprint('Test shape after engineering', test.shape)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.array(test)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test/255.","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.reshape(-1,24,24,1)\nprint(\"test shape: \",test.shape)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_test = model.predict(test)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/amex-default-prediction/sample_submission.csv')\n\nsub.head","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['prediction']=pred_test","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv',index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}