{"metadata":{"kernelspec":{"display_name":"Python [conda env:ml4t]","language":"python","name":"conda-env-ml4t-py"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.8.10"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom lightgbm import LGBMClassifier\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import classification_report\nfrom sklearn.metrics import log_loss, f1_score\nimport seaborn as sns\nfrom sklearn.preprocessing import OneHotEncoder\npd.set_option('display.max_columns', 500)\npd.set_option('display.max_rows', 190)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test","metadata":{}},{"cell_type":"code","source":"df_sub2 = pd.read_csv(\"test.csv\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = df_sub2.copy()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train","metadata":{}},{"cell_type":"code","source":"df_t2 = pd.read_csv(\"train.csv\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t = df_t2.copy()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"date_cols = df_t.columns[df_t.columns.str.contains(\"date\")]\nfor col in date_cols:\n    df_t[col] = pd.to_datetime(df_t[col])\n    df_sub[col] = pd.to_datetime(df_sub[col])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.league_name.nunique(), df_t.home_team_name.nunique(), df_t.home_team_name.nunique()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.columns[df_t.columns.str.contains(\"home_days_since_game\")].to_list()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols_k1 = df_t.columns[df_t.columns.str.contains(\"home_days_since_game\")].to_list()\ncols_k2 = df_t.columns[df_t.columns.str.contains(\"home_rating_diff_\")].to_list()\ncols_k3 = df_t.columns[df_t.columns.str.contains(\"home_goal_diff_\")].to_list()\ncols_k4 = df_t.columns[df_t.columns.str.contains(\"home_rating_ratio_\")].to_list()\ncols_k = [\"home_team_name\", \"target\", \"league_name\"] + cols_k1 + cols_k2 + cols_k3 + cols_k4\ndf_t[cols_k].T","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_scores = pd.read_csv(\"train_target_and_scores.csv\")\ndf_scores[\"home_goal\"] = df_scores[\"score\"].str.get(0).astype(\"int\")\ndf_scores[\"away_goal\"] = df_scores[\"score\"].str.get(-1).astype(\"int\")\ndf_scores[\"goal_tot\"] = df_scores[\"home_goal\"] + df_scores[\"away_goal\"]\ndf_scores[\"goal_diff_abs\"] = np.abs(df_scores[\"home_goal\"] - df_scores[\"away_goal\"])\ndf_scores[\"goal_diff\"] = df_scores[\"home_goal\"] - df_scores[\"away_goal\"]\ndf_scores.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Goals for against per coach_id","metadata":{}},{"cell_type":"code","source":"def coach_goals_for_against(df_train, df_test, game_thresh=20):\n    df_train = df_train.merge(df_scores.drop(columns=\"target\"), on=\"id\")\n\n    df_home_team_goals = df_train.groupby([\"home_team_coach_id\"]).agg({\"home_goal\":\"sum\", \"away_goal\":\"sum\", \"home_team_coach_id\":\"count\"})\n    df_home_team_goals.rename(columns=({\"home_team_coach_id\":\"matches\", \"home_goal\":\"goals_for\", \"away_goal\":\"goals_against\"}), inplace=True)\n    df_home_team_goals.reset_index(inplace=True)\n    df_home_team_goals[\"home_goals_diff_ratio\"] = (df_home_team_goals[\"goals_for\"] - df_home_team_goals[\"goals_against\"]) / df_home_team_goals[\"matches\"]\n    \n    df_away_team_goals = df_train.groupby([\"away_team_coach_id\"]).agg({\"home_goal\":\"sum\", \"away_goal\":\"sum\", \"away_team_coach_id\":\"count\"})\n    df_away_team_goals.rename(columns=({\"away_team_coach_id\":\"matches\", \"home_goal\":\"goals_against\", \"away_goal\":\"goals_for\"}), inplace=True)\n    df_away_team_goals.reset_index(inplace=True)\n    df_away_team_goals[\"away_goals_diff_ratio\"] = (df_away_team_goals[\"goals_for\"] - df_away_team_goals[\"goals_against\"]) / df_away_team_goals[\"matches\"]\n    #display(df_home_team_goals.head())\n    #display(df_away_team_goals.head())\n    \n    df_goals_tot = df_home_team_goals.merge(df_away_team_goals, left_on=\"home_team_coach_id\", right_on=\"away_team_coach_id\")\n    df_goals_tot[\"goals_for\"] = df_goals_tot[\"goals_for_x\"] + df_goals_tot[\"goals_for_y\"]\n    df_goals_tot[\"goals_against\"] = df_goals_tot[\"goals_against_x\"] + df_goals_tot[\"goals_against_y\"]\n    df_goals_tot[\"matches\"] = df_goals_tot[\"matches_x\"] + df_goals_tot[\"matches_y\"]\n    df_goals_tot.rename(columns=({\"home_team_coach_id\":\"coach\"}),inplace=True)\n    df_goals_tot = df_goals_tot[[\"coach\", \"goals_for\", \"goals_against\", \"matches\"]].copy()\n    df_goals_tot[\"goals_diff_ratio\"] = (df_goals_tot[\"goals_for\"] - df_goals_tot[\"goals_against\"]) / df_goals_tot[\"matches\"]\n    #df_goals_tot.head()\n    \n    # reduce overfitting\n    cond = df_away_team_goals[\"away_team_coach_id\"] == 37252510.0 # this coach is in way to many games and teams\n    cond2 = df_away_team_goals[\"matches\"] < game_thresh\n    means_away = df_away_team_goals.loc[~cond | ~cond2].mean(axis=0)\n    df_away_team_goals.loc[cond | cond2, \"goals_against\"] = means_away[\"goals_against\"]\n    df_away_team_goals.loc[cond | cond2, \"goals_for\"] = means_away[\"goals_for\"]\n    df_away_team_goals.loc[cond | cond2, \"away_goals_diff_ratio\"] = means_away[\"away_goals_diff_ratio\"]\n\n    # reduce overfitting\n    cond = df_home_team_goals[\"home_team_coach_id\"] == 37252510.0 # this coach is in way to many games and teams\n    cond2 = df_home_team_goals[\"matches\"] < game_thresh\n    means_home = df_home_team_goals.loc[~cond | ~cond2].mean(axis=0)\n    df_home_team_goals.loc[cond | cond2, \"goals_against\"] = means_home[\"goals_against\"]\n    df_home_team_goals.loc[cond | cond2, \"goals_for\"] = means_home[\"goals_for\"]\n    df_home_team_goals.loc[cond | cond2, \"home_goals_diff_ratio\"] = means_home[\"home_goals_diff_ratio\"]\n    \n    # reduce overfitting\n    cond = df_goals_tot[\"coach\"] == 37252510.0 # this coach is in way to many games and teams\n    cond2 = df_goals_tot[\"matches\"] < game_thresh\n    means_tot = df_goals_tot.loc[~cond | ~cond2].mean(axis=0)\n    df_goals_tot.loc[cond | cond2, \"goals_against\"] = means_tot[\"goals_against\"]\n    df_goals_tot.loc[cond | cond2, \"goals_for\"] = means_tot[\"goals_for\"]\n    df_goals_tot.loc[cond | cond2, \"goals_diff_ratio\"] = means_tot[\"goals_diff_ratio\"]\n        \n    df_train = df_train.merge(df_home_team_goals[[\"home_team_coach_id\", \"home_goals_diff_ratio\"]], on=\"home_team_coach_id\", how=\"left\")\n    df_train = df_train.merge(df_away_team_goals[[\"away_team_coach_id\", \"away_goals_diff_ratio\"]], on=\"away_team_coach_id\", how=\"left\")\n\n    df_train = df_train.merge(df_goals_tot[[\"coach\", \"goals_diff_ratio\"]], left_on=\"home_team_coach_id\", right_on=\"coach\", how=\"left\")\n    df_train.rename(columns=({\"goals_diff_ratio\":\"home_team_tot_goals_diff_ratio\"}),inplace=True)\n\n    df_train = df_train.merge(df_goals_tot[[\"coach\", \"goals_diff_ratio\"]], left_on=\"away_team_coach_id\", right_on=\"coach\", how=\"left\")\n    df_train.rename(columns=({\"goals_diff_ratio\":\"away_team_tot_goals_diff_ratio\"}),inplace=True)\n\n    df_train.drop(columns=[\"coach_x\", \"coach_y\"], inplace=True)\n\n    df_test = df_test.merge(df_home_team_goals[[\"home_team_coach_id\", \"home_goals_diff_ratio\"]], on=\"home_team_coach_id\", how=\"left\")\n    df_test = df_test.merge(df_away_team_goals[[\"away_team_coach_id\", \"away_goals_diff_ratio\"]], on=\"away_team_coach_id\", how=\"left\")\n\n    df_test = df_test.merge(df_goals_tot[[\"coach\", \"goals_diff_ratio\"]], left_on=\"home_team_coach_id\", right_on=\"coach\", how=\"left\")\n    df_test.rename(columns=({\"goals_diff_ratio\":\"home_team_tot_goals_diff_ratio\"}),inplace=True)\n\n    df_test = df_test.merge(df_goals_tot[[\"coach\", \"goals_diff_ratio\"]], left_on=\"away_team_coach_id\", right_on=\"coach\", how=\"left\")\n    df_test.rename(columns=({\"goals_diff_ratio\":\"away_team_tot_goals_diff_ratio\"}),inplace=True)\n\n    df_test.drop(columns=[\"coach_x\", \"coach_y\"], inplace=True)\n    \n    df_train.drop(columns=['away_goal', 'goal_diff', 'goal_diff_abs', 'goal_tot', 'home_goal', 'score'], inplace=True)\n    \n    # fill in missing values\n    df_test[\"home_goals_diff_ratio\"].fillna(means_home[\"home_goals_diff_ratio\"], inplace=True)\n    df_test[\"away_goals_diff_ratio\"].fillna(means_away[\"away_goals_diff_ratio\"], inplace=True)\n    df_test[\"home_team_tot_goals_diff_ratio\"].fillna(means_tot[\"goals_diff_ratio\"], inplace=True)\n    df_test[\"away_team_tot_goals_diff_ratio\"].fillna(means_tot[\"goals_diff_ratio\"], inplace=True)\n    \n    return df_train, df_test","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### win, draw and home ratio per coach_id","metadata":{}},{"cell_type":"code","source":"def league_ratio(df_train, df_test, game_thresh=20):\n    #league stats\n    df_targets = df_train[[\"id\", \"target\"]].pivot(\"id\", \"target\", \"target\")#.fillna(0)\n    cols = df_targets.columns\n    df_targets = df_targets.where(df_targets.isna(),1)\n    df_targets.fillna(0, inplace=True)\n    df_targets.reset_index(inplace=True)\n    #df_targets.head()\n    \n    df_train = df_train.merge(df_targets, on=\"id\")\n    \n    df_leageu_stats = df_train.groupby(\"league_name\").agg({\"away\":\"sum\", \"draw\":\"sum\", \"home\":\"sum\", \"league_name\":\"count\"})\n    df_leageu_stats.rename(columns=({\"league_name\":\"games\"}), inplace=True)\n    df_leageu_stats.reset_index(inplace=True)\n    df_leageu_stats[\"away_ratio\"] = df_leageu_stats[\"away\"] / df_leageu_stats[\"games\"]\n    df_leageu_stats[\"draw_ratio\"] = df_leageu_stats[\"draw\"] / df_leageu_stats[\"games\"]\n    df_leageu_stats[\"home_ratio\"] = df_leageu_stats[\"home\"] / df_leageu_stats[\"games\"]\n    df_leageu_stats = df_leageu_stats[[\"league_name\", \"away_ratio\", \"draw_ratio\", \"home_ratio\", \"games\"]].copy()\n    df_leageu_stats.rename(columns=({\"away_ratio\":\"league_away_ratio\", \"home_ratio\":\"league_home_ratio\", \"draw_ratio\":\"league_draw_ratio\"}), inplace=True)\n    \n    # reduce overfitting\n    cond = df_leageu_stats[\"games\"] > game_thresh\n    means = df_leageu_stats[cond].select_dtypes(include=\"number\").mean(axis=0)\n    df_leageu_stats.loc[~cond, \"league_away_ratio\"] = means[\"league_away_ratio\"]\n    df_leageu_stats.loc[~cond, \"league_draw_ratio\"] = means[\"league_draw_ratio\"]\n    df_leageu_stats.loc[~cond, \"league_home_ratio\"] = means[\"league_home_ratio\"]\n    df_leageu_stats.drop(columns=[\"games\"], inplace=True)\n\n    df_train = df_train.merge(df_leageu_stats, on=\"league_name\", how=\"left\")\n    df_test = df_test.merge(df_leageu_stats, on=\"league_name\", how=\"left\")\n    df_test[\"league_away_ratio\"].fillna(means[\"league_away_ratio\"])\n    df_test[\"league_draw_ratio\"].fillna(means[\"league_draw_ratio\"])\n    df_test[\"league_home_ratio\"].fillna(means[\"league_home_ratio\"])\n    \n    df_train.drop(columns=[\"away\", \"draw\", \"home\"], inplace=True)\n    \n    return df_train, df_test\n    \n    \ndef coach_win_ratio(df_train, df_test, game_thresh=20):\n    # coach win ratio\n    #league stats\n    df_targets = df_train[[\"id\", \"target\"]].pivot(\"id\", \"target\", \"target\")#.fillna(0)\n    cols = df_targets.columns\n    df_targets = df_targets.where(df_targets.isna(),1)\n    df_targets.fillna(0, inplace=True)\n    df_targets.reset_index(inplace=True)\n    #df_targets.head()\n    \n    df_train = df_train.merge(df_targets, on=\"id\")\n    \n    df_wins_home = df_train.groupby(\"home_team_coach_id\").agg({\"away\":\"sum\", \"draw\":\"sum\", \"home\":\"sum\", \"home_team_coach_id\":\"count\"})\n    df_wins_home.rename(columns=({\"home_team_coach_id\":\"games\"}), inplace=True)\n    df_wins_home.reset_index(inplace=True)\n    df_wins_home.rename(columns=({\"home_team_coach_id\":\"coach\"}), inplace=True)\n\n    df_wins_away = df_train.groupby(\"away_team_coach_id\").agg({\"away\":\"sum\", \"draw\":\"sum\", \"home\":\"sum\", \"away_team_coach_id\":\"count\"})\n    df_wins_away.rename(columns=({\"away_team_coach_id\":\"games\"}), inplace=True)\n    df_wins_away.reset_index(inplace=True)\n    df_wins_away.rename(columns=({\"away_team_coach_id\":\"coach\"}), inplace=True)\n\n    df_wins_total = df_wins_away.merge(df_wins_home, how=\"outer\", on=\"coach\")\n    df_wins_total.fillna(0, inplace=True)\n    df_wins_total[\"away\"] = df_wins_total[\"away_x\"] + df_wins_total[\"away_y\"]\n    df_wins_total[\"draw\"] = df_wins_total[\"draw_x\"] + df_wins_total[\"draw_y\"]\n    df_wins_total[\"home\"] = df_wins_total[\"home_x\"] + df_wins_total[\"home_y\"]\n    df_wins_total[\"games\"] = df_wins_total[\"games_x\"] + df_wins_total[\"games_y\"]\n    df_wins_total = df_wins_total[[\"coach\", \"away\", \"draw\", \"home\", \"games\"]].copy()\n\n\n    df_wins_total[\"away_ratio\"] = df_wins_total[\"away\"] / df_wins_total[\"games\"]\n    df_wins_total[\"draw_ratio\"] = df_wins_total[\"draw\"] / df_wins_total[\"games\"]\n    df_wins_total[\"home_ratio\"] = df_wins_total[\"home\"] / df_wins_total[\"games\"]\n    #df_wins = df_wins.reset_index()\n    df_wins_total = df_wins_total[[\"coach\", \"games\", \"away_ratio\", \"draw_ratio\", \"home_ratio\"]].copy()\n        \n    # reduce overfitting\n    cond = df_wins_total[\"coach\"] == 37252510.0 # this coach is in way to many games and teams\n    cond2 = df_wins_total[\"games\"] < game_thresh\n    means = df_wins_total.loc[~cond | ~cond2].mean(axis=0)\n    df_wins_total.loc[cond | cond2, \"away_ratio\"] = means[\"away_ratio\"]\n    df_wins_total.loc[cond | cond2, \"draw_ratio\"] = means[\"draw_ratio\"]\n    df_wins_total.loc[cond | cond2, \"home_ratio\"] = means[\"home_ratio\"]\n    \n    df_train = df_train.merge(df_wins_total, left_on=\"home_team_coach_id\", right_on=\"coach\", how=\"left\")\n    df_train.rename(columns=({\"games\":\"home_coach_games\", \"away_ratio\":\"home_coach_away_ratio\", \"home_ratio\":\"home_coach_home_ratio\", \"draw_ratio\":\"home_coach_draw_ratio\"}), inplace=True)\n    df_train.drop(columns=\"coach\", inplace=True)\n\n    df_train = df_train.merge(df_wins_total, left_on=\"away_team_coach_id\", right_on=\"coach\", how=\"left\")\n    df_train.rename(columns=({\"games\":\"away_coach_games\", \"away_ratio\":\"away_coach_away_ratio\", \"home_ratio\":\"away_coach_home_ratio\", \"draw_ratio\":\"away_coach_draw_ratio\"}), inplace=True)\n    df_train.drop(columns=\"coach\", inplace=True)\n    \n    df_test = df_test.merge(df_wins_total, left_on=\"home_team_coach_id\", right_on=\"coach\", how=\"left\")\n    df_test.rename(columns=({\"games\":\"home_coach_games\", \"away_ratio\":\"home_coach_away_ratio\", \"home_ratio\":\"home_coach_home_ratio\", \"draw_ratio\":\"home_coach_draw_ratio\"}), inplace=True)\n    df_test.drop(columns=\"coach\", inplace=True)\n\n    df_test = df_test.merge(df_wins_total, left_on=\"away_team_coach_id\", right_on=\"coach\", how=\"left\")\n    df_test.rename(columns=({\"games\":\"away_coach_games\", \"away_ratio\":\"away_coach_away_ratio\", \"home_ratio\":\"away_coach_home_ratio\", \"draw_ratio\":\"away_coach_draw_ratio\"}), inplace=True)\n    df_test.drop(columns=\"coach\", inplace=True)\n    \n    df_train.fillna(0, inplace=True)\n    df_test.fillna(0, inplace=True)\n    \n    # fill in missing values for test set\n    #cond = df_test[\"home_coach_away_ratio\"].isna()\n    #df_test.loc[cond, \"home_coach_away_ratio\"] = means[\"away_ratio\"]\n    #df_test.loc[cond, \"away_coach_away_ratio\"] = means[\"away_ratio\"]\n    #df_test.loc[cond, \"home_coach_home_ratio\"] = means[\"home_ratio\"]\n    #df_test.loc[cond, \"away_coach_home_ratio\"] = means[\"home_ratio\"]\n    #df_test.loc[cond, \"home_coach_draw_ratio\"] = means[\"draw_ratio\"]\n    #df_test.loc[cond, \"away_coach_draw_ratio\"] = means[\"draw_ratio\"]\n    \n    df_train.drop(columns=[\"away\", \"draw\", \"home\"], inplace=True)\n    \n    return df_train, df_test\n    ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"df_home_team_goals = df_t.groupby([\"home_team_name\"]).agg({\"home_goal\":\"sum\", \"away_goal\":\"sum\", \"home_team_name\":\"count\"})\ndf_home_team_goals.rename(columns=({\"home_team_name\":\"matches\", \"home_goal\":\"goals_for\", \"away_goal\":\"goals_against\"}), inplace=True)\ndf_home_team_goals.reset_index(inplace=True)\ndf_home_team_goals[\"home_goals_diff_ratio\"] = (df_home_team_goals[\"goals_for\"] - df_home_team_goals[\"goals_against\"]) / df_home_team_goals[\"matches\"]\n\ndf_away_team_goals = df_t.groupby([\"away_team_name\"]).agg({\"home_goal\":\"sum\", \"away_goal\":\"sum\", \"away_team_name\":\"count\"})\ndf_away_team_goals.rename(columns=({\"away_team_name\":\"matches\", \"home_goal\":\"goals_against\", \"away_goal\":\"goals_for\"}), inplace=True)\ndf_away_team_goals.reset_index(inplace=True)\ndf_away_team_goals[\"away_goals_diff_ratio\"] = (df_away_team_goals[\"goals_for\"] - df_away_team_goals[\"goals_against\"]) / df_away_team_goals[\"matches\"]\ndisplay(df_home_team_goals.head())\ndisplay(df_away_team_goals.head())","metadata":{}},{"cell_type":"markdown","source":"df_goals_tot = df_home_team_goals.merge(df_away_team_goals, left_on=\"home_team_name\", right_on=\"away_team_name\")\ndf_goals_tot[\"goals_for\"] = df_goals_tot[\"goals_for_x\"] + df_goals_tot[\"goals_for_y\"]\ndf_goals_tot[\"goals_against\"] = df_goals_tot[\"goals_against_x\"] + df_goals_tot[\"goals_against_y\"]\ndf_goals_tot[\"matches\"] = df_goals_tot[\"matches_x\"] + df_goals_tot[\"matches_y\"]\ndf_goals_tot.rename(columns=({\"home_team_name\":\"team\"}),inplace=True)\ndf_goals_tot = df_goals_tot[[\"team\", \"goals_for\", \"goals_against\", \"matches\"]].copy()\ndf_goals_tot[\"goals_diff_ratio\"] = (df_goals_tot[\"goals_for\"] - df_goals_tot[\"goals_against\"]) / df_goals_tot[\"matches\"]\ndf_goals_tot.head()","metadata":{}},{"cell_type":"markdown","source":"df_t = df_t.merge(df_home_team_goals[[\"home_team_name\", \"home_goals_diff_ratio\"]], on=\"home_team_name\")\ndf_t = df_t.merge(df_away_team_goals[[\"away_team_name\", \"away_goals_diff_ratio\"]], on=\"away_team_name\")","metadata":{}},{"cell_type":"markdown","source":"df_t = df_t.merge(df_goals_tot[[\"team\", \"goals_diff_ratio\"]], left_on=\"home_team_name\", right_on=\"team\")\ndf_t.rename(columns=({\"goals_diff_ratio\":\"home_team_tot_goals_diff_ratio\"}),inplace=True)\n\ndf_t = df_t.merge(df_goals_tot[[\"team\", \"goals_diff_ratio\"]], left_on=\"away_team_name\", right_on=\"team\")\ndf_t.rename(columns=({\"goals_diff_ratio\":\"away_team_tot_goals_diff_ratio\"}),inplace=True)\n\ndf_t.drop(columns=[\"team_x\", \"team_y\"], inplace=True)","metadata":{}},{"cell_type":"markdown","source":"df_sub = df_sub.merge(df_home_team_goals[[\"home_team_name\", \"home_goals_diff_ratio\"]], on=\"home_team_name\")\ndf_sub = df_sub.merge(df_away_team_goals[[\"away_team_name\", \"away_goals_diff_ratio\"]], on=\"away_team_name\")\n\ndf_sub = df_sub.merge(df_goals_tot[[\"team\", \"goals_diff_ratio\"]], left_on=\"home_team_name\", right_on=\"team\")\ndf_sub.rename(columns=({\"goals_diff_ratio\":\"home_team_tot_goals_diff_ratio\"}),inplace=True)\n\ndf_sub = df_sub.merge(df_goals_tot[[\"team\", \"goals_diff_ratio\"]], left_on=\"away_team_name\", right_on=\"team\")\ndf_sub.rename(columns=({\"goals_diff_ratio\":\"away_team_tot_goals_diff_ratio\"}),inplace=True)\n\ndf_sub.drop(columns=[\"team_x\", \"team_y\"], inplace=True)","metadata":{}},{"cell_type":"code","source":"for i in range(1,11):\n    df_t[f\"home_days_since_game{i}\"] = (df_t[\"match_date\"] - df_t[f\"home_team_history_match_date_{i}\"]).dt.days\n    df_t[f\"away_days_since_game{i}\"] = (df_t[\"match_date\"] - df_t[f\"away_team_history_match_date_{i}\"]).dt.days\n    df_sub[f\"home_days_since_game{i}\"] = (df_sub[\"match_date\"] - df_sub[f\"home_team_history_match_date_{i}\"]).dt.days\n    df_sub[f\"away_days_since_game{i}\"] = (df_sub[\"match_date\"] - df_sub[f\"away_team_history_match_date_{i}\"]).dt.days\n\n    df_t[f\"home_rating_diff_{i}\"] = df_t[f\"home_team_history_rating_{i}\"] - df_t[f\"home_team_history_opponent_rating_{i}\"]\n    df_t[f\"away_rating_diff_{i}\"] = df_t[f\"away_team_history_rating_{i}\"] - df_t[f\"away_team_history_opponent_rating_{i}\"]\n    df_sub[f\"home_rating_diff_{i}\"] = df_sub[f\"home_team_history_rating_{i}\"] - df_sub[f\"home_team_history_opponent_rating_{i}\"]\n    df_sub[f\"away_rating_diff_{i}\"] = df_sub[f\"away_team_history_rating_{i}\"] - df_sub[f\"away_team_history_opponent_rating_{i}\"]\n    \n    df_t[f\"home_rating_ratio_{i}\"] = df_t[f\"home_team_history_rating_{i}\"] / df_t[f\"home_team_history_opponent_rating_{i}\"]\n    df_t[f\"away_rating_ratio_{i}\"] = df_t[f\"away_team_history_rating_{i}\"] / df_t[f\"away_team_history_opponent_rating_{i}\"]\n    df_sub[f\"home_rating_ratio_{i}\"] = df_sub[f\"home_team_history_rating_{i}\"] / df_sub[f\"home_team_history_opponent_rating_{i}\"]\n    df_sub[f\"away_rating_ratio_{i}\"] = df_sub[f\"away_team_history_rating_{i}\"] / df_sub[f\"away_team_history_opponent_rating_{i}\"]\n    \n    df_t[f\"home_goal_diff_{i}\"] = df_t[f\"home_team_history_goal_{i}\"] - df_t[f\"home_team_history_opponent_goal_{i}\"]\n    df_t[f\"away_goal_diff_{i}\"] = df_t[f\"away_team_history_goal_{i}\"] - df_t[f\"away_team_history_opponent_goal_{i}\"]\n    df_sub[f\"home_goal_diff_{i}\"] = df_sub[f\"home_team_history_goal_{i}\"] - df_sub[f\"home_team_history_opponent_goal_{i}\"]\n    df_sub[f\"away_goal_diff_{i}\"] = df_sub[f\"away_team_history_goal_{i}\"] - df_sub[f\"away_team_history_opponent_goal_{i}\"]\n    \n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ncols = df_t.columns[df_t.columns.str.contains(\"home_rating_diff_\")]\ndf_t[cols]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = df_t.columns[df_t.columns.str.contains(\"home_rating_diff_\")].tolist()\ncols2 = [*[\"league_name\"] + cols]\ndf_rate = pd.melt(df_t[cols2], id_vars=\"league_name\", value_vars=cols, value_name=\"rating\")\ndf_rate","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig, ax = plt.subplots(1,1,figsize=(6,4))\nsns.histplot(data=df_t.sample(n=1000), x=\"away_team_history_rating_2\", ax=ax)\nax.set_xlim(0,20);","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig, ax = plt.subplots(1,1,figsize=(6,4))\nsns.histplot(data=df_t.sample(n=1000), x=\"home_rating_diff_1\", ax=ax)\nax.set_xlim(-10,10);","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.scatterplot(data=df_t.sample(n=1000), x=\"home_rating_diff_1\", y=\"home_game_status_1\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.scatterplot(data=df_t.sample(n=1000), x=\"home_rating_diff_1\", y=\"home_goal_diff_1\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = df_t.columns[df_t.columns.str.contains(\"home_rating_diff_\")].tolist()\ncols2 = [*[\"league_name\"] + cols]\ndf_rate = pd.melt(df_t[cols2], id_vars=\"league_name\", value_vars=cols, value_name=\"rating\")\ndf_rate.drop(columns=[\"variable\"], inplace=True)\ncols3 = df_t.columns[df_t.columns.str.contains(\"home_goal_diff_\")].tolist() #home_game_status_\n#cols3 = df_t.columns[df_t.columns.str.contains(\"home_game_status_\")].tolist() #home_game_status_\ncols4 = [*[\"league_name\"] + cols3]\ndf_goals = pd.melt(df_t[cols4], id_vars=\"league_name\", value_vars=cols3, value_name=\"goals\")\ndf_goals.drop(columns=[\"league_name\", \"variable\"], inplace=True)\ndf_sum = pd.concat([df_rate, df_goals], axis=1)\ndisplay(df_sum[[\"rating\", \"goals\"]].corr())\ndisplay(df_sum.sample(n=10000)[[\"rating\", \"goals\"]].corr(method=\"spearman\"))\ndisplay(df_sum.sample(n=10000)[[\"rating\", \"goals\"]].corr(method=\"kendall\"))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_corr = df_sum.groupby(\"league_name\").corr().reset_index()\ncond = df_corr[\"level_1\"] == \"rating\"\ndf_corr = df_corr[cond].copy()\ndf_corr.drop(columns=[\"level_1\", \"rating\"], inplace=True)\ndf_corr.rename(columns=({\"goals\":\"corr\"}), inplace=True)\n\ndf_league_counts = df_sum.groupby(\"league_name\")[[\"league_name\"]].count()\ndf_league_counts.rename(columns=({\"league_name\":\"counts\"}), inplace=True)\ndf_league_counts.reset_index(inplace=True)\n\n\ndf_corr = df_corr.merge(df_league_counts, on=\"league_name\")\ncond = df_corr[\"counts\"] > 60\ncond2 = df_corr.isna().any(axis=1)\ndf_corr[~cond2 & cond].sort_values(\"corr\").head(20)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# woman feature\ndf_t[\"women\"] = df_t[\"league_name\"].str.lower().str.contains(\"women\")#.astype(\"int\")\ndf_t[\"women\"].fillna(False, inplace=True)\ndf_t[\"women\"] = df_t[\"women\"].astype(\"int\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = df_t.columns[df_t.columns.str.contains(\"home_goal_diff_\")]\ndf_t[\"home_goals_net\"] = df_t[cols].sum(axis=1)\ndf_sub[\"home_goals_net\"] = df_t[cols].sum(axis=1)\n\ncols = df_t.columns[df_t.columns.str.contains(\"away_goal_diff_\")]\ndf_t[\"away_goals_net\"] = df_t[cols].sum(axis=1)\ndf_sub[\"away_goals_net\"] = df_t[cols].sum(axis=1)\n\nfor i in range(1, 11):\n    conds = [df_t[f\"home_goal_diff_{i}\"] > 0, df_t[f\"home_goal_diff_{i}\"] == 0, df_t[f\"home_goal_diff_{i}\"] < 0]\n    df_t[f\"home_game_status_{i}\"] = np.select(conds, [1,0,-1])\n    conds = [df_t[f\"away_goal_diff_{i}\"] > 0, df_t[f\"away_goal_diff_{i}\"] == 0, df_t[f\"away_goal_diff_{i}\"] < 0]\n    df_t[f\"away_game_status_{i}\"] = np.select(conds, [1,0,-1])\n    \n    conds = [df_sub[f\"home_goal_diff_{i}\"] > 0, df_sub[f\"home_goal_diff_{i}\"] == 0, df_sub[f\"home_goal_diff_{i}\"] < 0]\n    df_sub[f\"home_game_status_{i}\"] = np.select(conds, [1,0,-1])\n    conds = [df_sub[f\"away_goal_diff_{i}\"] > 0, df_sub[f\"away_goal_diff_{i}\"] == 0, df_sub[f\"away_goal_diff_{i}\"] < 0]\n    df_sub[f\"away_game_status_{i}\"] = np.select(conds, [1,0,-1])\n\n\ncols = df_t.columns[df_t.columns.str.contains(\"home_game_status_\")]\ndf_t[\"home_wins\"] = df_t[cols].sum(axis=1)\ndf_sub[\"home_wins\"] = df_t[cols].sum(axis=1)\n\ncols = df_t.columns[df_t.columns.str.contains(\"away_game_status_\")]\ndf_t[\"away_wins\"] = df_t[cols].sum(axis=1)\ndf_sub[\"away_wins\"] = df_t[cols].sum(axis=1)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop date info\ncols_date = df_t.columns[df_t.columns.str.contains(\"date\")]\ndf_t.drop(columns=cols_date, inplace=True)\ndf_sub.drop(columns=cols_date, inplace=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop league info\ncols_league = df_t.columns[df_t.columns.str.contains(\"league\")]\ncols_league2 = [col for col in cols_league if col not in [\"league_name\"]]\ndf_t.drop(columns=cols_league2, inplace=True)\ndf_sub.drop(columns=cols_league, inplace=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop is_play info\ncols_is_play = df_t.columns[df_t.columns.str.contains(\"is_play\")]\ndf_t.drop(columns=cols_is_play, inplace=True)\ndf_sub.drop(columns=cols_is_play, inplace=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop is_cup info\ncols_is_cup = df_t.columns[df_t.columns.str.contains(\"is_cup\")]\ndf_t.drop(columns=cols_is_cup, inplace=True)\ndf_sub.drop(columns=cols_is_cup, inplace=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Drop columns\ncols_drop = [\"score\", \"home_goal\", \"away_goal\", \"goal_tot\", \"goal_diff_abs\", \"goal_diff\", \"away\", \"draw\", \"home\"]\ncols_drop_sub = [\"id\"]\ncols_drop2 = [\"home_team_name\", \"away_team_name\"]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_t.drop(columns=cols_drop_sub, inplace=True)\ndf_t.drop(columns=cols_drop2, inplace=True)\n\n#df_sub.drop(columns=cols_drop_sub, inplace=True)\ndf_sub.drop(columns=cols_drop2, inplace=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.is_cup.fillna(0, inplace=True)\ndf_sub.is_cup.fillna(0, inplace=True)\ndf_t[\"is_cup\"] = df_t[\"is_cup\"].astype(\"int\")\ndf_sub[\"is_cup\"] = df_sub[\"is_cup\"].astype(\"int\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = df_t.drop(columns=\"target\").copy()\ntarget = df_t[\"target\"].copy()\n\n\n\n#features.fillna(0, inplace=True)\n\nx_train, x_test, y_train, y_test = train_test_split(features, target, random_state=42)\n\nx_train2 = x_train.copy()\nx_test2 = x_test.copy()\nx_train2.reset_index(drop=True, inplace=True)\nx_test2.reset_index(drop=True, inplace=True)\ny_train2 = y_train.copy()\ny_test2 = y_test.copy()\ny_train2.reset_index(drop=True, inplace=True)\ny_test2.reset_index(drop=True, inplace=True)\n\ndf_train = pd.concat([x_train, y_train], axis=1)\n#df_train, x_test = coach_goals_for_against(df_train, x_test, game_thresh=50) # home_team_tot_goals_diff_ratio\ndf_train, x_test = coach_win_ratio(df_train, x_test, game_thresh=500) # 500 was best, seem like only number of games coach has been in helps\n#df_train, x_test = league_ratio(df_train, x_test, game_thresh=100) # around 100 seems to be the best\n\n# Drop coach info\ncols_coach = df_train.columns[df_train.columns.str.contains(\"coach\")]\ncols_keep1 = df_train.columns[df_train.columns.str.contains(\"away_coach\")].to_list()\ncols_keep2 = df_train.columns[df_train.columns.str.contains(\"home_coach\")].to_list()\ncols_keep = cols_keep1 + cols_keep2\ncols_coach = [col for col in cols_coach if col not in cols_keep]\ndf_train.drop(columns=cols_coach, inplace=True)\nx_test.drop(columns=cols_coach, inplace=True)\n\n# Drop goal info\ncols_drop = df_train.columns[df_train.columns.str.contains(\"goal\")]\ncols_drop = [col for col in cols_drop if \"diff_ratio\" not in col]\ndf_train.drop(columns=cols_drop, inplace=True)\nx_test.drop(columns=cols_drop, inplace=True)\n\n# Drop rating info\ncols_coach = df_train.columns[df_train.columns.str.contains(\"rating\")]\ndf_train.drop(columns=cols_coach, inplace=True)\nx_test.drop(columns=cols_coach, inplace=True)\n\nx_train = df_train.drop(columns=\"target\")\n\nx_train = x_train.drop(columns=[\"league_name\", \"id\"])\nx_test.drop(columns=[\"league_name\", \"id\"], inplace=True)\n\n#x_train = x_train[[\"home_goals_net\", \"away_goals_net\"]].copy()\n#x_test = x_test[[\"home_goals_net\", \"away_goals_net\"]].copy()\n\n#x_train = x_train[[\"home_team_tot_goals_diff_ratio\", \"away_team_tot_goals_diff_ratio\"]].copy()\n#x_test = x_test[[\"home_team_tot_goals_diff_ratio\", \"away_team_tot_goals_diff_ratio\"]].copy()\n\n\n\ny_test_ohe = pd.get_dummies(y_test).values\ny_train_ohe = pd.get_dummies(y_train).values\n#target = target.map({\"home\":1, \"draw\":3, \"away\":2})","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = LGBMClassifier()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params={\n    'learning_rate':0.03,\n    'boosting_type':'gbdt', #GradientBoostingDecisionTree\n    'objective':'multiclass', #Multi-class target feature\n    'metric':'multi_logloss', #metric for multi-class\n    'max_depth':10,\n    'num_class':3, #no.of unique values in the target class not inclusive of the end value\n}\n#training the model\nclf.fit(x_train, y_train)  #training the model on 100 epocs","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(zip(x_train.columns, clf.feature_importances_), columns=[\"feat\", \"imp\"]).sort_values(\"imp\", ascending=False).head(20)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# home_team_tot_goals_diff_ratio, away_team_tot_goals_diff_ratio, thresh=50\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# home_team_tot_goals_diff_ratio, away_team_tot_goals_diff_ratio, thresh=10\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# home goals net and away goals net\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# away and home wins\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coach_win_ratio(500)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coach_win_ratio(100)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coach_win_ratio(50)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coach_win_ratio(20)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coach_win_ratio(5)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# league ratio(200)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# league ratio(100)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# league ratio(50)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# league ratio(20)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# league ratio(5)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# league ratio(1000)\npreds_p_test = clf.predict_proba(x_test)\npreds_p_train = clf.predict_proba(x_train)\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(log_loss(y_train_ohe, preds_p_train))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test = clf.predict(x_test)\npreds_p_test = clf.predict_proba(x_test)\npreds_train = clf.predict(x_train)\npreds_p_train = clf.predict_proba(x_train)\n\n# use all features, except date, remove coach target, remove league target, added fe lags, add new features, remove almost all\nprint(log_loss(y_test_ohe, preds_p_test))\nprint(classification_report(y_test, preds_test))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.ones((27735,3))*0.33\nprint(log_loss(y_test_ohe, test))\ntest = np.ones((83203,3))*0.33\nprint(log_loss(y_train_ohe, test))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(log_loss(y_train_ohe, preds_p_train))\nprint(classification_report(y_train, preds_train))#, target_names=target_names))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### kfold loss funcitons","metadata":{}},{"cell_type":"code","source":"from collections import defaultdict\nres = defaultdict(lambda: defaultdict(dict))\nfeatures2 = df_t.drop(columns=[\"target\"])\ntarget = df_t[\"target\"].copy()\n\nskf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)\nskf.get_n_splits(features2, target)\nfor i, (train_index, test_index) in enumerate(skf.split(features2, target)):\n    print(i)\n    x_train = features2.iloc[train_index]\n    x_test = features2.iloc[test_index]\n    y_train = target.iloc[train_index]\n    y_test = target.iloc[test_index]\n    \n    #pdb.set_trace()\n    df_train = pd.concat([x_train, y_train], axis=1)\n    #pdb.set_trace()\n    #df_train, x_test = coach_fols_for_against(df_train, x_test)\n    df_train, x_test = coach_win_ratio(df_train, x_test)\n    \n    \n    # Drop coach info\n    cols_coach = x_train.columns[x_train.columns.str.contains(\"coach\")]\n    df_train.drop(columns=cols_coach, inplace=True)\n    x_test.drop(columns=cols_coach, inplace=True)\n    \n    cols_drop2 = [\"home_team_name\", \"away_team_name\", \"id\", \"league_name\", \"home_team_name\", \"away_team_name\", \"league_name\", \"is_cup\"]\n    cols_drop3 = ['away', 'draw', 'home']\n    df_train.drop(columns=cols_drop2, inplace=True)\n    df_train.drop(columns=cols_drop3, inplace=True)\n    x_test.drop(columns=cols_drop2, inplace=True)\n    \n    x_train = df_train.drop(columns=[\"target\"])\n    #y_train = y_train[\"target\"].copy()\n    \n    clf.fit(x_train, y_train)\n    \n    preds_test = clf.predict(x_test)\n    preds_p_test = clf.predict_proba(x_test)\n    preds_train = clf.predict(x_train)\n    preds_p_train = clf.predict_proba(x_train)\n    res[\"train\"][\"log_loss\"][f\"fold_{i}\"] = log_loss(y_train, preds_p_train)\n    res[\"test\"][\"log_loss\"][f\"fold_{i}\"] = log_loss(y_test, preds_p_test)\n    res[\"train\"][\"f1_score\"][f\"fold_{i}\"] = f1_score(y_train, preds_train, average=\"micro\")\n    res[\"test\"][\"f1_score\"][f\"fold_{i}\"] = f1_score(y_test, preds_test, average=\"micro\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# use all features, except date, remove coach target, remove league target, added fe lags, add new features\nprint(log_loss(y_test_ohe, preds_p_test))\ntarget_names = [\"home\", \"draw\", \"away\"]\nprint(classification_report(y_test, preds_test, target_names=target_names))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# use all features, except date, remove coach target, remove league target, added fe lags\nprint(log_loss(y_test_ohe, preds_p_test))\ntarget_names = [\"home\", \"draw\", \"away\"]\nprint(classification_report(y_test, preds_test, target_names=target_names))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# use all features, except date, remove coach target, added league target\nprint(log_loss(y_test_ohe, preds_p_test))\ntarget_names = [\"home\", \"draw\", \"away\"]\nprint(classification_report(y_test, preds_test, target_names=target_names))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# use all features, except date, added coach target\ntarget_names = [\"home\", \"draw\", \"away\"]\nprint(classification_report(y_test, preds_test, target_names=target_names))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# use all features, except date\ntarget_names = [\"home\", \"draw\", \"away\"]\nprint(classification_report(y_test, preds_test, target_names=target_names))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_names = [\"home\", \"draw\", \"away\"]\nprint(classification_report(y_test, preds_test, target_names=target_names))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(log_loss(y_train_ohe, preds_p_train))\ntarget_names = [\"home\", \"away\", \"draw\"]\nprint(classification_report(y_train, preds_train, target_names=target_names))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.concat([x_test2, pd.Series(preds_test, name=\"preds\"), y_test2], axis=1)\ndf_test.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[\"diff\"] = df_test[\"preds\"] == df_test[\"target\"]\ndf_test.groupby(\"league_name\").agg(matches=(\"league_name\",\"count\"),\n                                   mean=(\"diff\",\"mean\")).reset_index().sort_values(\"matches\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_coach - train_coach","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(zip(x_train.columns, clf.feature_importances_), columns=[\"feat\", \"imp\"]).sort_values(\"imp\", ascending=False).head(20)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(zip(x_train.columns, clf.feature_importances_), columns=[\"feat\", \"imp\"]).sort_values(\"imp\", ascending=False).tail(20)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.fit(features.drop(columns=\"league_name\"), target)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.league_name.value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_preds = clf.predict_proba(df_sub.drop(columns=[\"id\", \"league_name\"]))\ndf_s_pred = pd.DataFrame(sub_preds, columns=[\"home\", \"away\", \"draw\"])\ndf_s_pred","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_s_pred = pd.concat([df_sub[[\"id\"]], df_s_pred], axis=1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_s_pred.to_csv(\"sub1.csv\", index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_s_pred","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.ones((72711,3))*0.33","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.DataFrame(test, columns=[\"home\", \"away\", \"draw\"])\npd.concat([df_sub[[\"id\"]], test], axis=1).to_csv(\"manual.csv\", index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.league_name.value_counts().head(10)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub.league_name.value_counts().head(10)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}