{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pickle\n\nimport gc\n\nfrom sklearn.preprocessing import LabelEncoder, OneHotEncoder, StandardScaler, RobustScaler, PowerTransformer\nimport lightgbm as lgb\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import KFold, GroupKFold, StratifiedKFold\nfrom sklearn.metrics import log_loss, auc, accuracy_score, roc_auc_score, matthews_corrcoef\n\nfrom tqdm import tqdm\nfrom collections import  defaultdict","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2023-02-28T10:35:45.090047Z","iopub.execute_input":"2023-02-28T10:35:45.091280Z","iopub.status.idle":"2023-02-28T10:35:45.098539Z","shell.execute_reply.started":"2023-02-28T10:35:45.091227Z","shell.execute_reply":"2023-02-28T10:35:45.096934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%load_ext memory_profiler","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:35:48.198413Z","iopub.execute_input":"2023-02-28T10:35:48.198939Z","iopub.status.idle":"2023-02-28T10:35:48.206266Z","shell.execute_reply.started":"2023-02-28T10:35:48.198898Z","shell.execute_reply":"2023-02-28T10:35:48.204958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# class and functions","metadata":{}},{"cell_type":"code","source":"class Rank:\n    def __init__(self, df) :\n        game_play = {str(e):i for i, e in enumerate(df['game_play'].unique())}\n        player = defaultdict(dict)\n        for i in tqdm(range(len(df))) :\n            e = df.iloc[i]\n            gp = game_play[str(e['game_play'])]\n            st = e['step']\n            pid = e['nfl_player_id']\n            player[(gp, st)][pid] = [e['x_position'], e['y_position']]    \n        rank = defaultdict(dict)\n        for e in tqdm(player):\n            for p0 in player[e]:\n                distance = []\n                x0, y0 = player[e][p0]\n                for p1 in player[e]:\n                    x1, y1 = player[e][p1]\n                    d = ((x1-x0)**2 + (y1-y0)**2)**0.5\n                    distance.append([d, p1])\n                distance.sort()\n                rank[(e[0], e[1], p0)] = {pid:i for i, (_, pid) in enumerate(distance)}\n        self.game_play = game_play\n        self.rank = rank\n    def get_rank(self, game_play, step, player1, player2):\n        if game_play not in self.game_play:\n            return float('nan')            \n        gp = self.game_play[game_play]\n        idx = (gp, step, player1)\n        if  idx not in self.rank:\n            return float('nan')\n        if player2 not in self.rank[idx]:\n            return float('nan')\n        return float(self.rank[idx][player2])\n    def contact_id2rank(self, x):\n        p = str(x['contact_id']).split(\"_\")\n        if p[4] == 'G':\n            return 0\n        game_play = p[0] + '_' + p[1]\n        step = int(p[2])\n        player1 = int(p[3])\n        player2 = int(p[4])\n        return self.get_rank(game_play, step, player1, player2)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:35:49.088396Z","iopub.execute_input":"2023-02-28T10:35:49.089105Z","iopub.status.idle":"2023-02-28T10:35:49.102718Z","shell.execute_reply.started":"2023-02-28T10:35:49.089047Z","shell.execute_reply":"2023-02-28T10:35:49.101604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem_usage(train_data):\n    \"\"\" iterate through all the columns of a dataframe and modify the data type\n    to reduce memory usage.\n    \"\"\"\n    start_mem = train_data.memory_usage().sum() / 1024**2\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n    \n    for col in train_data.columns:\n        col_type = str(train_data[col].dtype)\n        if 'int' in col_type or 'float' in col_type:\n            c_min = train_data[col].min()\n            c_max = train_data[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    train_data[col] = train_data[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    train_data[col] = train_data[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    train_data[col] = train_data[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    train_data[col] = train_data[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    train_data[col] = train_data[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    train_data[col] = train_data[col].astype(np.float32)\n                else:\n                    train_data[col] = train_data[col].astype(np.float64)\n\n    end_mem = train_data.memory_usage().sum() / 1024**2\n    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n\n    return train_data","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:35:49.669871Z","iopub.execute_input":"2023-02-28T10:35:49.670606Z","iopub.status.idle":"2023-02-28T10:35:49.682636Z","shell.execute_reply.started":"2023-02-28T10:35:49.670548Z","shell.execute_reply":"2023-02-28T10:35:49.681576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# helmets data","metadata":{}},{"cell_type":"code","source":"test_baseline=pd.read_csv(\"../input/nfl-player-contact-detection/test_baseline_helmets.csv\")\ntest_baseline=test_baseline[test_baseline['view']!='Endzone2']\ntest_baseline=reduce_mem_usage(test_baseline)\ndisplay(test_baseline.shape)\ndisplay(test_baseline.head())","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:35:53.485488Z","iopub.execute_input":"2023-02-28T10:35:53.485964Z","iopub.status.idle":"2023-02-28T10:35:53.694183Z","shell.execute_reply.started":"2023-02-28T10:35:53.485924Z","shell.execute_reply":"2023-02-28T10:35:53.693060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_helmet_dict(helmet):\n    helmet['step'] = ((helmet['frame']-5*59.95 ) *10 / 59.95).astype(int)\n    helmet.loc[helmet['view']==\"Endzone2\", 'view'] = \"Endzone\"\n    helmet['id'] = helmet['game_play'].astype(str) + \"_\" + helmet['step'].astype(str) + \"_\" + \\\n                            helmet['view'].astype(str) + \"_\" + helmet['nfl_player_id'].astype(str)\n    helmet['right'] = helmet['left']+helmet['width']\n    helmet['bottom'] = helmet['top']+helmet['height']\n\n    grouped = helmet.groupby('id')\n    df = grouped.mean()\n    df['left'] = grouped['left'].min()\n    df['right'] = grouped['right'].max()\n    df['top'] = grouped['top'].min()\n    df['bottom'] = grouped['bottom'].max()\n    df['width'] = df['right'] - df['left']\n    df['height'] = df['bottom'] - df['top']\n\n    df = helmet.groupby('id').mean()\n    df = df.drop(['game_key','play_id','frame','nfl_player_id','step'], axis=1)\n\n    game_play, step, view, nfl_player_id = [], [], [], []\n    for i, e in enumerate(df.index) :\n        sep  = str(e).split(\"_\")\n        game_play.append(sep[0] + \"_\" + sep[1])\n        step.append(int(sep[2]))\n        view.append(sep[3])\n        nfl_player_id.append(sep[4])\n\n    df['game_play'] = game_play\n    df['step'] = step\n    df['view'] = view\n    df['nfl_player_id'] = nfl_player_id\n\n    df['game_play'] = df['game_play'].astype(str)\n    df['step'] = df['step'].astype(np.int16)\n    df['nfl_player_id'] = df['nfl_player_id'].astype(np.int32)\n\n    print(df.columns)\n    endzone_dict = defaultdict(tuple)\n    sideline_dict = defaultdict(tuple)\n    for i in tqdm(range(len(df))) :\n        e = df.iloc[i]\n        if e['view'] == 'Endzone':\n            endzone_dict[(e['game_play'], e['step'], e['nfl_player_id'])] = (e['left'], e['width'], e['top'], e['height'], e['right'], e['bottom'])\n        else:\n            sideline_dict[(e['game_play'], e['step'], e['nfl_player_id'])] = (e['left'], e['width'], e['top'], e['height'], e['right'], e['bottom'])\n\n\n    del df, game_play, step, view, nfl_player_id \n    return endzone_dict, sideline_dict","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:35:55.380130Z","iopub.execute_input":"2023-02-28T10:35:55.380621Z","iopub.status.idle":"2023-02-28T10:35:55.400719Z","shell.execute_reply.started":"2023-02-28T10:35:55.380571Z","shell.execute_reply":"2023-02-28T10:35:55.399740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"endzone_dict_test, sideline_dict_test = make_helmet_dict(test_baseline)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:35:56.075124Z","iopub.execute_input":"2023-02-28T10:35:56.075559Z","iopub.status.idle":"2023-02-28T10:35:58.126687Z","shell.execute_reply.started":"2023-02-28T10:35:56.075515Z","shell.execute_reply":"2023-02-28T10:35:58.124984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_baseline\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:35:58.131238Z","iopub.execute_input":"2023-02-28T10:35:58.131871Z","iopub.status.idle":"2023-02-28T10:35:58.308113Z","shell.execute_reply.started":"2023-02-28T10:35:58.131830Z","shell.execute_reply":"2023-02-28T10:35:58.306951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Tracking data","metadata":{}},{"cell_type":"code","source":"test_tracking=pd.read_csv(\"../input/nfl-player-contact-detection/test_player_tracking.csv\")\ntest_tracking=reduce_mem_usage(test_tracking)\ndisplay(test_tracking.shape)\ndisplay(test_tracking.head())","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:01.145974Z","iopub.execute_input":"2023-02-28T10:36:01.146840Z","iopub.status.idle":"2023-02-28T10:36:01.254247Z","shell.execute_reply.started":"2023-02-28T10:36:01.146789Z","shell.execute_reply":"2023-02-28T10:36:01.253150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nclass_le = LabelEncoder()\nclass_le.classes_ = [\n    # offence\n    'QB',\n    'RB',\n    'FB',\n    'HB',\n    'WR',\n    'TE',\n    'C',    \n    'G',\n    'T',\n    'OG',\n    'OT',\n    # deffence\n    'DL',\n    'DE',\n    'DT',\n    'NT',\n    'LB',\n    'MLB',\n    'ILB',\n    'OLB',\n    'CB',\n    'DB',\n    'S',\n    'SS',\n    'FS',\n    # ???\n    'SAF',\n    # special team\n    'K',\n    'P',\n    'LS',    \n]\n# class_le.classes_ = set(train_tracking.position.unique().astype(str)).union(set(test_tracking.position.unique().astype(str)))\ntest_tracking['position'] = class_le.transform(test_tracking['position'])\ndisplay(test_tracking.head())","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:01.977798Z","iopub.execute_input":"2023-02-28T10:36:01.978532Z","iopub.status.idle":"2023-02-28T10:36:02.006266Z","shell.execute_reply.started":"2023-02-28T10:36:01.978493Z","shell.execute_reply":"2023-02-28T10:36:02.005101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_tracking_dict(df):\n    print(df.columns)\n    tracking_dict = defaultdict(tuple)\n    for i in tqdm(range(len(df))):\n        e = df.iloc[i]\n        tracking_dict[(e['game_play'], e['step'],  e['nfl_player_id'])] = tuple(e.to_list()[6:])\n    return tracking_dict","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:02.915780Z","iopub.execute_input":"2023-02-28T10:36:02.916531Z","iopub.status.idle":"2023-02-28T10:36:02.923365Z","shell.execute_reply.started":"2023-02-28T10:36:02.916490Z","shell.execute_reply":"2023-02-28T10:36:02.922052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking_dict_test = make_tracking_dict(test_tracking)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:03.439717Z","iopub.execute_input":"2023-02-28T10:36:03.440389Z","iopub.status.idle":"2023-02-28T10:36:05.822777Z","shell.execute_reply.started":"2023-02-28T10:36:03.440350Z","shell.execute_reply":"2023-02-28T10:36:05.821467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_rank = Rank(test_tracking)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:05.825150Z","iopub.execute_input":"2023-02-28T10:36:05.826088Z","iopub.status.idle":"2023-02-28T10:36:10.536142Z","shell.execute_reply.started":"2023-02-28T10:36:05.826030Z","shell.execute_reply":"2023-02-28T10:36:10.535065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test_tracking\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:10.537490Z","iopub.execute_input":"2023-02-28T10:36:10.538557Z","iopub.status.idle":"2023-02-28T10:36:10.672245Z","shell.execute_reply.started":"2023-02-28T10:36:10.538514Z","shell.execute_reply":"2023-02-28T10:36:10.669910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# READ submission data","metadata":{}},{"cell_type":"code","source":"submission=pd.read_csv(\"../input/nfl-player-contact-detection/sample_submission.csv\")\nsubmission[\"game_play\"]=submission[\"contact_id\"].str[:12]\nsubmission[\"step\"] = submission[\"contact_id\"].str.split(\"_\").str[-3].astype(\"int\")\nsubmission[\"nfl_player_id_1\"] =submission[\"contact_id\"].str.split(\"_\").str[-2].astype(int)\nsubmission[\"nfl_player_id_2\"] =submission[\"contact_id\"].str.split(\"_\").str[-1]\nsubmission=reduce_mem_usage(submission)\ndisplay(submission.head())","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:11.836745Z","iopub.execute_input":"2023-02-28T10:36:11.837265Z","iopub.status.idle":"2023-02-28T10:36:12.396376Z","shell.execute_reply.started":"2023-02-28T10:36:11.837221Z","shell.execute_reply":"2023-02-28T10:36:12.395258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:13.732023Z","iopub.execute_input":"2023-02-28T10:36:13.732402Z","iopub.status.idle":"2023-02-28T10:36:13.861455Z","shell.execute_reply.started":"2023-02-28T10:36:13.732371Z","shell.execute_reply":"2023-02-28T10:36:13.858721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# add rank","metadata":{}},{"cell_type":"code","source":"tqdm.pandas()\n# train_labels['rank'] = train_labels.progress_apply(train_rank.contact_id2rank, axis=1).astype(np.int8)\n# submission['rank'] = submission.progress_apply(test_rank.contact_id2rank, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:14.781207Z","iopub.execute_input":"2023-02-28T10:36:14.782379Z","iopub.status.idle":"2023-02-28T10:36:14.788115Z","shell.execute_reply.started":"2023-02-28T10:36:14.782330Z","shell.execute_reply":"2023-02-28T10:36:14.786796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rank = []\nfor i in tqdm(range(len(submission))):\n    rank.append(test_rank.contact_id2rank(submission.iloc[i]))\nsubmission['rank'] = rank\nsubmission['rank'] = submission['rank'].astype(np.int8)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:15.049958Z","iopub.execute_input":"2023-02-28T10:36:15.051254Z","iopub.status.idle":"2023-02-28T10:36:22.049256Z","shell.execute_reply.started":"2023-02-28T10:36:15.051204Z","shell.execute_reply":"2023-02-28T10:36:22.048043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del train_rank, test_rank\n# _ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:22.052603Z","iopub.execute_input":"2023-02-28T10:36:22.052918Z","iopub.status.idle":"2023-02-28T10:36:22.057675Z","shell.execute_reply.started":"2023-02-28T10:36:22.052886Z","shell.execute_reply":"2023-02-28T10:36:22.056237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(submission.head())","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:22.059883Z","iopub.execute_input":"2023-02-28T10:36:22.060939Z","iopub.status.idle":"2023-02-28T10:36:22.075733Z","shell.execute_reply.started":"2023-02-28T10:36:22.060829Z","shell.execute_reply":"2023-02-28T10:36:22.074501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# add player1 and player2 tracking data","metadata":{}},{"cell_type":"code","source":"import math\ndef add_player(x, use_dict):\n    game_play, step, p1, p2 = x['game_play'], x['step'],x['nfl_player_id_1'], x['nfl_player_id_2']\n#     print(game_play, step, p1, p2)\n    ret1 = [math.nan for _ in range(11)]\n    ret1[0] = ''\n    ret1[1] = -1\n    if (game_play,step,p1) in use_dict:\n        ret1 = list(use_dict[(game_play,step,p1)])\n    else:\n        pass\n\n    ret2 = [math.nan for _ in range(11)]\n    ret2[0] = ''\n    ret2[1] = -1\n    if p2 == 'G' :\n        pass\n    elif (game_play,step,int(p2)) in use_dict:\n        ret2 = list(use_dict[(game_play,step, int(p2))])\n    else:\n        pass\n            \n    return tuple(ret1+ret2)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:22.079302Z","iopub.execute_input":"2023-02-28T10:36:22.079717Z","iopub.status.idle":"2023-02-28T10:36:22.089345Z","shell.execute_reply.started":"2023-02-28T10:36:22.079615Z","shell.execute_reply":"2023-02-28T10:36:22.088146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['team_1', 'position_1', 'jersey_number_1', 'x_position_1', 'y_position_1','speed_1', 'distance_1', 'direction_1', 'orientation_1', 'acceleration_1', 'sa_1',\n          'team_2', 'position_2', 'jersey_number_2', 'x_position_2', 'y_position_2','speed_2', 'distance_2', 'direction_2', 'orientation_2', 'acceleration_2', 'sa_2']","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:22.090746Z","iopub.execute_input":"2023-02-28T10:36:22.091144Z","iopub.status.idle":"2023-02-28T10:36:22.098705Z","shell.execute_reply.started":"2023-02-28T10:36:22.091104Z","shell.execute_reply":"2023-02-28T10:36:22.097580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item = [] \nfor i in tqdm(range(len(submission))):\n    item.append(add_player(submission.loc[i, ['game_play','step','nfl_player_id_1','nfl_player_id_2']], tracking_dict_test))\n\nsubmission = pd.concat([submission, pd.DataFrame(item, columns=labels)], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:22.100886Z","iopub.execute_input":"2023-02-28T10:36:22.102108Z","iopub.status.idle":"2023-02-28T10:36:52.201630Z","shell.execute_reply.started":"2023-02-28T10:36:22.102049Z","shell.execute_reply":"2023-02-28T10:36:52.200610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(submission.head())","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:52.203032Z","iopub.execute_input":"2023-02-28T10:36:52.203513Z","iopub.status.idle":"2023-02-28T10:36:52.230714Z","shell.execute_reply.started":"2023-02-28T10:36:52.203467Z","shell.execute_reply":"2023-02-28T10:36:52.229672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_labels=reduce_mem_usage(train_labels)\nsubmission=reduce_mem_usage(submission)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:52.231898Z","iopub.execute_input":"2023-02-28T10:36:52.232249Z","iopub.status.idle":"2023-02-28T10:36:52.280519Z","shell.execute_reply.started":"2023-02-28T10:36:52.232210Z","shell.execute_reply":"2023-02-28T10:36:52.279457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# add distance ","metadata":{}},{"cell_type":"code","source":"#distance\nsubmission[\"distance\"]=np.sqrt(\n    np.square(submission['x_position_1']-submission['x_position_2']) +\n    np.square(submission['y_position_1']-submission['y_position_2']))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:52.281883Z","iopub.execute_input":"2023-02-28T10:36:52.282256Z","iopub.status.idle":"2023-02-28T10:36:52.293150Z","shell.execute_reply.started":"2023-02-28T10:36:52.282217Z","shell.execute_reply":"2023-02-28T10:36:52.292100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(submission.head())","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:52.297593Z","iopub.execute_input":"2023-02-28T10:36:52.298339Z","iopub.status.idle":"2023-02-28T10:36:52.322614Z","shell.execute_reply.started":"2023-02-28T10:36:52.298303Z","shell.execute_reply":"2023-02-28T10:36:52.321524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# select train distance < th","metadata":{}},{"cell_type":"code","source":"distance_th = 2\nsel_test = submission[(submission['distance'] < distance_th)|(submission['distance'].isna())].copy()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:52.324015Z","iopub.execute_input":"2023-02-28T10:36:52.324539Z","iopub.status.idle":"2023-02-28T10:36:52.339879Z","shell.execute_reply.started":"2023-02-28T10:36:52.324503Z","shell.execute_reply":"2023-02-28T10:36:52.338933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# add helmet","metadata":{}},{"cell_type":"code","source":"#'left', 'width', 'top', 'height', 'right', 'bottom'\ndef add_player(x, use_dict):\n    game_play, step, p1, p2 = x['game_play'], x['step'],x['nfl_player_id_1'], x['nfl_player_id_2']\n#     print(game_play, step, p1, p2)\n    ret1 = [math.nan for _ in range(6)]\n    if (game_play,step,p1) in use_dict:\n        ret1 = list(use_dict[(game_play,step,p1)])\n    else:\n        pass\n\n    ret2 = [math.nan for _ in range(6)]\n    if p2 == 'G' :\n        pass\n    elif (game_play,step,int(p2)) in use_dict:\n        ret2 = list(use_dict[(game_play,step, int(p2))])\n    else:\n        pass\n            \n    return tuple(ret1+ret2)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:52.341312Z","iopub.execute_input":"2023-02-28T10:36:52.341862Z","iopub.status.idle":"2023-02-28T10:36:52.349344Z","shell.execute_reply.started":"2023-02-28T10:36:52.341823Z","shell.execute_reply":"2023-02-28T10:36:52.348123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['left_e_1', 'width_e_1', 'top_e_1', 'height_e_1', 'right_e_1', 'bottom_e_1',\n          'left_e_2', 'width_e_2', 'top_e_2', 'height_e_2', 'right_e_2', 'bottom_e_2',]\nsel_test[labels] = sel_test[['game_play','step','nfl_player_id_1','nfl_player_id_2']].progress_apply(add_player, args=(endzone_dict_test,), axis=1, result_type='expand')","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:52.350739Z","iopub.execute_input":"2023-02-28T10:36:52.351128Z","iopub.status.idle":"2023-02-28T10:36:52.768481Z","shell.execute_reply.started":"2023-02-28T10:36:52.351092Z","shell.execute_reply":"2023-02-28T10:36:52.767052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['left_s_1', 'width_s_1', 'top_s_1', 'height_s_1', 'right_s_1', 'bottom_s_1',\n          'left_s_2', 'width_s_2', 'top_s_2', 'height_s_2', 'right_s_2', 'bottom_s_2',]\nsel_test[labels] = sel_test[['game_play','step','nfl_player_id_1','nfl_player_id_2']].progress_apply(add_player, args=(sideline_dict_test,), axis=1, result_type='expand')","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:52.769835Z","iopub.execute_input":"2023-02-28T10:36:52.771324Z","iopub.status.idle":"2023-02-28T10:36:53.181881Z","shell.execute_reply.started":"2023-02-28T10:36:52.771282Z","shell.execute_reply":"2023-02-28T10:36:53.180773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(sel_test.head())","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:53.183462Z","iopub.execute_input":"2023-02-28T10:36:53.183835Z","iopub.status.idle":"2023-02-28T10:36:53.209451Z","shell.execute_reply.started":"2023-02-28T10:36:53.183796Z","shell.execute_reply":"2023-02-28T10:36:53.208203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sel_test=reduce_mem_usage(sel_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:53.210910Z","iopub.execute_input":"2023-02-28T10:36:53.211426Z","iopub.status.idle":"2023-02-28T10:36:53.251819Z","shell.execute_reply.started":"2023-02-28T10:36:53.211390Z","shell.execute_reply":"2023-02-28T10:36:53.250760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# calc iou","metadata":{}},{"cell_type":"code","source":"# 矩形aと、複数の矩形bのIoUを計算\ndef iou_np(a, b):\n    # aは1つの矩形を表すshape=(4,)のnumpy配列\n    # array([xmin, ymin, xmax, ymax])\n    # bは任意のN個の矩形を表すshape=(N, 4)のnumpy配列\n    # 2次元目の4は、array([xmin, ymin, xmax, ymax])\n    \n    # 矩形aの面積a_areaを計算\n    a_area = (a[2] - a[0] + 1) \\\n             * (a[3] - a[1] + 1)\n    # bに含まれる矩形のそれぞれの面積b_areaを計算\n    # shape=(N,)のnumpy配列。Nは矩形の数\n    b_area = (b[:,2] - b[:,0] + 1) \\\n             * (b[:,3] - b[:,1] + 1)\n    \n    # aとbの矩形の共通部分(intersection)の面積を計算するために、\n    # N個のbについて、aとの共通部分のxmin, ymin, xmax, ymaxを一気に計算\n    abx_mn = np.maximum(a[0], b[:,0]) # xmin\n    aby_mn = np.maximum(a[1], b[:,1]) # ymin\n    abx_mx = np.minimum(a[2], b[:,2]) # xmax\n    aby_mx = np.minimum(a[3], b[:,3]) # ymax\n    # 共通部分の矩形の幅を計算。共通部分が無ければ0\n    w = np.maximum(0, abx_mx - abx_mn + 1)\n    # 共通部分の矩形の高さを計算。共通部分が無ければ0\n    h = np.maximum(0, aby_mx - aby_mn + 1)\n    # 共通部分の面積を計算。共通部分が無ければ0\n    intersect = w*h\n    \n    # N個のbについて、aとのIoUを一気に計算\n    iou = intersect / (a_area + b_area - intersect)\n    return iou","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:53.253294Z","iopub.execute_input":"2023-02-28T10:36:53.253691Z","iopub.status.idle":"2023-02-28T10:36:53.262974Z","shell.execute_reply.started":"2023-02-28T10:36:53.253652Z","shell.execute_reply":"2023-02-28T10:36:53.261904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = np.array([sel_test['left_e_1'], \n                     sel_test['top_e_1'],\n                     sel_test['left_e_1'] + sel_test['width_e_1'],\n                     sel_test['top_e_1'] + sel_test['height_e_1']\n             ])\nb = np.column_stack((sel_test['left_e_2'], \n                     sel_test['top_e_2'],\n                     sel_test['left_e_2'] + sel_test['width_e_2'],\n                     sel_test['top_e_2'] + sel_test['height_e_2']\n                     ))\niou = iou_np(a, b)\nsel_test['iou_e'] = iou","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:53.264446Z","iopub.execute_input":"2023-02-28T10:36:53.265113Z","iopub.status.idle":"2023-02-28T10:36:53.277007Z","shell.execute_reply.started":"2023-02-28T10:36:53.265060Z","shell.execute_reply":"2023-02-28T10:36:53.275985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = np.array([sel_test['left_s_1'], \n                     sel_test['top_s_1'],\n                     sel_test['left_s_1'] + sel_test['width_s_1'],\n                     sel_test['top_s_1'] + sel_test['height_s_1']\n             ])\nb = np.column_stack((sel_test['left_s_2'], \n                     sel_test['top_s_2'],\n                     sel_test['left_s_2'] + sel_test['width_s_2'],\n                     sel_test['top_s_2'] + sel_test['height_s_2']\n                     ))\niou = iou_np(a, b)\nsel_test['iou_s'] = iou","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:53.278704Z","iopub.execute_input":"2023-02-28T10:36:53.279058Z","iopub.status.idle":"2023-02-28T10:36:53.291103Z","shell.execute_reply.started":"2023-02-28T10:36:53.279023Z","shell.execute_reply":"2023-02-28T10:36:53.289957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del a, b, iou\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:53.292612Z","iopub.execute_input":"2023-02-28T10:36:53.292961Z","iopub.status.idle":"2023-02-28T10:36:53.418971Z","shell.execute_reply.started":"2023-02-28T10:36:53.292926Z","shell.execute_reply":"2023-02-28T10:36:53.417552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# add delay iou and distance, acceleration, sa","metadata":{}},{"cell_type":"markdown","source":"### endzone_dict_train, sideline_dict_train \nIndex(['left', 'width', 'top', 'height', 'right', 'bottom', 'game_play','step', 'view', 'nfl_player_id'],\n\n### tracking_dict_train\nIndex(['team', 'position', 'jersey_number', 'x_position', 'y_position',\n       'speed', 'distance', 'direction', 'orientation', 'acceleration', 'sa'],","metadata":{}},{"cell_type":"code","source":"def iou(a, b):\n    # a, bは矩形を表すリストで、a=[xmin, ymin, xmax, ymax]\n    ax_mn, ay_mn, ax_mx, ay_mx = a[0], a[1], a[2], a[3]\n    bx_mn, by_mn, bx_mx, by_mx = b[0], b[1], b[2], b[3]\n\n    a_area = (ax_mx - ax_mn + 1) * (ay_mx - ay_mn + 1)\n    b_area = (bx_mx - bx_mn + 1) * (by_mx - by_mn + 1)\n\n    abx_mn = max(ax_mn, bx_mn)\n    aby_mn = max(ay_mn, by_mn)\n    abx_mx = min(ax_mx, bx_mx)\n    aby_mx = min(ay_mx, by_mx)\n    w = max(0, abx_mx - abx_mn + 1)\n    h = max(0, aby_mx - aby_mn + 1)\n    intersect = w*h\n\n    iou = intersect / (a_area + b_area - intersect)\n    return iou\n\ndef calc_delay(x, delay, endzone_dict, sideline_dict, tracking_dict, rank_dict):\n#     print(x['contact_id'])\n    game_play, step, p1, p2 = x['game_play'], x['step'],x['nfl_player_id_1'], x['nfl_player_id_2']\n#     if p2 == 'G':\n#         return [math.nan for _ in range(9)]\n\n    rank = math.nan\n    if p2 != 'G':\n        rank = rank_dict.get_rank(game_play, step+delay, p1, int(p2)) \n\n    # IOU_e\n    iou_e = math.nan\n    \n    p1_e_x1, p1_e_y1 = math.nan, math.nan\n    r1 = endzone_dict[(game_play, step + delay, p1)]\n    if len(r1) != 0:\n        p1_e_x1, p1_e_y1 = r1[0] + r1[1]/2, r1[2] + r1[3]/2\n   \n    if p2 != 'G':\n        r1 = endzone_dict[(game_play, step + delay, p1)]\n        r2 = endzone_dict[(game_play, step + delay, int(p2))]\n        if len(r1) == 0 or len(r2) == 0: pass\n        else :\n            iou_e = iou([r1[0], r1[2], r1[4], r1[5]], [r2[0], r2[2], r2[4], r2[5]])\n    \n            \n    # IOU_s\n    iou_s = math.nan\n    if p2 != 'G':\n        r1 = sideline_dict[(game_play, step + delay, p1)]\n        r2 = sideline_dict[(game_play, step + delay, int(p2))]\n        if len(r1) == 0 or len(r2) == 0: pass\n        else :\n            iou_s = iou([r1[0], r1[2], r1[4], r1[5]], [r2[0], r2[2], r2[4], r2[5]])\n            \n    dist = math.nan\n    x_p1, y_p1, speed_p1, ori_p1, acc_p1, sa_p1 = math.nan, math.nan, math.nan, math.nan, math.nan, math.nan\n    r1 = tracking_dict[(game_play, step + delay, p1)]\n    if len(r1) != 0: \n        x_p1, y_p1, speed_p1, ori_p1, acc_p1, sa_p1 = r1[3], r1[4], r1[5], r1[8], r1[9], r1[10]\n\n    x_p2, y_p2, speed_p2, ori_p2, acc_p2, sa_p2 = math.nan, math.nan, math.nan, math.nan, math.nan, math.nan\n    if p2 != 'G':\n        r2 = tracking_dict[(game_play, step + delay, int(p2))]\n        if len(r2) != 0:\n            x_p2, y_p2, speed_p2, ori_p2, acc_p2, sa_p2 = r2[3], r2[4], r2[5], r2[8],  r2[9], r2[10]\n\n    dist = math.sqrt(math.sqrt((x_p2 - x_p1) ** 2 + (y_p2 - y_p1) ** 2))\n        \n    inner_product =  math.cos(math.radians(ori_p1)) * math.cos(math.radians(ori_p2)) + \\\n                     math.sin(math.radians(ori_p1)) * math.sin(math.radians(ori_p2))\n    \n    return p1_e_x1, p1_e_y1 ,iou_e, iou_s, speed_p1, acc_p1, sa_p1, speed_p2, acc_p2, sa_p2, dist, inner_product, rank\n#     return iou_e, iou_s, speed_p1, speed_p2, dist","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:53.420831Z","iopub.execute_input":"2023-02-28T10:36:53.421876Z","iopub.status.idle":"2023-02-28T10:36:53.441752Z","shell.execute_reply.started":"2023-02-28T10:36:53.421836Z","shell.execute_reply":"2023-02-28T10:36:53.440789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_delay_range(x, delay, rng, endzone_dict, sideline_dict, tracking_dict):\n#     print(x['contact_id'])\n    game_play, step, p1, p2 = x['game_play'], x['step'],x['nfl_player_id_1'], x['nfl_player_id_2']\n#     if p2 == 'G':\n#         return [math.nan for _ in range(9)]\n\n    \n    iou_e = math.nan\n    iou_s = math.nan\n    \n    e_x11, e_y11, e_x12, e_y12 = math.inf, math.inf, -math.inf, -math.inf, \n    e_x21, e_y21, e_x22, e_y22 = math.inf, math.inf, -math.inf, -math.inf, \n\n    s_x11, s_y11, s_x12, s_y12 = math.inf, math.inf, -math.inf, -math.inf,\n    s_x21, s_y21, s_x22, s_y22 = math.inf, math.inf, -math.inf, -math.inf,\n    \n    x_p1, y_p1, speed_p1, acc_p1, sa_p1 = 0,0,0,0,0\n    x_p2, y_p2, speed_p2, acc_p2, sa_p2 = 0,0,0,0,0\n    cnt_e, cnt_s, cnt_p1, cnt_p2 = 0,0,0,0\n    \n    for e in range(delay-rng, delay+rng+1):\n#         print(e)\n        # IOU_e\n        if p2 != 'G':\n            r1 = endzone_dict[(game_play, step + e, p1)]\n            r2 = endzone_dict[(game_play, step + e, int(p2))]\n            if len(r1) == 0 or len(r2) == 0: pass\n            else :\n                e_x11 = min(e_x11, r1[0])\n                e_y11 = min(e_y11, r1[2])\n                e_x12 = max(e_x12, r1[4])\n                e_y12 = max(e_y12, r1[5])\n                e_x21 = min(e_x21, r2[0])\n                e_y21 = min(e_y21, r2[2])\n                e_x22 = max(e_x22, r2[4])\n                e_y22 = max(e_y22, r2[5])\n#                 if cnt_e == 0:\n#                     iou_e = iou([r1[0], r1[2], r1[4], r1[5]], [r2[0], r2[2], r2[4], r2[5]])\n#                 else:\n#                     iou_e = max(iou_e, iou([r1[0], r1[2], r1[4], r1[5]], [r2[0], r2[2], r2[4], r2[5]]))\n                cnt_e += 1\n\n        # IOU_s\n        if p2 != 'G':\n            r1 = sideline_dict[(game_play, step + e, p1)]\n            r2 = sideline_dict[(game_play, step + e, int(p2))]\n            if len(r1) == 0 or len(r2) == 0: pass\n            else :\n                s_x11 = min(s_x11, r1[0])\n                s_y11 = min(s_y11, r1[2])\n                s_x12 = max(s_x12, r1[4])\n                s_y12 = max(s_y12, r1[5])\n                s_x21 = min(s_x21, r2[0])\n                s_y21 = min(s_y21, r2[2])\n                s_x22 = max(s_x22, r2[4])\n                s_y22 = max(s_y22, r2[5])\n#                 if cnt_s == 0:\n#                     iou_s = iou([r1[0], r1[2], r1[4], r1[5]], [r2[0], r2[2], r2[4], r2[5]])\n#                 else:\n#                     iou_s = max(iou_s, iou([r1[0], r1[2], r1[4], r1[5]], [r2[0], r2[2], r2[4], r2[5]]))\n                cnt_s += 1\n\n        dist = math.nan\n        r1 = tracking_dict[(game_play, step + e, p1)]\n        if len(r1) != 0: \n            x_p1 += r1[3]\n            y_p1 += r1[4]\n            speed_p1 += r1[5]\n            acc_p1 += r1[9]\n            sa_p1 += r1[10]\n            cnt_p1 += 1\n\n        if p2 != 'G':\n            r2 = tracking_dict[(game_play, step + e, int(p2))]\n            if len(r2) != 0:\n                x_p2 += r2[3]\n                y_p2 += r2[4]\n                speed_p2 += r2[5]\n                acc_p2 += r2[9]\n                sa_p2 += r2[10]\n#                 x_p2, y_p2, speed_p2, acc_p2, sa_p2 = r2[3], r2[4], r2[5], r2[9], r2[10]\n                cnt_p2 += 1\n    \n    if cnt_e != 0: iou_e /= cnt_e\n    if cnt_s != 0: iou_s /= cnt_s\n    if cnt_p1 != 0:\n        x_p1 /= cnt_p1\n        y_p1 /= cnt_p1\n        speed_p1 /= cnt_p1\n        acc_p1 /= cnt_p1\n        sa_p1 /= cnt_p1\n    else:\n        x_p1, y_p1, speed_p1, acc_p1, sa_p1 = math.nan, math.nan, math.nan, math.nan, math.nan\n\n    if cnt_p2 != 0:\n        x_p2 /= cnt_p2\n        y_p2 /= cnt_p2\n        speed_p2 /= cnt_p2\n        acc_p2 /= cnt_p2\n        sa_p2 /= cnt_p2\n    else:\n        x_p2, y_p2, speed_p2, acc_p2, sa_p2 = math.nan, math.nan, math.nan, math.nan, math.nan\n        \n    \n    dist = math.sqrt(math.sqrt((x_p2 - x_p1) ** 2 + (y_p2 - y_p1) ** 2))\n    if cnt_e != 0:\n        iou_e = iou([e_x11, e_y11, e_x12, e_y12],[e_x21, e_y21, e_x22, e_y22])\n    if cnt_s != 0:\n        iou_s = iou([s_x11, s_y11, s_x12, s_y12],[s_x21, s_y21, s_x22, s_y22])\n#     print(\"cnt = \" , cnt_e, cnt_e, cnt_p1, cnt_p2)\n    return iou_e, iou_s, speed_p1, acc_p1, sa_p1, speed_p2, acc_p2, sa_p2, dist","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:53.445034Z","iopub.execute_input":"2023-02-28T10:36:53.445318Z","iopub.status.idle":"2023-02-28T10:36:53.467567Z","shell.execute_reply.started":"2023-02-28T10:36:53.445282Z","shell.execute_reply":"2023-02-28T10:36:53.466474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n# labels = ['iou_e', 'iou_s', 'speed_p1', 'speed_p2','distance']\nlabels = ['pos_e_x', 'pos_e_y','iou_e', 'iou_s', 'speed_p1', 'acc_p1', 'sa_p1', \n          'speed_p2', 'acc_p2', 'sa_p2','distance', 'product', 'rank']\n\n#delays = [-1,-2,-3,-4,-8,-12,-16,-20,-24, 1,2,3,4,8,12,16,20,24]\n#delays = [-1, -2, -3, -5, -8, -13, -21, 1, 2, 3, 5, 8, 13, 21] # フィボナッチ数列\ndelays = [-1, -2, -3, -5, -8, -13, -21, -34, 1, 2, 3] # フィボナッチ数列\n\n# for delay in delays:\n#     l = [x + '_delay_' + str(delay) for x in labels]\n#     print(l)\n#     sel_train[l] = sel_train[['game_play','step','nfl_player_id_1','nfl_player_id_2']].progress_apply(calc_delay, \n#                                             args=(delay, endzone_dict_train, sideline_dict_train, tracking_dict_train, train_rank), \n#                                             axis=1, result_type='expand').astype(np.float16)\n#     _ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:53.469027Z","iopub.execute_input":"2023-02-28T10:36:53.469394Z","iopub.status.idle":"2023-02-28T10:36:53.481934Z","shell.execute_reply.started":"2023-02-28T10:36:53.469355Z","shell.execute_reply":"2023-02-28T10:36:53.480640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfor delay in delays:\n    l = [x + '_delay_' + str(delay) for x in labels]\n    print(l)\n    sel_test[l] = sel_test[['game_play','step','nfl_player_id_1','nfl_player_id_2']].progress_apply(calc_delay, \n                                              args=(delay, endzone_dict_test, sideline_dict_test, tracking_dict_test, test_rank), \n                                              axis=1, result_type='expand').astype(np.float16)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:53.483780Z","iopub.execute_input":"2023-02-28T10:36:53.484194Z","iopub.status.idle":"2023-02-28T10:36:59.784744Z","shell.execute_reply.started":"2023-02-28T10:36:53.484156Z","shell.execute_reply":"2023-02-28T10:36:59.783642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nlabels = ['iou_e', 'iou_s', 'speed_p1', 'acc_p1', 'sa_p1', 'speed_p2', 'acc_p2', 'sa_p2','distance']\n# delays = [0, -5, 5,-15,15]\n# ranges = [3,  5, 5,  5, 5]\ndelays = [0, -5, 5,-15]\nranges = [3,  5, 5,  5]\n","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:59.786360Z","iopub.execute_input":"2023-02-28T10:36:59.787068Z","iopub.status.idle":"2023-02-28T10:36:59.794262Z","shell.execute_reply.started":"2023-02-28T10:36:59.787023Z","shell.execute_reply":"2023-02-28T10:36:59.793115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n# labels = ['iou_e', 'iou_s', 'speed_p1', 'acc_p1', 'sa_p1','speed_p2', 'acc_p2', 'sa_p2','distance']\n# delays = [-1,1]\n\nfor delay, rng in zip(delays, ranges):\n    l = [x + '_delay_' + str(delay) + \"_\" + str(rng) for x in labels]\n    print(l)\n    sel_test[l] = sel_test[['game_play','step','nfl_player_id_1','nfl_player_id_2']].progress_apply(calc_delay_range, args=(delay, rng, endzone_dict_test, sideline_dict_test, tracking_dict_test,), axis=1, result_type='expand').astype(np.float16)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:36:59.795788Z","iopub.execute_input":"2023-02-28T10:36:59.796973Z","iopub.status.idle":"2023-02-28T10:37:04.546903Z","shell.execute_reply.started":"2023-02-28T10:36:59.796935Z","shell.execute_reply":"2023-02-28T10:37:04.545827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ndelay_col = []\nfor e in sel_test.columns:\n#     if '8' in e : continue\n#     if '13' in e : continue\n#     if '21' in e : continue\n    if '_delay_' in e:\n        delay_col.append(e)\nprint(delay_col)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.548280Z","iopub.execute_input":"2023-02-28T10:37:04.549268Z","iopub.status.idle":"2023-02-28T10:37:04.556375Z","shell.execute_reply.started":"2023-02-28T10:37:04.549225Z","shell.execute_reply":"2023-02-28T10:37:04.555235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nsel_test=reduce_mem_usage(sel_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.563351Z","iopub.execute_input":"2023-02-28T10:37:04.564290Z","iopub.status.idle":"2023-02-28T10:37:04.712276Z","shell.execute_reply.started":"2023-02-28T10:37:04.564248Z","shell.execute_reply":"2023-02-28T10:37:04.711144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sel_test = sel_test.copy()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.713550Z","iopub.execute_input":"2023-02-28T10:37:04.713970Z","iopub.status.idle":"2023-02-28T10:37:04.722264Z","shell.execute_reply.started":"2023-02-28T10:37:04.713933Z","shell.execute_reply":"2023-02-28T10:37:04.721036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.723852Z","iopub.execute_input":"2023-02-28T10:37:04.724264Z","iopub.status.idle":"2023-02-28T10:37:04.850311Z","shell.execute_reply.started":"2023-02-28T10:37:04.724221Z","shell.execute_reply":"2023-02-28T10:37:04.849272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 速度に関するもの\n## 一時刻前との速度差\nsel_test['diff_1'] = sel_test['speed_p1_delay_-5_5'] - sel_test['speed_p1_delay_0_3']\n\nsel_test['diff_2'] = sel_test['speed_p2_delay_-5_5'] - sel_test['speed_p2_delay_0_3']","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.851899Z","iopub.execute_input":"2023-02-28T10:37:04.852410Z","iopub.status.idle":"2023-02-28T10:37:04.862254Z","shell.execute_reply.started":"2023-02-28T10:37:04.852372Z","shell.execute_reply":"2023-02-28T10:37:04.861262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# プレイヤー間の差\nc = ['x_position', 'y_position', 'speed','distance', 'orientation', 'acceleration', 'sa']\nfor e in c:\n    sel_test['diff_'+e] = sel_test[e+'_1'] - sel_test[e+'_2']\n","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.863573Z","iopub.execute_input":"2023-02-28T10:37:04.863989Z","iopub.status.idle":"2023-02-28T10:37:04.877424Z","shell.execute_reply.started":"2023-02-28T10:37:04.863952Z","shell.execute_reply":"2023-02-28T10:37:04.876466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# オフェンスーディフェンスの組み合わせフラグ追加\nsel_test['offence-diffence'] = 0\nsel_test.loc[(sel_test['position_1'] >= 10) & \n              (11<=sel_test['position_2']) & (sel_test['position_2'] <= 23),  'offence-diffence'] = 1\nsel_test.loc[(sel_test['position_2'] >= 10) & \n              (11<=sel_test['position_1']) & (sel_test['position_1'] <= 23),  'offence-diffence'] = 1\n\n# (sel_train['offence-diffence'](sel_train['position_1'] >= 10) & (11<=sel_train['position_2']) & (sel_train['position_2'] <= 23)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.878619Z","iopub.execute_input":"2023-02-28T10:37:04.879102Z","iopub.status.idle":"2023-02-28T10:37:04.889850Z","shell.execute_reply.started":"2023-02-28T10:37:04.879051Z","shell.execute_reply":"2023-02-28T10:37:04.888830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# direction -> dir_x, dir_y\nsel_test['dir_x_1'] = np.cos(np.radians(sel_test['direction_1']))\nsel_test['dir_y_1'] = np.sin(np.radians(sel_test['direction_1']))\nsel_test['dir_x_2'] = np.cos(np.radians(sel_test['direction_2']))\nsel_test['dir_y_2'] = np.sin(np.radians(sel_test['direction_2']))\nsel_test['dir_product'] = sel_test['dir_x_1'] * sel_test['dir_x_2'] + sel_test['dir_y_1'] * sel_test['dir_y_2']\n","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.891018Z","iopub.execute_input":"2023-02-28T10:37:04.891912Z","iopub.status.idle":"2023-02-28T10:37:04.903865Z","shell.execute_reply.started":"2023-02-28T10:37:04.891872Z","shell.execute_reply":"2023-02-28T10:37:04.902865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# orientation -> orientation_x, orientation_y    \nsel_test['orientation_x_1'] = np.cos(np.radians(sel_test['orientation_1']))\nsel_test['orientation_y_1'] = np.sin(np.radians(sel_test['orientation_1']))\nsel_test['orientation_x_2'] = np.cos(np.radians(sel_test['orientation_2']))\nsel_test['orientation_y_2'] = np.sin(np.radians(sel_test['orientation_2']))\nsel_test['orientation_product'] = sel_test['orientation_x_1'] * sel_test['orientation_x_2'] + \\\n                                       sel_test['orientation_y_1'] * sel_test['orientation_y_2']\n","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.905231Z","iopub.execute_input":"2023-02-28T10:37:04.905813Z","iopub.status.idle":"2023-02-28T10:37:04.918555Z","shell.execute_reply.started":"2023-02-28T10:37:04.905778Z","shell.execute_reply":"2023-02-28T10:37:04.917556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in [-1,-2,-3,-5,-8,-13,-21,-34]:\n    sel_test['move_xy'+str(i)] = np.abs(sel_test['x_position_1'] - sel_test['pos_e_x_delay_'+str(i)]) + \\\n                                np.abs(sel_test['y_position_1'] - sel_test['pos_e_x_delay_'+str(i)])\n\n    \nfor i,j in zip([-1,-2,-3,-5,-8,-13,-21], [-2,-3,-5,-8,-13,-21,-34]):\n    sel_test['move_xy'+str(i)+\"-\"+str(j)] = np.abs(sel_test['pos_e_x_delay_'+str(i)] - sel_test['pos_e_x_delay_'+str(j)]) + \\\n                                np.abs(sel_test['pos_e_x_delay_'+str(i)] - sel_test['pos_e_x_delay_'+str(j)])\n\nfor i in [-1,-2,-3,-5,-8,-13,-21,-34]:\n    sel_test['move_y'+str(i)] = np.abs(sel_test['y_position_1'] - sel_test['pos_e_x_delay_'+str(i)])\n\n    \nfor i,j in zip([-1,-2,-3,-5,-8,-13,-21], [-2,-3,-5,-8,-13,-21,-34]):\n    sel_test['move_y'+str(i)+\"-\"+str(j)] = np.abs(sel_test['pos_e_x_delay_'+str(i)] - sel_test['pos_e_x_delay_'+str(j)])\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.919896Z","iopub.execute_input":"2023-02-28T10:37:04.920614Z","iopub.status.idle":"2023-02-28T10:37:04.959263Z","shell.execute_reply.started":"2023-02-28T10:37:04.920577Z","shell.execute_reply":"2023-02-28T10:37:04.958366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"move = []\nfor e in sel_test.columns:\n    if 'move_' in e : \n        move.append(e)\nmove","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.962157Z","iopub.execute_input":"2023-02-28T10:37:04.962414Z","iopub.status.idle":"2023-02-28T10:37:04.972610Z","shell.execute_reply.started":"2023-02-28T10:37:04.962390Z","shell.execute_reply":"2023-02-28T10:37:04.971617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sel_test['ground_flg'] = 0\nsel_test.loc[sel_test['nfl_player_id_2']=='G', 'ground_flg'] = 1","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.973881Z","iopub.execute_input":"2023-02-28T10:37:04.974724Z","iopub.status.idle":"2023-02-28T10:37:04.982946Z","shell.execute_reply.started":"2023-02-28T10:37:04.974686Z","shell.execute_reply":"2023-02-28T10:37:04.981917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# add xgboost","metadata":{}},{"cell_type":"code","source":"# dir1 = \"./input/xgboost\"\n# oof_all=np.load(dir1+'/oof_pred.npy')\n# a=np.load(dir1+'/train_contact_id.npy', allow_pickle=True)\n# train_pick=pd.concat([pd.DataFrame(data={'contact_id':a}),pd.DataFrame(data={'xgb_pred':oof_all})],axis=1)\n# # train_pick=train_pick.merge(labels_org[['contact_id','contact']],on='contact_id',how='left')\n# train_pick.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.984237Z","iopub.execute_input":"2023-02-28T10:37:04.985129Z","iopub.status.idle":"2023-02-28T10:37:04.992782Z","shell.execute_reply.started":"2023-02-28T10:37:04.985092Z","shell.execute_reply":"2023-02-28T10:37:04.992053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sel_train = sel_train.merge(train_pick, on=\"contact_id\", how='left')\n# submission['xgb_pred'] = 0.0","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:04.993902Z","iopub.execute_input":"2023-02-28T10:37:04.994951Z","iopub.status.idle":"2023-02-28T10:37:05.002915Z","shell.execute_reply.started":"2023-02-28T10:37:04.994894Z","shell.execute_reply":"2023-02-28T10:37:05.002032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# change category","metadata":{}},{"cell_type":"code","source":"merge_test = sel_test","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:05.004083Z","iopub.execute_input":"2023-02-28T10:37:05.004830Z","iopub.status.idle":"2023-02-28T10:37:05.012601Z","shell.execute_reply.started":"2023-02-28T10:37:05.004793Z","shell.execute_reply":"2023-02-28T10:37:05.011605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\n\nprint(\"{}{: >25}{}{: >10}{}\".format('|','Variable Name','|','Memory','|'))\nprint(\" ------------------------------------ \")\n\nmem = []\nfor var_name in dir():\n    if not var_name.startswith(\"_\"):\n#         if \"dict\" not in var_name : continue\n        print(\"{}{: >25}{}{: >10}{}\".format('|',var_name,'|',sys.getsizeof(eval(var_name)),'|'))\n        mem.append([sys.getsizeof(eval(var_name)), var_name])","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:05.014483Z","iopub.execute_input":"2023-02-28T10:37:05.015138Z","iopub.status.idle":"2023-02-28T10:37:05.110034Z","shell.execute_reply.started":"2023-02-28T10:37:05.015102Z","shell.execute_reply":"2023-02-28T10:37:05.108860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%memit -c print(\"here\")","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:05.111525Z","iopub.execute_input":"2023-02-28T10:37:05.111936Z","iopub.status.idle":"2023-02-28T10:37:05.367337Z","shell.execute_reply.started":"2023-02-28T10:37:05.111899Z","shell.execute_reply":"2023-02-28T10:37:05.366011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# train","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=merge_test[\n     [ 'rank',  \n       'position_1', #'jersey_number_1',\n       'x_position_1', 'y_position_1', 'speed_1', 'distance_1',\n       'orientation_1', 'acceleration_1', 'sa_1', \n       'position_2', #'jersey_number_2',\n       'x_position_2', 'y_position_2', 'speed_2','distance_2',\n       'orientation_2', 'acceleration_2', 'sa_2',\n       \n       'dir_x_1', 'dir_y_1', 'dir_x_2', 'dir_y_2','dir_product',\n       'orientation_x_1', 'orientation_y_1', 'orientation_x_2', 'orientation_y_2','orientation_product',\n       'distance', \n       \n       'left_e_1', 'width_e_1', 'top_e_1', 'height_e_1',#'right_e_1', 'bottom_e_1', \n       'left_e_2', 'width_e_2', 'top_e_2', 'height_e_2',# 'right_e_2', 'bottom_e_2',\n      \n       'left_s_1', 'width_s_1','top_s_1', 'height_s_1', # 'right_s_1', 'bottom_s_1', \n       'left_s_2', 'width_s_2', 'top_s_2', 'height_s_2', #'right_s_2', 'bottom_s_2', \n       'iou_e', 'iou_s',\n       'offence-diffence', 'diff_1', 'diff_2',\n       'diff_speed', 'diff_distance', 'diff_orientation', 'diff_acceleration','diff_sa',\n      'move_xy-1', 'move_xy-2', 'move_xy-3', 'move_xy-5', 'move_xy-8', 'move_xy-13', 'move_xy-21', 'move_xy-34',\n      'move_xy-1--2', 'move_xy-2--3', 'move_xy-3--5', 'move_xy-5--8', 'move_xy-8--13','move_xy-13--21', 'move_xy-21--34',\n      'move_y-1', 'move_y-2', 'move_y-3','move_y-5','move_y-8','move_y-13','move_y-21',  'move_y-34',\n      'move_y-1--2','move_y-2--3','move_y-3--5','move_y-5--8','move_y-8--13','move_y-13--21', 'move_y-21--34',\n      'ground_flg',\n     ] + delay_col\n    ]\n\n\n# X_train=merge_train[importance.index]\n\n\n#      [ 'rank',  'position_1', 'jersey_number_1',\n#        'x_position_1', 'y_position_1', 'speed_1', 'distance_1', 'direction_1',\n#        'orientation_1', 'acceleration_1', 'sa_1', 'position_2',\n#        'jersey_number_2', 'x_position_2', 'y_position_2', 'speed_2',\n#        'distance_2', 'direction_2', 'orientation_2', 'acceleration_2', 'sa_2',\n#        'dir_x_1', 'dir_y_1', 'dir_x_2', 'dir_y_2',\n#        'distance', 'left_e_1', 'width_e_1', 'top_e_1', 'height_e_1',\n#        'right_e_1', 'bottom_e_1', 'left_e_2', 'width_e_2', 'top_e_2',\n#        'height_e_2', 'right_e_2', 'bottom_e_2', 'left_s_1', 'width_s_1',\n#        'top_s_1', 'height_s_1', 'right_s_1', 'bottom_s_1', 'left_s_2',\n#        'width_s_2', 'top_s_2', 'height_s_2', 'right_s_2', 'bottom_s_2', 'iou_e', 'iou_s'] + delay_col\n","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:05.371047Z","iopub.execute_input":"2023-02-28T10:37:05.371368Z","iopub.status.idle":"2023-02-28T10:37:05.393923Z","shell.execute_reply.started":"2023-02-28T10:37:05.371334Z","shell.execute_reply":"2023-02-28T10:37:05.392922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\ndef predict_cv(model, X_train, Y_train, X_test):\n    preds=[]\n    preds_test=[]\n    va_idxes=[]\n    AUC=[]\n    \n    \n    for i in range(5):\n        file = '/kaggle/input/lgbm-train-model/lgbm_model_fold{}.pkl'.format(i)    \n        print(file)\n        # load model\n        model = pickle.load(open(file, 'rb'))\n        \n        #テストデータの予測\n        pred_test = model.predict_proba(X_test)[:, 1]     \n        preds_test.append(pred_test)\n                \n        \n    #テストデータに対する予測の平均をとる\n    preds_test=np.mean(preds_test, axis=0)\n        \n    return preds_test ","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:05.395739Z","iopub.execute_input":"2023-02-28T10:37:05.398312Z","iopub.status.idle":"2023-02-28T10:37:05.405150Z","shell.execute_reply.started":"2023-02-28T10:37:05.398274Z","shell.execute_reply":"2023-02-28T10:37:05.404010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_1a = lgb.LGBMClassifier(boosting_type='gbdt', num_leaves=31, max_depth= -1, \n#                               learning_rate=0.1, n_estimators=100, subsample_for_bin=200000, \n#                               objective=None, class_weight=None, min_split_gain=0.0, \n#                               min_child_weight=0.001, min_child_samples=20, subsample=1.0, \n#                               subsample_freq=0, colsample_bytree=1.0, reg_alpha=0.0, reg_lambda=0.0, \n#                               random_state=71, n_jobs=- 1, silent=True, importance_type='gain')\n\n# pred_train_1a, pred_test_1a=predict_cv(model_1a, X_train, Y_train ,X_test)\n\n\n# model_1a = lgb.LGBMClassifier(boosting_type='gbdt', num_leaves=31, max_depth= -1, \n#                               learning_rate=0.05, n_estimators=256, subsample_for_bin=200000, \n#                               objective=None, class_weight=None, min_split_gain=0.0, \n#                               min_child_weight=0.001, min_child_samples=20, subsample=1.0, \n#                               subsample_freq=0, colsample_bytree=1.0, reg_alpha=0.0, reg_lambda=0.0, \n#                               random_state=71, n_jobs=- 1, silent=True, importance_type='gain')\n\nparams = {'objective': 'binary',\n          'metric': ['binary_logloss', 'binary_error']}\n\nmodel_1a = lgb.LGBMClassifier(**params, boosting_type='gbdt', num_leaves=31, max_depth= -1, \n                              learning_rate=0.05, n_estimators=256, subsample_for_bin=200000, \n                              class_weight=None, min_split_gain=0.0, \n                              min_child_weight=0.001, min_child_samples=20, subsample=1.0, \n                              subsample_freq=0, colsample_bytree=1.0, reg_alpha=0.0, reg_lambda=0.0, \n                              random_state=71, n_jobs=- 1, silent=True, importance_type='gain')\n\n%memit -c  pred_test_1a=predict_cv(model_1a, None, None ,X_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:11.671305Z","iopub.execute_input":"2023-02-28T10:37:11.672338Z","iopub.status.idle":"2023-02-28T10:37:12.817587Z","shell.execute_reply.started":"2023-02-28T10:37:11.672285Z","shell.execute_reply":"2023-02-28T10:37:12.815673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"pred\"]=0\nsubmission.loc[(submission['distance'] < distance_th)|(submission['distance'].isna()), 'pred'] = pred_test_1a","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:12.820932Z","iopub.execute_input":"2023-02-28T10:37:12.821260Z","iopub.status.idle":"2023-02-28T10:37:12.832657Z","shell.execute_reply.started":"2023-02-28T10:37:12.821224Z","shell.execute_reply":"2023-02-28T10:37:12.831302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"pred\"].hist()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:13.563240Z","iopub.execute_input":"2023-02-28T10:37:13.565936Z","iopub.status.idle":"2023-02-28T10:37:13.828949Z","shell.execute_reply.started":"2023-02-28T10:37:13.565894Z","shell.execute_reply":"2023-02-28T10:37:13.827912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thersold = 0.32792968749999984\nsubmission.loc[submission[\"pred\"]>=thersold, \"contact\"]=1\nsubmission.loc[submission[\"pred\"]<thersold, \"contact\"]=0\nsubmission.loc[submission[\"distance\"]>=distance_th, \"contact\"]=0\n\n# submission=submission.iloc[:, :2]\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:17.813598Z","iopub.execute_input":"2023-02-28T10:37:17.814470Z","iopub.status.idle":"2023-02-28T10:37:17.873563Z","shell.execute_reply.started":"2023-02-28T10:37:17.814430Z","shell.execute_reply":"2023-02-28T10:37:17.872400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[['contact_id','contact','pred']].to_csv(\"submission_lgbm.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:45.494827Z","iopub.execute_input":"2023-02-28T10:37:45.495407Z","iopub.status.idle":"2023-02-28T10:37:45.660405Z","shell.execute_reply.started":"2023-02-28T10:37:45.495363Z","shell.execute_reply":"2023-02-28T10:37:45.659281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[['contact_id','contact']].to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T10:37:46.091738Z","iopub.execute_input":"2023-02-28T10:37:46.094632Z","iopub.status.idle":"2023-02-28T10:37:46.166611Z","shell.execute_reply.started":"2023-02-28T10:37:46.094591Z","shell.execute_reply":"2023-02-28T10:37:46.165425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}