{"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":"I found a data leakage and I used it. I got a very good score. Since I do not want to bump the competition I just publish what I found. In my opinion, the score could be improved","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-24T10:23:44.576519Z","iopub.execute_input":"2022-03-24T10:23:44.577207Z","iopub.status.idle":"2022-03-24T10:23:44.588762Z","shell.execute_reply.started":"2022-03-24T10:23:44.57715Z","shell.execute_reply":"2022-03-24T10:23:44.587895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For this example we just need the test file","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/football-match-probability-prediction/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:23:48.313216Z","iopub.execute_input":"2022-03-24T10:23:48.314067Z","iopub.status.idle":"2022-03-24T10:23:52.720026Z","shell.execute_reply.started":"2022-03-24T10:23:48.314011Z","shell.execute_reply":"2022-03-24T10:23:52.71916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"I am curious for which may I should made a prediction for my favourite team, Barcelona","metadata":{}},{"cell_type":"code","source":"test[test['home_team_name'] == 'Barcelona']","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:24:57.717545Z","iopub.execute_input":"2022-03-24T10:24:57.717956Z","iopub.status.idle":"2022-03-24T10:24:57.780759Z","shell.execute_reply.started":"2022-03-24T10:24:57.717917Z","shell.execute_reply":"2022-03-24T10:24:57.779841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So for one team we have to make more than one prediction. And we have the historical...so let's check if the predicted results are inside the data","metadata":{}},{"cell_type":"code","source":"test[test['home_team_name'] == 'Barcelona'][['match_date', 'home_team_history_match_date_1', 'home_team_history_match_date_2', 'home_team_history_match_date_3']]","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:30:50.900516Z","iopub.execute_input":"2022-03-24T10:30:50.901251Z","iopub.status.idle":"2022-03-24T10:30:50.929549Z","shell.execute_reply.started":"2022-03-24T10:30:50.901191Z","shell.execute_reply":"2022-03-24T10:30:50.928546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So the results of the 3 first matches are in the historical of the other matches. The exact result is not in the data, but there are the goals scored and the goals the oponent scored. So the final result is easy to calculate","metadata":{}},{"cell_type":"markdown","source":"With all this, only a function that looks for the results of that match is needed\nSince we are sure of the result, the predictions are 1 and the loss should be small. For the games for which there are not results, the sample submission is kept","metadata":{}},{"cell_type":"code","source":"# All teams are listed\nteams = set(list(test['home_team_name'].unique()) + list(test['away_team_name'].unique()))\nscoreMatrix = pd.read_csv('../input/football-match-probability-prediction/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-24T10:36:06.845073Z","iopub.execute_input":"2022-03-24T10:36:06.845851Z","iopub.status.idle":"2022-03-24T10:36:06.952659Z","shell.execute_reply.started":"2022-03-24T10:36:06.845812Z","shell.execute_reply":"2022-03-24T10:36:06.95173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculates the predictions given the scored goals\ndef getPreds(teamGoals, oponentGoals, home):\n    #print(teamGoals, oponentGoals)\n    if home:\n        if teamGoals > oponentGoals:\n            preds = np.array([1.0, 0.0, 0.0])\n        elif teamGoals == oponentGoals:\n            preds = np.array([0.0, 1.0, 0.0])\n        else:\n            preds = np.array([0.0, 0.0, 1.0])\n    else:\n        if teamGoals > oponentGoals:\n            preds = np.array([0.0, 0.0, 1.0])\n        elif teamGoals == oponentGoals:\n            preds = np.array([0.0, 1.0, 0.0])\n        else:\n            preds = np.array([1.0, 0.0, 0.0])   \n    #print(preds)    \n    return preds \n\n# The corresponding match is searched\ndef findMatchPreds(matchId, homeData, awayData):\n    foundMatch = False\n    preds = np.full((3), 0.33)\n    # Date is searched in home data\n    # It is searched from next match on\n    matchDate = homeData.iloc[matchId]['match_date']\n    for id in range(matchId + 1, len(homeData)):\n        for dateId in range(1,11):\n            if str(matchDate) == str(homeData.iloc[id]['home_team_history_match_date_' + str(dateId)]):\n                foundMatch = True\n                teamGoals = homeData.iloc[id]['home_team_history_goal_' + str(dateId)]\n                oponentGoals = homeData.iloc[id]['home_team_history_opponent_goal_' + str(dateId)]\n                preds = getPreds(teamGoals, oponentGoals, home=True)\n                break\n        if foundMatch:\n            break\n    \n    return foundMatch, preds\n\n# Previous results are searched in the test data\nleakIds = []\n\nfor team in teams:\n# for team in ['Getafe']:\n    homeData = test[test['home_team_name'] == team]\n    awayData = test[test['away_team_name'] == team]\n    for matchId in range(len(homeData['id'])):\n        # Duplicate searches are avoided\n        if matchId not in leakIds:\n            foundMatch, preds = findMatchPreds(matchId, homeData, awayData)\n            if foundMatch:\n                leakIds.append(homeData.iloc[matchId]['id'])\n                scoreMatrix.loc[scoreMatrix['id'] == homeData.iloc[matchId]['id'], 'home'] = preds[0]\n                scoreMatrix.loc[scoreMatrix['id'] == homeData.iloc[matchId]['id'], 'draw'] = preds[1]\n                scoreMatrix.loc[scoreMatrix['id'] == homeData.iloc[matchId]['id'], 'away'] = preds[2]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scoreMatrix.to_csv('./submission.csv', index = False)","metadata":{},"execution_count":null,"outputs":[]}]}