{"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 sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.decomposition import PCA\nimport matplotlib.pyplot as plt\nimport gc","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:01.432671Z","iopub.execute_input":"2022-06-08T07:48:01.433157Z","iopub.status.idle":"2022-06-08T07:48:01.440372Z","shell.execute_reply.started":"2022-06-08T07:48:01.433121Z","shell.execute_reply":"2022-06-08T07:48:01.439216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain = pd.read_feather('../input/amexfeather/train_data.ftr')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:01.755613Z","iopub.execute_input":"2022-06-08T07:48:01.756056Z","iopub.status.idle":"2022-06-08T07:48:18.029562Z","shell.execute_reply.started":"2022-06-08T07:48:01.756022Z","shell.execute_reply":"2022-06-08T07:48:18.02839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:18.031298Z","iopub.execute_input":"2022-06-08T07:48:18.031617Z","iopub.status.idle":"2022-06-08T07:48:18.039206Z","shell.execute_reply.started":"2022-06-08T07:48:18.031588Z","shell.execute_reply":"2022-06-08T07:48:18.038084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train =  (train\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-08T07:48:18.040675Z","iopub.execute_input":"2022-06-08T07:48:18.041379Z","iopub.status.idle":"2022-06-08T07:48:20.927951Z","shell.execute_reply.started":"2022-06-08T07:48:18.041346Z","shell.execute_reply":"2022-06-08T07:48:20.92693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:20.930109Z","iopub.execute_input":"2022-06-08T07:48:20.93044Z","iopub.status.idle":"2022-06-08T07:48:20.937303Z","shell.execute_reply.started":"2022-06-08T07:48:20.930403Z","shell.execute_reply":"2022-06-08T07:48:20.936072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:20.938842Z","iopub.execute_input":"2022-06-08T07:48:20.939359Z","iopub.status.idle":"2022-06-08T07:48:21.066584Z","shell.execute_reply.started":"2022-06-08T07:48:20.939324Z","shell.execute_reply":"2022-06-08T07:48:21.065421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = train.columns.to_list()\ncategory_cols = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\nnumerical_cols = [col for col in cols if col not in category_cols + ['target']]","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:21.068248Z","iopub.execute_input":"2022-06-08T07:48:21.068675Z","iopub.status.idle":"2022-06-08T07:48:21.077584Z","shell.execute_reply.started":"2022-06-08T07:48:21.068628Z","shell.execute_reply":"2022-06-08T07:48:21.076547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train[category_cols + numerical_cols]\ny = train['target']\n\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:21.078999Z","iopub.execute_input":"2022-06-08T07:48:21.079378Z","iopub.status.idle":"2022-06-08T07:48:21.379461Z","shell.execute_reply.started":"2022-06-08T07:48:21.079347Z","shell.execute_reply":"2022-06-08T07:48:21.378382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_cid = X['customer_ID'] \nX = X.drop(columns=['customer_ID'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:21.381387Z","iopub.execute_input":"2022-06-08T07:48:21.38189Z","iopub.status.idle":"2022-06-08T07:48:21.675344Z","shell.execute_reply.started":"2022-06-08T07:48:21.381842Z","shell.execute_reply":"2022-06-08T07:48:21.674356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=X.fillna(X.mean())","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:49:13.149306Z","iopub.execute_input":"2022-06-08T07:49:13.149766Z","iopub.status.idle":"2022-06-08T07:49:14.389267Z","shell.execute_reply.started":"2022-06-08T07:49:13.149734Z","shell.execute_reply":"2022-06-08T07:49:14.388154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:49:09.2293Z","iopub.execute_input":"2022-06-08T07:49:09.230057Z","iopub.status.idle":"2022-06-08T07:49:09.237404Z","shell.execute_reply.started":"2022-06-08T07:49:09.23001Z","shell.execute_reply":"2022-06-08T07:49:09.236226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder\n\nenc = OrdinalEncoder()\nX[category_cols] = enc.fit_transform(X[category_cols])\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:23.095537Z","iopub.execute_input":"2022-06-08T07:48:23.096101Z","iopub.status.idle":"2022-06-08T07:48:24.029578Z","shell.execute_reply.started":"2022-06-08T07:48:23.096069Z","shell.execute_reply":"2022-06-08T07:48:24.028181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numerical_cols.remove('customer_ID')\n# numerical_cols","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:24.031704Z","iopub.execute_input":"2022-06-08T07:48:24.032463Z","iopub.status.idle":"2022-06-08T07:48:24.037449Z","shell.execute_reply.started":"2022-06-08T07:48:24.032415Z","shell.execute_reply":"2022-06-08T07:48:24.03659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc= StandardScaler()\nX[numerical_cols] = sc.fit_transform(X[numerical_cols])","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:24.038735Z","iopub.execute_input":"2022-06-08T07:48:24.039787Z","iopub.status.idle":"2022-06-08T07:48:30.899247Z","shell.execute_reply.started":"2022-06-08T07:48:24.03975Z","shell.execute_reply":"2022-06-08T07:48:30.898063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:30.903086Z","iopub.execute_input":"2022-06-08T07:48:30.903474Z","iopub.status.idle":"2022-06-08T07:48:31.020703Z","shell.execute_reply.started":"2022-06-08T07:48:30.903443Z","shell.execute_reply":"2022-06-08T07:48:31.019761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.all(np.isfinite(X))","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:48:31.022023Z","iopub.execute_input":"2022-06-08T07:48:31.022527Z","iopub.status.idle":"2022-06-08T07:48:31.281638Z","shell.execute_reply.started":"2022-06-08T07:48:31.022494Z","shell.execute_reply":"2022-06-08T07:48:31.280939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.any(np.isnan(X))","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:49:17.605745Z","iopub.execute_input":"2022-06-08T07:49:17.606186Z","iopub.status.idle":"2022-06-08T07:49:17.905691Z","shell.execute_reply.started":"2022-06-08T07:49:17.606153Z","shell.execute_reply":"2022-06-08T07:49:17.904717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA()\ncomp = pca.fit(X)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:49:20.42291Z","iopub.execute_input":"2022-06-08T07:49:20.423292Z","iopub.status.idle":"2022-06-08T07:49:34.892319Z","shell.execute_reply.started":"2022-06-08T07:49:20.423261Z","shell.execute_reply":"2022-06-08T07:49:34.89109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(comp.explained_variance_ratio_))\nplt.grid(axis=\"both\")\nplt.xlabel(\"PRINCIPAL COMPONENTS\")\nplt.ylabel(\"VARIANCE\")\n# sb.despine()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:49:50.21744Z","iopub.execute_input":"2022-06-08T07:49:50.218543Z","iopub.status.idle":"2022-06-08T07:49:50.465313Z","shell.execute_reply.started":"2022-06-08T07:49:50.218501Z","shell.execute_reply":"2022-06-08T07:49:50.463975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca_n = PCA(n_components=125)\nX_new = pca_n.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:49:51.51863Z","iopub.execute_input":"2022-06-08T07:49:51.519329Z","iopub.status.idle":"2022-06-08T07:50:21.885963Z","shell.execute_reply.started":"2022-06-08T07:49:51.519294Z","shell.execute_reply":"2022-06-08T07:50:21.884681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_new.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:50:21.888022Z","iopub.execute_input":"2022-06-08T07:50:21.888488Z","iopub.status.idle":"2022-06-08T07:50:21.895584Z","shell.execute_reply.started":"2022-06-08T07:50:21.888452Z","shell.execute_reply":"2022-06-08T07:50:21.894767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.svm import SVC\n\nclf = SVC(gamma='auto')\nclf.fit(X_new, y)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T07:50:21.89696Z","iopub.execute_input":"2022-06-08T07:50:21.89807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train, X, y\n_ = gc.collect()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest = pd.read_feather('../input/amexfeather/test_data.ftr')\ntest.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test =  (\n    test\n    .groupby('customer_ID')\n    .tail(1)\n#     .set_index('customer_ID', drop=True)\n    .sort_index()\n    .drop(['S_2'], axis='columns')\n)\n\ntest[category_cols] = enc.transform(test[category_cols])\n_ = gc.collect()\n\ntest[\"prediction\"] = clf.predict_proba(test[category_cols + numerical_cols])[:,1]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"prediction\"].to_csv(\"submission.csv\", index=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}