{"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 seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-11T05:29:15.502942Z","iopub.execute_input":"2022-08-11T05:29:15.503351Z","iopub.status.idle":"2022-08-11T05:29:15.509017Z","shell.execute_reply.started":"2022-08-11T05:29:15.503312Z","shell.execute_reply":"2022-08-11T05:29:15.507584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data exploration**","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/Covid19-Death-Predictions/train.csv\",index_col=\"Id\")\n#total_len = len(train)\n#train.dropna(inplace=True)\naverage_values = train.mean(numeric_only=True)\ntrain_ = train.drop([\"Location\"],axis=1).fillna(average_values)\ntrain.loc[:, train.columns != \"Location\"] = train_\ntrain['Location'] = pd.Categorical(train.Location)\n#print(f'training exemples : {len(train)},  {(1-len(train)/total_len)*100:.1f} % data not NaN')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:29:15.756354Z","iopub.execute_input":"2022-08-11T05:29:15.757510Z","iopub.status.idle":"2022-08-11T05:29:16.082562Z","shell.execute_reply.started":"2022-08-11T05:29:15.757453Z","shell.execute_reply":"2022-08-11T05:29:16.081618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I made the decision not to take the lines containing NaN. In practice, there is offently a bias on the fact that there are missing data. An improvement would be to train another specialized model for the rows where data are missing (as there is in the testset) on the remaining 14.5%.","metadata":{}},{"cell_type":"markdown","source":"# Correlation analysis","metadata":{}},{"cell_type":"code","source":"corr_mat = train.corr()\nplt.figure(figsize=(15,10), dpi=80)\nsns.heatmap(corr_mat,annot=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:29:16.083968Z","iopub.execute_input":"2022-08-11T05:29:16.085214Z","iopub.status.idle":"2022-08-11T05:29:17.988903Z","shell.execute_reply.started":"2022-08-11T05:29:16.085178Z","shell.execute_reply":"2022-08-11T05:29:17.987669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Next Week's Deaths distribution","metadata":{}},{"cell_type":"code","source":"sns.displot(train, x=\"Next Week's Deaths\", kind=\"kde\");","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:29:17.991049Z","iopub.execute_input":"2022-08-11T05:29:17.991477Z","iopub.status.idle":"2022-08-11T05:29:18.779788Z","shell.execute_reply.started":"2022-08-11T05:29:17.991433Z","shell.execute_reply":"2022-08-11T05:29:18.778619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split X,y","metadata":{}},{"cell_type":"code","source":"X = train.drop([\"Next Week's Deaths\"],axis=1)\ny = train[\"Next Week's Deaths\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:29:18.781249Z","iopub.execute_input":"2022-08-11T05:29:18.781691Z","iopub.status.idle":"2022-08-11T05:29:18.792179Z","shell.execute_reply.started":"2022-08-11T05:29:18.781656Z","shell.execute_reply":"2022-08-11T05:29:18.790880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model definition**","metadata":{}},{"cell_type":"code","source":"!pip install flaml","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:29:18.795602Z","iopub.execute_input":"2022-08-11T05:29:18.796121Z","iopub.status.idle":"2022-08-11T05:29:29.804156Z","shell.execute_reply.started":"2022-08-11T05:29:18.796071Z","shell.execute_reply":"2022-08-11T05:29:29.802639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from flaml import AutoML\nautoml = AutoML()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:29:29.807584Z","iopub.execute_input":"2022-08-11T05:29:29.808171Z","iopub.status.idle":"2022-08-11T05:29:29.815376Z","shell.execute_reply.started":"2022-08-11T05:29:29.808115Z","shell.execute_reply":"2022-08-11T05:29:29.814008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model training**","metadata":{}},{"cell_type":"code","source":"automl.fit(X, y,task=\"regression\",metric='rmse',time_budget=60*60*1)# 1h","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:29:29.816792Z","iopub.execute_input":"2022-08-11T05:29:29.818148Z","iopub.status.idle":"2022-08-11T05:40:12.469647Z","shell.execute_reply.started":"2022-08-11T05:29:29.818096Z","shell.execute_reply":"2022-08-11T05:40:12.467720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Training statistics**","metadata":{}},{"cell_type":"code","source":"print('Best ML leaner:', automl.best_estimator)\nprint('Best hyperparmeter config:', automl.best_config)\nprint('Best rmse on validation data: {0:.4g}'.format(automl.best_loss))\nprint('Training duration of best run: {0:.4g} s'.format(automl.best_config_train_time))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:40:54.716262Z","iopub.execute_input":"2022-08-11T05:40:54.716701Z","iopub.status.idle":"2022-08-11T05:40:54.724100Z","shell.execute_reply.started":"2022-08-11T05:40:54.716667Z","shell.execute_reply":"2022-08-11T05:40:54.722410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Load testset**","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv(\"../input/Covid19-Death-Predictions/test.csv\",index_col=\"Id\")\nprint(f'len : {len(test)}')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:40:57.157754Z","iopub.execute_input":"2022-08-11T05:40:57.158532Z","iopub.status.idle":"2022-08-11T05:40:57.272107Z","shell.execute_reply.started":"2022-08-11T05:40:57.158492Z","shell.execute_reply":"2022-08-11T05:40:57.270644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I will fill the NaN with the average values as I made the decision not to take them into account previously","metadata":{}},{"cell_type":"code","source":"average_values = test.mean(numeric_only=True)\ntest_ = test.drop([\"Location\"],axis=1).fillna(average_values)\ntest.loc[:, test.columns != \"Location\"] = test_\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:40:59.206007Z","iopub.execute_input":"2022-08-11T05:40:59.206476Z","iopub.status.idle":"2022-08-11T05:40:59.260655Z","shell.execute_reply.started":"2022-08-11T05:40:59.206439Z","shell.execute_reply":"2022-08-11T05:40:59.259669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Prediction**","metadata":{}},{"cell_type":"code","source":"pred = automl.predict(test)\ntest[\"Next Week's Deaths\"] = pred\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:41:00.807074Z","iopub.execute_input":"2022-08-11T05:41:00.808794Z","iopub.status.idle":"2022-08-11T05:41:05.720069Z","shell.execute_reply.started":"2022-08-11T05:41:00.808749Z","shell.execute_reply":"2022-08-11T05:41:05.719015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(test, x=\"Next Week's Deaths\", kind=\"kde\");","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:41:05.721918Z","iopub.execute_input":"2022-08-11T05:41:05.722519Z","iopub.status.idle":"2022-08-11T05:41:06.186400Z","shell.execute_reply.started":"2022-08-11T05:41:05.722484Z","shell.execute_reply":"2022-08-11T05:41:06.185152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Submission**","metadata":{}},{"cell_type":"code","source":"sub = test[\"Next Week's Deaths\"]\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:41:06.187795Z","iopub.execute_input":"2022-08-11T05:41:06.188193Z","iopub.status.idle":"2022-08-11T05:41:06.197577Z","shell.execute_reply.started":"2022-08-11T05:41:06.188158Z","shell.execute_reply":"2022-08-11T05:41:06.196260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-11T05:41:06.199670Z","iopub.execute_input":"2022-08-11T05:41:06.200080Z","iopub.status.idle":"2022-08-11T05:41:06.321473Z","shell.execute_reply.started":"2022-08-11T05:41:06.200015Z","shell.execute_reply":"2022-08-11T05:41:06.320396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Upvote if it helped you, Thanks**","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}