{"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":"This is a basic **XGBoost** model with some EDA to get you started.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn import preprocessing\nimport xgboost as xgb","metadata":{"execution":{"iopub.status.busy":"2022-08-03T12:41:31.298630Z","iopub.execute_input":"2022-08-03T12:41:31.299095Z","iopub.status.idle":"2022-08-03T12:41:32.487867Z","shell.execute_reply.started":"2022-08-03T12:41:31.299000Z","shell.execute_reply":"2022-08-03T12:41:32.486760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  <div style=\"padding:20px;color:white;margin:0;font-size:175%;text-align:center;display:fill;border-radius:5px;background-color:#016CC9;overflow:hidden;font-weight:500\">1. LOAD DATA</div>","metadata":{}},{"cell_type":"code","source":"train=pd.read_csv('../input/Covid19-Death-Predictions/train.csv')\ntest=pd.read_csv('../input/Covid19-Death-Predictions/test.csv')\nsubmit = pd.read_csv('../input/Covid19-Death-Predictions/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-03T12:41:32.490005Z","iopub.execute_input":"2022-08-03T12:41:32.490477Z","iopub.status.idle":"2022-08-03T12:41:33.032220Z","shell.execute_reply.started":"2022-08-03T12:41:32.490432Z","shell.execute_reply":"2022-08-03T12:41:33.030626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  <div style=\"padding:20px;color:white;margin:0;font-size:175%;text-align:center;display:fill;border-radius:5px;background-color:#016CC9;overflow:hidden;font-weight:500\">2. Exploratory Data Analysis</div>","metadata":{}},{"cell_type":"code","source":"train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-03T12:41:33.033808Z","iopub.execute_input":"2022-08-03T12:41:33.036413Z","iopub.status.idle":"2022-08-03T12:41:33.051001Z","shell.execute_reply.started":"2022-08-03T12:41:33.036353Z","shell.execute_reply":"2022-08-03T12:41:33.049805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Dataframe Shapes. Train: {train.shape} - Test: {test.shape} - Submit: {submit.shape}.')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-03T12:41:33.053970Z","iopub.execute_input":"2022-08-03T12:41:33.054641Z","iopub.status.idle":"2022-08-03T12:41:33.060072Z","shell.execute_reply.started":"2022-08-03T12:41:33.054598Z","shell.execute_reply":"2022-08-03T12:41:33.059198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"there are **18 features** (we don't count **Id** and **Next Week's Deaths**) , 129k rows to train, 43k rows to predict.","metadata":{}},{"cell_type":"markdown","source":"**Location** is the only categorical data requiring to be transformed in numerical value. We will **One Hot Encode** it. ","metadata":{}},{"cell_type":"markdown","source":"#  <div style=\"padding:20px;color:white;margin:0;font-size:175%;text-align:center;display:fill;border-radius:5px;background-color:#016CC9;overflow:hidden;font-weight:500\">3. One Hot Encoding</div>","metadata":{}},{"cell_type":"code","source":"#APPEND TEST TO TRAIN \ntrain['train']=True\ntest['train']=False\ntest[\"Next Week's Deaths\"]=0\ntraintest=train.append(test,ignore_index=True)\n\n#One Hot Encode\ntraintest=pd.get_dummies(traintest)\n\n#SEPARATE TRAIN AND TEST\ntrain=traintest[traintest['train']]\ntest=traintest[~traintest['train']]","metadata":{"execution":{"iopub.status.busy":"2022-08-03T12:41:33.092418Z","iopub.execute_input":"2022-08-03T12:41:33.093099Z","iopub.status.idle":"2022-08-03T12:41:33.773214Z","shell.execute_reply.started":"2022-08-03T12:41:33.093056Z","shell.execute_reply":"2022-08-03T12:41:33.772304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  <div style=\"padding:20px;color:white;margin:0;font-size:175%;text-align:center;display:fill;border-radius:5px;background-color:#016CC9;overflow:hidden;font-weight:500\">4. Train XGBoost</div>","metadata":{}},{"cell_type":"code","source":"features = [col for col in train.columns if (col != 'Id' and col != \"Next Week's Deaths\")]  ","metadata":{"execution":{"iopub.status.busy":"2022-08-03T12:41:33.774363Z","iopub.execute_input":"2022-08-03T12:41:33.775107Z","iopub.status.idle":"2022-08-03T12:41:33.780318Z","shell.execute_reply.started":"2022-08-03T12:41:33.775075Z","shell.execute_reply":"2022-08-03T12:41:33.778887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We need a **regressor** here as the target is a numerical value (it is **not a classification** problem).","metadata":{}},{"cell_type":"code","source":"model = xgb.XGBRegressor() \nmodel.fit(train[features],train[\"Next Week's Deaths\"])","metadata":{"execution":{"iopub.status.busy":"2022-08-03T12:41:33.781973Z","iopub.execute_input":"2022-08-03T12:41:33.782418Z","iopub.status.idle":"2022-08-03T12:42:42.417192Z","shell.execute_reply.started":"2022-08-03T12:41:33.782383Z","shell.execute_reply":"2022-08-03T12:42:42.415829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  <div style=\"padding:20px;color:white;margin:0;font-size:175%;text-align:center;display:fill;border-radius:5px;background-color:#016CC9;overflow:hidden;font-weight:500\">5. Predict and submit</div>","metadata":{}},{"cell_type":"code","source":"#Predict\npredict = model.predict(test[features])\nprint(predict)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T12:42:42.418634Z","iopub.execute_input":"2022-08-03T12:42:42.418981Z","iopub.status.idle":"2022-08-03T12:42:43.344082Z","shell.execute_reply.started":"2022-08-03T12:42:42.418943Z","shell.execute_reply":"2022-08-03T12:42:43.343168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"some values are negative. This is impossible, so we will adjust to zero.","metadata":{}},{"cell_type":"code","source":"#Submit\nsubmit[\"Next Week's Deaths\"]=np.maximum(predict, 0)\nsubmit.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-03T12:42:43.347582Z","iopub.execute_input":"2022-08-03T12:42:43.348354Z","iopub.status.idle":"2022-08-03T12:42:43.437520Z","shell.execute_reply.started":"2022-08-03T12:42:43.348309Z","shell.execute_reply":"2022-08-03T12:42:43.436325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  <div style=\"padding:20px;color:white;margin:0;font-size:175%;text-align:center;display:fill;border-radius:5px;background-color:#016CC9;overflow:hidden;font-weight:500\">6. Conclusion</div>","metadata":{}},{"cell_type":"markdown","source":"This simple XGBoost model will get you an rmse of **339.56 in the Public Leaderboard**.","metadata":{}}]}