{"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":"The goal of this example is to show how to make a submission. This is a toy model and should probably not be taken as a starting point. It does not take into accound all the information or the features temporality.\n\n> **Fork/edit this version to run the notebook and try the submission.**","metadata":{}},{"cell_type":"markdown","source":"## Import modules and loading the data\nFirst let's import modules, the training and the test set. The test set contains the same columns than the training set without the target.","metadata":{}},{"cell_type":"code","source":"#import modules\nfrom sklearn import metrics, model_selection\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.impute import SimpleImputer\nimport pandas as pd\n\n#loading the data\ntrain = pd.read_csv('/kaggle/input/football-match-probability-prediction/train.csv')\ntest = pd.read_csv('/kaggle/input/football-match-probability-prediction/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:02:05.394694Z","iopub.execute_input":"2022-03-25T17:02:05.395171Z","iopub.status.idle":"2022-03-25T17:02:14.869047Z","shell.execute_reply.started":"2022-03-25T17:02:05.395078Z","shell.execute_reply":"2022-03-25T17:02:14.868005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics, model_selection\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.impute import SimpleImputer\nimport pandas as pd\n\n#loading the data\ntrain = pd.read_csv('/kaggle/input/football-match-probability-prediction/train.csv')\ntrain['home_team_history_all_goal']=train['home_team_history_goal_1']+train['home_team_history_goal_2']\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:02:14.870857Z","iopub.execute_input":"2022-03-25T17:02:14.871108Z","iopub.status.idle":"2022-03-25T17:02:18.709414Z","shell.execute_reply.started":"2022-03-25T17:02:14.871078Z","shell.execute_reply":"2022-03-25T17:02:18.708643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make a model\nThe model is very simple and use only rating data where missing values are filled with their mean. The a logistic regression is fitted. Since we predict the probabilites of home, away and draw is it a multiclass logistic regression.","metadata":{}},{"cell_type":"code","source":"#keep only rating features\nrating_features = [x for x in  train if 'rating' in x]\n\n#Make X and y\nX = train[rating_features]\ny = train['target']","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:02:18.710335Z","iopub.execute_input":"2022-03-25T17:02:18.710526Z","iopub.status.idle":"2022-03-25T17:02:18.846226Z","shell.execute_reply.started":"2022-03-25T17:02:18.710502Z","shell.execute_reply":"2022-03-25T17:02:18.845292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#build a simple pipeline that fill nans values with the mean and fit a logistic regression classifier without regulariation.\npipeline = Pipeline(steps=[\n    ('inputer', SimpleImputer(strategy='mean')), \n    (\"classifier\", LogisticRegression(solver='sag',C=1e5))\n])","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:02:18.847964Z","iopub.execute_input":"2022-03-25T17:02:18.848174Z","iopub.status.idle":"2022-03-25T17:02:18.852874Z","shell.execute_reply.started":"2022-03-25T17:02:18.848149Z","shell.execute_reply":"2022-03-25T17:02:18.852010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#evaluate the model log loss using cross validation\ncv_scores = model_selection.cross_val_score(pipeline, X, y, cv=10,scoring='neg_log_loss')\nprint(f'The cross validation log loss is {-cv_scores.mean().round(4)}')","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:02:18.853965Z","iopub.execute_input":"2022-03-25T17:02:18.854179Z","iopub.status.idle":"2022-03-25T17:03:01.546721Z","shell.execute_reply.started":"2022-03-25T17:02:18.854154Z","shell.execute_reply":"2022-03-25T17:03:01.545874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fit the model and a make submission\nOnce the model is fitted we make the **probabilities prediction**. Then we make the submission dataframe with **4 columns**. The columns home, away and draw contain probability while the column id contains the match id.","metadata":{}},{"cell_type":"code","source":"pipeline.fit(X, y)\npredictions = pipeline.predict_proba(test[rating_features])","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:03:01.548140Z","iopub.execute_input":"2022-03-25T17:03:01.548593Z","iopub.status.idle":"2022-03-25T17:03:06.122374Z","shell.execute_reply.started":"2022-03-25T17:03:01.548535Z","shell.execute_reply":"2022-03-25T17:03:06.121407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(predictions ,columns=pipeline.classes_)\n\n#do not forget the id column\nsubmission['id'] = test['id']\n\n#submit!\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-25T17:03:06.124016Z","iopub.execute_input":"2022-03-25T17:03:06.124519Z","iopub.status.idle":"2022-03-25T17:03:06.548851Z","shell.execute_reply.started":"2022-03-25T17:03:06.124475Z","shell.execute_reply":"2022-03-25T17:03:06.547972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now the submission file is written down, just click on the submit button on the top right box.","metadata":{}}]}