{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing Libraries","metadata":{}},{"cell_type":"code","source":"# Pandas \nimport pandas as pd\npd.set_option(\"display.max_columns\", 1000)\npd.set_option(\"display.max_rows\", 100)\npd.options.plotting.backend = \"plotly\"\nfrom pandas.core.common import SettingWithCopyWarning\nimport warnings\nwarnings.simplefilter(action=\"ignore\", category=SettingWithCopyWarning)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-23T13:58:18.585674Z","iopub.execute_input":"2022-10-23T13:58:18.586115Z","iopub.status.idle":"2022-10-23T13:58:18.659137Z","shell.execute_reply.started":"2022-10-23T13:58:18.586029Z","shell.execute_reply":"2022-10-23T13:58:18.658179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotly\nfrom plotly.subplots import make_subplots\nimport plotly.io as pio\nimport plotly.express as px\nimport plotly.graph_objects as go\npio.templates[\"draft\"] = go.layout.Template(\n    layout_annotations=[\n        dict(\n            textangle=-30,\n            opacity=0.1,\n            font=dict(color=\"black\", size=100),\n            xref=\"paper\",\n            yref=\"paper\",\n            x=0.5,\n            y=0.5,\n            showarrow=False,\n        )\n    ]\n)\npio.templates.default = \"draft\"","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:04:32.627690Z","iopub.execute_input":"2022-10-23T14:04:32.628113Z","iopub.status.idle":"2022-10-23T14:04:32.639819Z","shell.execute_reply.started":"2022-10-23T14:04:32.628080Z","shell.execute_reply":"2022-10-23T14:04:32.638676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom datetime import datetime","metadata":{"execution":{"iopub.status.busy":"2022-10-23T13:58:20.000800Z","iopub.execute_input":"2022-10-23T13:58:20.001072Z","iopub.status.idle":"2022-10-23T13:58:20.681146Z","shell.execute_reply.started":"2022-10-23T13:58:20.001047Z","shell.execute_reply":"2022-10-23T13:58:20.680131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb\nimport optuna","metadata":{"execution":{"iopub.status.busy":"2022-10-23T13:58:20.683732Z","iopub.execute_input":"2022-10-23T13:58:20.684320Z","iopub.status.idle":"2022-10-23T13:58:21.396887Z","shell.execute_reply.started":"2022-10-23T13:58:20.684292Z","shell.execute_reply":"2022-10-23T13:58:21.395919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-10-23T13:58:21.398215Z","iopub.execute_input":"2022-10-23T13:58:21.398586Z","iopub.status.idle":"2022-10-23T13:58:21.403302Z","shell.execute_reply.started":"2022-10-23T13:58:21.398520Z","shell.execute_reply":"2022-10-23T13:58:21.402517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading Data","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-10-23T13:58:21.404421Z","iopub.execute_input":"2022-10-23T13:58:21.404902Z","iopub.status.idle":"2022-10-23T13:58:21.415525Z","shell.execute_reply.started":"2022-10-23T13:58:21.404874Z","shell.execute_reply":"2022-10-23T13:58:21.414345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Column Dtypes & Memory Usage","metadata":{}},{"cell_type":"markdown","source":"- In this section we'll see the importance of precising the column dtypes when reading heavy CSV files with Pandas.","metadata":{}},{"cell_type":"code","source":"train_dtypes = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/train_dtypes.csv\")\ntrain_dtypes = dict(train_dtypes.to_records(index=False))","metadata":{"execution":{"iopub.status.busy":"2022-10-23T13:58:21.417047Z","iopub.execute_input":"2022-10-23T13:58:21.417351Z","iopub.status.idle":"2022-10-23T13:58:21.454759Z","shell.execute_reply.started":"2022-10-23T13:58:21.417323Z","shell.execute_reply":"2022-10-23T13:58:21.453648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0_0 = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/train_0.csv\")\ntrain_0_1 = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/train_0.csv\", dtype=train_dtypes)\nmem_usage_0 = round(train_0_0.memory_usage().sum()/(1024**2), 3)\nmem_usage_1 = round(train_0_1.memory_usage().sum()/(1024**2), 3)\nprint(f\"Reading Data Without Precising Dtypes: {mem_usage_0}MB\")\nprint(f\"Reading Data When Precising Dtypes: {mem_usage_1}MB\")\nprint(f\"Memory Usage Reduction: {mem_usage_0 - mem_usage_1}MB -- {round(100*(mem_usage_0-mem_usage_1)/mem_usage_0, 2)}%\")\ndel train_0_0, train_0_1","metadata":{"execution":{"iopub.status.busy":"2022-10-23T13:58:21.455930Z","iopub.execute_input":"2022-10-23T13:58:21.456794Z","iopub.status.idle":"2022-10-23T13:58:57.144244Z","shell.execute_reply.started":"2022-10-23T13:58:21.456752Z","shell.execute_reply":"2022-10-23T13:58:57.142906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Having 10 train files with almost the same size, we can approximate the total <b>memory usage reduction</b> when we precise the column dtypes to <b>~5.6GB</b> !","metadata":{}},{"cell_type":"markdown","source":"## Reading Train Sets","metadata":{}},{"cell_type":"code","source":"df_all_train = pd.DataFrame()\n\nfor i in tqdm(range(10)):\n    train_i = pd.read_csv(f\"/kaggle/input/tabular-playground-series-oct-2022/train_{i}.csv\", dtype=train_dtypes)\n    df_all_train = pd.concat([df_all_train, train_i])\n    del train_i","metadata":{"execution":{"iopub.status.busy":"2022-10-23T13:58:57.146097Z","iopub.execute_input":"2022-10-23T13:58:57.146482Z","iopub.status.idle":"2022-10-23T14:02:47.841654Z","shell.execute_reply.started":"2022-10-23T13:58:57.146451Z","shell.execute_reply":"2022-10-23T14:02:47.840054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Global Train Dataframe Memory Usage: {round(df_all_train.memory_usage().sum()/(1024**3), 3)}GB\")","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:02:47.845787Z","iopub.execute_input":"2022-10-23T14:02:47.846391Z","iopub.status.idle":"2022-10-23T14:02:48.089273Z","shell.execute_reply.started":"2022-10-23T14:02:47.846358Z","shell.execute_reply":"2022-10-23T14:02:48.087671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"nb_events_by_game = df_all_train.groupby(\"game_num\")[\"event_id\"].nunique()\nfig = nb_events_by_game.plot(kind=\"hist\")\nfig.update_layout(xaxis_title=\"Number of Events\", yaxis_title=\"Number of Games\", title=\"Distribution of Events Number by Game\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:02:48.091677Z","iopub.execute_input":"2022-10-23T14:02:48.092503Z","iopub.status.idle":"2022-10-23T14:02:48.961922Z","shell.execute_reply.started":"2022-10-23T14:02:48.092468Z","shell.execute_reply":"2022-10-23T14:02:48.961052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- We observe that the big part of Games have 4 Events.","metadata":{"execution":{"iopub.status.busy":"2022-10-09T18:03:43.831883Z","iopub.execute_input":"2022-10-09T18:03:43.832301Z","iopub.status.idle":"2022-10-09T18:03:43.839565Z","shell.execute_reply.started":"2022-10-09T18:03:43.832269Z","shell.execute_reply":"2022-10-09T18:03:43.837666Z"}}},{"cell_type":"code","source":"# We'll choose a game with 3 events\ngame_num = nb_events_by_game[nb_events_by_game==3].index[0]\ngame_num","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:02:48.963288Z","iopub.execute_input":"2022-10-23T14:02:48.963577Z","iopub.status.idle":"2022-10-23T14:02:48.971455Z","shell.execute_reply.started":"2022-10-23T14:02:48.963535Z","shell.execute_reply":"2022-10-23T14:02:48.970271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Game Use Case Study","metadata":{}},{"cell_type":"code","source":"df_game = df_all_train.loc[df_all_train.game_num==game_num]\ndf_game","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:02:48.972896Z","iopub.execute_input":"2022-10-23T14:02:48.973208Z","iopub.status.idle":"2022-10-23T14:02:50.406083Z","shell.execute_reply.started":"2022-10-23T14:02:48.973185Z","shell.execute_reply":"2022-10-23T14:02:50.404920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for event_id in df_game.event_id.unique():\n    fig = go.Figure()\n\n    fig.add_trace(go.Scatter3d(x=df_game.loc[df_game.event_id==event_id, \"ball_pos_x\"],\n                               y=df_game.loc[df_game.event_id==event_id, \"ball_pos_y\"],\n                               z=df_game.loc[df_game.event_id==event_id, \"ball_pos_z\"]))\n\n    fig.update_layout(title=f\"<b>Ball Trajectory</b> - Game {game_num} | Event {event_id}\")\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:02:50.407771Z","iopub.execute_input":"2022-10-23T14:02:50.408585Z","iopub.status.idle":"2022-10-23T14:02:50.440042Z","shell.execute_reply.started":"2022-10-23T14:02:50.408523Z","shell.execute_reply":"2022-10-23T14:02:50.438761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for event_id in df_game.event_id.unique():\n    event_data = df_game.loc[df_game.event_id==event_id]\n    fig = px.scatter_3d(data_frame=event_data,\n                        x=\"ball_pos_x\",\n                        y=\"ball_pos_y\",\n                        z=\"ball_pos_z\",\n                        range_x=(event_data.ball_pos_x.min(), event_data.ball_pos_x.max()),\n                        range_y=(event_data.ball_pos_y.min(), event_data.ball_pos_y.max()),\n                        range_z=(event_data.ball_pos_z.min(), event_data.ball_pos_z.max()),\n                        animation_frame=\"event_time\")\n    fig.layout.updatemenus[0].buttons[0].args[1][\"transition\"][\"duration\"] = 0.0001\n    fig.layout.updatemenus[0].buttons[0].args[1][\"frame\"][\"duration\"] = 0.0001\n    fig.update_layout(title=f\"<b>Simulation of Ball Movement During Time</b> - Game {game_num} | Event {event_id}\",\n                      scene = dict(\n                        xaxis = dict(\n                             backgroundcolor=\"rgb(200, 200, 230)\",\n                             gridcolor=\"white\",\n                             showbackground=True,\n                             zerolinecolor=\"white\",),\n                        yaxis = dict(\n                            backgroundcolor=\"rgb(230, 200,230)\",\n                            gridcolor=\"white\",\n                            showbackground=True,\n                            zerolinecolor=\"white\"),\n                        zaxis = dict(\n                            backgroundcolor=\"rgb(230, 230,200)\",\n                            gridcolor=\"white\",\n                            showbackground=True,\n                            zerolinecolor=\"white\",),))\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:02:50.442342Z","iopub.execute_input":"2022-10-23T14:02:50.442720Z","iopub.status.idle":"2022-10-23T14:03:03.377488Z","shell.execute_reply.started":"2022-10-23T14:02:50.442692Z","shell.execute_reply":"2022-10-23T14:03:03.376103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df_game[[\"event_id\", \"ball_pos_x\", \"ball_pos_y\", \"ball_pos_z\", \"player_scoring_next\", \"team_scoring_next\", \"team_A_scoring_within_10sec\", \"team_B_scoring_within_10sec\"]].copy()\ndf[\"goal_within_10sec\"] = df[\"team_A_scoring_within_10sec\"] + df[\"team_B_scoring_within_10sec\"]\ndf[\"goal_within_10sec\"] = df[\"goal_within_10sec\"].apply(lambda x:\"YES\" if x else \"NO\")","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:03:03.378973Z","iopub.execute_input":"2022-10-23T14:03:03.379265Z","iopub.status.idle":"2022-10-23T14:03:03.389757Z","shell.execute_reply.started":"2022-10-23T14:03:03.379238Z","shell.execute_reply":"2022-10-23T14:03:03.389026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter(df, x=\"ball_pos_x\", y=\"ball_pos_y\", facet_col=\"event_id\", color=\"goal_within_10sec\")\nfig.update_layout(title=f\"Ball Position in Stadium -- Game {game_num}\",\n                  legend=dict(orientation=\"h\", x=0.4, y=-0.2))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:03:03.391072Z","iopub.execute_input":"2022-10-23T14:03:03.391345Z","iopub.status.idle":"2022-10-23T14:03:03.736529Z","shell.execute_reply.started":"2022-10-23T14:03:03.391320Z","shell.execute_reply":"2022-10-23T14:03:03.735465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- The following functions takes as input data, a game number and an event id. It plots the Ball and Players Movements in 3 Dimensions.\n- The function considers the sceanrio of the event: it plots the players of the team that is going to score in the event, if the event ends with no goal the function plots all players.\n- We'll use this function to plot all events of a given game.","metadata":{}},{"cell_type":"code","source":"def plot_event(df_all_train, game_num, event_id):\n    \n    df_event = df_all_train.loc[(df_all_train.game_num==game_num) & (df_all_train.event_id==event_id)].reset_index(drop=True)\n    \n    # If no teams score we'll plot all players\n    if df_game.team_scoring_next.unique() == \"A\":\n        team_scoring = \"Team A\"\n        players = [0, 1, 2]\n    elif df_game.team_scoring_next.unique() == \"B\":\n        team_scoring = \"Team B\"\n        players = [3, 4, 5]\n    else:\n        team_scoring = \"All Players\"\n        players = [0, 1, 2, 3, 4, 5]\n        \n        \n    fig = go.Figure()\n\n    fig.add_trace(go.Scatter3d(x=[df_event[\"ball_pos_x\"][0]],\n                               y=[df_event[\"ball_pos_y\"][0]],\n                               z=[df_event[\"ball_pos_z\"][0]],\n                               name=\"Ball\"\n                               ))\n\n    for i in players:\n        fig.add_trace(go.Scatter3d(x=[df_event[f\"p{i}_pos_x\"][0]],\n                                   y=[df_event[f\"p{i}_pos_y\"][0]],\n                                   z=[df_event[f\"p{i}_pos_z\"][0]],\n                                   name=f\"Player {i}\"\n                                   ))\n\n    n_frames = len(df_event)\n\n    frames = []\n\n    for k in range(n_frames):\n        frames.append(go.Frame(data=[\n                                     go.Scatter3d(x=[df_event[\"ball_pos_x\"][k]], y=[df_event[\"ball_pos_y\"][k]], z=[df_event[\"ball_pos_z\"][k]]),\n                                     *[go.Scatter3d(x=[df_event[f\"p{i}_pos_x\"][k]], y=[df_event[f\"p{i}_pos_y\"][k]], z=[df_event[f\"p{i}_pos_z\"][k]]) for i in players]\n                                    ],\n                                traces=[0, *[i for i in range(1, len(players)+1)]],\n                                name=f\"fr{k}\"))      \n\n\n    fig.update(frames=frames)\n\n\n    def frame_args(duration):\n        return {\n                \"frame\": {\"duration\": duration},\n                \"mode\": \"immediate\",\n                \"fromcurrent\": True,\n                \"transition\": {\"duration\": duration, \"easing\": \"linear\"},\n                }\n\n    updatemenus = [dict(\n            buttons = [{\n                        \"args\": [None, frame_args(5)],\n                        \"label\": \"Play\", \n                        \"method\": \"animate\",\n                        },\n                        {\n                        \"args\": [[None], frame_args(0)],\n                        \"label\": \"Pause\", \n                        \"method\": \"animate\",\n                      }],\n            direction = \"left\",\n            pad = {\"r\": 10, \"t\": 87},\n            showactive = False,\n            type = \"buttons\",\n            x = 0.1,\n            xanchor = \"right\",\n            y = -0.1,\n            yanchor = \"top\"\n        )]  \n\n\n\n    frame_duration=5\n    sliders = [\n                {\n                    \"pad\": {\"b\": 10, \"t\": 50},\n                    \"len\": 0.9,\n                    \"x\": 0.1,\n                    \"y\": 0,\n                    \"steps\": [\n                        {\n                            \"args\": [[f.name], frame_args(frame_duration)],\n                            \"label\": str(df_event[\"event_time\"][k]),\n                            \"method\": \"animate\",\n                        }\n                        for k, f in enumerate(fig.frames)\n                    ],\n                    \"currentvalue\": dict(font=dict(size=15), \n                                      prefix='Event Time: ', \n                                      visible=True, \n                                      xanchor= 'center'\n                                     )\n                }\n    ]\n\n\n\n    # Define Animations Scene Coordinates\n    x_min = min([df_event.ball_pos_x.min(), *[df_event[f\"p{i}_pos_x\"].min() for i in players]])\n    x_max = max([df_event.ball_pos_x.max(), *[df_event[f\"p{i}_pos_x\"].max() for i in players]])\n    y_min = min([df_event.ball_pos_y.min(), *[df_event[f\"p{i}_pos_y\"].min() for i in players]])\n    y_max = max([df_event.ball_pos_y.max(), *[df_event[f\"p{i}_pos_y\"].max() for i in players]])\n    z_min = min([df_event.ball_pos_z.min(), *[df_event[f\"p{i}_pos_z\"].min() for i in players]])\n    z_max = max([df_event.ball_pos_z.max(), *[df_event[f\"p{i}_pos_z\"].max() for i in players]])\n\n    h=0.25\n    fig.update_layout(\n                      title=f\"<b>Animation of Event {event_id} From Game {game_num} - {team_scoring}\",\n                      width=1200,\n                      height=800,  \n                      scene=dict(\n                                  xaxis=dict(range=[x_min-h, x_max+h],\n                                             backgroundcolor=\"rgb(200, 200, 230)\",\n                                             gridcolor=\"white\",\n                                             showbackground=True,\n                                             zerolinecolor=\"white\"),\n                                  yaxis=dict(range=[y_min-h, y_max+h],\n                                             backgroundcolor=\"rgb(230, 200,230)\",\n                                             gridcolor=\"white\",\n                                             showbackground=True,\n                                             zerolinecolor=\"white\"),\n                                  zaxis=dict(range=[z_min-h, z_max+h],\n                                             backgroundcolor=\"rgb(230, 230,200)\",\n                                             gridcolor=\"white\",\n                                             showbackground=True,\n                                             zerolinecolor=\"white\"),\n                                  camera=dict(eye=dict(x=1.25, y=1.55, z=1))\n                      ),\n                      updatemenus=updatemenus,\n                      sliders=sliders,\n                      legend=dict(orientation=\"h\", x=0.4, y=-0.3)\n                     )\n\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:03:03.738517Z","iopub.execute_input":"2022-10-23T14:03:03.739233Z","iopub.status.idle":"2022-10-23T14:03:03.763903Z","shell.execute_reply.started":"2022-10-23T14:03:03.739197Z","shell.execute_reply":"2022-10-23T14:03:03.763136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for event_id in df_all_train.loc[df_all_train.game_num == game_num, \"event_id\"].unique():\n    plot_event(df_all_train, game_num, event_id)","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:03:03.765205Z","iopub.execute_input":"2022-10-23T14:03:03.766482Z","iopub.status.idle":"2022-10-23T14:03:10.325972Z","shell.execute_reply.started":"2022-10-23T14:03:03.766445Z","shell.execute_reply":"2022-10-23T14:03:10.324635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Player Use Case Study","metadata":{"execution":{"iopub.status.busy":"2022-10-15T12:31:50.958997Z","iopub.execute_input":"2022-10-15T12:31:50.960169Z","iopub.status.idle":"2022-10-15T12:31:50.983201Z","shell.execute_reply.started":"2022-10-15T12:31:50.960062Z","shell.execute_reply":"2022-10-15T12:31:50.981623Z"}}},{"cell_type":"code","source":"fig = df_all_train.loc[df_all_train.player_scoring_next!=-1].groupby([\"game_num\"])[\"player_scoring_next\"].nunique().plot(kind=\"hist\")\nfig.update_layout(xaxis_title=\"Number Of Players Scoring in a Game\", yaxis_title=\"Number of Games\", title=\"Players Scoring In Games\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:03:10.327542Z","iopub.execute_input":"2022-10-23T14:03:10.327993Z","iopub.status.idle":"2022-10-23T14:03:15.782232Z","shell.execute_reply.started":"2022-10-23T14:03:10.327962Z","shell.execute_reply":"2022-10-23T14:03:15.781234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_game[\"team_scoring_next\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:03:15.783587Z","iopub.execute_input":"2022-10-23T14:03:15.784319Z","iopub.status.idle":"2022-10-23T14:03:15.791594Z","shell.execute_reply.started":"2022-10-23T14:03:15.784284Z","shell.execute_reply":"2022-10-23T14:03:15.790524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- In the example game (Game 3), Team A is the only teams that is going to score.","metadata":{}},{"cell_type":"code","source":"for player in [0, 1, 2]:\n    # For Plotly Use only, we'll ser dtype to np.float32 for some columns of the game dataframe\n    df_game[f\"p{player}_boost\"] = df_game[f\"p{player}_boost\"].astype(np.float32)\n    fig = px.area(df_game, x=f\"event_time\", y=f\"p{player}_boost\", color=\"team_A_scoring_within_10sec\", facet_col=\"event_id\")\n    for i in range(len(fig['data'])):\n        fig['data'][i]['line']['width']=0\n    fig.update_layout(title=f\"<b>Player {player} Boost Over Time - Game {game_num}</b>\")\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:03:15.792802Z","iopub.execute_input":"2022-10-23T14:03:15.793141Z","iopub.status.idle":"2022-10-23T14:03:16.067234Z","shell.execute_reply.started":"2022-10-23T14:03:15.793108Z","shell.execute_reply":"2022-10-23T14:03:16.066271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- In the Data Description: <i>All p{i} columns will be NaN if and only if the player is demolished (destroyed by an enemy player; will respawn within a few seconds)</i>.<br>\n==> The parts in plots without p{i}_boost values (i.e: without plotted area) match the times where player was demolished.","metadata":{}},{"cell_type":"markdown","source":"# Modeling ","metadata":{}},{"cell_type":"markdown","source":"## Goals Scored Distributions.","metadata":{}},{"cell_type":"code","source":"print(f\"Number of Games in Data: {df_all_train.game_num.nunique()}\")\nprint(f\"Number of Events in Data: {df_all_train.event_id.nunique()}\")","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:03:16.086989Z","iopub.execute_input":"2022-10-23T14:03:16.087903Z","iopub.status.idle":"2022-10-23T14:03:16.227614Z","shell.execute_reply.started":"2022-10-23T14:03:16.087858Z","shell.execute_reply":"2022-10-23T14:03:16.226614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In the train data, Every event has one out of 3 possible scenarios:\n- Team A is goign to score.\n- Team B is going to score.\n- None of them is going to score.","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:03:20.579511Z","iopub.execute_input":"2022-10-23T14:03:20.579999Z","iopub.status.idle":"2022-10-23T14:03:20.585831Z","shell.execute_reply.started":"2022-10-23T14:03:20.579972Z","shell.execute_reply":"2022-10-23T14:03:20.584623Z"}}},{"cell_type":"code","source":"events_stats = df_all_train[[\"game_num\", \"event_id\", \"team_scoring_next\"]]\nevents_stats.loc[pd.isnull(events_stats.team_scoring_next), \"team_scoring_next\"] = \"None\"\nevents_stats = events_stats.drop_duplicates()\n# Plot\nfig = go.Figure()\n\nfig.add_trace(go.Pie(labels=events_stats[\"team_scoring_next\"].value_counts().index,\n                     values=events_stats[\"team_scoring_next\"].value_counts().values,\n                     marker=dict(line=dict(width=2.5)), textfont_size=15,\n                     hole=.40, hovertemplate=\"%{label}: %{value}<extra></extra>\"))\n\nfig.update_layout(title=\"<b>Distribution of Events By Scoring Team</b>\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:04:03.512407Z","iopub.execute_input":"2022-10-23T14:04:03.512811Z","iopub.status.idle":"2022-10-23T14:04:07.156742Z","shell.execute_reply.started":"2022-10-23T14:04:03.512780Z","shell.execute_reply":"2022-10-23T14:04:07.155780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_stats = df_all_train[[\"game_num\", \"event_id\", \"player_scoring_next\"]]\nplayer_stats[\"player_scoring_next\"] = player_stats[\"player_scoring_next\"]\nplayer_stats.loc[player_stats.player_scoring_next==-1, \"player_scoring_next\"] = \"None\"\nplayer_stats = player_stats.drop_duplicates()\n# Plot\nfig = go.Figure()\n\n\nfig.add_trace(go.Pie(labels=player_stats[\"player_scoring_next\"].value_counts().index,\n                     values=player_stats[\"player_scoring_next\"].value_counts().values,\n                     marker=dict(line=dict(width=2.5)), textfont_size=15,\n                     hole=.40, hovertemplate=\"%{label}: %{value}<extra></extra>\"))\n\nfig.update_layout(title=\"<b>Distirbution of Events by Scoring Player</b>\", yaxis_title=\"Events Count\", xaxis_title=\"Player Scoring\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:04:07.158290Z","iopub.execute_input":"2022-10-23T14:04:07.158624Z","iopub.status.idle":"2022-10-23T14:04:09.483054Z","shell.execute_reply.started":"2022-10-23T14:04:07.158596Z","shell.execute_reply":"2022-10-23T14:04:09.482109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Target Variables","metadata":{}},{"cell_type":"markdown","source":"target_variables are:\n\n- \"team_A_scoring_within_10sec\"\n- \"team_B_scoring_within_10sec\"","metadata":{}},{"cell_type":"code","source":"target_variables = [\"team_A_scoring_within_10sec\", \"team_B_scoring_within_10sec\"]","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:04:09.484167Z","iopub.execute_input":"2022-10-23T14:04:09.484434Z","iopub.status.idle":"2022-10-23T14:04:09.489903Z","shell.execute_reply.started":"2022-10-23T14:04:09.484411Z","shell.execute_reply":"2022-10-23T14:04:09.488044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]], subplot_titles=target_variables)\n\nfor i, col in enumerate([\"team_A_scoring_within_10sec\", \"team_B_scoring_within_10sec\"]):\n    fig.add_trace(go.Pie(labels=df_all_train[col].value_counts().index,\n                         values=df_all_train[col].value_counts().values,\n                         marker=dict(line=dict(width=2.5)), textfont_size=15,\n                         hole=.40, hovertemplate=\"%{label}: %{value}<extra></extra>\"), 1, i+1)\n\nfig.update_layout(title=\"<b>Target Variables Distribution</b>\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:04:38.775479Z","iopub.execute_input":"2022-10-23T14:04:38.775914Z","iopub.status.idle":"2022-10-23T14:04:39.325849Z","shell.execute_reply.started":"2022-10-23T14:04:38.775873Z","shell.execute_reply":"2022-10-23T14:04:39.324892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Both target variables have very similar distirbutions.","metadata":{}},{"cell_type":"code","source":"# Next:\n# - Features Engineering & Analysis\n# - Modeling: LightGBM","metadata":{"execution":{"iopub.status.busy":"2022-10-23T14:03:16.068330Z","iopub.execute_input":"2022-10-23T14:03:16.068608Z","iopub.status.idle":"2022-10-23T14:03:16.072750Z","shell.execute_reply.started":"2022-10-23T14:03:16.068584Z","shell.execute_reply":"2022-10-23T14:03:16.071800Z"},"trusted":true},"execution_count":null,"outputs":[]}]}