{"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-02-13T07:05:25.958906Z","iopub.execute_input":"2022-02-13T07:05:25.959282Z","iopub.status.idle":"2022-02-13T07:05:33.765059Z","shell.execute_reply.started":"2022-02-13T07:05:25.959229Z","shell.execute_reply":"2022-02-13T07:05:33.763893Z"},"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":{"jupyter":{"source_hidden":true}}},{"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-02-13T07:05:33.767100Z","iopub.execute_input":"2022-02-13T07:05:33.767406Z","iopub.status.idle":"2022-02-13T07:05:33.846033Z","shell.execute_reply.started":"2022-02-13T07:05:33.767377Z","shell.execute_reply":"2022-02-13T07:05:33.844839Z"},"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-02-13T07:05:33.847433Z","iopub.execute_input":"2022-02-13T07:05:33.847669Z","iopub.status.idle":"2022-02-13T07:05:33.861958Z","shell.execute_reply.started":"2022-02-13T07:05:33.847640Z","shell.execute_reply":"2022-02-13T07:05:33.860830Z"},"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-02-13T07:05:33.863861Z","iopub.execute_input":"2022-02-13T07:05:33.864536Z","iopub.status.idle":"2022-02-13T07:06:29.268305Z","shell.execute_reply.started":"2022-02-13T07:05:33.864497Z","shell.execute_reply":"2022-02-13T07:06:29.267057Z"},"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":{"jupyter":{"source_hidden":true}}},{"cell_type":"code","source":"pipeline.fit(X, y)\npredictions = pipeline.predict_proba(test[rating_features])","metadata":{"execution":{"iopub.status.busy":"2022-02-13T07:06:29.275001Z","iopub.execute_input":"2022-02-13T07:06:29.278612Z","iopub.status.idle":"2022-02-13T07:06:35.169449Z","shell.execute_reply.started":"2022-02-13T07:06:29.278538Z","shell.execute_reply":"2022-02-13T07:06:35.168397Z"},"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-02-13T07:06:35.171038Z","iopub.execute_input":"2022-02-13T07:06:35.174498Z","iopub.status.idle":"2022-02-13T07:06:35.790532Z","shell.execute_reply.started":"2022-02-13T07:06:35.174436Z","shell.execute_reply":"2022-02-13T07:06:35.789853Z"},"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":{}}]}