{"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","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-10-05T12:33:25.593310Z","iopub.execute_input":"2022-10-05T12:33:25.593857Z","iopub.status.idle":"2022-10-05T12:33:25.637104Z","shell.execute_reply.started":"2022-10-05T12:33:25.593776Z","shell.execute_reply":"2022-10-05T12:33:25.636454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Library import","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-10-05T12:33:35.003531Z","iopub.execute_input":"2022-10-05T12:33:35.003894Z","iopub.status.idle":"2022-10-05T12:33:35.707035Z","shell.execute_reply.started":"2022-10-05T12:33:35.003868Z","shell.execute_reply":"2022-10-05T12:33:35.705920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview\nBall and player motion in train0 were checked.","metadata":{}},{"cell_type":"markdown","source":"# Data Loading","metadata":{}},{"cell_type":"code","source":"dtypes_df = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_dtypes.csv')\ndtypes = {k: v for (k, v) in zip(dtypes_df.column, dtypes_df.dtype)}\ntrain0_df = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_0.csv', dtype=dtypes)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T12:33:41.489029Z","iopub.execute_input":"2022-10-05T12:33:41.489414Z","iopub.status.idle":"2022-10-05T12:34:08.043644Z","shell.execute_reply.started":"2022-10-05T12:33:41.489369Z","shell.execute_reply":"2022-10-05T12:34:08.042363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T12:34:08.045543Z","iopub.execute_input":"2022-10-05T12:34:08.046683Z","iopub.status.idle":"2022-10-05T12:34:08.089033Z","shell.execute_reply.started":"2022-10-05T12:34:08.046614Z","shell.execute_reply":"2022-10-05T12:34:08.087022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# What size is the field of train0 data?","metadata":{}},{"cell_type":"code","source":"temp = train0_df\nfig = plt.figure(figsize=(12,12))\nfig.suptitle('ball x-y-z area in train0', fontsize =16)\nplt.subplots_adjust(wspace=0.4, hspace=0.3)\nplt.subplot(2, 2, 1)  \nplt.scatter(temp['ball_pos_x'],temp['ball_pos_y'])\nplt.title('ball_pos x_y')\nplt.xlabel('ball_pos_x')\nplt.ylabel('ball_pos_y')\nplt.xlim(-110,110)\nplt.ylim(-110,110)\n\nplt.subplot(2, 2, 2)  \nplt.scatter(temp['ball_pos_z'],temp['ball_pos_y'])\nplt.title('ball_pos z_y')\nplt.xlabel('ball_pos_z')\nplt.ylabel('ball_pos_y')\nplt.xlim(-110,110)\nplt.ylim(-110,110)\n\nplt.subplot(2, 2, 3)  \nplt.scatter(temp['ball_pos_x'],temp['ball_pos_z'])\nplt.title('ball_pos x_z')\nplt.xlabel('ball_pos_x')\nplt.ylabel('ball_pos_z')\nplt.xlim(-110,110)\nplt.ylim(-110,110)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-10-05T12:34:08.092697Z","iopub.execute_input":"2022-10-05T12:34:08.093832Z","iopub.status.idle":"2022-10-05T12:34:23.594909Z","shell.execute_reply.started":"2022-10-05T12:34:08.093697Z","shell.execute_reply":"2022-10-05T12:34:23.593614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# motion of the ball and players in event1002(B_team get score)","metadata":{}},{"cell_type":"code","source":"ball_pos = [c for c in train0_df.columns if 'ball_pos' in c]\np0_pos = [c for c in train0_df.columns if 'p0_pos' in c]\np1_pos = [c for c in train0_df.columns if 'p1_pos' in c]\np2_pos = [c for c in train0_df.columns if 'p2_pos' in c]\np3_pos = [c for c in train0_df.columns if 'p3_pos' in c]\np4_pos = [c for c in train0_df.columns if 'p4_pos' in c]\np5_pos = [c for c in train0_df.columns if 'p5_pos' in c]\npos_list =[ball_pos, p0_pos, p1_pos, p2_pos, p3_pos, p4_pos, p5_pos]\nplayer_list = ['ball', 'p0', 'p1', 'p2', 'p3', 'p4', 'p5']","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-10-05T12:34:28.326743Z","iopub.execute_input":"2022-10-05T12:34:28.327109Z","iopub.status.idle":"2022-10-05T12:34:28.335064Z","shell.execute_reply.started":"2022-10-05T12:34:28.327083Z","shell.execute_reply":"2022-10-05T12:34:28.333992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.graph_objects as go\n#x,y,zの座標のdataframeを作ってみる。\ntemp = train0_df[train0_df['event_id']==1002]\ntemp['event_time'].unique()\npos_df = pd.DataFrame(columns=['time', 'x', 'y', 'z', 'player'])\n\nfor time in temp['event_time'].unique():\n    for i, (pos, player) in enumerate(zip(pos_list, player_list)):\n        temp2 = temp[temp['event_time']==time][['event_time'] + pos].set_axis(['time', 'x', 'y', 'z'], axis='columns')\n        temp2['player']=player\n        pos_df = pd.concat([pos_df, temp2])\n# 配列の要素にプロット内容を格納\nplot=[]\nfor time in pos_df['time'].unique():\n    temp = pos_df[pos_df['time']==time]\n    plot.append(go.Scatter(x=temp['x'], y=temp['y'], mode = 'markers'))\n\n\n# stepも同時に作ってみる。\nplot=[]\nsteps = []\n\nfor i, time in enumerate(pos_df['time'].unique()):\n    temp = pos_df[pos_df['time']==time]\n    plot.append(go.Scatter(x=temp['x'], \n                         y=temp['y'], \n                         mode = 'markers', \n                         marker = dict(color=['black', 'blue', 'blue', 'blue', 'red', 'red', 'red'], \n                                       size=[10, 10, 10, 10, 10, 10, 10]))\n               )\n\n    false_list =[False]*len(pos_df['time'].unique())\n    false_list[i] = True\n    step = dict(\n              #label='ball',  # スライダーのラベル\n              method='update',  # スライダーの適用範囲はデータプロットとレイアウト\n              args=[\n                     dict(visible=false_list),  # y0のみ表示\n                     #dict(title='ball'),  # グラフタイトル\n                     ]\n               )\n    steps.append(step)\n\nsliders = [\n    dict(\n        active=0,  # 初期状態で表示するプロットのインデックス\n        # 各スライダーの傾きをスライダーの上に表示\n        currentvalue=dict(prefix='time'),\n        steps=steps,\n    )\n]\n\n# レイアウトの作成\nlayout = go.Layout(\n    title='x-y motion of the ball and the players in event 1002',\n    autosize=False,\n    width=600,\n    height=600,\n    font_size=10,  # グラフ全体のフォントサイズ\n    hoverlabel_font_size=10,  # ホバーのフォントサイズ\n    xaxis=dict(title='pos_x'),\n    yaxis=dict(title='pos_y'),\n    xaxis_range=(-100, 100),  # 縦軸の表示範囲    \n    yaxis_range=(-100, 100),  # 縦軸の表示範囲\n    sliders=sliders  # スライダーを設置\n)\n# グラフの表示\nfig = go.Figure(data=plot, layout=layout)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-10-05T12:34:30.603466Z","iopub.execute_input":"2022-10-05T12:34:30.604962Z","iopub.status.idle":"2022-10-05T12:34:35.944950Z","shell.execute_reply.started":"2022-10-05T12:34:30.604919Z","shell.execute_reply":"2022-10-05T12:34:35.943694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#3d\nplot=[]\nsteps = []\n\nfor i, time in enumerate(pos_df['time'].unique()):\n    temp = pos_df[pos_df['time']==time]\n    plot.append(go.Scatter3d(x=temp['x'], \n                         y=temp['y'], \n                         z=temp['z'],\n                         mode = 'markers', \n                         marker = dict(color=['black', 'blue', 'blue', 'blue', 'red', 'red', 'red'], \n                                       size=[10, 10, 10, 10, 10, 10, 10]))\n               )\n\n    false_list =[False]*len(pos_df['time'].unique())\n    false_list[i] = True\n    step = dict(\n              #label='ball',  # スライダーのラベル\n              method='update',  # スライダーの適用範囲はデータプロットとレイアウト\n              args=[\n                     dict(visible=false_list),  # y0のみ表示\n                     #dict(title='ball'),  # グラフタイトル\n                     ]\n               )\n    steps.append(step)\nsliders = [\n    dict(\n        active=0,  # 初期状態で表示するプロットのインデックス\n        # 各スライダーの傾きをスライダーの上に表示\n        currentvalue=dict(prefix='time'),\n        steps=steps,\n    )\n]\n\n# グラフの表示\nfig = go.Figure(data=plot)\n\n\nfig.update_layout(\n    title=dict(text='3d motion of the ball and the players in event 1002'),\n    autosize=False,\n    scene = dict(\n        xaxis = dict(nticks=4, range=[-100,100],),\n                     yaxis = dict(nticks=4, range=[-100,100],),\n                     zaxis = dict(nticks=4, range=[0,50],),),\n    width=700,\n    margin=dict(r=20, l=10, b=10, t=10),\n    sliders=sliders)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-10-05T12:34:35.946919Z","iopub.execute_input":"2022-10-05T12:34:35.948259Z","iopub.status.idle":"2022-10-05T12:34:36.834291Z","shell.execute_reply.started":"2022-10-05T12:34:35.948206Z","shell.execute_reply":"2022-10-05T12:34:36.832907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"event1002: Bteam get score   \nblack: ball   \nblue: A_team   \nred: B_team   ","metadata":{}},{"cell_type":"markdown","source":"# motion of the ball and players in event2001(A_team get score)","metadata":{}},{"cell_type":"code","source":"#A_team get score event\ntemp = train0_df[train0_df['event_id']==2001]\ntemp['event_time'].unique()\npos_df = pd.DataFrame(columns=['time', 'x', 'y', 'z', 'player'])\n\nfor time in temp['event_time'].unique():\n    for i, (pos, player) in enumerate(zip(pos_list, player_list)):\n        temp2 = temp[temp['event_time']==time][['event_time'] + pos].set_axis(['time', 'x', 'y', 'z'], axis='columns')\n        temp2['player']=player\n        pos_df = pd.concat([pos_df, temp2])\nplot=[]\nfor time in pos_df['time'].unique():\n    temp = pos_df[pos_df['time']==time]\n    plot.append(go.Scatter(x=temp['x'], y=temp['y'], mode = 'markers'))\n\n\n# stepも同時に作ってみる。\nplot=[]\nsteps = []\n\nfor i, time in enumerate(pos_df['time'].unique()):\n    temp = pos_df[pos_df['time']==time]\n    plot.append(go.Scatter(x=temp['x'], \n                         y=temp['y'], \n                         mode = 'markers', \n                         marker = dict(color=['black', 'blue', 'blue', 'blue', 'red', 'red', 'red'], \n                                       size=[10, 10, 10, 10, 10, 10, 10]))\n               )\n    false_list =[False]*len(pos_df['time'].unique())\n    false_list[i] = True\n    step = dict(\n              #label='ball',  # スライダーのラベル\n              method='update',  # スライダーの適用範囲はデータプロットとレイアウト\n              args=[\n                     dict(visible=false_list),  # y0のみ表示\n                     #dict(title='ball'),  # グラフタイトル\n                     ]\n               )\n    steps.append(step)\n\nsliders = [\n    dict(\n        active=0,  # 初期状態で表示するプロットのインデックス\n        # 各スライダーの傾きをスライダーの上に表示\n        currentvalue=dict(prefix='time'),\n        steps=steps,\n    )\n]\n# レイアウトの作成\nlayout = go.Layout(\n    title='x-y motion of the ball and the players in event 2001',\n    autosize=False,\n    width=600,\n    height=600,\n    font_size=10,  # グラフ全体のフォントサイズ\n    hoverlabel_font_size=10,  # ホバーのフォントサイズ\n    xaxis=dict(title='pos_x'),\n    yaxis=dict(title='pos_y'),\n    xaxis_range=(-100, 100),  # 縦軸の表示範囲    \n    yaxis_range=(-100, 100),  # 縦軸の表示範囲\n    sliders=sliders  # スライダーを設置\n)\n# グラフの表示\nfig = go.Figure(data=plot, layout=layout)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-10-05T12:34:36.836156Z","iopub.execute_input":"2022-10-05T12:34:36.836510Z","iopub.status.idle":"2022-10-05T12:34:49.921073Z","shell.execute_reply.started":"2022-10-05T12:34:36.836486Z","shell.execute_reply":"2022-10-05T12:34:49.918374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#3d\nplot=[]\nsteps = []\n\nfor i, time in enumerate(pos_df['time'].unique()):\n    temp = pos_df[pos_df['time']==time]\n    plot.append(go.Scatter3d(x=temp['x'], \n                         y=temp['y'], \n                         z=temp['z'],\n                         mode = 'markers', \n                         marker = dict(color=['black', 'blue', 'blue', 'blue', 'red', 'red', 'red'], \n                                       size=[10, 10, 10, 10, 10, 10, 10]))\n               )\n\n    false_list =[False]*len(pos_df['time'].unique())\n    false_list[i] = True\n    step = dict(\n              #label='ball',  # スライダーのラベル\n              method='update',  # スライダーの適用範囲はデータプロットとレイアウト\n              args=[\n                     dict(visible=false_list),  # y0のみ表示\n                     #dict(title='ball'),  # グラフタイトル\n                     ]\n               )\n    steps.append(step)\nsliders = [\n    dict(\n        active=0,  # 初期状態で表示するプロットのインデックス\n        # 各スライダーの傾きをスライダーの上に表示\n        currentvalue=dict(prefix='time'),\n        steps=steps,\n    )\n]\n\n# グラフの表示\nfig = go.Figure(data=plot)\n\n\nfig.update_layout(\n    title=dict(text='3d motion of the ball and the players in event 2001'),\n    autosize=False,\n    scene = dict(\n        xaxis = dict(nticks=4, range=[-100,100],),\n                     yaxis = dict(nticks=4, range=[-100,100],),\n                     zaxis = dict(nticks=4, range=[0,50],),),\n    width=700,\n    margin=dict(r=20, l=10, b=10, t=10),\n    sliders=sliders)\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-10-05T12:34:49.923936Z","iopub.execute_input":"2022-10-05T12:34:49.924313Z","iopub.status.idle":"2022-10-05T12:34:52.290215Z","shell.execute_reply.started":"2022-10-05T12:34:49.924287Z","shell.execute_reply":"2022-10-05T12:34:52.288707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"event2001: A_team get score   \nblack: ball   \nblue: A_team   \nred: B_team  ","metadata":{}},{"cell_type":"markdown","source":"# heatmap of ball x-y position when A and B team scoring within 10s\nI am inspired by this [notebook](https://www.kaggle.com/code/mattop/rocket-league-tps-eda)","metadata":{}},{"cell_type":"code","source":"a10 = train0_df[train0_df['team_A_scoring_within_10sec']==1]\nb10 = train0_df[train0_df['team_B_scoring_within_10sec']==1]","metadata":{"execution":{"iopub.status.busy":"2022-10-05T12:37:04.625824Z","iopub.execute_input":"2022-10-05T12:37:04.626992Z","iopub.status.idle":"2022-10-05T12:37:04.717389Z","shell.execute_reply.started":"2022-10-05T12:37:04.626905Z","shell.execute_reply":"2022-10-05T12:37:04.716261Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n\nfig = px.density_heatmap(a10,\n                          x=\"ball_pos_x\",\n                          y=\"ball_pos_y\",\n                          nbinsx = 200,\n                          nbinsy = 150,\n                          color_continuous_scale = \"Portland\",\n                          width =600,\n                          height = 600,\n                          range_color=(0, 15))\nfig.update_layout(title='heatmap of ball x-y position when A team scoring within 10s',\n                  template = \"plotly_dark\", \n                  font = dict(family = \"PT Sans\", size = 12, color = \"#FFFFFF\"))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T12:37:07.017553Z","iopub.execute_input":"2022-10-05T12:37:07.017912Z","iopub.status.idle":"2022-10-05T12:37:09.908054Z","shell.execute_reply.started":"2022-10-05T12:37:07.017878Z","shell.execute_reply":"2022-10-05T12:37:09.906901Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.density_heatmap(b10,\n                          x=\"ball_pos_x\",\n                          y=\"ball_pos_y\",\n                          nbinsx = 200,\n                          nbinsy = 150,\n                          color_continuous_scale = \"Portland\",\n                          width =600,\n                          height = 600,\n                          range_color=(0, 15))\nfig.update_layout(title='heatmap of ball x-y position when B team scoring within 10s',\n                  template = \"plotly_dark\", \n                  font = dict(family = \"PT Sans\", size = 12, color = \"#FFFFFF\"))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T12:37:29.582106Z","iopub.execute_input":"2022-10-05T12:37:29.582453Z","iopub.status.idle":"2022-10-05T12:37:29.698980Z","shell.execute_reply.started":"2022-10-05T12:37:29.582391Z","shell.execute_reply":"2022-10-05T12:37:29.697027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# heatmap of A team member x-y position when A team scoring within 10s","metadata":{}},{"cell_type":"code","source":"a10_A = a10[['p0_pos_x', 'p0_pos_y']].set_axis(['x', 'y'], axis='columns')\na10_A = pd.concat([a10_A, a10[['p1_pos_x', 'p1_pos_y']].set_axis(['x', 'y'], axis='columns')])\na10_A = pd.concat([a10_A, a10[['p2_pos_x', 'p2_pos_y']].set_axis(['x', 'y'], axis='columns')])\nfig = px.density_heatmap(a10_A,\n                          x=\"x\",\n                          y=\"y\",\n                          nbinsx = 200,\n                          nbinsy = 150,\n                          color_continuous_scale = \"Portland\",\n                          width =600,\n                          height = 600,\n                          range_color=(0, 45))\nfig.update_layout(title='heatmap of A team member x-y position when A team scoring within 10s',\n                  template = \"plotly_dark\", \n                  font = dict(family = \"PT Sans\", size = 12, color = \"#FFFFFF\"))\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T12:48:42.243911Z","iopub.execute_input":"2022-10-05T12:48:42.244796Z","iopub.status.idle":"2022-10-05T12:48:42.458696Z","shell.execute_reply.started":"2022-10-05T12:48:42.244764Z","shell.execute_reply":"2022-10-05T12:48:42.457365Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]}]}