{"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":"markdown","source":"<p style=\"color:#F6F6F6;background-color:#31B8EA;text-align:center;border-radius:10px 10px;font-weight:bold;font-size:22px\"> ⭐ TPS AUGUST: EDA+NULLS+LGBMImpute+XGBoost ⭐ <span style='font-size:28px; background-color:#F6F6F6 ;'></span></p>\n\n<center><img src=\"https://i.postimg.cc/Df5FQcYw/Kaggle.png\" \n             style='border-radius:10px'></center>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"></a> \n# <b>1 <span style='color:#31B8EA'>|</span> Importing Libraries</b>","metadata":{}},{"cell_type":"code","source":"import sys\n!git clone https://github.com/analokmaus/kuma_utils.git\nsys.path.append(\"kuma_utils/\")","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-08-13T17:22:43.350198Z","iopub.execute_input":"2022-08-13T17:22:43.350866Z","iopub.status.idle":"2022-08-13T17:22:44.500503Z","shell.execute_reply.started":"2022-08-13T17:22:43.350814Z","shell.execute_reply":"2022-08-13T17:22:44.498782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport missingno as msno\nimport matplotlib.pyplot as plt\nfrom matplotlib import rcParams\nfrom matplotlib.colors import ListedColormap\nimport seaborn as sns \nimport plotly.express as px\n\nfrom xgboost import XGBClassifier\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn import preprocessing\nfrom kuma_utils.preprocessing.imputer import LGBMImputer\n\nfrom sklearn import metrics\nfrom sklearn.metrics import accuracy_score , f1_score , precision_score , recall_score , confusion_matrix , precision_recall_curve, classification_report\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport colorama\nfrom colorama import Fore, Style\nwarnings.simplefilter(action='ignore')\nprint(Fore.BLUE + \"All Libraries Imported Successfully!\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-13T17:22:47.352917Z","iopub.execute_input":"2022-08-13T17:22:47.353402Z","iopub.status.idle":"2022-08-13T17:22:49.983480Z","shell.execute_reply.started":"2022-08-13T17:22:47.353350Z","shell.execute_reply":"2022-08-13T17:22:49.981824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('fivethirtyeight')\nprint(Fore.BLUE + \"All Styles Set!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:22:49.986151Z","iopub.execute_input":"2022-08-13T17:22:49.986610Z","iopub.status.idle":"2022-08-13T17:22:49.994534Z","shell.execute_reply.started":"2022-08-13T17:22:49.986569Z","shell.execute_reply":"2022-08-13T17:22:49.993307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a> \n# <b>2 <span style='color:#31B8EA'>|</span> Setting Colors</b>\n\n### CREDITS: https://www.kaggle.com/andradaolteanu ","metadata":{}},{"cell_type":"code","source":"# Custom colors\nclass clr:\n    S = '\\033[1m' + '\\033[94m'\n    E = '\\033[0m'","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:22:50.559451Z","iopub.execute_input":"2022-08-13T17:22:50.559993Z","iopub.status.idle":"2022-08-13T17:22:50.566851Z","shell.execute_reply.started":"2022-08-13T17:22:50.559953Z","shell.execute_reply":"2022-08-13T17:22:50.565465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_colors = [\"#10002b\",\"#240046\",\"#3c096c\",\"#5a189a\",\"#7b2cbf\",\"#9d4edd\",\"#c77dff\",\"#e0aaff\"]\nCMAP1 = ListedColormap(my_colors)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:22:51.656521Z","iopub.execute_input":"2022-08-13T17:22:51.656979Z","iopub.status.idle":"2022-08-13T17:22:51.662352Z","shell.execute_reply.started":"2022-08-13T17:22:51.656946Z","shell.execute_reply":"2022-08-13T17:22:51.661450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_colors_1 = [\"#001219\",\"#005f73\",\"#0a9396\",\"#94d2bd\",\"#e9d8a6\",\"#ee9b00\",\"#ca6702\",\"#bb3e03\",\"#ae2012\",\"#9b2226\"]\n\nCMAP2 = ListedColormap(my_colors_1)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:22:52.462826Z","iopub.execute_input":"2022-08-13T17:22:52.463289Z","iopub.status.idle":"2022-08-13T17:22:52.469727Z","shell.execute_reply.started":"2022-08-13T17:22:52.463252Z","shell.execute_reply":"2022-08-13T17:22:52.468292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clr.S+\"Notebook Color Scheme ONE:\"+clr.S)\nsns.palplot(sns.color_palette(my_colors))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:22:53.174472Z","iopub.execute_input":"2022-08-13T17:22:53.175805Z","iopub.status.idle":"2022-08-13T17:22:53.303678Z","shell.execute_reply.started":"2022-08-13T17:22:53.175756Z","shell.execute_reply":"2022-08-13T17:22:53.301848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(clr.S+\"Notebook Color Scheme Two:\"+clr.S)\nsns.palplot(sns.color_palette(my_colors_1))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:22:53.787717Z","iopub.execute_input":"2022-08-13T17:22:53.788193Z","iopub.status.idle":"2022-08-13T17:22:53.898813Z","shell.execute_reply.started":"2022-08-13T17:22:53.788158Z","shell.execute_reply":"2022-08-13T17:22:53.896450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a> \n# <b>3 <span style='color:#31B8EA'>|</span> LOADING DATA</b>","metadata":{}},{"cell_type":"code","source":"data_train = pd.read_csv(\"../input/tabular-playground-series-aug-2022/train.csv\", index_col=0)\ndata_test = pd.read_csv(\"../input/tabular-playground-series-aug-2022/test.csv\", index_col=0)\nsample_submission = pd.read_csv(\"../input/tabular-playground-series-aug-2022/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:22:55.460036Z","iopub.execute_input":"2022-08-13T17:22:55.460514Z","iopub.status.idle":"2022-08-13T17:22:55.779551Z","shell.execute_reply.started":"2022-08-13T17:22:55.460480Z","shell.execute_reply":"2022-08-13T17:22:55.778298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a> \n# <b>4 <span style='color:#31B8EA'>|</span> Explore</b>","metadata":{}},{"cell_type":"code","source":"print(Fore.GREEN + \"data_train Shape is\", data_train.shape)\nprint(Fore.GREEN + \"data_test Shape is\", data_test.shape)\nprint(Fore.GREEN + \"sample_submission Shape is\", sample_submission.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:22:57.499409Z","iopub.execute_input":"2022-08-13T17:22:57.500612Z","iopub.status.idle":"2022-08-13T17:22:57.507768Z","shell.execute_reply.started":"2022-08-13T17:22:57.500567Z","shell.execute_reply":"2022-08-13T17:22:57.506579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:22:58.629729Z","iopub.execute_input":"2022-08-13T17:22:58.630629Z","iopub.status.idle":"2022-08-13T17:22:58.660021Z","shell.execute_reply.started":"2022-08-13T17:22:58.630585Z","shell.execute_reply":"2022-08-13T17:22:58.658730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.options.display.max_columns = 100\ndata_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:22:59.265208Z","iopub.execute_input":"2022-08-13T17:22:59.266469Z","iopub.status.idle":"2022-08-13T17:22:59.319687Z","shell.execute_reply.started":"2022-08-13T17:22:59.266414Z","shell.execute_reply":"2022-08-13T17:22:59.318466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Zero is', round(data_train['failure'].value_counts()[0]/len(data_train) * 100,2), '% of the failure class!')\nprint('One is', round(data_train['failure'].value_counts()[1]/len(data_train) * 100,2), '% of the failure class!')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:00.374955Z","iopub.execute_input":"2022-08-13T17:23:00.375677Z","iopub.status.idle":"2022-08-13T17:23:00.387165Z","shell.execute_reply.started":"2022-08-13T17:23:00.375622Z","shell.execute_reply":"2022-08-13T17:23:00.385897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.describe().style.background_gradient(cmap='Spectral')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:01.157074Z","iopub.execute_input":"2022-08-13T17:23:01.158696Z","iopub.status.idle":"2022-08-13T17:23:01.380401Z","shell.execute_reply.started":"2022-08-13T17:23:01.158629Z","shell.execute_reply":"2022-08-13T17:23:01.378749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"5\"></a> \n# <b>5 <span style='color:#31B8EA'>|</span> Explore Nulls for Test & Train</b>","metadata":{}},{"cell_type":"code","source":"# Plot to see null values in our train data\nsns.set(rc={'figure.figsize':(14,10)})\nsns.heatmap(data_train.isnull(), cbar=False, cmap=CMAP2)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:02.803857Z","iopub.execute_input":"2022-08-13T17:23:02.804391Z","iopub.status.idle":"2022-08-13T17:23:04.830696Z","shell.execute_reply.started":"2022-08-13T17:23:02.804338Z","shell.execute_reply":"2022-08-13T17:23:04.829668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Null Value Percentage Per column\npercent_missing = data_train.isnull().sum() * 100 / len(data_train)\nmissing_value_df = pd.DataFrame({'column_name': data_train.columns,\n                                 'percent_missing': percent_missing})\nmissing_value_df.sort_values('percent_missing', ascending=False).style.background_gradient(cmap='Spectral')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:04.832538Z","iopub.execute_input":"2022-08-13T17:23:04.832996Z","iopub.status.idle":"2022-08-13T17:23:04.864114Z","shell.execute_reply.started":"2022-08-13T17:23:04.832938Z","shell.execute_reply":"2022-08-13T17:23:04.863070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"msno.matrix(data_train, color=(0.60, 0.42, 0.51))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:04.865709Z","iopub.execute_input":"2022-08-13T17:23:04.866089Z","iopub.status.idle":"2022-08-13T17:23:05.898685Z","shell.execute_reply.started":"2022-08-13T17:23:04.866057Z","shell.execute_reply":"2022-08-13T17:23:05.897587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot to see null values in our test data\nsns.set(rc={'figure.figsize':(14,10)})\nsns.heatmap(data_test.isnull(), cbar=False, cmap=CMAP2)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:05.958394Z","iopub.execute_input":"2022-08-13T17:23:05.959786Z","iopub.status.idle":"2022-08-13T17:23:07.497792Z","shell.execute_reply.started":"2022-08-13T17:23:05.959727Z","shell.execute_reply":"2022-08-13T17:23:07.496553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Null Value Percentage Per column\npercent_missing_ts = data_test.isnull().sum() * 100 / len(data_test)\nmissing_value_df_ts = pd.DataFrame({'column_name': data_test.columns,\n                                 'percent_missing': percent_missing_ts})\nmissing_value_df_ts.sort_values('percent_missing', ascending=False).style.background_gradient(cmap='Spectral')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:07.499872Z","iopub.execute_input":"2022-08-13T17:23:07.500303Z","iopub.status.idle":"2022-08-13T17:23:07.528750Z","shell.execute_reply.started":"2022-08-13T17:23:07.500268Z","shell.execute_reply":"2022-08-13T17:23:07.527584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"msno.matrix(data_test, color=(0.60, 0.42, 0.51))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:08.529064Z","iopub.execute_input":"2022-08-13T17:23:08.529708Z","iopub.status.idle":"2022-08-13T17:23:09.398252Z","shell.execute_reply.started":"2022-08-13T17:23:08.529655Z","shell.execute_reply":"2022-08-13T17:23:09.396460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### (1) What we can see from above analysis is that the percentage of null values in test data as well as the train dataset is quite similar.\n\n### (2) Only Float Columns have Missing Values.","metadata":{}},{"cell_type":"markdown","source":"<a id=\"6\"></a> \n# <b>6 <span style='color:#31B8EA'>|</span> Correlation Heatmap</b>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(30,30))\ncor = data_train.corr()\nsns.heatmap(cor, annot=True, cmap='Spectral', fmt='.2f')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:11.589080Z","iopub.execute_input":"2022-08-13T17:23:11.590313Z","iopub.status.idle":"2022-08-13T17:23:14.182803Z","shell.execute_reply.started":"2022-08-13T17:23:11.590263Z","shell.execute_reply":"2022-08-13T17:23:14.181799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"7\"></a> \n# <b>7 <span style='color:#31B8EA'>|</span> Taking Care of Missing Values</b>\n\n## Credits \n### [Ambrosm's Notebook](https://www.kaggle.com/code/ambrosm/tpsaug22-eda-which-makes-sense)\n### [Desalegngeb's Notebook](https://www.kaggle.com/code/desalegngeb/tps08-logisticregression-and-some-fe)\nAll four attributes are directly related with product code their value is determined by product code column so we can impute the missing values using the same group data.","metadata":{}},{"cell_type":"code","source":"data_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:18.507861Z","iopub.execute_input":"2022-08-13T17:23:18.508346Z","iopub.status.idle":"2022-08-13T17:23:18.541083Z","shell.execute_reply.started":"2022-08-13T17:23:18.508308Z","shell.execute_reply":"2022-08-13T17:23:18.539970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = data_train.pop('failure')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:19.477291Z","iopub.execute_input":"2022-08-13T17:23:19.478284Z","iopub.status.idle":"2022-08-13T17:23:19.485034Z","shell.execute_reply.started":"2022-08-13T17:23:19.478235Z","shell.execute_reply":"2022-08-13T17:23:19.483755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train['product_code'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:20.773993Z","iopub.execute_input":"2022-08-13T17:23:20.775407Z","iopub.status.idle":"2022-08-13T17:23:20.788439Z","shell.execute_reply.started":"2022-08-13T17:23:20.775331Z","shell.execute_reply":"2022-08-13T17:23:20.787183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test['product_code'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:22.447945Z","iopub.execute_input":"2022-08-13T17:23:22.448854Z","iopub.status.idle":"2022-08-13T17:23:22.459619Z","shell.execute_reply.started":"2022-08-13T17:23:22.448812Z","shell.execute_reply":"2022-08-13T17:23:22.457897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_A = data_train[data_train['product_code']=='A']\ndf_B = data_train[data_train['product_code']=='B']\ndf_C = data_train[data_train['product_code']=='C']\ndf_D = data_train[data_train['product_code']=='D']\ndf_E = data_train[data_train['product_code']=='E']\nprint(Fore.GREEN + \"Train Data Done!\")\ndf_F_t = data_test[data_test['product_code']=='F']\ndf_G_t = data_test[data_test['product_code']=='G']\ndf_H_t = data_test[data_test['product_code']=='H']\ndf_I_t = data_test[data_test['product_code']=='I']\nprint(Fore.GREEN + \"Test Data Done!\")","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:23.113401Z","iopub.execute_input":"2022-08-13T17:23:23.114084Z","iopub.status.idle":"2022-08-13T17:23:23.157612Z","shell.execute_reply.started":"2022-08-13T17:23:23.114046Z","shell.execute_reply":"2022-08-13T17:23:23.155981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"object_cols = [col for col in data_train.columns if data_train[col].dtypes == 'object']\nnullValue_cols = [col for col in data_train.columns if data_train[col].isnull().sum()!=0]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:24.537202Z","iopub.execute_input":"2022-08-13T17:23:24.538864Z","iopub.status.idle":"2022-08-13T17:23:24.556697Z","shell.execute_reply.started":"2022-08-13T17:23:24.538816Z","shell.execute_reply":"2022-08-13T17:23:24.555390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbm_imtr = LGBMImputer(cat_features=object_cols, n_iter=50)\n\n# train dataset\ntrain_iterimp_A = lgbm_imtr.fit_transform(df_A[nullValue_cols])\ntrain_iterimp_B = lgbm_imtr.fit_transform(df_B[nullValue_cols])\ntrain_iterimp_C = lgbm_imtr.fit_transform(df_C[nullValue_cols])\ntrain_iterimp_D = lgbm_imtr.fit_transform(df_D[nullValue_cols])\ntrain_iterimp_E = lgbm_imtr.fit_transform(df_E[nullValue_cols])\n\n# test dataset\ntest_iterimp_F = lgbm_imtr.fit_transform(df_F_t[nullValue_cols])\ntest_iterimp_G = lgbm_imtr.fit_transform(df_G_t[nullValue_cols])\ntest_iterimp_H = lgbm_imtr.fit_transform(df_H_t[nullValue_cols])\ntest_iterimp_I = lgbm_imtr.fit_transform(df_I_t[nullValue_cols])\n","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:25.566015Z","iopub.execute_input":"2022-08-13T17:23:25.566463Z","iopub.status.idle":"2022-08-13T17:23:52.430792Z","shell.execute_reply.started":"2022-08-13T17:23:25.566428Z","shell.execute_reply":"2022-08-13T17:23:52.429602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"none_na_cols = [col for col in data_train.columns if col not in nullValue_cols]\nnone_na_cols_ = [col for col in data_test.columns if col not in nullValue_cols]\ndf_train = data_train[none_na_cols]\ndf_test = data_test[none_na_cols_]\n\ntrain_ = pd.concat([train_iterimp_A, train_iterimp_B,train_iterimp_C,train_iterimp_D,train_iterimp_E], axis=0)\ntrain = pd.concat([df_train, train_], axis=1)\n\ntest_ = pd.concat([test_iterimp_F, test_iterimp_G,test_iterimp_H,test_iterimp_I], axis=0)\ntest = pd.concat([df_test, test_], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:23:56.212604Z","iopub.execute_input":"2022-08-13T17:23:56.213025Z","iopub.status.idle":"2022-08-13T17:23:56.235324Z","shell.execute_reply.started":"2022-08-13T17:23:56.212993Z","shell.execute_reply":"2022-08-13T17:23:56.234091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Fore.GREEN + \"Missing values in train dataset after pre-peocessing is: \", format(train.isna().sum().sum()))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:03.603692Z","iopub.execute_input":"2022-08-13T17:24:03.604149Z","iopub.status.idle":"2022-08-13T17:24:03.620189Z","shell.execute_reply.started":"2022-08-13T17:24:03.604116Z","shell.execute_reply":"2022-08-13T17:24:03.618537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Fore.GREEN + \"Missing values in test dataset after pre-peocessing is: \", format(test.isna().sum().sum()))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:04.346895Z","iopub.execute_input":"2022-08-13T17:24:04.347694Z","iopub.status.idle":"2022-08-13T17:24:04.360401Z","shell.execute_reply.started":"2022-08-13T17:24:04.347650Z","shell.execute_reply":"2022-08-13T17:24:04.359156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train['attribute_2'].unique())\ndisplay(train['attribute_3'].unique())\nprint()\nprint()\ndisplay(train['measurement_0'].unique())\ndisplay(train['measurement_1'].unique())\ndisplay(train['measurement_2'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:05.300137Z","iopub.execute_input":"2022-08-13T17:24:05.300595Z","iopub.status.idle":"2022-08-13T17:24:05.319561Z","shell.execute_reply.started":"2022-08-13T17:24:05.300559Z","shell.execute_reply":"2022-08-13T17:24:05.318732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['attribute_2*3'] = train['attribute_2'] * train['attribute_3']\ntest['attribute_2*3'] = test['attribute_2'] * test['attribute_3']","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:06.896235Z","iopub.execute_input":"2022-08-13T17:24:06.896650Z","iopub.status.idle":"2022-08-13T17:24:06.904900Z","shell.execute_reply.started":"2022-08-13T17:24:06.896619Z","shell.execute_reply":"2022-08-13T17:24:06.903957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meas_gr1_cols = [f\"measurement_{i:d}\" for i in list(range(3, 5)) + list(range(9, 17))]\ntrain['meas_gr1_avg'] = np.mean(train[meas_gr1_cols], axis=1)\ntrain['meas_gr1_std'] = np.std(train[meas_gr1_cols], axis=1)\n\ntest['meas_gr1_avg'] = np.mean(test[meas_gr1_cols], axis=1)\ntest['meas_gr1_std'] = np.std(test[meas_gr1_cols], axis=1) \n\nmeas_gr2_cols = [f\"measurement_{i:d}\" for i in list(range(5, 9))]\ntrain['meas_gr2_avg'] = np.mean(train[meas_gr2_cols], axis=1)\ntest['meas_gr2_avg'] = np.mean(test[meas_gr2_cols], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:07.270321Z","iopub.execute_input":"2022-08-13T17:24:07.271144Z","iopub.status.idle":"2022-08-13T17:24:07.314451Z","shell.execute_reply.started":"2022-08-13T17:24:07.271107Z","shell.execute_reply":"2022-08-13T17:24:07.313587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['meas17/meas_gr2_avg'] = train['measurement_17'] / train['meas_gr2_avg']\ntest['meas17/meas_gr2_avg'] = test['measurement_17'] / test['meas_gr2_avg']","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:07.681864Z","iopub.execute_input":"2022-08-13T17:24:07.682623Z","iopub.status.idle":"2022-08-13T17:24:07.689202Z","shell.execute_reply.started":"2022-08-13T17:24:07.682587Z","shell.execute_reply":"2022-08-13T17:24:07.688416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols_to_use = ['measurement_0', 'measurement_1', 'measurement_2', 'attribute_0', 'attribute_1', \n               'meas_gr1_avg', 'meas_gr1_std', 'attribute_2*3', 'loading', 'measurement_17', 'meas17/meas_gr2_avg']","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:08.579050Z","iopub.execute_input":"2022-08-13T17:24:08.579665Z","iopub.status.idle":"2022-08-13T17:24:08.585543Z","shell.execute_reply.started":"2022-08-13T17:24:08.579630Z","shell.execute_reply":"2022-08-13T17:24:08.584077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[cols_to_use]\ntest = test[cols_to_use]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:09.718906Z","iopub.execute_input":"2022-08-13T17:24:09.719879Z","iopub.status.idle":"2022-08-13T17:24:09.732515Z","shell.execute_reply.started":"2022-08-13T17:24:09.719842Z","shell.execute_reply":"2022-08-13T17:24:09.731353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train['attribute_0'].unique())\ndisplay(test['attribute_0'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:10.106448Z","iopub.execute_input":"2022-08-13T17:24:10.107732Z","iopub.status.idle":"2022-08-13T17:24:10.121849Z","shell.execute_reply.started":"2022-08-13T17:24:10.107683Z","shell.execute_reply":"2022-08-13T17:24:10.120520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train['attribute_1'].unique())\ndisplay(test['attribute_1'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:10.474285Z","iopub.execute_input":"2022-08-13T17:24:10.474794Z","iopub.status.idle":"2022-08-13T17:24:10.489798Z","shell.execute_reply.started":"2022-08-13T17:24:10.474755Z","shell.execute_reply":"2022-08-13T17:24:10.488339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined_data = pd.concat([train,test],axis = 0)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:10.815613Z","iopub.execute_input":"2022-08-13T17:24:10.816293Z","iopub.status.idle":"2022-08-13T17:24:10.826597Z","shell.execute_reply.started":"2022-08-13T17:24:10.816255Z","shell.execute_reply":"2022-08-13T17:24:10.825249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:11.220808Z","iopub.execute_input":"2022-08-13T17:24:11.221514Z","iopub.status.idle":"2022-08-13T17:24:11.240264Z","shell.execute_reply.started":"2022-08-13T17:24:11.221475Z","shell.execute_reply":"2022-08-13T17:24:11.238858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder = preprocessing.LabelEncoder() \ncombined_data['attribute_0'] = label_encoder.fit_transform(combined_data['attribute_0'])\ncombined_data['attribute_1'] = label_encoder.fit_transform(combined_data['attribute_1'])","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:12.998926Z","iopub.execute_input":"2022-08-13T17:24:12.999421Z","iopub.status.idle":"2022-08-13T17:24:13.033083Z","shell.execute_reply.started":"2022-08-13T17:24:12.999383Z","shell.execute_reply":"2022-08-13T17:24:13.032074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:13.399234Z","iopub.execute_input":"2022-08-13T17:24:13.400274Z","iopub.status.idle":"2022-08-13T17:24:13.422020Z","shell.execute_reply.started":"2022-08-13T17:24:13.400229Z","shell.execute_reply":"2022-08-13T17:24:13.420556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = combined_data[combined_data.index<=26569]\ntest = combined_data[combined_data.index>=26570]","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:13.790486Z","iopub.execute_input":"2022-08-13T17:24:13.791177Z","iopub.status.idle":"2022-08-13T17:24:13.804887Z","shell.execute_reply.started":"2022-08-13T17:24:13.791140Z","shell.execute_reply":"2022-08-13T17:24:13.803562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(5).style.background_gradient(cmap='Spectral')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:15.593316Z","iopub.execute_input":"2022-08-13T17:24:15.594060Z","iopub.status.idle":"2022-08-13T17:24:15.630046Z","shell.execute_reply.started":"2022-08-13T17:24:15.594021Z","shell.execute_reply":"2022-08-13T17:24:15.628748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(5).style.background_gradient(cmap='Spectral')","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:15.969011Z","iopub.execute_input":"2022-08-13T17:24:15.969469Z","iopub.status.idle":"2022-08-13T17:24:16.003981Z","shell.execute_reply.started":"2022-08-13T17:24:15.969433Z","shell.execute_reply":"2022-08-13T17:24:16.002260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train['attribute_0'].unique())\ndisplay(train['attribute_1'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:18.147217Z","iopub.execute_input":"2022-08-13T17:24:18.147736Z","iopub.status.idle":"2022-08-13T17:24:18.160474Z","shell.execute_reply.started":"2022-08-13T17:24:18.147698Z","shell.execute_reply":"2022-08-13T17:24:18.158951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(test['attribute_0'].unique())\ndisplay(test['attribute_1'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:18.473938Z","iopub.execute_input":"2022-08-13T17:24:18.474395Z","iopub.status.idle":"2022-08-13T17:24:18.485909Z","shell.execute_reply.started":"2022-08-13T17:24:18.474349Z","shell.execute_reply":"2022-08-13T17:24:18.484649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Fore.GREEN + \"train Shape is\", train.shape)\nprint(Fore.GREEN + \"test Shape is\", test.shape)\nprint(Fore.GREEN + \"target Shape is\", target.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:18.971581Z","iopub.execute_input":"2022-08-13T17:24:18.972052Z","iopub.status.idle":"2022-08-13T17:24:18.979178Z","shell.execute_reply.started":"2022-08-13T17:24:18.972017Z","shell.execute_reply":"2022-08-13T17:24:18.977855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"8\"></a> \n# <b>8 <span style='color:#31B8EA'>|</span> XGBoost with GridSearchCV</b>","metadata":{}},{"cell_type":"code","source":"\"\"\"# hyperparameter tuning with XGBoost\n\n# creating a KFold object \nfolds = 3\n\n# specify range of hyperparameters\nparam_grid = {'learning_rate': [0.2, 0.6], \n             'subsample': [0.3, 0.6, 0.9]}          \n\n\n# specify model\nxgb_model = XGBClassifier(max_depth=2, n_estimators=200)\n\n# set up GridSearchCV()\nmodel_cv = GridSearchCV(estimator = xgb_model, \n                        param_grid = param_grid, \n                        scoring = 'roc_auc', \n                        cv = folds, \n                        verbose = 1,\n                        return_train_score=True)      \n\n# fit the model\nmodel_cv.fit(train, target)       \"\"\"","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model_cv.best_params_","metadata":{"execution":{"iopub.status.busy":"2022-08-13T12:57:00.448243Z","iopub.execute_input":"2022-08-13T12:57:00.448716Z","iopub.status.idle":"2022-08-13T12:57:00.456145Z","shell.execute_reply.started":"2022-08-13T12:57:00.448678Z","shell.execute_reply":"2022-08-13T12:57:00.455315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {'learning_rate': 0.2,\n          'max_depth': 2, \n          'n_estimators':200,\n          'subsample':0.9,\n         'objective':'binary:logistic'}\n\n# fit model on training data\nxgb_imb_model = XGBClassifier(params = params)\nxgb_imb_model.fit(train, target)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:24.927784Z","iopub.execute_input":"2022-08-13T17:24:24.928181Z","iopub.status.idle":"2022-08-13T17:24:27.856360Z","shell.execute_reply.started":"2022-08-13T17:24:24.928151Z","shell.execute_reply":"2022-08-13T17:24:27.855232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predictions on the train set\ntrain_pred = xgb_imb_model.predict(train)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:31.910931Z","iopub.execute_input":"2022-08-13T17:24:31.911541Z","iopub.status.idle":"2022-08-13T17:24:31.957974Z","shell.execute_reply.started":"2022-08-13T17:24:31.911477Z","shell.execute_reply":"2022-08-13T17:24:31.956846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Accuracy\nprint(\"Accuracy:-\", metrics.accuracy_score(target, train_pred))\n\n# F1 score\nprint(\"F1-Score:-\", f1_score(target, train_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:38.130295Z","iopub.execute_input":"2022-08-13T17:24:38.130770Z","iopub.status.idle":"2022-08-13T17:24:38.155140Z","shell.execute_reply.started":"2022-08-13T17:24:38.130736Z","shell.execute_reply":"2022-08-13T17:24:38.153623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# classification_report\nprint(classification_report(target, train_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:41.993148Z","iopub.execute_input":"2022-08-13T17:24:41.995781Z","iopub.status.idle":"2022-08-13T17:24:42.048158Z","shell.execute_reply.started":"2022-08-13T17:24:41.995698Z","shell.execute_reply":"2022-08-13T17:24:42.046794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = xgb_imb_model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:43.235445Z","iopub.execute_input":"2022-08-13T17:24:43.235855Z","iopub.status.idle":"2022-08-13T17:24:43.270888Z","shell.execute_reply.started":"2022-08-13T17:24:43.235822Z","shell.execute_reply":"2022-08-13T17:24:43.269455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.failure = test_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:44.824165Z","iopub.execute_input":"2022-08-13T17:24:44.824578Z","iopub.status.idle":"2022-08-13T17:24:44.831903Z","shell.execute_reply.started":"2022-08-13T17:24:44.824549Z","shell.execute_reply":"2022-08-13T17:24:44.829875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:45.719304Z","iopub.execute_input":"2022-08-13T17:24:45.719757Z","iopub.status.idle":"2022-08-13T17:24:45.734035Z","shell.execute_reply.started":"2022-08-13T17:24:45.719724Z","shell.execute_reply":"2022-08-13T17:24:45.733217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['failure'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:46.688314Z","iopub.execute_input":"2022-08-13T17:24:46.689314Z","iopub.status.idle":"2022-08-13T17:24:46.698456Z","shell.execute_reply.started":"2022-08-13T17:24:46.689272Z","shell.execute_reply":"2022-08-13T17:24:46.697158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-13T17:24:48.074078Z","iopub.execute_input":"2022-08-13T17:24:48.075200Z","iopub.status.idle":"2022-08-13T17:24:48.103946Z","shell.execute_reply.started":"2022-08-13T17:24:48.075156Z","shell.execute_reply":"2022-08-13T17:24:48.102671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Disclamer: This Notebook is a Work in progress!\n\n### 🍇 I'm Hungry Now 🍇","metadata":{}},{"cell_type":"markdown","source":"<center><img src=\"https://images.unsplash.com/photo-1527598041828-aea5d622f3a8?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1470&q=80\" \n             style='border-radius:10px'></center>","metadata":{}},{"cell_type":"markdown","source":"<p style=\"color:#F6F6F6;background-color:#25AAE1;text-align:center;border-radius:10px 10px;font-weight:bold;font-size:22px\"> If you Like this Notebook please Upvote and also give your kind suggestions in Comments!<span style='font-size:28px; background-color:#F6F6F6 ;'></span></p>","metadata":{}}]}