{"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":"# TPS_OCT_2022_EDA_LGBM_part1🥅🚀","metadata":{}},{"cell_type":"markdown","source":"* This month, Taubler Playground Series is \"Rocket Regue\" data.\n* This game is a vechiculer(\"Rocket-powered🚀\"cars) soccer video game. Developed and published by Psyonix.\n* This game is using Unreal Engine 3 and also using Physics engine(gravity, collision detection and rotation).\n* Matches are usually five minutes long with a sudden death overtime if the game is tied at that point.\n* This data is very large data size. And it is diviede into 10 parts.\n\n> * reference\n* https://en.wikipedia.org/wiki/Rocket_League\n* https://en.wikipedia.org/wiki/Physics_engine\n* https://en.wikipedia.org/wiki/Velocity\n* https://en.wikipedia.org/wiki/Euclidean_vector\n","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set(style = 'darkgrid', palette = 'muted')\n%matplotlib inline\n\nfrom tqdm.notebook import tqdm\n\nfrom lightgbm import LGBMClassifier\nimport lightgbm as lgb\nimport optuna\n\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split, KFold, StratifiedKFold\nfrom sklearn.metrics import log_loss, accuracy_score\n\nimport 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-23T04:33:34.239168Z","iopub.execute_input":"2022-10-23T04:33:34.239689Z","iopub.status.idle":"2022-10-23T04:33:36.287020Z","shell.execute_reply.started":"2022-10-23T04:33:34.239593Z","shell.execute_reply":"2022-10-23T04:33:36.285947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0= pd.read_csv('../input/tabular-playground-series-oct-2022/train_0.csv')\n#train_1 = pd.read_csv('../input/tabular-playground-series-oct-2022/train_1.csv')\n#train_2 = pd.read_csv('../input/tabular-playground-series-oct-2022/train_2.csv')\n#train_3 = pd.read_csv('../input/tabular-playground-series-oct-2022/train_3.csv')\n#train_4 = pd.read_csv('../input/tabular-playground-series-oct-2022/train_4.csv')\n#train_5 = pd.read_csv('../input/tabular-playground-series-oct-2022/train_5.csv')\n#train_6 = pd.read_csv('../input/tabular-playground-series-oct-2022/train_6.csv')\n#train_7 = pd.read_csv('../input/tabular-playground-series-oct-2022/train_7.csv')\n#train_8 = pd.read_csv('../input/tabular-playground-series-oct-2022/train_8.csv')\n#train_9 = pd.read_csv('../input/tabular-playground-series-oct-2022/train_9.csv')\n#train_dt = pd.read_csv('../input/tabular-playground-series-oct-2022/train_dytpes.csv')\n\n#test = pd.read_csv('../input/tabular-playground-series-oct-2022/test.csv')\n#submission = pd.read_csv('../input/tabular-playground-series-oct-2022/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:33:36.288742Z","iopub.execute_input":"2022-10-23T04:33:36.289290Z","iopub.status.idle":"2022-10-23T04:34:13.985539Z","shell.execute_reply.started":"2022-10-23T04:33:36.289257Z","shell.execute_reply":"2022-10-23T04:34:13.984140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:34:13.987201Z","iopub.execute_input":"2022-10-23T04:34:13.987564Z","iopub.status.idle":"2022-10-23T04:34:14.030208Z","shell.execute_reply.started":"2022-10-23T04:34:13.987531Z","shell.execute_reply":"2022-10-23T04:34:14.029024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = train_0.columns\ncol","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:34:14.032786Z","iopub.execute_input":"2022-10-23T04:34:14.033243Z","iopub.status.idle":"2022-10-23T04:34:14.040865Z","shell.execute_reply.started":"2022-10-23T04:34:14.033211Z","shell.execute_reply":"2022-10-23T04:34:14.039915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0.info()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-10-23T04:34:14.041978Z","iopub.execute_input":"2022-10-23T04:34:14.042724Z","iopub.status.idle":"2022-10-23T04:34:14.074882Z","shell.execute_reply.started":"2022-10-23T04:34:14.042687Z","shell.execute_reply":"2022-10-23T04:34:14.073556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check(df,col_list):\n    rows = []\n    for col in col_list:\n        tmp = (col,\n              df[col].dtype,\n              df[col].isnull().sum(),\n              (df[col].isnull().sum() / df[col].count()).round(2) * 100,\n              df[col].count(),\n              df[col].nunique(),\n              df[col].unique())\n        rows.append(tmp)\n    df = pd.DataFrame(rows) \n    df.columns = ['feature','dtype','nan','missing_%','count','nunique','unique']\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:34:14.076628Z","iopub.execute_input":"2022-10-23T04:34:14.077117Z","iopub.status.idle":"2022-10-23T04:34:14.086420Z","shell.execute_reply.started":"2022-10-23T04:34:14.077072Z","shell.execute_reply":"2022-10-23T04:34:14.084951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col_list_p = ['game_num', 'event_id', 'event_time', 'ball_pos_x', 'ball_pos_y','ball_pos_z', 'ball_vel_x', 'ball_vel_y', 'ball_vel_z'] \ncol_list_pt = ['ball_pos_x', 'ball_pos_y','ball_pos_z', 'ball_vel_x', 'ball_vel_y', 'ball_vel_z'] \ncol_list_0 = ['p0_pos_x','p0_pos_y', 'p0_pos_z', 'p0_vel_x', 'p0_vel_y', 'p0_vel_z', 'p0_boost', 'boost0_timer']\ncol_list_1 = ['p1_pos_x', 'p1_pos_y', 'p1_pos_z', 'p1_vel_x', 'p1_vel_y', 'p1_vel_z','p1_boost','boost1_timer']\ncol_list_2 = ['p2_pos_x', 'p2_pos_y', 'p2_pos_z', 'p2_vel_x', 'p2_vel_y','p2_vel_z', 'p2_boost','boost2_timer'] \ncol_list_3 = ['p3_pos_x', 'p3_pos_y', 'p3_pos_z', 'p3_vel_x','p3_vel_y', 'p3_vel_z', 'p3_boost','boost3_timer']\ncol_list_4 = ['p4_pos_x', 'p4_pos_y', 'p4_pos_z','p4_vel_x', 'p4_vel_y', 'p4_vel_z', 'p4_boost','boost4_timer']\ncol_list_5 = ['p5_pos_x', 'p5_pos_y','p5_pos_z', 'p5_vel_x', 'p5_vel_y', 'p5_vel_z', 'p5_boost','boost5_timer']\n#col_list_t = ['boost0_timer', 'boost1_timer', 'boost2_timer', 'boost3_timer','boost4_timer', 'boost5_timer']\ncol_list_s = ['player_scoring_next','team_scoring_next', 'team_A_scoring_within_10sec','team_B_scoring_within_10sec']","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:34:14.088532Z","iopub.execute_input":"2022-10-23T04:34:14.089082Z","iopub.status.idle":"2022-10-23T04:34:14.101511Z","shell.execute_reply.started":"2022-10-23T04:34:14.089031Z","shell.execute_reply":"2022-10-23T04:34:14.100385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reducing data memory\ndef reduce(data):\n    for col in data.columns:\n        if data[col].dtype == \"int64\":\n            data[col]=pd.to_numeric(data[col], downcast=\"integer\")\n        elif data[col].dtype == \"float64\":\n            data[col]=pd.to_numeric(data[col], downcast=\"float\")\n","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:34:14.103396Z","iopub.execute_input":"2022-10-23T04:34:14.103811Z","iopub.status.idle":"2022-10-23T04:34:14.113512Z","shell.execute_reply.started":"2022-10-23T04:34:14.103778Z","shell.execute_reply":"2022-10-23T04:34:14.112393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* This is the content of train_0 data. ","metadata":{}},{"cell_type":"code","source":"display(check(train_0, col_list_p))\ndisplay(check(train_0, col_list_0))\ndisplay(check(train_0, col_list_1))\ndisplay(check(train_0, col_list_2))\ndisplay(check(train_0, col_list_3))\ndisplay(check(train_0, col_list_4))\ndisplay(check(train_0, col_list_5))\ndisplay(check(train_0, col_list_s))","metadata":{"_kg_hide-input":true,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2022-10-23T04:34:14.114887Z","iopub.execute_input":"2022-10-23T04:34:14.115528Z","iopub.status.idle":"2022-10-23T04:34:25.995991Z","shell.execute_reply.started":"2022-10-23T04:34:14.115488Z","shell.execute_reply":"2022-10-23T04:34:25.994808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0.iloc[:, 2:].describe().T.sort_values(by='mean', ascending=False) .style.background_gradient(cmap='terrain_r')","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:34:26.001453Z","iopub.execute_input":"2022-10-23T04:34:26.001827Z","iopub.status.idle":"2022-10-23T04:34:32.565089Z","shell.execute_reply.started":"2022-10-23T04:34:26.001796Z","shell.execute_reply":"2022-10-23T04:34:32.563688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_count_a = train_0.team_A_scoring_within_10sec.value_counts()\ntarget_count_a\n\ntarget_count_b = train_0.team_B_scoring_within_10sec.value_counts()\ntarget_count_b\n\nteam_scoring_count = train_0.team_scoring_next.value_counts()\nplayer_scoring_count = train_0.player_scoring_next.value_counts()\n\n\nplt.figure(figsize=(22,5))\nplt.subplot(1,4,1)\nlabels = ['0','1']\nplt.pie(target_count_a, labels = labels, autopct = '%.0f%%')\nplt.title('team_A scoring percentage')\nplt.legend()\n\nplt.subplot(1,4,2)\nlabels = ['0','1']\nplt.pie(target_count_b, labels = labels, autopct = '%.0f%%')\nplt.title('team_B scoring percentage')\nplt.legend()\n    \nplt.subplot(1,4,3)\nlabels = ['A','B']\nplt.pie(team_scoring_count, labels = labels, autopct = '%.0f%%')\nplt.title('Team scoring next')\nplt.legend()\n    \nplt.subplot(1,4,4)\nlabels = ['-1','0','1','2','3','4','5']\nplt.pie(player_scoring_count, labels = labels, autopct = '%.0f%%')\nplt.title('Player scoring next')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:34:32.566926Z","iopub.execute_input":"2022-10-23T04:34:32.568012Z","iopub.status.idle":"2022-10-23T04:34:33.494376Z","shell.execute_reply.started":"2022-10-23T04:34:32.567969Z","shell.execute_reply":"2022-10-23T04:34:33.492720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values_train = train_0.isna().sum().sum()\nprint('Missing values in train data: {0}'.format(missing_values_train[missing_values_train > 0]))","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:34:33.495529Z","iopub.execute_input":"2022-10-23T04:34:33.495938Z","iopub.status.idle":"2022-10-23T04:34:33.848185Z","shell.execute_reply.started":"2022-10-23T04:34:33.495907Z","shell.execute_reply":"2022-10-23T04:34:33.846844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(train_0.iloc[:, 10:].isna().transpose(), cmap=\"YlGnBu\", cbar_kws={'label': 'Missing Measurement Data'})","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:34:33.849984Z","iopub.execute_input":"2022-10-23T04:34:33.850383Z","iopub.status.idle":"2022-10-23T04:38:05.382058Z","shell.execute_reply.started":"2022-10-23T04:34:33.850350Z","shell.execute_reply":"2022-10-23T04:38:05.380852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Missing number is only 1% of tatal data. \n* It appears that some player data is missing.","metadata":{}},{"cell_type":"code","source":"def add_feature(data):\n    # between ball and player (Distance = sqrt((Bx - Px)^2) + ((By - Py)^2) +((Bz - Pz)^2)) e.g. Bx = ball position x, Px = player position x\n    data['p0_dist_ball'] = ((data['ball_pos_x'] - data['p0_pos_x'])**2 + (data['ball_pos_y'] - data['p0_pos_y'])**2 + (data['ball_pos_z'] - data['p0_pos_z'])**2)** 0.5\n    data['p1_dist_ball'] = ((data['ball_pos_x'] - data['p1_pos_x'])**2 + (data['ball_pos_y'] - data['p1_pos_y'])**2 + (data['ball_pos_z'] - data['p1_pos_z'])**2)** 0.5\n    data['p2_dist_ball'] = ((data['ball_pos_x'] - data['p2_pos_x'])**2 + (data['ball_pos_y'] - data['p2_pos_y'])**2 + (data['ball_pos_z'] - data['p2_pos_z'])**2)** 0.5\n    data['p3_dist_ball'] = ((data['ball_pos_x'] - data['p3_pos_x'])**2 + (data['ball_pos_y'] - data['p3_pos_y'])**2 + (data['ball_pos_z'] - data['p3_pos_z'])**2)** 0.5\n    data['p4_dist_ball'] = ((data['ball_pos_x'] - data['p4_pos_x'])**2 + (data['ball_pos_y'] - data['p2_pos_y'])**2 + (data['ball_pos_z'] - data['p4_pos_z'])**2)** 0.5\n    data['p5_dist_ball'] = ((data['ball_pos_x'] - data['p5_pos_x'])**2 + (data['ball_pos_y'] - data['p5_pos_y'])**2 + (data['ball_pos_z'] - data['p5_pos_z'])**2)** 0.5\n    \n     # distance between goal and player\n    goal_a_0 = [0, -120, 1.2]\n    goal_a_1 = [20, -120, 1.2]\n    goal_a_2 = [-20, -120, 1.2]\n    goal_b_0 = [0, -120, 1.2]\n    goal_b_1 = [20, 120, 1.2]\n    goal_b_2 = [-20, 120, 1.2]\n    \n    data['p0_dist_goal_0'] = ((goal_a_0[0] - data['p0_pos_x'])**2 + (goal_a_1[1] - data['p1_pos_y'])**2 + (goal_a_1[2] - data['p0_pos_z'])**2)** 0.5\n    data['p1_dist_goal_0'] = ((goal_a_0[0] - data['p1_pos_x'])**2 + (goal_a_1[1] - data['p1_pos_y'])**2 + (goal_a_1[2] - data['p1_pos_z'])**2)** 0.5\n    data['p2_dist_goal_0'] = ((goal_a_0[0] - data['p2_pos_x'])**2 + (goal_a_1[1] - data['p2_pos_y'])**2 + (goal_a_1[2] - data['p2_pos_z'])**2)** 0.5\n    data['p0_dist_goal_1'] = ((goal_a_1[0] - data['p0_pos_x'])**2 + (goal_a_1[1] - data['p1_pos_y'])**2 + (goal_a_1[2] - data['p0_pos_z'])**2)** 0.5\n    data['p1_dist_goal_1'] = ((goal_a_1[0] - data['p1_pos_x'])**2 + (goal_a_1[1] - data['p1_pos_y'])**2 + (goal_a_1[2] - data['p1_pos_z'])**2)** 0.5\n    data['p2_dist_goal_1'] = ((goal_a_1[0] - data['p2_pos_x'])**2 + (goal_a_1[1] - data['p2_pos_y'])**2 + (goal_a_1[2] - data['p2_pos_z'])**2)** 0.5\n    data['p0_dist_goal_2'] = ((goal_a_2[0] - data['p0_pos_x'])**2 + (goal_a_2[1] - data['p1_pos_y'])**2 + (goal_a_2[2] - data['p0_pos_z'])**2)** 0.5\n    data['p1_dist_goal_2'] = ((goal_a_2[0] - data['p1_pos_x'])**2 + (goal_a_2[1] - data['p1_pos_y'])**2 + (goal_a_2[2] - data['p1_pos_z'])**2)** 0.5\n    data['p2_dist_goal_2'] = ((goal_a_2[0] - data['p2_pos_x'])**2 + (goal_a_2[1] - data['p2_pos_y'])**2 + (goal_a_2[2] - data['p2_pos_z'])**2)** 0.5\n    \n    data['p3_dist_goal_0'] = ((goal_b_0[0] - data['p3_pos_x'])**2 + (goal_b_1[1] - data['p3_pos_y'])**2 + (goal_b_1[2] - data['p3_pos_z'])**2)** 0.5\n    data['p4_dist_goal_0'] = ((goal_b_0[0] - data['p4_pos_x'])**2 + (goal_b_1[1] - data['p4_pos_y'])**2 + (goal_b_1[2] - data['p4_pos_z'])**2)** 0.5\n    data['p5_dist_goal_0'] = ((goal_b_0[0] - data['p5_pos_x'])**2 + (goal_b_1[1] - data['p5_pos_y'])**2 + (goal_b_1[2] - data['p5_pos_z'])**2)** 0.5\n    data['p3_dist_goal_1'] = ((goal_b_1[0] - data['p3_pos_x'])**2 + (goal_b_1[1] - data['p3_pos_y'])**2 + (goal_b_1[2] - data['p3_pos_z'])**2)** 0.5\n    data['p4_dist_goal_1'] = ((goal_b_1[0] - data['p4_pos_x'])**2 + (goal_b_1[1] - data['p4_pos_y'])**2 + (goal_b_1[2] - data['p4_pos_z'])**2)** 0.5\n    data['p5_dist_goal_1'] = ((goal_b_1[0] - data['p5_pos_x'])**2 + (goal_b_1[1] - data['p5_pos_y'])**2 + (goal_b_1[2] - data['p5_pos_z'])**2)** 0.5\n    data['p3_dist_goal_2'] = ((goal_b_2[0] - data['p3_pos_x'])**2 + (goal_b_2[1] - data['p3_pos_y'])**2 + (goal_b_2[2] - data['p3_pos_z'])**2)** 0.5\n    data['p4_dist_goal_2'] = ((goal_b_2[0] - data['p4_pos_x'])**2 + (goal_b_2[1] - data['p4_pos_y'])**2 + (goal_b_2[2] - data['p4_pos_z'])**2)** 0.5\n    data['p5_dist_goal_2'] = ((goal_b_2[0] - data['p5_pos_x'])**2 + (goal_b_2[1] - data['p5_pos_y'])**2 + (goal_b_2[2] - data['p5_pos_z'])**2)** 0.5\n    \n     # between ball and goal\n    data[\"goal_a_distance\"] = ((data[\"ball_pos_x\"] - 0)**2 + (data[\"ball_pos_y\"] - 120)**2 + (data[\"ball_pos_z\"] - 1.2)**2)**0.5\n    data[\"goal_b_distance\"] = ((data[\"ball_pos_x\"] - 0)**2 + (data[\"ball_pos_y\"] + 120)**2 + (data[\"ball_pos_z\"] - 1.2)**2)**0.5\n    \n     # spped (speed = sqrt(Vx^2 + Vy^2 + Vz^2)) \n    data['ball_speed'] = ((data['ball_vel_x'])**2 + (data['ball_vel_y'])**2 +  (data['ball_vel_z'])**2)** 0.5\n    data['p0_speed'] = ((data['p0_vel_x'])**2 + (data['p0_vel_y'])**2 +  (data['p0_vel_z'])**2)** 0.5\n    data['p1_speed'] = ((data['p1_vel_x'])**2 + (data['p1_vel_y'])**2 + (data['p1_vel_z'])**2)** 0.5\n    data['p2_speed'] = ((data['p2_vel_x'])**2 + (data['p2_vel_y'])**2 + (data['p2_vel_z'])**2)** 0.5\n    data['p3_speed'] = ((data['p3_vel_x'])**2 + (data['p3_vel_y'])**2 + (data['p3_vel_z'])**2)** 0.5\n    data['p4_speed'] = ((data['p4_vel_x'])**2 + (data['p4_vel_y'])**2 + (data['p4_vel_z'])**2)** 0.5\n    data['p5_speed'] = ((data['p5_vel_x'])**2 + (data['p5_vel_y'])**2 + (data['p5_vel_z'])**2)** 0.5\n    \n    # coordinate direction angle\n    data['ball-cos_a'] = data['ball_vel_x'] / (((data['ball_vel_x'])**2 + (data['ball_vel_y'])**2 +  (data['ball_vel_z'])**2)** 0.5)\n    data['ball-cos_b'] = data['ball_vel_y'] / (((data['ball_vel_x'])**2 + (data['ball_vel_y'])**2 +  (data['ball_vel_z'])**2)** 0.5)\n    data['ball-cos_c'] = data['ball_vel_z'] / (((data['ball_vel_x'])**2 + (data['ball_vel_y'])**2 +  (data['ball_vel_z'])**2)** 0.5)\n    \n    data['p0_cos_a'] = data['p0_vel_x'] /  (((data['p0_vel_x'])**2 + (data['p0_vel_y'])**2 + (data['p0_vel_z'])**2)** 0.5)\n    data['p0_cos_b'] = data['p0_vel_y'] /  (((data['p0_vel_x'])**2 + (data['p0_vel_y'])**2 + (data['p0_vel_z'])**2)** 0.5)\n    data['p0_cos_c'] = data['p0_vel_z'] /  (((data['p0_vel_x'])**2 + (data['p0_vel_y'])**2 + (data['p0_vel_z'])**2)** 0.5)\n    data['p1_cos_a'] = data['p1_vel_x'] / (((data['p1_vel_x'])**2 + (data['p1_vel_y'])**2 + (data['p1_vel_z'])**2)** 0.5)\n    data['p1_cos_b'] = data['p1_vel_y'] / (((data['p1_vel_x'])**2 + (data['p1_vel_y'])**2 + (data['p1_vel_z'])**2)** 0.5)\n    data['p1_cos_c'] = data['p1_vel_z'] / (((data['p1_vel_x'])**2 + (data['p1_vel_y'])**2 + (data['p1_vel_z'])**2)** 0.5)\n    data['p2_cos_a'] = data['p2_vel_x'] / (((data['p2_vel_x'])**2 + (data['p2_vel_y'])**2 + (data['p2_vel_z'])**2)** 0.5)\n    data['p2_cos_b'] = data['p2_vel_y'] / (((data['p2_vel_x'])**2 + (data['p2_vel_y'])**2 + (data['p2_vel_z'])**2)** 0.5)\n    data['p2_cos_c'] = data['p2_vel_z'] / (((data['p2_vel_x'])**2 + (data['p2_vel_y'])**2 + (data['p2_vel_z'])**2)** 0.5)\n    data['p3_cos_a'] = data['p3_vel_x'] / (((data['p3_vel_x'])**2 + (data['p3_vel_y'])**2 + (data['p3_vel_z'])**2)** 0.5)\n    data['p3_cos_b'] = data['p3_vel_y'] / (((data['p3_vel_x'])**2 + (data['p3_vel_y'])**2 + (data['p3_vel_z'])**2)** 0.5)\n    data['p3_cos_c'] = data['p3_vel_z'] / (((data['p3_vel_x'])**2 + (data['p3_vel_y'])**2 + (data['p3_vel_z'])**2)** 0.5)\n    data['p4_cos_a'] = data['p4_vel_x'] / (((data['p4_vel_x'])**2 + (data['p4_vel_y'])**2 + (data['p4_vel_z'])**2)** 0.5)\n    data['p4_cos_b'] = data['p4_vel_y'] / (((data['p4_vel_x'])**2 + (data['p4_vel_y'])**2 + (data['p4_vel_z'])**2)** 0.5)\n    data['p4_cos_c'] = data['p4_vel_z'] / (((data['p4_vel_x'])**2 + (data['p4_vel_y'])**2 + (data['p4_vel_z'])**2)** 0.5)\n    data['p5_cos_a'] = data['p5_vel_x'] / (((data['p5_vel_x'])**2 + (data['p5_vel_y'])**2 + (data['p5_vel_z'])**2)** 0.5)\n    data['p5_cos_b'] = data['p5_vel_y'] / (((data['p5_vel_x'])**2 + (data['p5_vel_y'])**2 + (data['p5_vel_z'])**2)** 0.5)\n    data['p5_cos_c'] = data['p5_vel_z'] / (((data['p5_vel_x'])**2 + (data['p5_vel_y'])**2 + (data['p5_vel_z'])**2)** 0.5)\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:05.384444Z","iopub.execute_input":"2022-10-23T04:38:05.385347Z","iopub.status.idle":"2022-10-23T04:38:05.434538Z","shell.execute_reply.started":"2022-10-23T04:38:05.385299Z","shell.execute_reply":"2022-10-23T04:38:05.433396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Distance between ball and player (Distance = sqrt((Bx - Px)^2) + ((By - Py)^2) +((Bz - Pz)^2)) e.g. Bx = ball position x, Px = player position x)\n* Distance between ball and goal\n* spped |V| = √ Vx^2 + Vy^2 + Vz^2 \n* 3D force vector(F) Fx = Fcos(θx),Fy = Fcos(θy),Fz = Fcos(θz) θ = V / speed","metadata":{}},{"cell_type":"code","source":"add_feature(train_0)","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:05.436096Z","iopub.execute_input":"2022-10-23T04:38:05.436550Z","iopub.status.idle":"2022-10-23T04:38:08.134153Z","shell.execute_reply.started":"2022-10-23T04:38:05.436506Z","shell.execute_reply":"2022-10-23T04:38:08.133022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0.query('p0_dist_ball < 3 and p0_dist_goal_0 < 40 and team_A_scoring_within_10sec == 1')","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:08.135567Z","iopub.execute_input":"2022-10-23T04:38:08.136032Z","iopub.status.idle":"2022-10-23T04:38:09.606144Z","shell.execute_reply.started":"2022-10-23T04:38:08.135984Z","shell.execute_reply":"2022-10-23T04:38:09.605009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0.query('p1_dist_ball < 3 and p1_dist_goal_1 < 40 and team_A_scoring_within_10sec == 1')","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:09.607389Z","iopub.execute_input":"2022-10-23T04:38:09.607732Z","iopub.status.idle":"2022-10-23T04:38:09.658093Z","shell.execute_reply.started":"2022-10-23T04:38:09.607702Z","shell.execute_reply":"2022-10-23T04:38:09.657013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0.query('p2_dist_ball < 3 and p1_dist_goal_2 < 40 and team_A_scoring_within_10sec == 1')","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:09.659471Z","iopub.execute_input":"2022-10-23T04:38:09.659806Z","iopub.status.idle":"2022-10-23T04:38:09.705860Z","shell.execute_reply.started":"2022-10-23T04:38:09.659773Z","shell.execute_reply":"2022-10-23T04:38:09.704690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0.query('p3_dist_ball < 2 and p1_dist_goal_0 < 40 and team_B_scoring_within_10sec == 1')","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:09.707335Z","iopub.execute_input":"2022-10-23T04:38:09.708387Z","iopub.status.idle":"2022-10-23T04:38:09.753485Z","shell.execute_reply.started":"2022-10-23T04:38:09.708344Z","shell.execute_reply":"2022-10-23T04:38:09.752309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0.query('p4_dist_ball < 1 and p1_dist_goal_1 < 40 and team_B_scoring_within_10sec == 1')","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:09.755189Z","iopub.execute_input":"2022-10-23T04:38:09.755537Z","iopub.status.idle":"2022-10-23T04:38:09.799158Z","shell.execute_reply.started":"2022-10-23T04:38:09.755506Z","shell.execute_reply":"2022-10-23T04:38:09.798000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0.query('p5_dist_ball < 3 and p4_dist_goal_2 < 40 and team_B_scoring_within_10sec == 1')","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:09.800659Z","iopub.execute_input":"2022-10-23T04:38:09.801046Z","iopub.status.idle":"2022-10-23T04:38:09.847341Z","shell.execute_reply.started":"2022-10-23T04:38:09.801013Z","shell.execute_reply":"2022-10-23T04:38:09.846063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Here is an excerpt.( Team A consists of p0,p1,p2 and Team B consists of p3,p4,p5.)","metadata":{}},{"cell_type":"code","source":"del train_0","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:09.848727Z","iopub.execute_input":"2022-10-23T04:38:09.849108Z","iopub.status.idle":"2022-10-23T04:38:09.855074Z","shell.execute_reply.started":"2022-10-23T04:38:09.849077Z","shell.execute_reply":"2022-10-23T04:38:09.853764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test data","metadata":{}},{"cell_type":"code","source":"test_dtypes = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/test_dtypes.csv\")\ntest_dtypes = dict(test_dtypes.to_records(index=False))\n\ntest = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/test.csv\", dtype = test_dtypes)\ntest.info()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-10-23T04:38:09.856581Z","iopub.execute_input":"2022-10-23T04:38:09.856949Z","iopub.status.idle":"2022-10-23T04:38:20.846998Z","shell.execute_reply.started":"2022-10-23T04:38:09.856914Z","shell.execute_reply":"2022-10-23T04:38:20.845884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:20.848831Z","iopub.execute_input":"2022-10-23T04:38:20.849199Z","iopub.status.idle":"2022-10-23T04:38:20.938623Z","shell.execute_reply.started":"2022-10-23T04:38:20.849165Z","shell.execute_reply":"2022-10-23T04:38:20.937430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:20.940369Z","iopub.execute_input":"2022-10-23T04:38:20.940873Z","iopub.status.idle":"2022-10-23T04:38:20.947635Z","shell.execute_reply.started":"2022-10-23T04:38:20.940842Z","shell.execute_reply":"2022-10-23T04:38:20.946398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(check(test, col_list_pt))\ndisplay(check(test, col_list_0))\ndisplay(check(test, col_list_1))\ndisplay(check(test, col_list_2))\ndisplay(check(test, col_list_3))\ndisplay(check(test, col_list_4))\ndisplay(check(test, col_list_5))","metadata":{"_kg_hide-output":false,"execution":{"iopub.status.busy":"2022-10-23T04:38:20.949360Z","iopub.execute_input":"2022-10-23T04:38:20.950156Z","iopub.status.idle":"2022-10-23T04:38:24.182249Z","shell.execute_reply.started":"2022-10-23T04:38:20.950112Z","shell.execute_reply":"2022-10-23T04:38:24.181176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values = test.isna().sum().sum()\nprint('Missing values in test data: {0}'.format(missing_values[missing_values > 0]))","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:24.183463Z","iopub.execute_input":"2022-10-23T04:38:24.183769Z","iopub.status.idle":"2022-10-23T04:38:24.278025Z","shell.execute_reply.started":"2022-10-23T04:38:24.183741Z","shell.execute_reply":"2022-10-23T04:38:24.276830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.iloc[:, 1:].describe().T.sort_values(by='mean', ascending=False) .style.background_gradient(cmap='terrain_r')","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:24.284239Z","iopub.execute_input":"2022-10-23T04:38:24.284649Z","iopub.status.idle":"2022-10-23T04:38:27.117002Z","shell.execute_reply.started":"2022-10-23T04:38:24.284618Z","shell.execute_reply":"2022-10-23T04:38:27.115760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MODEL","metadata":{}},{"cell_type":"markdown","source":"* This time, I tried LGBM. However the memory problem colud not be solved. so the results obtained by dividing the data and took the averaged.","metadata":{}},{"cell_type":"code","source":"n_splits = 5\nseed = 42\n\nparams = {'objective':'binary',\n          'metric' : 'auc',\n          'seed': 42,\n          'num_leaves' : 64,\n          'min_child_samples': 20,\n          'max_depth' :7,\n          'n_estimators': 300,\n          'learning_rate': 0.1,\n         }\n\nmodel = lgb.LGBMClassifier(**params)   \n\n\ndef models(X,y, model,sub):\n    \n    for i in range(2):\n        X_train, X_val, y_train, y_val = train_test_split(X,y[i],test_size=0.2, random_state = seed)\n        model.fit(X_train,y_train)\n        pred_ = model.predict_proba(X_val)[:,1]\n        loss = log_loss(y_val ,pred_)\n        pred = model.predict_proba(test)[:,1]\n        if i == 0:\n            sub['team_A_scoring_within_10sec'] =  pred\n        else:\n            sub['team_B_scoring_within_10sec'] = pred\n        print(f\"\\n{y[i]} Logloss = {loss}\\n  prediction {pred}\\n\")\n        \n    return sub","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:27.118423Z","iopub.execute_input":"2022-10-23T04:38:27.118781Z","iopub.status.idle":"2022-10-23T04:38:27.128618Z","shell.execute_reply.started":"2022-10-23T04:38:27.118747Z","shell.execute_reply":"2022-10-23T04:38:27.127438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ntest_dtypes = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/test_dtypes.csv\")\ntest_dtypes = dict(test_dtypes.to_records(index=False))\ntest = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/test.csv\")\ntest = test.interpolate(limit_direction = 'both', axis=1)\nadd_feature(test)\ntest = test.drop(['id'], axis = 1)\ndisplay(test)\n\nsub = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/sample_submission.csv\")\n\ntrain_dtypes = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/train_dtypes.csv\")\ntrain_dtypes = dict(train_dtypes.to_records(index=False))\ntrain_ = pd.DataFrame()\n\nfor i in tqdm(range(0,2)): # range (2,4)(4,6),(6,8),(8,10)\n    train_i = pd.read_csv(f\"/kaggle/input/tabular-playground-series-oct-2022/train_{i}.csv\", dtype = train_dtypes)\n    train_ = pd.concat([train_, train_i])\n    del train_i\n    \ntrain_['team_A_scoring_within_10sec']= train_['team_A_scoring_within_10sec'].astype(int)\ntrain_['team_B_scoring_within_10sec']= train_['team_B_scoring_within_10sec'].astype(int)\n\ny = [train_['team_A_scoring_within_10sec'],train_['team_B_scoring_within_10sec']]\n\ntrain_ = train_.drop(['game_num', 'event_id', 'event_time', 'player_scoring_next', 'team_scoring_next', 'team_A_scoring_within_10sec', 'team_B_scoring_within_10sec'], axis=1)\ntrain_ = train_.interpolate(limit_direction = 'both', axis=1)\nadd_feature(train_)\n\nX = train_\ndisplay(X)\n\nmodels(X,y,model, sub)\nsub.to_csv('submission_01.csv', index=False)\n\ndel train_\ndel sub\n'''","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-10-23T04:38:27.130341Z","iopub.execute_input":"2022-10-23T04:38:27.131281Z","iopub.status.idle":"2022-10-23T04:38:27.145051Z","shell.execute_reply.started":"2022-10-23T04:38:27.131246Z","shell.execute_reply":"2022-10-23T04:38:27.143785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Missing values are compensated for with pandas interploate.\n* This time, running the model with EDA wolud cause memory overflow. So I have adeed a link to it below. \n* Please refer to it for your reference.\n* [TPS_OCT_2022_LGBM_part2](https://www.kaggle.com/code/m1y7k8/tps-oct-2022-eda-lgbm-part2?scriptVersionId=108124956)","metadata":{}},{"cell_type":"code","source":"train_1 = pd.read_csv('../input/tps-oct-2022-eda-lgbm-part2/submission_01.csv')\ntrain_2 = pd.read_csv('../input/tps-oct-2022-eda-lgbm-part2/submission_23.csv')\ntrain_3 = pd.read_csv('../input/tps-oct-2022-eda-lgbm-part2/submission_45.csv')\ntrain_4 = pd.read_csv('../input/tps-oct-2022-eda-lgbm-part2/submission_67.csv')\ntrain_5 = pd.read_csv('../input/tps-oct-2022-eda-lgbm-part2/submission_89.csv')\n\ntrain_1, train_2, train_3, train_4, train_5","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:27.146589Z","iopub.execute_input":"2022-10-23T04:38:27.147381Z","iopub.status.idle":"2022-10-23T04:38:30.665372Z","shell.execute_reply.started":"2022-10-23T04:38:27.147329Z","shell.execute_reply":"2022-10-23T04:38:30.664016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = (train_1+ train_2 + train_3 + train_4 + train_5)/5\nsub['id'] = sub['id'].astype(int)\nsub.to_csv('submission_p2.csv', index=False)\nsub","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:30.667360Z","iopub.execute_input":"2022-10-23T04:38:30.667817Z","iopub.status.idle":"2022-10-23T04:38:33.350014Z","shell.execute_reply.started":"2022-10-23T04:38:30.667762Z","shell.execute_reply":"2022-10-23T04:38:33.348781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The results were summed to obtain an averge value.","metadata":{}},{"cell_type":"code","source":"del train_1\ndel train_2\ndel train_3\ndel train_4\ndel train_5\ndel test\ndel sub","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:33.351649Z","iopub.execute_input":"2022-10-23T04:38:33.352145Z","iopub.status.idle":"2022-10-23T04:38:33.358648Z","shell.execute_reply.started":"2022-10-23T04:38:33.352097Z","shell.execute_reply":"2022-10-23T04:38:33.357551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Other ways","metadata":{}},{"cell_type":"code","source":"\ndef add_feature_(data):\n    # between ball and player (Distance = sqrt((Bx - Px)^2) + ((By - Py)^2) +((Bz - Pz)^2)) e.g. Bx = ball position x, Px = player position x\n    data['p0_dist_ball'] = ((data['ball_pos_x'] - data['p0_pos_x'])**2 + (data['ball_pos_y'] - data['p0_pos_y'])**2 + (data['ball_pos_z'] - data['p0_pos_z'])**2)** 0.5\n    data['p1_dist_ball'] = ((data['ball_pos_x'] - data['p1_pos_x'])**2 + (data['ball_pos_y'] - data['p1_pos_y'])**2 + (data['ball_pos_z'] - data['p1_pos_z'])**2)** 0.5\n    data['p2_dist_ball'] = ((data['ball_pos_x'] - data['p2_pos_x'])**2 + (data['ball_pos_y'] - data['p2_pos_y'])**2 + (data['ball_pos_z'] - data['p2_pos_z'])**2)** 0.5\n    data['p3_dist_ball'] = ((data['ball_pos_x'] - data['p3_pos_x'])**2 + (data['ball_pos_y'] - data['p3_pos_y'])**2 + (data['ball_pos_z'] - data['p3_pos_z'])**2)** 0.5\n    data['p4_dist_ball'] = ((data['ball_pos_x'] - data['p4_pos_x'])**2 + (data['ball_pos_y'] - data['p2_pos_y'])**2 + (data['ball_pos_z'] - data['p4_pos_z'])**2)** 0.5\n    data['p5_dist_ball'] = ((data['ball_pos_x'] - data['p5_pos_x'])**2 + (data['ball_pos_y'] - data['p5_pos_y'])**2 + (data['ball_pos_z'] - data['p5_pos_z'])**2)** 0.5\n    \n     # distance between goal and player\n    goal_a_0 = [0, -120, 1.2]\n    goal_a_1 = [20, -120, 1.2]\n    goal_a_2 = [-20, -120, 1.2]\n    goal_b_0 = [0, -120, 1.2]\n    goal_b_1 = [20, 120, 1.2]\n    goal_b_2 = [-20, 120, 1.2]\n    \n    data['p0_dist_goal_0'] = ((goal_a_0[0] - data['p0_pos_x'])**2 + (goal_a_1[1] - data['p1_pos_y'])**2 + (goal_a_1[2] - data['p0_pos_z'])**2)** 0.5\n    data['p1_dist_goal_0'] = ((goal_a_0[0] - data['p1_pos_x'])**2 + (goal_a_1[1] - data['p1_pos_y'])**2 + (goal_a_1[2] - data['p1_pos_z'])**2)** 0.5\n    data['p2_dist_goal_0'] = ((goal_a_0[0] - data['p2_pos_x'])**2 + (goal_a_1[1] - data['p2_pos_y'])**2 + (goal_a_1[2] - data['p2_pos_z'])**2)** 0.5\n    data['p0_dist_goal_1'] = ((goal_a_1[0] - data['p0_pos_x'])**2 + (goal_a_1[1] - data['p1_pos_y'])**2 + (goal_a_1[2] - data['p0_pos_z'])**2)** 0.5\n    data['p1_dist_goal_1'] = ((goal_a_1[0] - data['p1_pos_x'])**2 + (goal_a_1[1] - data['p1_pos_y'])**2 + (goal_a_1[2] - data['p1_pos_z'])**2)** 0.5\n    data['p2_dist_goal_1'] = ((goal_a_1[0] - data['p2_pos_x'])**2 + (goal_a_1[1] - data['p2_pos_y'])**2 + (goal_a_1[2] - data['p2_pos_z'])**2)** 0.5\n    data['p0_dist_goal_2'] = ((goal_a_2[0] - data['p0_pos_x'])**2 + (goal_a_2[1] - data['p1_pos_y'])**2 + (goal_a_2[2] - data['p0_pos_z'])**2)** 0.5\n    data['p1_dist_goal_2'] = ((goal_a_2[0] - data['p1_pos_x'])**2 + (goal_a_2[1] - data['p1_pos_y'])**2 + (goal_a_2[2] - data['p1_pos_z'])**2)** 0.5\n    data['p2_dist_goal_2'] = ((goal_a_2[0] - data['p2_pos_x'])**2 + (goal_a_2[1] - data['p2_pos_y'])**2 + (goal_a_2[2] - data['p2_pos_z'])**2)** 0.5\n    \n    data['p3_dist_goal_0'] = ((goal_b_0[0] - data['p3_pos_x'])**2 + (goal_b_1[1] - data['p3_pos_y'])**2 + (goal_b_1[2] - data['p3_pos_z'])**2)** 0.5\n    data['p4_dist_goal_0'] = ((goal_b_0[0] - data['p4_pos_x'])**2 + (goal_b_1[1] - data['p4_pos_y'])**2 + (goal_b_1[2] - data['p4_pos_z'])**2)** 0.5\n    data['p5_dist_goal_0'] = ((goal_b_0[0] - data['p5_pos_x'])**2 + (goal_b_1[1] - data['p5_pos_y'])**2 + (goal_b_1[2] - data['p5_pos_z'])**2)** 0.5\n    data['p3_dist_goal_1'] = ((goal_b_1[0] - data['p3_pos_x'])**2 + (goal_b_1[1] - data['p3_pos_y'])**2 + (goal_b_1[2] - data['p3_pos_z'])**2)** 0.5\n    data['p4_dist_goal_1'] = ((goal_b_1[0] - data['p4_pos_x'])**2 + (goal_b_1[1] - data['p4_pos_y'])**2 + (goal_b_1[2] - data['p4_pos_z'])**2)** 0.5\n    data['p5_dist_goal_1'] = ((goal_b_1[0] - data['p5_pos_x'])**2 + (goal_b_1[1] - data['p5_pos_y'])**2 + (goal_b_1[2] - data['p5_pos_z'])**2)** 0.5\n    data['p3_dist_goal_2'] = ((goal_b_2[0] - data['p3_pos_x'])**2 + (goal_b_2[1] - data['p3_pos_y'])**2 + (goal_b_2[2] - data['p3_pos_z'])**2)** 0.5\n    data['p4_dist_goal_2'] = ((goal_b_2[0] - data['p4_pos_x'])**2 + (goal_b_2[1] - data['p4_pos_y'])**2 + (goal_b_2[2] - data['p4_pos_z'])**2)** 0.5\n    data['p5_dist_goal_2'] = ((goal_b_2[0] - data['p5_pos_x'])**2 + (goal_b_2[1] - data['p5_pos_y'])**2 + (goal_b_2[2] - data['p5_pos_z'])**2)** 0.5\n    \n      # between ball and goal\n    data[\"goal_a_distance\"] = ((data[\"ball_pos_x\"] - 0)**2 + (data[\"ball_pos_y\"] - 120)**2 + (data[\"ball_pos_z\"] - 1.2)**2)**0.5\n    data[\"goal_b_distance\"] = ((data[\"ball_pos_x\"] - 0)**2 + (data[\"ball_pos_y\"] + 120)**2 + (data[\"ball_pos_z\"] - 1.2)**2)**0.5\n    \n     # spped (speed = sqrt(Vx^2 + Vy^2 + Vz^2)) \n    data['ball_speed'] = ((data['ball_vel_x'])**2 + (data['ball_vel_y'])**2 +  (data['ball_vel_z'])**2)** 0.5\n    data['p0_speed'] = ((data['p0_vel_x'])**2 + (data['p0_vel_y'])**2 +  (data['p0_vel_z'])**2)** 0.5\n    data['p1_speed'] = ((data['p1_vel_x'])**2 + (data['p1_vel_y'])**2 + (data['p1_vel_z'])**2)** 0.5\n    data['p2_speed'] = ((data['p2_vel_x'])**2 + (data['p2_vel_y'])**2 + (data['p2_vel_z'])**2)** 0.5\n    data['p3_speed'] = ((data['p3_vel_x'])**2 + (data['p3_vel_y'])**2 + (data['p3_vel_z'])**2)** 0.5\n    data['p4_speed'] = ((data['p4_vel_x'])**2 + (data['p4_vel_y'])**2 + (data['p4_vel_z'])**2)** 0.5\n    data['p5_speed'] = ((data['p5_vel_x'])**2 + (data['p5_vel_y'])**2 + (data['p5_vel_z'])**2)** 0.5\n    \n    return data\n","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-10-23T04:38:33.360608Z","iopub.execute_input":"2022-10-23T04:38:33.361087Z","iopub.status.idle":"2022-10-23T04:38:33.396424Z","shell.execute_reply.started":"2022-10-23T04:38:33.361042Z","shell.execute_reply":"2022-10-23T04:38:33.395124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop list\ndrp_ = ['game_num', 'event_id', 'event_time','player_scoring_next', 'team_scoring_next', 'team_A_scoring_within_10sec', 'team_B_scoring_within_10sec']\ndrp_1 = ['ball_pos_x','ball_pos_y','ball_pos_z','p0_pos_x','p0_pos_y','p0_pos_z','p1_pos_x','p1_pos_y','p1_pos_z','p2_pos_x','p2_pos_y','p2_pos_z','p3_pos_x','p3_pos_y','p3_pos_z','p4_pos_x','p4_pos_y','p4_pos_z','p5_pos_x','p5_pos_y','p5_pos_z']\ndrp_2 = ['ball_vel_x','ball_vel_y','ball_vel_z','p0_vel_x','p0_vel_y','p0_vel_z','p1_vel_x','p1_vel_y','p1_vel_z','p2_vel_x','p2_vel_y','p2_vel_z','p3_vel_x','p3_vel_y','p3_vel_z','p4_vel_x','p4_vel_y','p4_vel_z','p5_vel_x','p5_vel_y','p5_vel_z']\ndrp_3 = ['p0_boost','p1_boost','p2_boost','p3_boost','p4_boost','p5_boost','boost0_timer','boost1_timer','boost2_timer','boost3_timer','boost4_timer','boost5_timer']","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:33.398165Z","iopub.execute_input":"2022-10-23T04:38:33.398855Z","iopub.status.idle":"2022-10-23T04:38:33.409603Z","shell.execute_reply.started":"2022-10-23T04:38:33.398715Z","shell.execute_reply":"2022-10-23T04:38:33.408505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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))\n\ntest_dtypes = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/test_dtypes.csv\")\ntest_dtypes = dict(test_dtypes.to_records(index=False))\n\ntest = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/test.csv\")\ntest = test.interpolate(limit_direction = 'both', axis=1)\nadd_feature_(test)\ntest = test.drop(['id'], axis = 1)\n#test = test.drop(drp_1, axis = 1)\ntest = test.drop(drp_2, axis = 1)\ntest = test.drop(drp_3, axis = 1)\ndisplay(test)","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:33.411497Z","iopub.execute_input":"2022-10-23T04:38:33.412405Z","iopub.status.idle":"2022-10-23T04:38:55.005928Z","shell.execute_reply.started":"2022-10-23T04:38:33.412369Z","shell.execute_reply":"2022-10-23T04:38:55.004796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_splits = 5\nseed = 42\n\nparams = {'objective':'binary',\n          'metric' : 'auc',\n          'seed': 42,\n          'num_leaves' : 64,\n          'min_child_samples': 20,\n          'max_depth' : 7,\n          'n_estimators': 300,\n          'learning_rate': 0.1,\n         }\n\nmodel = lgb.LGBMClassifier(**params)   ","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:55.008061Z","iopub.execute_input":"2022-10-23T04:38:55.008532Z","iopub.status.idle":"2022-10-23T04:38:55.015839Z","shell.execute_reply.started":"2022-10-23T04:38:55.008480Z","shell.execute_reply":"2022-10-23T04:38:55.014389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def models(X,y,test, model,sub):\n    \n    for i in range(2):\n        X_train, X_val, y_train, y_val = train_test_split(X,y[i],test_size=0.2, random_state = seed)\n        model.fit(X_train,y_train)\n        pred_ = model.predict_proba(X_val)[:,1]\n        loss = log_loss(y_val ,pred_)\n        pred = model.predict_proba(test)[:,1]\n        if i == 0:\n            sub['team_A_scoring_within_10sec'] =  pred\n        else:\n            sub['team_B_scoring_within_10sec'] = pred\n        print(f\"\\n{y[i]} Logloss = {loss}\\n  prediction {pred}\\n\")\n        \n    return sub","metadata":{"execution":{"iopub.status.busy":"2022-10-23T04:38:55.017404Z","iopub.execute_input":"2022-10-23T04:38:55.017817Z","iopub.status.idle":"2022-10-23T04:38:55.029907Z","shell.execute_reply.started":"2022-10-23T04:38:55.017784Z","shell.execute_reply":"2022-10-23T04:38:55.028521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):\n    \n    sub = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/sample_submission.csv\")\n    train_ = pd.read_csv(f\"/kaggle/input/tabular-playground-series-oct-2022/train_{i}.csv\", dtype = train_dtypes)\n   \n    train_['team_A_scoring_within_10sec']= train_['team_A_scoring_within_10sec'].astype(int)\n    train_['team_B_scoring_within_10sec']= train_['team_B_scoring_within_10sec'].astype(int)\n\n    y = [train_['team_A_scoring_within_10sec'],train_['team_B_scoring_within_10sec']]\n    \n    train_ = train_.drop(drp_, axis=1)\n    train_ = train_.interpolate(limit_direction = 'both', axis=1)\n    add_feature_(train_)\n    #train_ = train_.drop(drp_1, axis = 1)\n    train_ = train_.drop(drp_2, axis = 1)\n    train_ = train_.drop(drp_3, axis = 1)\n    \n    X = train_\n    display(X)\n    \n    models(X,y,test,model, sub)\n    sub.to_csv(f'submission_{i}.csv', index=False)\n\n    del train_\n    del sub","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2022-10-23T04:38:55.032137Z","iopub.execute_input":"2022-10-23T04:38:55.032536Z","iopub.status.idle":"2022-10-23T05:27:08.117306Z","shell.execute_reply.started":"2022-10-23T04:38:55.032500Z","shell.execute_reply":"2022-10-23T05:27:08.114589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_0 = pd.read_csv('submission_0.csv')\ntrain_1 = pd.read_csv('submission_1.csv')\ntrain_2 = pd.read_csv('submission_2.csv')\ntrain_3 = pd.read_csv('submission_3.csv')\ntrain_4 = pd.read_csv('submission_4.csv')\ntrain_5 = pd.read_csv('submission_5.csv')\ntrain_6 = pd.read_csv('submission_6.csv')\ntrain_7 = pd.read_csv('submission_7.csv')\ntrain_8 = pd.read_csv('submission_8.csv')\ntrain_9 = pd.read_csv('submission_9.csv')\n\ntrain_0, train_1, train_2, train_3, train_4, train_5,train_6,train_7,train_8, train_9","metadata":{"execution":{"iopub.status.busy":"2022-10-23T05:27:08.120478Z","iopub.execute_input":"2022-10-23T05:27:08.121127Z","iopub.status.idle":"2022-10-23T05:27:11.519142Z","shell.execute_reply.started":"2022-10-23T05:27:08.121033Z","shell.execute_reply":"2022-10-23T05:27:11.517990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = (train_0 + train_1 + train_2 + train_3 + train_4 + train_5 + train_6 +train_7 + train_8 + train_9)/10\nsub['id'] = sub['id'].astype(int)\nsub.to_csv('submission_10.csv', index=False)\nsub","metadata":{"execution":{"iopub.status.busy":"2022-10-23T05:27:11.521382Z","iopub.execute_input":"2022-10-23T05:27:11.522138Z","iopub.status.idle":"2022-10-23T05:27:14.291259Z","shell.execute_reply.started":"2022-10-23T05:27:11.522092Z","shell.execute_reply":"2022-10-23T05:27:14.290064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* This is the results for each training dataset. Unfortunately, while the log-loss values are lower than the previous model, the results(public score)are slightly higher. \n* I added the angle value θ(direction angle) and got a slightly better result.\n* This time, I tried a different set of conditions.(Delete velocityand boost data)","metadata":{}},{"cell_type":"code","source":"del train_0\ndel train_1\ndel train_2\ndel train_3\ndel train_4\ndel train_5\ndel train_6\ndel train_7\ndel train_8\ndel train_9\ndel test\ndel sub","metadata":{"execution":{"iopub.status.busy":"2022-10-23T05:27:14.293289Z","iopub.execute_input":"2022-10-23T05:27:14.293772Z","iopub.status.idle":"2022-10-23T05:27:14.313856Z","shell.execute_reply.started":"2022-10-23T05:27:14.293725Z","shell.execute_reply":"2022-10-23T05:27:14.312610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# One more thing","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))\n\ntest_dtypes = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/test_dtypes.csv\")\ntest_dtypes = dict(test_dtypes.to_records(index=False))\n\ntest = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/test.csv\")\ntest = test.interpolate(limit_direction = 'both', axis=1)\nadd_feature_(test)\ntest = test.drop(['id'], axis = 1)\ntest = test.drop(drp_1, axis = 1)\ntest = test.drop(drp_2, axis = 1)\ntest = test.drop(drp_3, axis = 1)\ndisplay(test)\n\nsub = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-10-23T05:27:14.315474Z","iopub.execute_input":"2022-10-23T05:27:14.315885Z","iopub.status.idle":"2022-10-23T05:27:41.659994Z","shell.execute_reply.started":"2022-10-23T05:27:14.315848Z","shell.execute_reply":"2022-10-23T05:27:41.658670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_splits = 3\nseed = 42\n\nparams = {'objective':'binary',\n          'metric' : 'auc',\n          'seed': 42,\n          'num_leaves' : 64,\n          'min_child_samples': 20,\n          'max_depth' :7,\n          'n_estimators': 300,\n          'learning_rate': 0.1,\n         }\n\nmodel = lgb.LGBMClassifier(**params)   ","metadata":{"execution":{"iopub.status.busy":"2022-10-23T05:27:41.662009Z","iopub.execute_input":"2022-10-23T05:27:41.663198Z","iopub.status.idle":"2022-10-23T05:27:41.670562Z","shell.execute_reply.started":"2022-10-23T05:27:41.663144Z","shell.execute_reply":"2022-10-23T05:27:41.669410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def models_(X,y,test, n_splits, model, sub):\n   \n    for i in range(2):\n        cv = list(StratifiedKFold(n_splits = n_splits, shuffle=True, random_state = seed).split(X, y[i]))\n        preds = []\n        for nfold in np.arange(n_splits):\n            idx_train, idx_val = cv[nfold][0], cv[nfold][1]\n            X_train, y_train = X.iloc[idx_train], y[i].iloc[idx_train]\n            X_val, y_val = X.iloc[idx_val], y[i].iloc[idx_val]\n            model.fit(X_train,y_train)\n            pred_ = model.predict_proba(X_val)[:,1]\n            loss = log_loss(y_val ,pred_)\n            pred = model.predict_proba(test)[:,1]\n            print(f\"{y[i]}\\n nfold = {nfold} Logloss = {loss}\\n prediction {pred}\\n\")\n            preds.append(pred)\n            \n        if i == 0:\n            sub['team_A_scoring_within_10sec'] = np.mean(np.column_stack(preds), axis = 1)\n        else:\n            sub['team_B_scoring_within_10sec'] = np.mean(np.column_stack(preds), axis = 1)\n            \n    return sub","metadata":{"execution":{"iopub.status.busy":"2022-10-23T05:27:41.672015Z","iopub.execute_input":"2022-10-23T05:27:41.672362Z","iopub.status.idle":"2022-10-23T05:27:41.684494Z","shell.execute_reply.started":"2022-10-23T05:27:41.672325Z","shell.execute_reply":"2022-10-23T05:27:41.683491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_0, train_1, train_2, train_3, train_4\ntrain_ = pd.DataFrame()\nfor i in tqdm(range(0,5)):\n    train_i = pd.read_csv(f\"/kaggle/input/tabular-playground-series-oct-2022/train_{i}.csv\", dtype = train_dtypes)\n    train_ = pd.concat([train_, train_i])\n    del train_i\n\ntrain_['team_A_scoring_within_10sec']= train_['team_A_scoring_within_10sec'].astype(int)\ntrain_['team_B_scoring_within_10sec']= train_['team_B_scoring_within_10sec'].astype(int)\n\ny = [train_['team_A_scoring_within_10sec'],train_['team_B_scoring_within_10sec']]\n    \ntrain_ = train_.drop(drp_, axis=1)\ntrain_ = train_.interpolate(limit_direction = 'both', axis=1)\nadd_feature_(train_)\ntrain_ = train_.drop(drp_1, axis = 1)\ntrain_ = train_.drop(drp_2, axis = 1)\ntrain_ = train_.drop(drp_3, axis = 1)\n    \nX = train_\ndisplay(X)\n    \nmodels_(X,y, test, n_splits, model, sub)\nsub.to_csv(f'submission_sk_1.csv', index=False)\n\ndisplay(sub)\n    \ndel sub\ndel train_","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-10-23T05:27:41.686252Z","iopub.execute_input":"2022-10-23T05:27:41.687219Z","iopub.status.idle":"2022-10-23T06:00:49.379509Z","shell.execute_reply.started":"2022-10-23T05:27:41.687169Z","shell.execute_reply":"2022-10-23T06:00:49.378191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_5, train_6, train_7, train_8 train_9\nsub = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/sample_submission.csv\")\ntrain_ = pd.DataFrame()\nfor i in tqdm(range(5,10)):\n    train_i = pd.read_csv(f\"/kaggle/input/tabular-playground-series-oct-2022/train_{i}.csv\", dtype = train_dtypes)\n    train_ = pd.concat([train_, train_i])\n    del train_i\n\ntrain_['team_A_scoring_within_10sec']= train_['team_A_scoring_within_10sec'].astype(int)\ntrain_['team_B_scoring_within_10sec']= train_['team_B_scoring_within_10sec'].astype(int)\n\ny = [train_['team_A_scoring_within_10sec'],train_['team_B_scoring_within_10sec']]\n    \ntrain_ = train_.drop(drp_, axis=1)\ntrain_ = train_.interpolate(limit_direction = 'both', axis=1)\nadd_feature_(train_)\ntrain_ = train_.drop(drp_1, axis = 1)\ntrain_ = train_.drop(drp_2, axis = 1)\ntrain_ = train_.drop(drp_3, axis = 1)\n    \nX = train_\ndisplay(X)\n    \nmodels_(X,y, test, n_splits, model, sub)\nsub.to_csv(f'submission_sk_2.csv', index=False)\n\ndisplay(sub)\n    \ndel sub\ndel train_\ndel test","metadata":{"execution":{"iopub.status.busy":"2022-10-23T06:34:08.765642Z","iopub.execute_input":"2022-10-23T06:34:08.766149Z","iopub.status.idle":"2022-10-23T07:05:37.342864Z","shell.execute_reply.started":"2022-10-23T06:34:08.766114Z","shell.execute_reply":"2022-10-23T07:05:37.341467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_1 = pd.read_csv('submission_sk_1.csv')\ntrain_2 = pd.read_csv('submission_sk_2.csv')\n\ntrain_1, train_2","metadata":{"execution":{"iopub.status.busy":"2022-10-23T07:13:47.884884Z","iopub.execute_input":"2022-10-23T07:13:47.885999Z","iopub.status.idle":"2022-10-23T07:13:48.460317Z","shell.execute_reply.started":"2022-10-23T07:13:47.885936Z","shell.execute_reply":"2022-10-23T07:13:48.459032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsub = (train_1 + train_2)/2\nsub['id'] = sub['id'].astype(int)\nsub.to_csv('submission_sk.csv', index=False)\nsub\n","metadata":{"execution":{"iopub.status.busy":"2022-10-23T07:14:05.199851Z","iopub.execute_input":"2022-10-23T07:14:05.200309Z","iopub.status.idle":"2022-10-23T07:14:07.824571Z","shell.execute_reply.started":"2022-10-23T07:14:05.200274Z","shell.execute_reply":"2022-10-23T07:14:07.823364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This time, I tried with StratifiedKFold, it takes more time.⏱","metadata":{}},{"cell_type":"markdown","source":"# next steps 💡\n* Change the seed, test_size, parameter( using oputna), Kfold, or try other models.\n","metadata":{}},{"cell_type":"markdown","source":"# Summary and Conclution\n* This game is played using a car🏎 that it can rotate(flip) with rocket engines🚀, which means speedy💨 and strong impact💥 when hitting the ball.⚽\n* It was a very large data set, so I had to do some work.👀\n* Careful selection of feature has reduced time and memory usage, and improved the score.\n> * Thank you for reading. Good Luck!","metadata":{}}]}