{"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-03-15T08:16:33.728338Z","iopub.execute_input":"2022-03-15T08:16:33.728992Z","iopub.status.idle":"2022-03-15T08:16:33.739862Z","shell.execute_reply.started":"2022-03-15T08:16:33.728944Z","shell.execute_reply":"2022-03-15T08:16:33.738981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', None)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:16:33.741333Z","iopub.execute_input":"2022-03-15T08:16:33.741798Z","iopub.status.idle":"2022-03-15T08:16:33.753289Z","shell.execute_reply.started":"2022-03-15T08:16:33.741756Z","shell.execute_reply":"2022-03-15T08:16:33.752516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read all datasets of the competition","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/football-match-probability-prediction/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/football-match-probability-prediction/test.csv\")\nsample_sub = pd.read_csv(\"/kaggle/input/football-match-probability-prediction/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:16:33.754882Z","iopub.execute_input":"2022-03-15T08:16:33.755179Z","iopub.status.idle":"2022-03-15T08:16:44.542032Z","shell.execute_reply.started":"2022-03-15T08:16:33.755149Z","shell.execute_reply":"2022-03-15T08:16:44.541175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.shape)\nprint(test_df.shape)\nprint(sample_sub.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:16:44.543608Z","iopub.execute_input":"2022-03-15T08:16:44.543945Z","iopub.status.idle":"2022-03-15T08:16:44.551263Z","shell.execute_reply.started":"2022-03-15T08:16:44.543901Z","shell.execute_reply":"2022-03-15T08:16:44.549915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:16:44.55295Z","iopub.execute_input":"2022-03-15T08:16:44.553297Z","iopub.status.idle":"2022-03-15T08:16:45.874548Z","shell.execute_reply.started":"2022-03-15T08:16:44.553255Z","shell.execute_reply":"2022-03-15T08:16:45.873667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.columns.values.tolist())","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:16:45.875754Z","iopub.execute_input":"2022-03-15T08:16:45.875996Z","iopub.status.idle":"2022-03-15T08:16:45.881247Z","shell.execute_reply.started":"2022-03-15T08:16:45.875966Z","shell.execute_reply":"2022-03-15T08:16:45.880449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def number_of_previous_games(row, home):\n    for i in range(1,11):\n        col = f\"{home}_team_history_match_date_{i}\"\n        if pd.isna(row[col]):\n            return i-1  \n    return i\n  \ntrain_df[\"number_home_previous_games\"] = train_df.apply(lambda row: number_of_previous_games(row, \"home\"), axis=1)\ntrain_df[\"number_away_previous_games\"] = train_df.apply(lambda row: number_of_previous_games(row, \"away\"), axis=1)\n\ntest_df[\"number_home_previous_games\"] = test_df.apply(lambda row: number_of_previous_games(row, \"home\"), axis=1)\ntest_df[\"number_away_previous_games\"] = test_df.apply(lambda row: number_of_previous_games(row, \"away\"), axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:16:45.882888Z","iopub.execute_input":"2022-03-15T08:16:45.88328Z","iopub.status.idle":"2022-03-15T08:17:16.995153Z","shell.execute_reply.started":"2022-03-15T08:16:45.883238Z","shell.execute_reply":"2022-03-15T08:17:16.993857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"number_home_previous_games\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:17:16.997208Z","iopub.execute_input":"2022-03-15T08:17:16.997775Z","iopub.status.idle":"2022-03-15T08:17:17.01328Z","shell.execute_reply.started":"2022-03-15T08:17:16.997728Z","shell.execute_reply":"2022-03-15T08:17:17.011671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"historical_columns_template = list(set([\"_\".join(x.split(\"_\")[:-1]) for x in train_df.columns.values.tolist() if x[-1].isnumeric()]))","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:17:17.01858Z","iopub.execute_input":"2022-03-15T08:17:17.018861Z","iopub.status.idle":"2022-03-15T08:17:17.024652Z","shell.execute_reply.started":"2022-03-15T08:17:17.018827Z","shell.execute_reply":"2022-03-15T08:17:17.023653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"is_cup_columns = [x for x in historical_columns_template if \"is_cup\" in x]\nprint(is_cup_columns)\nfor col in is_cup_columns:\n    for i in range(1, 11):\n        train_df[col + f\"_{i}\"].fillna(0, inplace=True)\n        test_df[col + f\"_{i}\"].fillna(0, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:17:17.026397Z","iopub.execute_input":"2022-03-15T08:17:17.02701Z","iopub.status.idle":"2022-03-15T08:17:17.059879Z","shell.execute_reply.started":"2022-03-15T08:17:17.026957Z","shell.execute_reply":"2022-03-15T08:17:17.058679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_rating_columns = [x for x in historical_columns_template if \"rating\" in x]\nnew_rating_columns = []\nfor rat in list_rating_columns:\n    for i in range(1,11):\n        new_rat = rat + f\"_{i}\"\n        new_rating_columns.append(new_rat)\nmean_rating = train_df[new_rating_columns].mean().mean()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:17:17.061806Z","iopub.execute_input":"2022-03-15T08:17:17.062428Z","iopub.status.idle":"2022-03-15T08:17:17.106518Z","shell.execute_reply.started":"2022-03-15T08:17:17.062374Z","shell.execute_reply":"2022-03-15T08:17:17.105549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in train_df.iterrows():\n    home = \"home\"\n    prev_coach = row[f\"{home}_team_coach_id\"]\n    if pd.isna(prev_coach):\n        prev_coach = 0\n        train_df.loc[index, f\"{home}_team_coach_id\"] = prev_coach\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    for i in range(1,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_coach_{i}\"]):\n                train_df.loc[index,f\"{home}_team_history_coach_{i}\"] = prev_coach\n            else:\n                prev_coach = row[f\"{home}_team_history_coach_{i}\"]\n                \n    home = \"away\"\n    prev_coach = row[f\"{home}_team_coach_id\"]\n    if pd.isna(prev_coach):\n        prev_coach = 0\n        train_df.loc[index, f\"{home}_team_coach_id\"] = prev_coach\n\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    for i in range(1,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_coach_{i}\"]):\n                train_df.loc[index,f\"{home}_team_history_coach_{i}\"] = prev_coach\n            else:\n                prev_coach = row[f\"{home}_team_history_coach_{i}\"]\n          \n    home = \"home\"\n    prev_rating = row[f\"{home}_team_history_rating_1\"]\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    if pd.isna(prev_rating):\n        for i in range(2,11):\n            if i<=number_previous_games:\n                if pd.notna(row[f\"{home}_team_history_rating_{i}\"]):\n                    prev_rating = row[f\"{home}_team_history_rating_{i}\"]\n                    train_df.loc[index,f\"{home}_team_history_rating_1\"] = prev_rating\n                    break\n            else:\n                prev_rating = mean_rating\n                train_df.loc[index,f\"{home}_team_history_rating_1\"] = prev_rating\n\n    for i in range(2,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_rating_{i}\"]):\n                train_df.loc[index,f\"{home}_team_history_rating_{i}\"] = prev_rating\n            else:\n                prev_rating = row[f\"{home}_team_history_rating_{i}\"]\n                \n    home = \"away\"\n    prev_rating = row[f\"{home}_team_history_rating_1\"]\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    if pd.isna(prev_rating):\n        for i in range(2,11):\n            if i<=number_previous_games:\n                if pd.notna(row[f\"{home}_team_history_rating_{i}\"]):\n                    prev_rating = row[f\"{home}_team_history_rating_{i}\"]\n                    train_df.loc[index,f\"{home}_team_history_rating_1\"] = prev_rating\n                    break\n            else:\n                prev_rating = mean_rating\n                train_df.loc[index,f\"{home}_team_history_rating_1\"] = prev_rating\n    for i in range(2,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_rating_{i}\"]):\n                train_df.loc[index,f\"{home}_team_history_rating_{i}\"] = prev_rating\n            else:\n                prev_rating = row[f\"{home}_team_history_rating_{i}\"]\n                \n                \n    home = \"home\"\n    prev_rating = row[f\"{home}_team_history_opponent_rating_1\"]\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    if pd.isna(prev_rating):\n        for i in range(2,11):\n            if i<=number_previous_games:\n                if pd.notna(row[f\"{home}_team_history_opponent_rating_{i}\"]):\n                    prev_rating = row[f\"{home}_team_history_opponent_rating_{i}\"]\n                    train_df.loc[index,f\"{home}_team_history_opponent_rating_1\"] = prev_rating\n                    break\n            else:\n                prev_rating = mean_rating\n                train_df.loc[index,f\"{home}_team_history_opponent_rating_1\"] = prev_rating\n    for i in range(2,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_opponent_rating_{i}\"]):\n                train_df.loc[index,f\"{home}_team_history_opponent_rating_{i}\"] = prev_rating\n            else:\n                prev_rating = row[f\"{home}_team_history_opponent_rating_{i}\"]\n                \n    home = \"away\"\n    prev_rating = row[f\"{home}_team_history_opponent_rating_1\"]\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    if pd.isna(prev_rating):\n        for i in range(2,11):\n            if i<=number_previous_games:\n                if pd.notna(row[f\"{home}_team_history_opponent_rating_{i}\"]):\n                    prev_rating = row[f\"{home}_team_history_opponent_rating_{i}\"]\n                    train_df.loc[index,f\"{home}_team_history_opponent_rating_1\"] = prev_rating\n                    break\n            else:\n                prev_rating = mean_rating\n                train_df.loc[index,f\"{home}_team_history_opponent_rating_1\"] = prev_rating\n    for i in range(2,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_opponent_rating_{i}\"]):\n                train_df.loc[index,f\"{home}_team_history_opponent_rating_{i}\"] = prev_rating\n            else:\n                prev_rating = row[f\"{home}_team_history_opponent_rating_{i}\"]","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:17:17.108127Z","iopub.execute_input":"2022-03-15T08:17:17.108592Z","iopub.status.idle":"2022-03-15T08:22:38.774859Z","shell.execute_reply.started":"2022-03-15T08:17:17.108549Z","shell.execute_reply":"2022-03-15T08:22:38.773987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in test_df.iterrows():\n    home = \"home\"\n    prev_coach = row[f\"{home}_team_coach_id\"]\n    if pd.isna(prev_coach):\n        prev_coach = 0\n        test_df.loc[index, f\"{home}_team_coach_id\"] = prev_coach\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    for i in range(1,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_coach_{i}\"]):\n                test_df.loc[index,f\"{home}_team_history_coach_{i}\"] = prev_coach\n            else:\n                prev_coach = row[f\"{home}_team_history_coach_{i}\"]\n                \n    home = \"away\"\n    prev_coach = row[f\"{home}_team_coach_id\"]\n    if pd.isna(prev_coach):\n        prev_coach = 0\n        test_df.loc[index, f\"{home}_team_coach_id\"] = prev_coach\n\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    for i in range(1,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_coach_{i}\"]):\n                test_df.loc[index,f\"{home}_team_history_coach_{i}\"] = prev_coach\n            else:\n                prev_coach = row[f\"{home}_team_history_coach_{i}\"]\n                \n    home = \"home\"\n    prev_rating = row[f\"{home}_team_history_rating_1\"]\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    if pd.isna(prev_rating):\n        for i in range(2,11):\n            if i<=number_previous_games:\n                if pd.notna(row[f\"{home}_team_history_rating_{i}\"]):\n                    prev_rating = row[f\"{home}_team_history_rating_{i}\"]\n                    test_df.loc[index,f\"{home}_team_history_rating_1\"] = prev_rating\n                    break\n            else:\n                prev_rating = mean_rating\n                test_df.loc[index,f\"{home}_team_history_rating_1\"] = prev_rating\n\n    for i in range(2,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_rating_{i}\"]):\n                test_df.loc[index,f\"{home}_team_history_rating_{i}\"] = prev_rating\n            else:\n                prev_rating = row[f\"{home}_team_history_rating_{i}\"]\n                \n    home = \"away\"\n    prev_rating = row[f\"{home}_team_history_rating_1\"]\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    if pd.isna(prev_rating):\n        for i in range(2,11):\n            if i<=number_previous_games:\n                if pd.notna(row[f\"{home}_team_history_rating_{i}\"]):\n                    prev_rating = row[f\"{home}_team_history_rating_{i}\"]\n                    test_df.loc[index,f\"{home}_team_history_rating_1\"] = prev_rating\n                    break\n            else:\n                prev_rating = mean_rating\n                test_df.loc[index,f\"{home}_team_history_rating_1\"] = prev_rating\n    for i in range(2,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_rating_{i}\"]):\n                test_df.loc[index,f\"{home}_team_history_rating_{i}\"] = prev_rating\n            else:\n                prev_rating = row[f\"{home}_team_history_rating_{i}\"]\n                \n                \n    home = \"home\"\n    prev_rating = row[f\"{home}_team_history_opponent_rating_1\"]\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    if pd.isna(prev_rating):\n        for i in range(2,11):\n            if i<=number_previous_games:\n                if pd.notna(row[f\"{home}_team_history_opponent_rating_{i}\"]):\n                    prev_rating = row[f\"{home}_team_history_opponent_rating_{i}\"]\n                    test_df.loc[index,f\"{home}_team_history_opponent_rating_1\"] = prev_rating\n                    break\n            else:\n                prev_rating = mean_rating\n                test_df.loc[index,f\"{home}_team_history_opponent_rating_1\"] = prev_rating\n    for i in range(2,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_opponent_rating_{i}\"]):\n                test_df.loc[index,f\"{home}_team_history_opponent_rating_{i}\"] = prev_rating\n            else:\n                prev_rating = row[f\"{home}_team_history_opponent_rating_{i}\"]\n                \n    home = \"away\"\n    prev_rating = row[f\"{home}_team_history_opponent_rating_1\"]\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    if pd.isna(prev_rating):\n        for i in range(2,11):\n            if i<=number_previous_games:\n                if pd.notna(row[f\"{home}_team_history_opponent_rating_{i}\"]):\n                    prev_rating = row[f\"{home}_team_history_opponent_rating_{i}\"]\n                    test_df.loc[index,f\"{home}_team_history_opponent_rating_1\"] = prev_rating\n                    break\n            else:\n                prev_rating = mean_rating\n                test_df.loc[index,f\"{home}_team_history_opponent_rating_1\"] = prev_rating\n    for i in range(2,11):\n        if i<=number_previous_games:\n            if pd.isna(row[f\"{home}_team_history_opponent_rating_{i}\"]):\n                test_df.loc[index,f\"{home}_team_history_opponent_rating_{i}\"] = prev_rating\n            else:\n                prev_rating = row[f\"{home}_team_history_opponent_rating_{i}\"]","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:22:38.777053Z","iopub.execute_input":"2022-03-15T08:22:38.777442Z","iopub.status.idle":"2022-03-15T08:26:36.248416Z","shell.execute_reply.started":"2022-03-15T08:22:38.777407Z","shell.execute_reply":"2022-03-15T08:26:36.247051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\ndef count_not_na(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    if number_previous_games!=10:\n        number_previous_games+=1\n    else:\n        return 0\n    count_notna = 0\n    historical_columns_template_home = [x for x in historical_columns_template if x.startswith(home)]\n    for column in historical_columns_template_home:\n        if pd.notna(row[column+f\"_{number_previous_games}\"]):\n            count_notna+=1\n            \n    return count_notna\n\ndef count_na_10(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    if number_previous_games!=10:\n        return 0\n    else:\n        count_na = 0\n        historical_columns_template_home = [x for x in historical_columns_template if x.startswith(home)]\n        for column in historical_columns_template_home:\n            if pd.isna(row[column+f\"_{number_previous_games}\"]):\n                count_na+=1\n\n        return count_na\n\ntrain_df[\"number_notna_home_previous_games\"] = train_df.apply(lambda row: count_not_na(row, \"home\"), axis=1)\ntrain_df[\"number_notna_away_previous_games\"] = train_df.apply(lambda row: count_not_na(row, \"away\"), axis=1)\ntrain_df[\"number_na10_home_previous_games\"] = train_df.apply(lambda row: count_na_10(row, \"home\"), axis=1)\ntrain_df[\"number_na10_away_previous_games\"] = train_df.apply(lambda row: count_na_10(row, \"away\"), axis=1)\n\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.250704Z","iopub.execute_input":"2022-03-15T08:26:36.251038Z","iopub.status.idle":"2022-03-15T08:26:36.261515Z","shell.execute_reply.started":"2022-03-15T08:26:36.250999Z","shell.execute_reply":"2022-03-15T08:26:36.260471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_df[\"number_notna_home_previous_games\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.263315Z","iopub.execute_input":"2022-03-15T08:26:36.264002Z","iopub.status.idle":"2022-03-15T08:26:36.280603Z","shell.execute_reply.started":"2022-03-15T08:26:36.263942Z","shell.execute_reply":"2022-03-15T08:26:36.279682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_df[\"number_notna_away_previous_games\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.282763Z","iopub.execute_input":"2022-03-15T08:26:36.28345Z","iopub.status.idle":"2022-03-15T08:26:36.294036Z","shell.execute_reply.started":"2022-03-15T08:26:36.283395Z","shell.execute_reply":"2022-03-15T08:26:36.293119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_df[\"number_na10_home_previous_games\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.29567Z","iopub.execute_input":"2022-03-15T08:26:36.296556Z","iopub.status.idle":"2022-03-15T08:26:36.307497Z","shell.execute_reply.started":"2022-03-15T08:26:36.296499Z","shell.execute_reply":"2022-03-15T08:26:36.306703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\ntrain_df[train_df[\"number_na10_home_previous_games\"]>0]\nlist_na_columns = []\nfor index, row in train_df[train_df[\"number_na10_home_previous_games\"]>0].iterrows():\n    historical_columns_template_home = [x for x in historical_columns_template if x.startswith(\"home\")]\n    for column in historical_columns_template_home:\n        if pd.isna(row[column+\"_10\"]):\n            list_na_columns.append(column)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.309007Z","iopub.execute_input":"2022-03-15T08:26:36.310017Z","iopub.status.idle":"2022-03-15T08:26:36.322543Z","shell.execute_reply.started":"2022-03-15T08:26:36.309966Z","shell.execute_reply":"2022-03-15T08:26:36.321711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_df[train_df[\"number_na10_home_previous_games\"]>0]","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.324321Z","iopub.execute_input":"2022-03-15T08:26:36.324655Z","iopub.status.idle":"2022-03-15T08:26:36.335202Z","shell.execute_reply.started":"2022-03-15T08:26:36.324601Z","shell.execute_reply":"2022-03-15T08:26:36.334134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#list(set(list_na_columns))","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.336653Z","iopub.execute_input":"2022-03-15T08:26:36.337276Z","iopub.status.idle":"2022-03-15T08:26:36.352316Z","shell.execute_reply.started":"2022-03-15T08:26:36.337222Z","shell.execute_reply":"2022-03-15T08:26:36.351034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_df[\"number_na10_away_previous_games\"].describe()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.353733Z","iopub.execute_input":"2022-03-15T08:26:36.354256Z","iopub.status.idle":"2022-03-15T08:26:36.363977Z","shell.execute_reply.started":"2022-03-15T08:26:36.354218Z","shell.execute_reply":"2022-03-15T08:26:36.362992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\ntrain_df[train_df[\"number_na10_away_previous_games\"]>0]\nlist_na_columns = []\nfor index, row in train_df[train_df[\"number_na10_away_previous_games\"]>0].iterrows():\n    historical_columns_template_home = [x for x in historical_columns_template if x.startswith(\"away\")]\n    for column in historical_columns_template_home:\n        if pd.isna(row[column+\"_10\"]):\n            list_na_columns.append(column)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.365802Z","iopub.execute_input":"2022-03-15T08:26:36.366144Z","iopub.status.idle":"2022-03-15T08:26:36.378505Z","shell.execute_reply.started":"2022-03-15T08:26:36.366097Z","shell.execute_reply":"2022-03-15T08:26:36.377794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#list(set(list_na_columns))","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.380231Z","iopub.execute_input":"2022-03-15T08:26:36.380798Z","iopub.status.idle":"2022-03-15T08:26:36.3906Z","shell.execute_reply.started":"2022-03-15T08:26:36.380751Z","shell.execute_reply":"2022-03-15T08:26:36.389898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Engineering","metadata":{}},{"cell_type":"markdown","source":"### Compute the number of days between match date and previous matches","metadata":{}},{"cell_type":"code","source":"# Day differences between matches\nlist_column_date_matches_home = [f\"home_team_history_match_date_{x}\" for x in range(1,11)]\nlist_column_date_matches_away = [f\"away_team_history_match_date_{x}\" for x in range(1,11)]","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.39241Z","iopub.execute_input":"2022-03-15T08:26:36.393002Z","iopub.status.idle":"2022-03-15T08:26:36.405249Z","shell.execute_reply.started":"2022-03-15T08:26:36.392966Z","shell.execute_reply":"2022-03-15T08:26:36.40403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\ndef compute_days_diff(row, column_name):\n    try:\n        d0 = datetime.strptime(row[column_name], '%Y-%m-%d %H:%M:%S')\n        d1 = datetime.strptime(row[\"match_date\"], '%Y-%m-%d %H:%M:%S')\n        delta = d1 - d0\n        return delta.days\n    except:\n        return 1000\n\n\nfor home_date, away_date in zip(list_column_date_matches_home, list_column_date_matches_away):\n    previous_match_index = home_date.split(\"_\")[-1]\n    train_df[f\"home_team_days_from_game_{previous_match_index}\"] = train_df.apply(lambda row: compute_days_diff(row, home_date), axis=1)\n    train_df[f\"away_team_days_from_game_{previous_match_index}\"] = train_df.apply(lambda row: compute_days_diff(row, away_date), axis=1)\n    \n    test_df[f\"home_team_days_from_game_{previous_match_index}\"] = test_df.apply(lambda row: compute_days_diff(row, home_date), axis=1)\n    test_df[f\"away_team_days_from_game_{previous_match_index}\"] = test_df.apply(lambda row: compute_days_diff(row, away_date), axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:26:36.407031Z","iopub.execute_input":"2022-03-15T08:26:36.407556Z","iopub.status.idle":"2022-03-15T08:30:41.068657Z","shell.execute_reply.started":"2022-03-15T08:26:36.40752Z","shell.execute_reply":"2022-03-15T08:30:41.067489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:30:41.070318Z","iopub.execute_input":"2022-03-15T08:30:41.07064Z","iopub.status.idle":"2022-03-15T08:30:41.077789Z","shell.execute_reply.started":"2022-03-15T08:30:41.070598Z","shell.execute_reply":"2022-03-15T08:30:41.077093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compute the average number of days between matches","metadata":{}},{"cell_type":"code","source":"def average_days_between_games(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    list_days_matches = [f\"{home}_team_days_from_game_{x}\" for x in range(1,number_previous_games+1)]\n    average_days = 0\n    total_games = 0\n    for days in list_days_matches[:-1]:\n        days_plus_1 = \"_\".join(days.split(\"_\")[:-1] + [str(int(days.split(\"_\")[-1])+1)])\n        if days!=1000:\n            if total_games==0:\n                average_days+=row[days]\n                total_games+=1\n            else:\n                average_days+=row[days_plus_1] - row[days]\n                total_games+=1\n        else:\n            break\n    if total_games==0:\n        return 1000\n    else:\n        return average_days / total_games\n\ntrain_df[\"home_team_average_days_between_matches\"] = train_df.apply(lambda row: average_days_between_games(row, \"home\"), axis=1)\ntrain_df[\"away_team_average_days_between_matches\"] = train_df.apply(lambda row: average_days_between_games(row, \"away\"), axis=1)\n\ntest_df[\"home_team_average_days_between_matches\"] = test_df.apply(lambda row: average_days_between_games(row, \"home\"), axis=1)\ntest_df[\"away_team_average_days_between_matches\"] = test_df.apply(lambda row: average_days_between_games(row, \"away\"), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:30:41.079252Z","iopub.execute_input":"2022-03-15T08:30:41.079464Z","iopub.status.idle":"2022-03-15T08:31:30.467652Z","shell.execute_reply.started":"2022-03-15T08:30:41.079437Z","shell.execute_reply":"2022-03-15T08:31:30.46666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compute the total number games played at home in the last 10 matches","metadata":{}},{"cell_type":"code","source":"def number_of_home_games(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    list_home_matches = [f\"{home}_team_history_is_play_home_{x}\" for x in range(1,number_previous_games+1)]\n    sum_home = 0\n    for column in list_home_matches:\n        try:\n            sum_home+=row[column]\n        except:\n            break\n    return sum_home\n\ntrain_df[\"home_team_number_of_home_games\"] = train_df.apply(lambda row: number_of_home_games(row, \"home\"), axis=1)\ntrain_df[\"away_team_number_of_home_games\"] = train_df.apply(lambda row: number_of_home_games(row, \"away\"), axis=1)\n\ntest_df[\"home_team_number_of_home_games\"] = test_df.apply(lambda row: number_of_home_games(row, \"home\"), axis=1)\ntest_df[\"away_team_number_of_home_games\"] = test_df.apply(lambda row: number_of_home_games(row, \"away\"), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:31:30.469342Z","iopub.execute_input":"2022-03-15T08:31:30.46965Z","iopub.status.idle":"2022-03-15T08:32:01.32102Z","shell.execute_reply.started":"2022-03-15T08:31:30.469612Z","shell.execute_reply":"2022-03-15T08:32:01.320139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compute the total number games played in cup in the last 10 matches","metadata":{"execution":{"iopub.status.busy":"2022-03-11T10:16:20.359434Z","iopub.execute_input":"2022-03-11T10:16:20.360319Z","iopub.status.idle":"2022-03-11T10:16:20.363975Z","shell.execute_reply.started":"2022-03-11T10:16:20.360273Z","shell.execute_reply":"2022-03-11T10:16:20.363214Z"}}},{"cell_type":"code","source":"def number_of_away_games(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    list_home_matches = [f\"{home}_team_history_is_cup_{x}\" for x in range(1,number_previous_games+1)]\n    sum_home = 0\n    for column in list_home_matches:\n        try:\n            sum_home+=row[column]\n        except:\n            break\n    return sum_home\n\ntrain_df[\"home_team_number_of_cup_games\"] = train_df.apply(lambda row: number_of_home_games(row, \"home\"), axis=1)\ntrain_df[\"away_team_number_of_cup_games\"] = train_df.apply(lambda row: number_of_home_games(row, \"away\"), axis=1)\n\ntest_df[\"home_team_number_of_cup_games\"] = test_df.apply(lambda row: number_of_home_games(row, \"home\"), axis=1)\ntest_df[\"away_team_number_of_cup_games\"] = test_df.apply(lambda row: number_of_home_games(row, \"away\"), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:32:01.322561Z","iopub.execute_input":"2022-03-15T08:32:01.322847Z","iopub.status.idle":"2022-03-15T08:32:31.946922Z","shell.execute_reply.started":"2022-03-15T08:32:01.32281Z","shell.execute_reply":"2022-03-15T08:32:31.946026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compute the total number of wins in the last 10 matches","metadata":{}},{"cell_type":"code","source":"def number_of_wins(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    list_home_goals = [f\"{home}_team_history_goal_{x}\" for x in range(1,number_previous_games+1)]\n    list_away_goals = [f\"{home}_team_history_opponent_goal_{x}\" for x in range(1,number_previous_games+1)]\n\n    sum_wins = 0\n    for column_home, column_away in zip(list_home_goals, list_away_goals):\n        try:\n            if row[column_home]>row[column_away]:\n                sum_wins+=1\n            else:\n                continue\n        except:\n            break\n    return sum_wins\n\ndef number_of_draws(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n\n    list_home_goals = [f\"{home}_team_history_goal_{x}\" for x in range(1,number_previous_games+1)]\n    list_away_goals = [f\"{home}_team_history_opponent_goal_{x}\" for x in range(1,number_previous_games+1)]\n\n    sum_draws = 0\n    for column_home, column_away in zip(list_home_goals, list_away_goals):\n        try:\n            if row[column_home]==row[column_away]:\n                sum_draws+=1\n            else:\n                continue\n        except:\n            break\n    return sum_draws\n\ndef number_of_losses(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n\n    list_home_goals = [f\"{home}_team_history_goal_{x}\" for x in range(1,number_previous_games+1)]\n    list_away_goals = [f\"{home}_team_history_opponent_goal_{x}\" for x in range(1,number_previous_games+1)]\n\n    sum_losses = 0\n    for column_home, column_away in zip(list_home_goals, list_away_goals):\n        try:\n            if row[column_home]<row[column_away]:\n                sum_losses+=1\n            else:\n                continue\n        except:\n            break\n    return sum_losses\n\n\ntrain_df[\"home_team_number_of_wins\"] = train_df.apply(lambda row: number_of_wins(row, \"home\"), axis=1)\ntrain_df[\"home_team_number_of_draws\"] = train_df.apply(lambda row: number_of_draws(row, \"home\"), axis=1)\ntrain_df[\"home_team_number_of_losses\"] = train_df.apply(lambda row: number_of_losses(row, \"home\"), axis=1)\ntrain_df[\"away_team_number_of_wins\"] = train_df.apply(lambda row: number_of_wins(row, \"away\"), axis=1)\ntrain_df[\"away_team_number_of_draws\"] = train_df.apply(lambda row: number_of_draws(row, \"away\"), axis=1)\ntrain_df[\"away_team_number_of_losses\"] = train_df.apply(lambda row: number_of_losses(row, \"away\"), axis=1)\n\ntest_df[\"home_team_number_of_wins\"] = test_df.apply(lambda row: number_of_wins(row, \"home\"), axis=1)\ntest_df[\"home_team_number_of_draws\"] = test_df.apply(lambda row: number_of_draws(row, \"home\"), axis=1)\ntest_df[\"home_team_number_of_losses\"] = test_df.apply(lambda row: number_of_losses(row, \"home\"), axis=1)\ntest_df[\"away_team_number_of_wins\"] = test_df.apply(lambda row: number_of_wins(row, \"away\"), axis=1)\ntest_df[\"away_team_number_of_draws\"] = test_df.apply(lambda row: number_of_draws(row, \"away\"), axis=1)\ntest_df[\"away_team_number_of_losses\"] = test_df.apply(lambda row: number_of_losses(row, \"away\"), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:32:31.948398Z","iopub.execute_input":"2022-03-15T08:32:31.948699Z","iopub.status.idle":"2022-03-15T08:34:59.488352Z","shell.execute_reply.started":"2022-03-15T08:32:31.948658Z","shell.execute_reply":"2022-03-15T08:34:59.487394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compute the total and average number of goals scored and taken in the last 10 matches","metadata":{}},{"cell_type":"code","source":"def number_of_goals_scored(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n\n    list_home_goals = [f\"{home}_team_history_goal_{x}\" for x in range(1,number_previous_games+1)]\n\n    sum_goals = 0\n    for goal_column in list_home_goals:\n        try:\n            sum_goals+=row[goal_column]\n        except:\n            break\n    return sum_goals\n\ndef average_goals_scored(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n\n    list_home_goals = [f\"{home}_team_history_goal_{x}\" for x in range(1,number_previous_games+1)]\n    \n    total_games=0\n    sum_goals = 0\n    for goal_column in list_home_goals:\n        try:\n            total_games+=1\n            sum_goals+=row[goal_column]\n        except:\n            break\n    if total_games!=0:\n        return sum_goals/total_games\n    else:\n        return 0\n\ndef number_of_goals_taken(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n\n    list_away_goals = [f\"{home}_team_history_opponent_goal_{x}\" for x in range(1,number_previous_games+1)]\n\n    sum_goals = 0\n    for goal_column in list_away_goals:\n        try:\n            sum_goals+=row[goal_column]\n        except:\n            break\n    return sum_goals\n\ndef average_goals_taken(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n\n    list_away_goals = [f\"{home}_team_history_opponent_goal_{x}\" for x in range(1,number_previous_games+1)]\n\n    total_games=0\n    sum_goals = 0\n    for goal_column in list_away_goals:\n        try:\n            total_games+=1\n            sum_goals+=row[goal_column]\n        except:\n            break\n    if total_games!=0:\n        return sum_goals/total_games\n    else:\n        return 0\n\ntrain_df[\"home_team_total_goals_scored\"] = train_df.apply(lambda row: number_of_goals_scored(row, \"home\"), axis=1)\ntrain_df[\"home_team_average_goals_scored\"] = train_df.apply(lambda row: average_goals_scored(row, \"home\"), axis=1)\ntrain_df[\"home_team_total_goals_taken\"] = train_df.apply(lambda row: number_of_goals_taken(row, \"home\"), axis=1)\ntrain_df[\"home_team_average_goals_taken\"] = train_df.apply(lambda row: average_goals_taken(row, \"home\"), axis=1)\n\ntrain_df[\"away_team_total_goals_scored\"] = train_df.apply(lambda row: number_of_goals_scored(row, \"away\"), axis=1)\ntrain_df[\"away_team_average_goals_scored\"] = train_df.apply(lambda row: average_goals_scored(row, \"away\"), axis=1)\ntrain_df[\"away_team_total_goals_taken\"] = train_df.apply(lambda row: number_of_goals_taken(row, \"away\"), axis=1)\ntrain_df[\"away_team_average_goals_taken\"] = train_df.apply(lambda row: average_goals_taken(row, \"away\"), axis=1)\n\n\n\ntest_df[\"home_team_total_goals_scored\"] = test_df.apply(lambda row: number_of_goals_scored(row, \"home\"), axis=1)\ntest_df[\"home_team_average_goals_scored\"] = test_df.apply(lambda row: average_goals_scored(row, \"home\"), axis=1)\ntest_df[\"home_team_total_goals_taken\"] = test_df.apply(lambda row: number_of_goals_taken(row, \"home\"), axis=1)\ntest_df[\"home_team_average_goals_taken\"] = test_df.apply(lambda row: average_goals_taken(row, \"home\"), axis=1)\n\ntest_df[\"away_team_total_goals_scored\"] = test_df.apply(lambda row: number_of_goals_scored(row, \"away\"), axis=1)\ntest_df[\"away_team_average_goals_scored\"] = test_df.apply(lambda row: average_goals_scored(row, \"away\"), axis=1)\ntest_df[\"away_team_total_goals_taken\"] = test_df.apply(lambda row: number_of_goals_taken(row, \"away\"), axis=1)\ntest_df[\"away_team_average_goals_taken\"] = test_df.apply(lambda row: average_goals_taken(row, \"away\"), axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:34:59.490187Z","iopub.execute_input":"2022-03-15T08:34:59.490511Z","iopub.status.idle":"2022-03-15T08:37:04.001934Z","shell.execute_reply.started":"2022-03-15T08:34:59.490468Z","shell.execute_reply":"2022-03-15T08:37:04.000981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compute the average team and opponent rating last 10 matches","metadata":{}},{"cell_type":"code","source":"def average_rating(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n\n    list_home_rating = [f\"{home}_team_history_rating_{x}\" for x in range(1,number_previous_games+1)]\n    \n    total_games=0\n    sum_rating = 0\n    for rating_column in list_home_rating:\n        try:\n            total_games+=1\n            sum_rating+=row[rating_column]\n        except:\n            break\n    if total_games!=0:\n        return sum_rating/total_games\n    else:\n        return 0\ndef average_opponent_rating(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n\n    list_oppontent_rating = [f\"{home}_team_history_opponent_rating_{x}\" for x in range(1,number_previous_games+1)]\n\n    total_games=0\n    sum_rating = 0\n    for rating_column in list_oppontent_rating:\n        try:\n            total_games+=1\n            sum_rating+=row[rating_column]\n        except:\n            break\n    if total_games!=0:\n        return sum_rating/total_games\n    else:\n        return 0\n\ntrain_df[\"home_team_average_rating\"] = train_df.apply(lambda row: average_rating(row, \"home\"), axis=1)\ntrain_df[\"home_team_average_opponent_rating\"] = train_df.apply(lambda row: average_opponent_rating(row, \"home\"), axis=1)\n\ntrain_df[\"away_team_average_rating\"] = train_df.apply(lambda row: average_rating(row, \"away\"), axis=1)\ntrain_df[\"away_team_average_opponent_rating\"] = train_df.apply(lambda row: average_opponent_rating(row, \"away\"), axis=1)\n\n\ntest_df[\"home_team_average_rating\"] = test_df.apply(lambda row: average_rating(row, \"home\"), axis=1)\ntest_df[\"home_team_average_opponent_rating\"] = test_df.apply(lambda row: average_opponent_rating(row, \"home\"), axis=1)\n\ntest_df[\"away_team_average_rating\"] = test_df.apply(lambda row: average_rating(row, \"away\"), axis=1)\ntest_df[\"away_team_average_opponent_rating\"] = test_df.apply(lambda row: average_opponent_rating(row, \"away\"), axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:37:04.008097Z","iopub.execute_input":"2022-03-15T08:37:04.008372Z","iopub.status.idle":"2022-03-15T08:38:07.394648Z","shell.execute_reply.started":"2022-03-15T08:37:04.008339Z","shell.execute_reply":"2022-03-15T08:38:07.393742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compute the change of coaches in last 10 matches","metadata":{}},{"cell_type":"code","source":"def change_coach_last_10(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n\n    list_coaches = [f\"{home}_team_history_coach_{x}\" for x in range(1,number_previous_games+1)]\n    current_coach = row[f\"{home}_team_coach_id\"]\n    change_coach=0\n    for coach_col in list_coaches:\n        prev_coach = row[coach_col]\n        if pd.notna(prev_coach):\n            if prev_coach!=current_coach:\n                change_coach=1\n                break\n            else:\n                current_coach = prev_coach\n            \n    return change_coach\ntrain_df[\"home_team_change_coach\"] = train_df.apply(lambda row: change_coach_last_10(row, \"home\"), axis=1)\ntrain_df[\"away_team_change_coach\"] = train_df.apply(lambda row: change_coach_last_10(row, \"away\"), axis=1)\n\n\ntest_df[\"home_team_change_coach\"] = test_df.apply(lambda row: change_coach_last_10(row, \"home\"), axis=1)\ntest_df[\"away_team_change_coach\"] = test_df.apply(lambda row: change_coach_last_10(row, \"away\"), axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:38:07.396065Z","iopub.execute_input":"2022-03-15T08:38:07.396382Z","iopub.status.idle":"2022-03-15T08:38:41.350407Z","shell.execute_reply.started":"2022-03-15T08:38:07.396345Z","shell.execute_reply":"2022-03-15T08:38:41.349347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Average number of home games\ntrain_df[\"home_team_average_number_of_home_games\"] = train_df[\"home_team_number_of_home_games\"] / train_df[\"number_home_previous_games\"]\ntrain_df[\"away_team_average_number_of_home_games\"] = train_df[\"away_team_number_of_home_games\"] / train_df[\"number_away_previous_games\"]\n\ntest_df[\"home_team_average_number_of_home_games\"] = test_df[\"home_team_number_of_home_games\"] / test_df[\"number_home_previous_games\"]\ntest_df[\"away_team_average_number_of_home_games\"] = test_df[\"away_team_number_of_home_games\"] / test_df[\"number_away_previous_games\"]\n\n# Average number of wins/draws/losses \ntrain_df['home_team_average_number_of_wins'] = train_df['home_team_number_of_wins'] / train_df[\"number_home_previous_games\"]\ntrain_df['home_team_average_number_of_draws'] = train_df['home_team_number_of_draws'] / train_df[\"number_home_previous_games\"]\ntrain_df['home_team_average_number_of_losses'] = train_df['home_team_number_of_losses'] / train_df[\"number_home_previous_games\"]\ntrain_df['away_team_average_number_of_wins'] = train_df['away_team_number_of_wins'] / train_df[\"number_away_previous_games\"]\ntrain_df['away_team_average_number_of_draws'] = train_df['away_team_number_of_draws'] / train_df[\"number_away_previous_games\"]\ntrain_df['away_team_average_number_of_losses'] = train_df['away_team_number_of_losses'] / train_df[\"number_away_previous_games\"]\n\ntest_df['home_team_average_number_of_wins'] = test_df['home_team_number_of_wins'] / test_df[\"number_home_previous_games\"]\ntest_df['home_team_average_number_of_draws'] = test_df['home_team_number_of_draws'] / test_df[\"number_home_previous_games\"]\ntest_df['home_team_average_number_of_losses'] = test_df['home_team_number_of_losses'] / test_df[\"number_home_previous_games\"]\ntest_df['away_team_average_number_of_wins'] = test_df['away_team_number_of_wins'] / test_df[\"number_away_previous_games\"]\ntest_df['away_team_average_number_of_draws'] = test_df['away_team_number_of_draws'] / test_df[\"number_away_previous_games\"]\ntest_df['away_team_average_number_of_losses'] = test_df['away_team_number_of_losses'] / test_df[\"number_away_previous_games\"]\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:38:41.351726Z","iopub.execute_input":"2022-03-15T08:38:41.352836Z","iopub.status.idle":"2022-03-15T08:38:41.384735Z","shell.execute_reply.started":"2022-03-15T08:38:41.352786Z","shell.execute_reply":"2022-03-15T08:38:41.383565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def streak(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    list_home_goals = [f\"{home}_team_history_goal_{x}\" for x in range(1,number_previous_games+1)]\n    list_away_goals = [f\"{home}_team_history_opponent_goal_{x}\" for x in range(1,number_previous_games+1)]\n\n    streak = \"\"\n    streak_count=0\n    for column_home, column_away in zip(list_home_goals, list_away_goals):\n        try:\n            if streak_count==0:\n                if row[column_home]>row[column_away]:\n                    streak=\"Wins\"\n                    streak_count+=1\n                elif row[column_home]==row[column_away]:\n                    streak=\"Draws\"\n                    streak_count+=1\n                else:\n                    streak=\"Losses\"\n                    streak_count+=1\n            else:\n                if (streak==\"Wins\") and (row[column_home]>row[column_away]):\n                    streak_count+=1\n                elif (streak==\"Draws\") and (row[column_home]==row[column_away]):\n                    streak_count+=1\n                elif (streak==\"Losses\") and (row[column_home]<row[column_away]):\n                    streak_count+=1\n                else:\n                    break\n        except:\n            break\n    return streak, streak_count\n\ntrain_df[\"home_team_streak_type\"] = train_df.apply(lambda row: streak(row, \"home\")[0], axis=1)\ntrain_df[\"home_team_streak_count\"] = train_df.apply(lambda row: streak(row, \"home\")[1], axis=1)\ntrain_df[\"away_team_streak_type\"] = train_df.apply(lambda row: streak(row, \"away\")[0], axis=1)\ntrain_df[\"away_team_streak_count\"] = train_df.apply(lambda row: streak(row, \"away\")[1], axis=1)\n\ntest_df[\"home_team_streak_type\"] = test_df.apply(lambda row: streak(row, \"home\")[0], axis=1)\ntest_df[\"home_team_streak_count\"] = test_df.apply(lambda row: streak(row, \"home\")[1], axis=1)\ntest_df[\"away_team_streak_type\"] = test_df.apply(lambda row: streak(row, \"away\")[0], axis=1)\ntest_df[\"away_team_streak_count\"] = test_df.apply(lambda row: streak(row, \"away\")[1], axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:38:41.385957Z","iopub.execute_input":"2022-03-15T08:38:41.386282Z","iopub.status.idle":"2022-03-15T08:39:37.279269Z","shell.execute_reply.started":"2022-03-15T08:38:41.386241Z","shell.execute_reply":"2022-03-15T08:39:37.27811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"home_team_streak_type\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:39:37.280548Z","iopub.execute_input":"2022-03-15T08:39:37.280757Z","iopub.status.idle":"2022-03-15T08:39:37.299024Z","shell.execute_reply.started":"2022-03-15T08:39:37.280729Z","shell.execute_reply":"2022-03-15T08:39:37.297868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def number_upsets(row, home):\n    number_previous_games = row[f\"number_{home}_previous_games\"]\n    list_home_goals = [f\"{home}_team_history_goal_{x}\" for x in range(1,number_previous_games+1)]\n    list_away_goals = [f\"{home}_team_history_opponent_goal_{x}\" for x in range(1,number_previous_games+1)]\n    list_home_rating = [f\"{home}_team_history_rating_{x}\" for x in range(1,number_previous_games+1)]\n    list_away_rating = [f\"{home}_team_history_opponent_rating_{x}\" for x in range(1,number_previous_games+1)]\n\n    sum_wins = 0\n    for column_home, column_away, rating_home, rating_away in zip(list_home_goals, list_away_goals, list_home_rating, list_away_rating):\n        try:\n            if rating_home < rating_away:\n                if row[column_home]>row[column_away]:\n                    sum_wins+=1\n                else:\n                    continue\n        except:\n            break\n    return sum_wins\n\ntrain_df[\"home_team_number_upsets\"] = train_df.apply(lambda row: number_upsets(row, \"home\"), axis=1)\ntrain_df[\"away_team_number_upsets\"] = train_df.apply(lambda row: number_upsets(row, \"away\"), axis=1)\n\ntest_df[\"home_team_number_upsets\"] = test_df.apply(lambda row: number_upsets(row, \"home\"), axis=1)\ntest_df[\"away_team_number_upsets\"] = test_df.apply(lambda row: number_upsets(row, \"away\"), axis=1)\n\ntrain_df['home_team_average_number_upsets'] = train_df['home_team_number_upsets'] / train_df[\"number_home_previous_games\"]\ntrain_df['away_team_average_number_upsets'] = train_df['away_team_number_upsets'] / train_df[\"number_away_previous_games\"]\n\ntest_df['home_team_average_number_upsets'] = test_df['home_team_number_upsets'] / train_df[\"number_home_previous_games\"]\ntest_df['away_team_average_number_upsets'] = test_df['away_team_number_upsets'] / train_df[\"number_away_previous_games\"]\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:39:37.300521Z","iopub.execute_input":"2022-03-15T08:39:37.301531Z","iopub.status.idle":"2022-03-15T08:39:54.7515Z","shell.execute_reply.started":"2022-03-15T08:39:37.301485Z","shell.execute_reply":"2022-03-15T08:39:54.750524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def numeric_streak_type(row, target_col):\n    if row[target_col]==\"Wins\":\n        return 0\n    elif row[target_col]==\"Draws\":\n        return 1\n    elif row[target_col]==\"Losses\":\n        return 2\n    else:\n        return 3\n\ntrain_df[\"home_team_numeric_streak_type\"] = train_df.apply(lambda row: numeric_streak_type(row, \"home_team_streak_type\"), axis=1)\ntrain_df[\"away_team_numeric_streak_type\"] = train_df.apply(lambda row: numeric_streak_type(row, \"away_team_streak_type\"), axis=1)\n\ntest_df[\"home_team_numeric_streak_type\"] = test_df.apply(lambda row: numeric_streak_type(row, \"home_team_streak_type\"), axis=1)\ntest_df[\"away_team_numeric_streak_type\"] = test_df.apply(lambda row: numeric_streak_type(row, \"away_team_streak_type\"), axis=1)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:39:54.753255Z","iopub.execute_input":"2022-03-15T08:39:54.753637Z","iopub.status.idle":"2022-03-15T08:40:09.349411Z","shell.execute_reply.started":"2022-03-15T08:39:54.753607Z","shell.execute_reply":"2022-03-15T08:40:09.348293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns.values.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:40:09.35094Z","iopub.execute_input":"2022-03-15T08:40:09.351547Z","iopub.status.idle":"2022-03-15T08:40:09.362513Z","shell.execute_reply.started":"2022-03-15T08:40:09.351504Z","shell.execute_reply":"2022-03-15T08:40:09.361868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def numeric_target(row):\n    if row[\"target\"]==\"home\":\n        return 0\n    elif row[\"target\"]==\"draw\":\n        return 1\n    else:\n        return 2\n    \ntrain_df[\"numeric_target\"] = train_df.apply(lambda row: numeric_target(row), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:40:09.363563Z","iopub.execute_input":"2022-03-15T08:40:09.364141Z","iopub.status.idle":"2022-03-15T08:40:13.547277Z","shell.execute_reply.started":"2022-03-15T08:40:09.364072Z","shell.execute_reply":"2022-03-15T08:40:13.546366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import preprocessing\nle_home = preprocessing.LabelEncoder()\nle_home.fit(train_df['home_team_streak_type'])\ntrain_df['home_team_streak_type_categorical'] = le_home.transform(train_df['home_team_streak_type'])\ntest_df['home_team_streak_type_categorical'] = le_home.transform(test_df['home_team_streak_type'])\n\nle_away = preprocessing.LabelEncoder()\nle_away.fit(train_df['away_team_streak_type'])\ntrain_df['away_team_streak_type_categorical'] = le_away.transform(train_df['away_team_streak_type'])\ntest_df['away_team_streak_type_categorical'] = le_away.transform(test_df['away_team_streak_type'])","metadata":{"execution":{"iopub.status.busy":"2022-03-15T08:55:21.547445Z","iopub.execute_input":"2022-03-15T08:55:21.547792Z","iopub.status.idle":"2022-03-15T08:55:21.663362Z","shell.execute_reply.started":"2022-03-15T08:55:21.547755Z","shell.execute_reply":"2022-03-15T08:55:21.66237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns_train = ['is_cup',\n 'home_team_average_days_between_matches',\n 'away_team_average_days_between_matches',\n 'home_team_number_of_home_games',\n 'away_team_number_of_home_games',\n 'home_team_number_of_cup_games',\n 'away_team_number_of_cup_games',\n 'home_team_number_of_wins',\n 'home_team_number_of_draws',\n 'home_team_number_of_losses',\n 'away_team_number_of_wins',\n 'away_team_number_of_draws',\n 'away_team_number_of_losses',\n 'home_team_total_goals_scored',\n 'home_team_average_goals_scored',\n 'home_team_total_goals_taken',\n 'home_team_average_goals_taken',\n 'away_team_total_goals_scored',\n 'away_team_average_goals_scored',\n 'away_team_total_goals_taken',\n 'away_team_average_goals_taken',\n 'home_team_average_rating',\n 'home_team_average_opponent_rating',\n 'away_team_average_rating',\n 'away_team_average_opponent_rating',\n 'home_team_change_coach',\n 'away_team_change_coach',\n 'home_team_average_number_of_home_games',\n 'away_team_average_number_of_home_games',\n 'home_team_average_number_of_wins',\n 'home_team_average_number_of_draws',\n 'home_team_average_number_of_losses',\n 'away_team_average_number_of_wins',\n 'away_team_average_number_of_draws',\n 'away_team_average_number_of_losses',\n 'home_team_streak_type_categorical',\n 'home_team_streak_count',\n 'away_team_streak_type_categorical',\n 'away_team_streak_count',\n 'home_team_number_upsets',\n 'away_team_number_upsets',\n 'home_team_average_number_upsets',\n 'away_team_average_number_upsets']\n\ntrain_df[\"is_cup\"] = train_df[\"is_cup\"].fillna(0).astype(int)\ntest_df[\"is_cup\"] = test_df[\"is_cup\"].fillna(0).astype(int)\nx = train_df[columns_train]\ny = train_df['numeric_target']\nfor column in x.columns:\n    print(column)\n    if x[column].isnull().sum()>0:\n        x[column].fillna(0, inplace=True)\n\n    print(x[column].isnull().sum())\nx_test = test_df[columns_train]\nfor column in x_test.columns:\n    print(column)\n    if x_test[column].isnull().sum()>0:\n        x_test[column].fillna(0, inplace=True)\n\n    print(x_test[column].isnull().sum())\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:00.502452Z","iopub.execute_input":"2022-03-15T09:12:00.50275Z","iopub.status.idle":"2022-03-15T09:12:00.771065Z","shell.execute_reply.started":"2022-03-15T09:12:00.502717Z","shell.execute_reply":"2022-03-15T09:12:00.769994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n# Scaling the features\nfrom sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()\nx = pd.DataFrame(scaler.fit_transform(x),columns=x.columns)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:05.902101Z","iopub.execute_input":"2022-03-15T09:12:05.902448Z","iopub.status.idle":"2022-03-15T09:12:05.910043Z","shell.execute_reply.started":"2022-03-15T09:12:05.902413Z","shell.execute_reply":"2022-03-15T09:12:05.908913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nfrom sklearn.linear_model import LogisticRegression\n\nmodel = LogisticRegression(C=0.001,penalty='l2',solver='lbfgs')\nmodel.fit(x,y)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:06.107796Z","iopub.execute_input":"2022-03-15T09:12:06.108069Z","iopub.status.idle":"2022-03-15T09:12:06.114566Z","shell.execute_reply.started":"2022-03-15T09:12:06.10804Z","shell.execute_reply":"2022-03-15T09:12:06.113583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nimport sklearn.metrics as metrics\ndef regression_results(y_true, y_pred, y_pred_proba):\n    # Regression metrics\n    explained_variance=metrics.explained_variance_score(y_true, y_pred)\n    mean_absolute_error=metrics.mean_absolute_error(y_true, y_pred) \n    mse=metrics.mean_squared_error(y_true, y_pred) \n    mean_squared_log_error=metrics.mean_squared_log_error(y_true, y_pred)\n    median_absolute_error=metrics.median_absolute_error(y_true, y_pred)\n    r2=metrics.r2_score(y_true, y_pred)\n    log_loss = metrics.log_loss(y_true, y_pred_proba)\n\n    print('explained_variance: ', round(explained_variance,4))    \n    print('mean_squared_log_error: ', round(mean_squared_log_error,4))\n    print('r2: ', round(r2,4))\n    print('MAE: ', round(mean_absolute_error,4))\n    print('MSE: ', round(mse,4))\n    print('RMSE: ', round(np.sqrt(mse),4))\n    print('Log-Loss: ', round(log_loss, 4))\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:06.277062Z","iopub.execute_input":"2022-03-15T09:12:06.277775Z","iopub.status.idle":"2022-03-15T09:12:06.285541Z","shell.execute_reply.started":"2022-03-15T09:12:06.277732Z","shell.execute_reply":"2022-03-15T09:12:06.284638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\ny_train_pred = model.predict(x)\ny_train_pred_proba = model.predict_proba(x)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:06.459072Z","iopub.execute_input":"2022-03-15T09:12:06.459779Z","iopub.status.idle":"2022-03-15T09:12:06.467448Z","shell.execute_reply.started":"2022-03-15T09:12:06.459707Z","shell.execute_reply":"2022-03-15T09:12:06.466443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nregression_results(y, y_train_pred, y_train_pred_proba)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:06.698253Z","iopub.execute_input":"2022-03-15T09:12:06.698549Z","iopub.status.idle":"2022-03-15T09:12:06.705698Z","shell.execute_reply.started":"2022-03-15T09:12:06.698519Z","shell.execute_reply":"2022-03-15T09:12:06.704705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nimport matplotlib.pyplot as plt\nimportance = model.coef_\n# summarize feature importance\ntop_positive_importance = {}\ntop_negative_importance = {}\ntop_positive_importance_name = {}\ntop_negative_importance_name = {}\n\nfor imp in range(len(importance)):\n    importance_reversed = sorted(importance[imp])[::-1]\n    top_positive_importance[imp] = importance_reversed[:6]\n    top_negative_importance[imp] = sorted(importance[imp])[:6]\n    top_positive_importance_name[imp] = []\n    top_negative_importance_name[imp] = []\n    for i,v in enumerate(zip(importance[imp], columns_train)):\n        print('Feature: {}, Name: {}, Score: {}'.format(i,v[1], v[0]))\n        if v[0] in top_positive_importance[imp]:\n            top_positive_importance_name[imp].append(v[1])\n        if v[0] in top_negative_importance[imp]:\n            top_negative_importance_name[imp].append(v[1])\n        \n            \n    # plot feature importance\n    plt.bar([x for x in range(len(importance[imp]))], importance[imp])\n    plt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:07.042495Z","iopub.execute_input":"2022-03-15T09:12:07.042757Z","iopub.status.idle":"2022-03-15T09:12:07.050994Z","shell.execute_reply.started":"2022-03-15T09:12:07.042728Z","shell.execute_reply":"2022-03-15T09:12:07.050149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#top_positive_importance_name","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:07.400205Z","iopub.execute_input":"2022-03-15T09:12:07.400464Z","iopub.status.idle":"2022-03-15T09:12:07.405222Z","shell.execute_reply.started":"2022-03-15T09:12:07.400436Z","shell.execute_reply":"2022-03-15T09:12:07.40417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#top_negative_importance_name","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:07.784466Z","iopub.execute_input":"2022-03-15T09:12:07.784784Z","iopub.status.idle":"2022-03-15T09:12:07.788925Z","shell.execute_reply.started":"2022-03-15T09:12:07.784751Z","shell.execute_reply":"2022-03-15T09:12:07.787822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# USE AUTOML\n# Create train and validation set (split on date)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:07.984102Z","iopub.execute_input":"2022-03-15T09:12:07.984366Z","iopub.status.idle":"2022-03-15T09:12:07.988655Z","shell.execute_reply.started":"2022-03-15T09:12:07.984338Z","shell.execute_reply":"2022-03-15T09:12:07.987591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eighty_percent = int(train_df.shape[0]*0.8)\ntrain = train_df.loc[:eighty_percent]\nval = train_df.loc[eighty_percent:]\n\nprint(train.match_date.min())\nprint(train.match_date.max())\nprint(val.match_date.min())\nprint(val.match_date.max())\n\nx_train = train[columns_train]\nx_val = val[columns_train]\ny_train = train['numeric_target']\ny_val = val['numeric_target']","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:08.203322Z","iopub.execute_input":"2022-03-15T09:12:08.203596Z","iopub.status.idle":"2022-03-15T09:12:08.270666Z","shell.execute_reply.started":"2022-03-15T09:12:08.203567Z","shell.execute_reply":"2022-03-15T09:12:08.269463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install flaml","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:08.459255Z","iopub.execute_input":"2022-03-15T09:12:08.459522Z","iopub.status.idle":"2022-03-15T09:12:17.240659Z","shell.execute_reply.started":"2022-03-15T09:12:08.459493Z","shell.execute_reply":"2022-03-15T09:12:17.239793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from flaml import AutoML\nautoml = AutoML()","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:17.242648Z","iopub.execute_input":"2022-03-15T09:12:17.242898Z","iopub.status.idle":"2022-03-15T09:12:17.24838Z","shell.execute_reply.started":"2022-03-15T09:12:17.24287Z","shell.execute_reply":"2022-03-15T09:12:17.247595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"automl.fit(x_train, y_train, task=\"classification\",metric='log_loss',time_budget=1000, estimator_list = ['lgbm', 'xgboost'])","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:12:17.249667Z","iopub.execute_input":"2022-03-15T09:12:17.249932Z","iopub.status.idle":"2022-03-15T09:14:00.05149Z","shell.execute_reply.started":"2022-03-15T09:12:17.2499Z","shell.execute_reply":"2022-03-15T09:14:00.050516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Best ML leaner:', automl.best_estimator)\nprint('Best hyperparmeter config:', automl.best_config)\nprint('Best log_loss 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-03-15T09:14:00.053638Z","iopub.execute_input":"2022-03-15T09:14:00.054249Z","iopub.status.idle":"2022-03-15T09:14:00.060973Z","shell.execute_reply.started":"2022-03-15T09:14:00.054211Z","shell.execute_reply":"2022-03-15T09:14:00.060351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if automl.best_estimator==\"xgboost\":\n    importance = automl.model.estimator.get_booster().get_score(importance_type=\"weight\", fmap='')\n    columns_train = list(importance.keys())\nelif automl.best_estimator==\"lgbm\":\n    columns_train = automl.model.estimator.feature_name_","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:14:00.062277Z","iopub.execute_input":"2022-03-15T09:14:00.062713Z","iopub.status.idle":"2022-03-15T09:14:00.07572Z","shell.execute_reply.started":"2022-03-15T09:14:00.062676Z","shell.execute_reply":"2022-03-15T09:14:00.074875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom xgboost import plot_importance\n\nif automl.best_estimator==\"xgboost\":\n    plot_importance(automl.model.estimator, max_num_features=20) # top 10 most important features\n    plt.show()\n\nelif len(columns_train)==len(automl.model.estimator.feature_importances_):\n    plt.barh(columns_train, automl.model.estimator.feature_importances_)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:14:00.07846Z","iopub.execute_input":"2022-03-15T09:14:00.079054Z","iopub.status.idle":"2022-03-15T09:14:00.631335Z","shell.execute_reply.started":"2022-03-15T09:14:00.079017Z","shell.execute_reply":"2022-03-15T09:14:00.630743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(y_train, automl.predict(x_train)))\n","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:14:00.632379Z","iopub.execute_input":"2022-03-15T09:14:00.6334Z","iopub.status.idle":"2022-03-15T09:14:01.584988Z","shell.execute_reply.started":"2022-03-15T09:14:00.633362Z","shell.execute_reply":"2022-03-15T09:14:01.584149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val, automl.predict(x_val)))","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:14:01.58625Z","iopub.execute_input":"2022-03-15T09:14:01.586496Z","iopub.status.idle":"2022-03-15T09:14:01.83369Z","shell.execute_reply.started":"2022-03-15T09:14:01.586467Z","shell.execute_reply":"2022-03-15T09:14:01.832758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preparing the test dy_train_pred\ntest_df_x = test_df[columns_train]\n\n# Making the predictions\n\npredictions = automl.model.predict_proba(test_df_x)\nprint(predictions)\nsubmission = pd.concat([pd.DataFrame(test_df.id),pd.DataFrame(predictions,columns=['home','draw','away'])[['home', 'away', 'draw']]],axis=1).set_index('id')\nprint(submission)\nsubmission.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-15T09:14:01.83515Z","iopub.execute_input":"2022-03-15T09:14:01.836111Z","iopub.status.idle":"2022-03-15T09:14:03.033188Z","shell.execute_reply.started":"2022-03-15T09:14:01.836039Z","shell.execute_reply":"2022-03-15T09:14:03.032093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}