{"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":"# Sorting a Teams Positions\n\nThe player ordering in the dataset is random, and this adds extra ambiguity for a model to tackle. \n\nInstead we will order the players by their distance from their own goal(or you could switch to the opponent goal for the opposite effect). We fill the null values of demolished players by the coordinates of their goals, this ensures they always appear first in the ordering, and their coordinate values will also be closer to their respawn point. p3/p6 will be the players always closest to the opponent's goal, and maybe this will let the model give more importance to that player(or maybe not).","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom tqdm import tqdm, trange\nimport gc\npd.set_option('max_columns', 200)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-10-15T22:20:13.892890Z","iopub.execute_input":"2022-10-15T22:20:13.893350Z","iopub.status.idle":"2022-10-15T22:20:13.900129Z","shell.execute_reply.started":"2022-10-15T22:20:13.893317Z","shell.execute_reply":"2022-10-15T22:20:13.899027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"game_info_cols = [\n    'game_num', 'event_id', 'event_time'\n]\n\nball_cols = [\n    f'ball_{i}_{j}' for i in ['pos', 'vel'] for j in list('xyz')\n]\n\nteam_1_cols = [\n    'p0_pos_x', 'p0_pos_y', 'p0_pos_z', 'p0_vel_x', 'p0_vel_y', 'p0_vel_z', 'p0_boost',\n    'p1_pos_x', 'p1_pos_y', 'p1_pos_z', 'p1_vel_x', 'p1_vel_y', 'p1_vel_z', 'p1_boost',\n    'p2_pos_x', 'p2_pos_y', 'p2_pos_z', 'p2_vel_x', 'p2_vel_y', 'p2_vel_z', 'p2_boost'\n]\n\nteam_2_cols = [\n    'p3_pos_x', 'p3_pos_y', 'p3_pos_z', 'p3_vel_x', 'p3_vel_y', 'p3_vel_z', 'p3_boost',\n    'p4_pos_x', 'p4_pos_y', 'p4_pos_z', 'p4_vel_x', 'p4_vel_y', 'p4_vel_z', 'p4_boost',\n    'p5_pos_x', 'p5_pos_y', 'p5_pos_z', 'p5_vel_x', 'p5_vel_y', 'p5_vel_z', 'p5_boost'\n]\n\nscoring_next_cols = [\n    'player_scoring_next', 'team_scoring_next'\n]\n\nboost_cols = [\n    f'boost{i}_timer' for i in range(6)\n]\n\ntarget_cols = [\n    f'team_{i}_scoring_within_10sec' for i in list('AB')\n]\n","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-10-15T22:20:13.902084Z","iopub.execute_input":"2022-10-15T22:20:13.902384Z","iopub.status.idle":"2022-10-15T22:20:13.913813Z","shell.execute_reply.started":"2022-10-15T22:20:13.902357Z","shell.execute_reply":"2022-10-15T22:20:13.912759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_data(num):\n    df = pd.read_feather(f'../input/fast-loading-high-compression-with-feather/feather_data/train_{num}_compressed.ftr')\n    return df\n\ndef sort_team(data, GOAL):\n    \n    # Choose the position columns only \n    p0 = data[:, :7]\n    p1 = data[:, 7:14]\n    p2 = data[:, 14:21]\n    \n    # Take distance of each player from their own goal\n    dists1 = np.sqrt(np.sum((p0[:, :3] - GOAL)**2, axis=1))\n    dists2 = np.sqrt(np.sum((p1[:, :3] - GOAL)**2, axis=1))\n    dists3 = np.sqrt(np.sum((p2[:, :3] - GOAL)**2, axis=1))\n    \n    # Stack the three dist arrays together, and argsort\n    comb = np.array([[dists1, dists2, dists3]]).T\n    ind = comb.argsort(axis=1)\n    \n    # Setting the dimensions to be able to \n    # fix indices accoring to the sorted indices\n    arrcomb = data.reshape(data.shape[0], 3, -1)    \n    ind = ind.reshape(ind.shape[0], ind.shape[1])\n    \n    # For each row, setting elements in the order of the sorted indices\n    ans = arrcomb[ np.arange(arrcomb.shape[0])[:, None], ind ]\n    ans = ans.reshape(data.shape[0], -1)\n    \n    del p0, p1, p2, dists1, dists2, dists3, comb, ind, arrcomb\n    gc.collect()\n    \n    return ans\n\n\n\nfor j in trange(10):\n    df = get_train_data(j)\n    \n    # TEAM 1 Sorting\n    team1 = df[team_1_cols]\n    team1[[f'p{i}_pos_x' for i in range(3)]].fillna(0, inplace=True)\n    team1[[f'p{i}_pos_y' for i in range(3)]].fillna(-100, inplace=True)\n    team1[[f'p{i}_pos_z' for i in range(3)]].fillna(5, inplace=True)\n    team1.fillna(0, inplace=True)\n    team1_sorted = sort_team(team1.values, np.array([0, -100, 5]))\n    \n    team2 = df[team_2_cols]\n    team2[[f'p{i}_pos_x' for i in range(3, 6)]].fillna(0, inplace=True)\n    team2[[f'p{i}_pos_y' for i in range(3, 6)]].fillna(100, inplace=True)\n    team2[[f'p{i}_pos_z' for i in range(3, 6)]].fillna(5, inplace=True)\n    team2.fillna(0, inplace=True)\n    team2_sorted = sort_team(team2.values, np.array([0, 100, 5]))\n    \n    new_df = pd.DataFrame()\n    \n    for col in game_info_cols + ball_cols:\n        new_df[col] = df.loc[:, col]\n    \n    for i, col in enumerate(team_1_cols):\n        new_df[col] = team1_sorted[:, i]\n        \n    for i, col in enumerate(team_2_cols):\n        new_df[col] = team2_sorted[:, i]\n    \n    for col in boost_cols + scoring_next_cols + target_cols:\n        new_df[col] = df.loc[:, col]\n    \n        \n    new_df.to_feather(f'train_{j}_sorted.ftr')\n    \n    del df, new_df, team1, team1_sorted, team2, team2_sorted\n    gc.collect()\n    \n    \n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-15T22:20:13.915665Z","iopub.execute_input":"2022-10-15T22:20:13.916020Z","iopub.status.idle":"2022-10-15T22:20:56.944086Z","shell.execute_reply.started":"2022-10-15T22:20:13.915981Z","shell.execute_reply":"2022-10-15T22:20:56.942958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ntest = pd.read_feather('../input/fast-loading-high-compression-with-feather/feather_data/test_compressed.ftr')\nids = test['id']\ntest = test.drop(['id'], axis=1)\n\nteam1 = test[team_1_cols]\nteam1[[f'p{i}_pos_x' for i in range(3)]].fillna(0, inplace=True)\nteam1[[f'p{i}_pos_y' for i in range(3)]].fillna(-100, inplace=True)\nteam1[[f'p{i}_pos_z' for i in range(3)]].fillna(5, inplace=True)\nteam1.fillna(0, inplace=True)\n\nteam2 = test[team_2_cols]\nteam2[[f'p{i}_pos_x' for i in range(3, 6)]].fillna(0, inplace=True)\nteam2[[f'p{i}_pos_y' for i in range(3, 6)]].fillna(100, inplace=True)\nteam2[[f'p{i}_pos_z' for i in range(3, 6)]].fillna(5, inplace=True)\nteam2.fillna(0, inplace=True)\n\nteam_1_sorted = sort_team(team1.values, np.array([0, -100, 5]))\nteam_2_sorted = sort_team(team2.values, np.array([0, 100, 5]))\n\nnew_test = pd.DataFrame(ids, columns=['id'])\n\nfor col in ball_cols:\n    new_test[col] = test.loc[:, col]\n\nfor i, col in enumerate(team_1_cols):\n    new_test[col] = team_1_sorted[:, i]\n\nfor i, col in enumerate(team_2_cols):\n    new_test[col] = team_2_sorted[:, i]\n\nfor col in boost_cols:\n    new_test[col] = test.loc[:, col]\n\nnew_test.to_feather('test_sorted.ftr')","metadata":{"execution":{"iopub.status.busy":"2022-10-15T22:20:56.946160Z","iopub.execute_input":"2022-10-15T22:20:56.946614Z","iopub.status.idle":"2022-10-15T22:20:58.597642Z","shell.execute_reply.started":"2022-10-15T22:20:56.946570Z","shell.execute_reply":"2022-10-15T22:20:58.596312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(test.head(5))\ndisplay(new_test.head(5))","metadata":{"execution":{"iopub.status.busy":"2022-10-15T22:20:58.600103Z","iopub.execute_input":"2022-10-15T22:20:58.601967Z","iopub.status.idle":"2022-10-15T22:20:58.696724Z","shell.execute_reply.started":"2022-10-15T22:20:58.601915Z","shell.execute_reply":"2022-10-15T22:20:58.695468Z"},"trusted":true},"execution_count":null,"outputs":[]}]}