{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-14T15:52:33.328223Z","iopub.execute_input":"2022-02-14T15:52:33.328786Z","iopub.status.idle":"2022-02-14T15:52:33.341759Z","shell.execute_reply.started":"2022-02-14T15:52:33.328728Z","shell.execute_reply":"2022-02-14T15:52:33.340683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = 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-14T15:52:33.343672Z","iopub.execute_input":"2022-02-14T15:52:33.344126Z","iopub.status.idle":"2022-02-14T15:52:42.348171Z","shell.execute_reply.started":"2022-02-14T15:52:33.344087Z","shell.execute_reply":"2022-02-14T15:52:42.346980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:42.349292Z","iopub.execute_input":"2022-02-14T15:52:42.349477Z","iopub.status.idle":"2022-02-14T15:52:43.145201Z","shell.execute_reply.started":"2022-02-14T15:52:42.349450Z","shell.execute_reply":"2022-02-14T15:52:43.144524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:43.146937Z","iopub.execute_input":"2022-02-14T15:52:43.147142Z","iopub.status.idle":"2022-02-14T15:52:43.235020Z","shell.execute_reply.started":"2022-02-14T15:52:43.147113Z","shell.execute_reply":"2022-02-14T15:52:43.234634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:43.235726Z","iopub.execute_input":"2022-02-14T15:52:43.236375Z","iopub.status.idle":"2022-02-14T15:52:44.037436Z","shell.execute_reply.started":"2022-02-14T15:52:43.236351Z","shell.execute_reply":"2022-02-14T15:52:44.037062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Descriptive columns\n* target - The variable you have to predict the probabilities only available in the train set.\n* home_team_name - The name of the Home the team.\n* away_team_name - The name of the Away the team.\n* match_date - The match date (UTC).\n* league_name - The league name.\n* league_id - The league id. Note that league names can be identical for two differents id.\n* is_cup - If the value is 1 the match is played for a cup compettion.\n* home_team_coach_id - The id of the Home team coach.\n* away_team_coach_id - The id of the Away team coach.\n\n\nHistorical home team features\n\n* home_team_history_match_date_{i} - The date of the last i-th match played by Home team.\n* home_team_history_is_play_home_{i} - If 1, the Home team played home.\n* home_team_history_is_cup_{i} - If 1, the match was a cup competition.\n* home_team_history_goal_{i} - The number of goals scored by the Home team on its last i-th match.\n* home_team_history_opponent_goal_{i} - The number of goals conceded by the Home team on its last i-th match.\n* home_team_history_rating_{i} - The rating of the Home team on its last i-th match (pre match rating).\n* home_team_history_opponent_rating_{i} - The rating of the opponent team on Home team last i-th match (pre match rating).\n* home_team_history_coach_{i} - The coach id of the Home team on its last i-th match.\n* home_team_history_league_id_{i} - The league name id by the Home team on its last i-th match.\n\n\nHistorical away team features\n\n* away_team_history_match_date_{i} - The date of the last i-th match played by Away team.\n* away_team_history_is_play_home_{i} - If 1, the Away team played home.\n* away_team_history_is_cup_{i} - If 1, the match was a cup competition.\n* away_team_history_goal_{i} - The number of goals scored by the Away team on its last i-th match.\n* away_team_history_opponent_goal_{i} - The number of goals conceded by the Away team on its last i-th match.\n* away_team_history_rating_{i} - The rating of the Away team on its last i-th match (pre match rating).\n* away_team_history_opponent_rating_{i} - The rating of the opponent team on Away team last i-th match (pre match rating).\n* away_team_history_coach_{i} - The coach id of the Away team on its last i-th match.\n* away_team_history_league_id_{i} - The league name id played by the Away on its last i-th match.","metadata":{}},{"cell_type":"markdown","source":"**We have to predict the probabilty of the home team winning, probability of the away team winning and the probability of a draw.**\n\n**Let us first try to gain some insights into the data**","metadata":{}},{"cell_type":"markdown","source":"# Missing Data","metadata":{}},{"cell_type":"markdown","source":"We have a lot of historical data missing, some descriptive columns like is cup, league name, away team name also have some missing data.\n\nWe would have to think of a strategy to impute this missing data.\n\nWe do not have any feature with more than 30% of its data being missing.","metadata":{}},{"cell_type":"code","source":"count_missing = pd.DataFrame([train.isna().mean(),test.isna().mean()]).T\ncount_missing = count_missing.rename(columns={0: \"train_missing\", 1: \"test_missing\"})\n\ncount_missing.query(\"train_missing > 0.1 and train_missing < 0.2\").plot(kind=\"barh\", figsize=(15, 30), title=\"% of Values Missing\")","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:44.038138Z","iopub.execute_input":"2022-02-14T15:52:44.039011Z","iopub.status.idle":"2022-02-14T15:52:45.411940Z","shell.execute_reply.started":"2022-02-14T15:52:44.038986Z","shell.execute_reply":"2022-02-14T15:52:45.411353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_missing.query(\"train_missing > 0.2 and train_missing < 0.3\").plot(kind=\"barh\", figsize=(15, 30), title=\"% of Values Missing\")","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:45.412720Z","iopub.execute_input":"2022-02-14T15:52:45.412968Z","iopub.status.idle":"2022-02-14T15:52:45.777238Z","shell.execute_reply.started":"2022-02-14T15:52:45.412946Z","shell.execute_reply":"2022-02-14T15:52:45.776178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_missing.query(\"train_missing > 0 and train_missing < 0.1\").plot(kind=\"barh\", figsize=(15, 30), title=\"% of Values Missing\")","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:45.778462Z","iopub.execute_input":"2022-02-14T15:52:45.778728Z","iopub.status.idle":"2022-02-14T15:52:48.212323Z","shell.execute_reply.started":"2022-02-14T15:52:45.778699Z","shell.execute_reply":"2022-02-14T15:52:48.211927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Target Balance","metadata":{}},{"cell_type":"markdown","source":"The data balance does not seem to be an issue at the moment, we will work with the data as is, as per cross validation make a decision of upsampling.","metadata":{}},{"cell_type":"code","source":"sns.set_theme(style=\"darkgrid\")\nsns.countplot(x=\"target\",data=train)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:48.213047Z","iopub.execute_input":"2022-02-14T15:52:48.213401Z","iopub.status.idle":"2022-02-14T15:52:48.397098Z","shell.execute_reply.started":"2022-02-14T15:52:48.213379Z","shell.execute_reply":"2022-02-14T15:52:48.396311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Home and Away team name","metadata":{}},{"cell_type":"markdown","source":"* Home team has 9813 unique team names whereas away team has 9892 unique team names\n* Top 5 teams in both home and away team name columns are the same.\n* Rangers from the home team column seems to have the highest home wins\n* River plate from the away team feature seems to have highest away wins","metadata":{}},{"cell_type":"code","source":"train[\"home_team_name\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:48.399373Z","iopub.execute_input":"2022-02-14T15:52:48.399763Z","iopub.status.idle":"2022-02-14T15:52:48.432463Z","shell.execute_reply.started":"2022-02-14T15:52:48.399722Z","shell.execute_reply":"2022-02-14T15:52:48.431423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"away_team_name\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:48.433553Z","iopub.execute_input":"2022-02-14T15:52:48.434542Z","iopub.status.idle":"2022-02-14T15:52:48.470186Z","shell.execute_reply.started":"2022-02-14T15:52:48.434509Z","shell.execute_reply":"2022-02-14T15:52:48.469303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nsns.countplot(x=\"home_team_name\",hue=\"target\",data=train,order=train.home_team_name.value_counts().iloc[:5].index)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:48.471161Z","iopub.execute_input":"2022-02-14T15:52:48.471328Z","iopub.status.idle":"2022-02-14T15:52:48.811384Z","shell.execute_reply.started":"2022-02-14T15:52:48.471302Z","shell.execute_reply":"2022-02-14T15:52:48.810179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nsns.countplot(x=\"away_team_name\",hue=\"target\",data=train,order=train.away_team_name.value_counts().iloc[:5].index)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:48.812835Z","iopub.execute_input":"2022-02-14T15:52:48.813024Z","iopub.status.idle":"2022-02-14T15:52:49.167580Z","shell.execute_reply.started":"2022-02-14T15:52:48.812998Z","shell.execute_reply":"2022-02-14T15:52:49.166735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install wordcloud","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:49.168413Z","iopub.execute_input":"2022-02-14T15:52:49.168570Z","iopub.status.idle":"2022-02-14T15:52:56.813379Z","shell.execute_reply.started":"2022-02-14T15:52:49.168550Z","shell.execute_reply":"2022-02-14T15:52:56.812835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"home_team = ''\nfor word in train[\"home_team_name\"]:\n    home_team += str(word) + ' ' + ','","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:56.814639Z","iopub.execute_input":"2022-02-14T15:52:56.814902Z","iopub.status.idle":"2022-02-14T15:52:56.917584Z","shell.execute_reply.started":"2022-02-14T15:52:56.814863Z","shell.execute_reply":"2022-02-14T15:52:56.916221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from wordcloud import WordCloud\nwordcloud = WordCloud(width = 800, height = 800,\n                background_color ='white',\n                min_font_size = 10).generate(home_team)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:56.918849Z","iopub.execute_input":"2022-02-14T15:52:56.919044Z","iopub.status.idle":"2022-02-14T15:52:59.133268Z","shell.execute_reply.started":"2022-02-14T15:52:56.919019Z","shell.execute_reply":"2022-02-14T15:52:59.132611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (8, 8), facecolor = None)\nplt.imshow(wordcloud)\nplt.axis(\"off\")\nplt.tight_layout(pad = 0)\n \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:59.134209Z","iopub.execute_input":"2022-02-14T15:52:59.134361Z","iopub.status.idle":"2022-02-14T15:52:59.654153Z","shell.execute_reply.started":"2022-02-14T15:52:59.134338Z","shell.execute_reply":"2022-02-14T15:52:59.653693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"away_team = ''\nfor word in train[\"away_team_name\"]:\n    away_team += str(word) + ' ' + ','\nfrom wordcloud import WordCloud\nwordcloud = WordCloud(width = 800, height = 800,\n                background_color ='white',\n                min_font_size = 10).generate(away_team)\nplt.figure(figsize = (8, 8), facecolor = None)\nplt.imshow(wordcloud)\nplt.axis(\"off\")\nplt.tight_layout(pad = 0)\n \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:52:59.654976Z","iopub.execute_input":"2022-02-14T15:52:59.655239Z","iopub.status.idle":"2022-02-14T15:53:02.238391Z","shell.execute_reply.started":"2022-02-14T15:52:59.655204Z","shell.execute_reply":"2022-02-14T15:53:02.237657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Is Cup","metadata":{}},{"cell_type":"markdown","source":"* Most of the matches in our training and test data are not cup competitions\n","metadata":{}},{"cell_type":"code","source":"\nsns.countplot(train[\"is_cup\"])","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:53:02.239372Z","iopub.execute_input":"2022-02-14T15:53:02.239610Z","iopub.status.idle":"2022-02-14T15:53:02.565817Z","shell.execute_reply.started":"2022-02-14T15:53:02.239584Z","shell.execute_reply":"2022-02-14T15:53:02.565109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(test[\"is_cup\"])","metadata":{"execution":{"iopub.status.busy":"2022-02-14T15:53:02.566797Z","iopub.execute_input":"2022-02-14T15:53:02.566982Z","iopub.status.idle":"2022-02-14T15:53:02.702522Z","shell.execute_reply.started":"2022-02-14T15:53:02.566956Z","shell.execute_reply":"2022-02-14T15:53:02.702022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Coach Id","metadata":{}},{"cell_type":"code","source":"train[\"home_team_coach_id\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T16:14:00.010829Z","iopub.execute_input":"2022-02-14T16:14:00.011167Z","iopub.status.idle":"2022-02-14T16:14:00.021251Z","shell.execute_reply.started":"2022-02-14T16:14:00.011144Z","shell.execute_reply":"2022-02-14T16:14:00.020801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"away_team_coach_id\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T16:15:27.094956Z","iopub.execute_input":"2022-02-14T16:15:27.096403Z","iopub.status.idle":"2022-02-14T16:15:27.114466Z","shell.execute_reply.started":"2022-02-14T16:15:27.096311Z","shell.execute_reply":"2022-02-14T16:15:27.113793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nsns.countplot(x=\"home_team_coach_id\",hue=\"target\",data=train,order=train.home_team_coach_id.value_counts().iloc[:5].index)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-14T16:15:55.005832Z","iopub.execute_input":"2022-02-14T16:15:55.006112Z","iopub.status.idle":"2022-02-14T16:15:55.319898Z","shell.execute_reply.started":"2022-02-14T16:15:55.006084Z","shell.execute_reply":"2022-02-14T16:15:55.318856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nsns.countplot(x=\"away_team_coach_id\",hue=\"target\",data=train,order=train.away_team_coach_id.value_counts().iloc[:5].index)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-14T16:18:50.187537Z","iopub.execute_input":"2022-02-14T16:18:50.188232Z","iopub.status.idle":"2022-02-14T16:18:50.535896Z","shell.execute_reply.started":"2022-02-14T16:18:50.188192Z","shell.execute_reply":"2022-02-14T16:18:50.534983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T16:22:21.722140Z","iopub.execute_input":"2022-02-14T16:22:21.722557Z","iopub.status.idle":"2022-02-14T16:22:21.750978Z","shell.execute_reply.started":"2022-02-14T16:22:21.722529Z","shell.execute_reply":"2022-02-14T16:22:21.750500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Work in Progress","metadata":{}}]}