{"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 gc\nimport numpy as np\nimport pandas as pd\nimport xgboost as xgb\nfrom scipy.optimize import minimize\nfrom sklearn.metrics import matthews_corrcoef\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-17T14:07:25.701361Z","iopub.execute_input":"2022-12-17T14:07:25.701667Z","iopub.status.idle":"2022-12-17T14:07:26.681833Z","shell.execute_reply.started":"2022-12-17T14:07:25.701596Z","shell.execute_reply":"2022-12-17T14:07:26.680713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('max_columns', None)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:07:26.688201Z","iopub.execute_input":"2022-12-17T14:07:26.690728Z","iopub.status.idle":"2022-12-17T14:07:26.698387Z","shell.execute_reply.started":"2022-12-17T14:07:26.690684Z","shell.execute_reply":"2022-12-17T14:07:26.697316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read in Data","metadata":{}},{"cell_type":"code","source":"train_helmets = pd.read_csv('../input/nfl-player-contact-detection/train_baseline_helmets.csv',\n                            usecols=['game_play','view','frame','nfl_player_id','left','width','top','height'],\n                            dtype={'game_play':'str',\n                                   'view':'str',\n                                   'frame':'str',\n                                   'nfl_player_id':'str',\n                                   'left':'int8',\n                                   'width':'int8',\n                                   'top':'int8',\n                                   'height':'int8'})\\\n                  .rename(columns={'frame':'step'})\n\nlabels = pd.read_csv('../input/nfl-player-contact-detection/train_labels.csv',\n                     usecols=['game_play','step','nfl_player_id_1','nfl_player_id_2','contact'],\n                     dtype={'game_play':'str',\n                            'step':'str',\n                            'nfl_player_id_1':'str',\n                            'nfl_player_id_2':'str',\n                            'contact':'int8'})","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:07:57.507068Z","iopub.execute_input":"2022-12-17T14:07:57.507929Z","iopub.status.idle":"2022-12-17T14:08:10.060213Z","shell.execute_reply.started":"2022-12-17T14:07:57.507893Z","shell.execute_reply":"2022-12-17T14:08:10.059208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_helmets['step_int'] = train_helmets['step'].astype(int)\nlabels['step_int'] = labels['step'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:08:10.061978Z","iopub.execute_input":"2022-12-17T14:08:10.062460Z","iopub.status.idle":"2022-12-17T14:08:10.780645Z","shell.execute_reply.started":"2022-12-17T14:08:10.062415Z","shell.execute_reply":"2022-12-17T14:08:10.779683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_step_pct(df):\n    df['step_pct'] = 100 * (df['step_int']-min(df['step_int']))/(max(df['step_int'])-min(df['step_int']))\n    df['step_pct'] = df['step_pct'].apply(np.ceil).astype(np.int32)\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:08:10.782162Z","iopub.execute_input":"2022-12-17T14:08:10.782515Z","iopub.status.idle":"2022-12-17T14:08:10.788662Z","shell.execute_reply.started":"2022-12-17T14:08:10.782480Z","shell.execute_reply":"2022-12-17T14:08:10.787433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_helmets = train_helmets.groupby('game_play').apply(add_step_pct)\nlabels = labels.groupby('game_play').apply(add_step_pct)\n\ntrain_helmets['helmet_id'] = train_helmets['game_play'] + '_' + train_helmets['nfl_player_id'].astype(str) + '_' + train_helmets['step_pct'].astype(str)\nlabels['helmet_id_1'] = labels['game_play'] + '_' + labels['nfl_player_id_1'].astype(str) + '_' + labels['step_pct'].astype(str)\nlabels['helmet_id_2'] = labels['game_play'] + '_' + labels['nfl_player_id_2'].astype(str) + '_' + labels['step_pct'].astype(str)\n\ntrain_helmets.drop('step_int', axis=1, inplace=True)\nlabels.drop('step_int', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:08:10.791800Z","iopub.execute_input":"2022-12-17T14:08:10.792513Z","iopub.status.idle":"2022-12-17T14:08:33.600312Z","shell.execute_reply.started":"2022-12-17T14:08:10.792475Z","shell.execute_reply":"2022-12-17T14:08:33.599302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_helmets = train_helmets.groupby(['helmet_id','view'])[['left','width','top','height']]\\\n                             .mean()\\\n                             .reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:08:33.602003Z","iopub.execute_input":"2022-12-17T14:08:33.602419Z","iopub.status.idle":"2022-12-17T14:08:35.125713Z","shell.execute_reply.started":"2022-12-17T14:08:33.602385Z","shell.execute_reply":"2022-12-17T14:08:35.124732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_helmets['x1'] = train_helmets['left']\ntrain_helmets['x2'] = train_helmets['x1'] + train_helmets['width']\ntrain_helmets['y1'] = train_helmets['top']\ntrain_helmets['y2'] = train_helmets['y1'] - train_helmets['height']\n\ntrain_helmets['x_mid'] = train_helmets['left'] + (train_helmets['width']/2)\ntrain_helmets['y_mid'] = train_helmets['top'] - (train_helmets['height']/2)\n\ntrain_helmets.drop(['left','width','top','height'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:08:35.127391Z","iopub.execute_input":"2022-12-17T14:08:35.127765Z","iopub.status.idle":"2022-12-17T14:08:35.267703Z","shell.execute_reply.started":"2022-12-17T14:08:35.127727Z","shell.execute_reply":"2022-12-17T14:08:35.266703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sideline_player1 = train_helmets[train_helmets['view'] == 'Sideline'].drop('view', axis=1).copy()\nsideline_player2 = train_helmets[train_helmets['view'] == 'Sideline'].drop('view', axis=1).copy()\n\nendzone_player1 = train_helmets[train_helmets['view'] == 'Endzone'].drop('view', axis=1).copy()\nendzone_player2 = train_helmets[train_helmets['view'] == 'Endzone'].drop('view', axis=1).copy()\n\nsideline_player1.columns = ['helmet_id_1','sl_x1_1','sl_x2_1','sl_y1_1','sl_y2_1','sl_x_mid_1','sl_y_mid_1']\nsideline_player2.columns = ['helmet_id_2','sl_x1_2','sl_x2_2','sl_y1_2','sl_y2_2','sl_x_mid_2','sl_y_mid_2']\n\nendzone_player1.columns = ['helmet_id_1','ez_x1_1','ez_x2_1','ez_y1_1','ez_y2_1','ez_x_mid_1','ez_y_mid_1']\nendzone_player2.columns = ['helmet_id_2','ez_x1_2','ez_x2_2','ez_y1_2','ez_y2_2','ez_x_mid_2','ez_y_mid_2']","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:08:35.269415Z","iopub.execute_input":"2022-12-17T14:08:35.269792Z","iopub.status.idle":"2022-12-17T14:08:35.668293Z","shell.execute_reply.started":"2022-12-17T14:08:35.269756Z","shell.execute_reply":"2022-12-17T14:08:35.667286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basetable = pd.merge(labels, sideline_player1, on=['helmet_id_1'], how='left')\nbasetable = pd.merge(basetable, sideline_player2, on=['helmet_id_2'], how='left')\nbasetable = pd.merge(basetable, endzone_player1, on=['helmet_id_1'], how='left')\nbasetable = pd.merge(basetable, endzone_player2, on=['helmet_id_2'], how='left')","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:08:35.669763Z","iopub.execute_input":"2022-12-17T14:08:35.671302Z","iopub.status.idle":"2022-12-17T14:08:44.305154Z","shell.execute_reply.started":"2022-12-17T14:08:35.671261Z","shell.execute_reply":"2022-12-17T14:08:44.304121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create IoU features\n\nhttps://stackoverflow.com/questions/25349178/calculating-percentage-of-bounding-box-overlap-for-image-detector-evaluation","metadata":{}},{"cell_type":"code","source":"def get_iou(df, view):\n    \n    if view == 'Sideline':\n        prefix = 'sl'\n    else:\n        prefix = 'ez'\n        \n    # determine the coordinates of the intersection rectangle\n    x_left = max(df['{}_x1_1'.format(prefix)], df['{}_x1_2'.format(prefix)])\n    y_top = max(df['{}_y1_1'.format(prefix)], df['{}_y1_2'.format(prefix)])\n    x_right = min(df['{}_x2_1'.format(prefix)], df['{}_x2_2'.format(prefix)])\n    y_bottom = min(df['{}_y2_1'.format(prefix)], df['{}_y2_2'.format(prefix)])\n\n    intersection_area = (x_right - x_left + 1) * (y_bottom - y_top + 1)\n\n    # compute the area of both AABBs\n    bb1_area = (df['{}_x2_1'.format(prefix)] - df['{}_x1_1'.format(prefix)] + 1) * (df['{}_y2_1'.format(prefix)] - df['{}_y1_1'.format(prefix)] + 1)\n    bb2_area = (df['{}_x2_2'.format(prefix)] - df['{}_x1_2'.format(prefix)] + 1) * (df['{}_y2_2'.format(prefix)] - df['{}_y1_2'.format(prefix)] + 1)\n    \n    try:\n        iou = intersection_area / float(bb1_area + bb2_area - intersection_area)\n    except:\n        iou = -99\n    \n    return iou","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:08:50.202642Z","iopub.execute_input":"2022-12-17T14:08:50.203049Z","iopub.status.idle":"2022-12-17T14:08:50.213545Z","shell.execute_reply.started":"2022-12-17T14:08:50.202992Z","shell.execute_reply":"2022-12-17T14:08:50.212486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nbasetable['sideline_iou'] = basetable.apply(lambda x: get_iou(x, view='Sideline'), axis=1)\nbasetable['endzone_iou'] = basetable.apply(lambda x: get_iou(x, view='Endzone'), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:08:54.280317Z","iopub.execute_input":"2022-12-17T14:08:54.280770Z","iopub.status.idle":"2022-12-17T14:20:08.182272Z","shell.execute_reply.started":"2022-12-17T14:08:54.280733Z","shell.execute_reply":"2022-12-17T14:20:08.181094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basetable['sideline_x1_diff'] = basetable['sl_x1_1'] - basetable['sl_x1_2']\nbasetable['sideline_x2_diff'] = basetable['sl_x2_1'] - basetable['sl_x2_2']\nbasetable['sideline_y1_diff'] = basetable['sl_y1_1'] - basetable['sl_y1_2']\nbasetable['sideline_y2_diff'] = basetable['sl_y2_1'] - basetable['sl_y2_2']\nbasetable['sideline_x1_x2_diff'] = basetable['sl_x1_1'] - basetable['sl_x2_2']\nbasetable['sideline_y1_y2_diff'] = basetable['sl_y1_1'] - basetable['sl_y2_2']\nbasetable['sideline_area'] = abs(basetable['sl_x2_1'] - basetable['sl_x1_1'])*abs(basetable['sl_y2_1'] - basetable['sl_y1_1'])\n\nbasetable['endzone_x1_diff'] = basetable['ez_x1_1'] - basetable['ez_x1_2']\nbasetable['endzone_x2_diff'] = basetable['ez_x2_1'] - basetable['ez_x2_2']\nbasetable['endzone_y1_diff'] = basetable['ez_y1_1'] - basetable['ez_y1_2']\nbasetable['endzone_y2_diff'] = basetable['ez_y2_1'] - basetable['ez_y2_2']\nbasetable['endzone_x1_x2_diff'] = basetable['ez_x1_1'] - basetable['ez_x2_2']\nbasetable['endzone_y1_y2_diff'] = basetable['ez_y1_1'] - basetable['ez_y2_2']\nbasetable['endzone_area'] = abs(basetable['ez_x2_1'] - basetable['ez_x1_1'])*abs(basetable['ez_y2_1'] - basetable['ez_y1_1'])\n\nbasetable['sideline_player_distance'] = np.sqrt((basetable['sl_x_mid_1']-basetable['sl_x_mid_2'])**2 + (basetable['sl_y_mid_1']-basetable['sl_y_mid_2'])**2)\nbasetable['endzone_player_distance'] = np.sqrt((basetable['ez_x_mid_1']-basetable['ez_x_mid_2'])**2 + (basetable['ez_y_mid_1']-basetable['ez_y_mid_2'])**2)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:23:19.364775Z","iopub.execute_input":"2022-12-17T14:23:19.365156Z","iopub.status.idle":"2022-12-17T14:23:20.314372Z","shell.execute_reply.started":"2022-12-17T14:23:19.365124Z","shell.execute_reply":"2022-12-17T14:23:20.313453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = basetable.drop(['game_play','step','nfl_player_id_1','nfl_player_id_2',\n                    'contact','helmet_id_1','helmet_id_2'], axis=1)\ny = basetable.contact","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:23:22.487245Z","iopub.execute_input":"2022-12-17T14:23:22.487691Z","iopub.status.idle":"2022-12-17T14:23:24.436316Z","shell.execute_reply.started":"2022-12-17T14:23:22.487650Z","shell.execute_reply":"2022-12-17T14:23:24.435163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Split data into train/val","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:23:28.844747Z","iopub.execute_input":"2022-12-17T14:23:28.845133Z","iopub.status.idle":"2022-12-17T14:23:31.760135Z","shell.execute_reply.started":"2022-12-17T14:23:28.845100Z","shell.execute_reply":"2022-12-17T14:23:31.759142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb_train = xgb.DMatrix(X_train, y_train)\nxgb_val = xgb.DMatrix(X_val, y_val)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:23:31.762294Z","iopub.execute_input":"2022-12-17T14:23:31.762679Z","iopub.status.idle":"2022-12-17T14:23:40.726140Z","shell.execute_reply.started":"2022-12-17T14:23:31.762643Z","shell.execute_reply":"2022-12-17T14:23:40.725006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Model","metadata":{}},{"cell_type":"code","source":"%%time\n\nxgb_params = {'objective': 'binary:logistic',\n              'eval_metric': 'auc',\n              'learning_rate':0.01,\n              'max_depth':5,\n              'tree_method':'gpu_hist',\n              'predictor':'gpu_predictor'}\n\nevals = [(xgb_train,'train'),(xgb_val,'val')]\n\nmodel = xgb.train(xgb_params, \n                  xgb_train,\n                  num_boost_round=100000,\n                  early_stopping_rounds=100,\n                  evals=evals, \n                  verbose_eval=500)\n\ntrain_preds = [x for x in model.predict(xgb_train)]\nval_preds = [x for x in model.predict(xgb_val)]","metadata":{"execution":{"iopub.status.busy":"2022-12-17T14:34:08.117035Z","iopub.execute_input":"2022-12-17T14:34:08.118055Z","iopub.status.idle":"2022-12-17T14:55:54.389098Z","shell.execute_reply.started":"2022-12-17T14:34:08.117997Z","shell.execute_reply":"2022-12-17T14:55:54.388062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def func(x):\n    score = matthews_corrcoef(y_val, val_preds>x)\n    return -score\n\nresult = minimize(func, 0.25, method=\"nelder-mead\")\nthresh = result.x[0]\n\nprint(\"score:\", round(matthews_corrcoef(y_val, val_preds>thresh), 5))\nprint(\"threshold\", thresh)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:02:52.036655Z","iopub.execute_input":"2022-12-17T15:02:52.037054Z","iopub.status.idle":"2022-12-17T15:02:58.920115Z","shell.execute_reply.started":"2022-12-17T15:02:52.036997Z","shell.execute_reply":"2022-12-17T15:02:58.918902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshs = [x/100. for x in range(20,41)]\ncorrs = []\nfor t in threshs:\n    labels = [1 if x > t else 0 for x in val_preds]\n    corr = round(matthews_corrcoef(y_val, labels), 5)\n    corrs.append(corr)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:03:15.039030Z","iopub.execute_input":"2022-12-17T15:03:15.039404Z","iopub.status.idle":"2022-12-17T15:03:51.635082Z","shell.execute_reply.started":"2022-12-17T15:03:15.039373Z","shell.execute_reply":"2022-12-17T15:03:51.634066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.plot(threshs, corrs)\nplt.axvline(thresh)\nplt.title('Matthews Correlation by threshold')\nplt.xlabel('Threshold')\nplt.ylabel('Correlation')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:04:05.683733Z","iopub.execute_input":"2022-12-17T15:04:05.684126Z","iopub.status.idle":"2022-12-17T15:04:05.920980Z","shell.execute_reply.started":"2022-12-17T15:04:05.684092Z","shell.execute_reply":"2022-12-17T15:04:05.919939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xgb.plot_importance(model)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:04:14.156191Z","iopub.execute_input":"2022-12-17T15:04:14.156611Z","iopub.status.idle":"2022-12-17T15:04:14.891110Z","shell.execute_reply.started":"2022-12-17T15:04:14.156576Z","shell.execute_reply":"2022-12-17T15:04:14.890201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Clean up data","metadata":{}},{"cell_type":"code","source":"del (train_helmets, labels, sideline_player1, sideline_player2, \n     endzone_player1, endzone_player2, basetable, X, y, X_train, y_train,\n     X_val, y_val, xgb_train, xgb_val)\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:04:40.052298Z","iopub.execute_input":"2022-12-17T15:04:40.052674Z","iopub.status.idle":"2022-12-17T15:04:40.539893Z","shell.execute_reply.started":"2022-12-17T15:04:40.052642Z","shell.execute_reply":"2022-12-17T15:04:40.538774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate Predictions for Test Data","metadata":{}},{"cell_type":"code","source":"sub = pd.read_csv('../input/nfl-player-contact-detection/sample_submission.csv')\nsub['id_split'] = sub['contact_id'].apply(lambda x: x.split('_'))\nsub['game_play'] = sub['id_split'].apply(lambda x: x[0]+'_'+x[1])\nsub['step'] = sub['id_split'].apply(lambda x: x[2])\nsub['nfl_player_id_1'] = sub['id_split'].apply(lambda x: x[3])\nsub['nfl_player_id_2'] = sub['id_split'].apply(lambda x: x[4])","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:04:52.959797Z","iopub.execute_input":"2022-12-17T15:04:52.960184Z","iopub.status.idle":"2022-12-17T15:04:53.076757Z","shell.execute_reply.started":"2022-12-17T15:04:52.960150Z","shell.execute_reply":"2022-12-17T15:04:53.075763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_helmets = pd.read_csv('../input/nfl-player-contact-detection/test_baseline_helmets.csv',\n                            usecols=['game_play','view','frame','nfl_player_id','left','width','top','height'],\n                            dtype={'game_play':'str',\n                                   'view':'str',\n                                   'frame':'str',\n                                   'nfl_player_id':'str',\n                                   'left':'int8',\n                                   'width':'int8',\n                                   'top':'int8',\n                                   'height':'int8'})\\\n                  .rename(columns={'frame':'step'})","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:04:55.793605Z","iopub.execute_input":"2022-12-17T15:04:55.793951Z","iopub.status.idle":"2022-12-17T15:04:55.881750Z","shell.execute_reply.started":"2022-12-17T15:04:55.793920Z","shell.execute_reply":"2022-12-17T15:04:55.880805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_helmets['step_int'] = test_helmets['step'].astype(int)\nsub['step_int'] = sub['step'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:04:58.467778Z","iopub.execute_input":"2022-12-17T15:04:58.468164Z","iopub.status.idle":"2022-12-17T15:04:58.486573Z","shell.execute_reply.started":"2022-12-17T15:04:58.468130Z","shell.execute_reply":"2022-12-17T15:04:58.485721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_helmets = test_helmets.groupby('game_play').apply(add_step_pct)\nsub = sub.groupby('game_play').apply(add_step_pct)\n\ntest_helmets['helmet_id'] = test_helmets['game_play'] + '_' + test_helmets['nfl_player_id'].astype(str) + '_' + test_helmets['step_pct'].astype(str)\nsub['helmet_id_1'] = sub['game_play'] + '_' + sub['nfl_player_id_1'].astype(str) + '_' + sub['step_pct'].astype(str)\nsub['helmet_id_2'] = sub['game_play'] + '_' + sub['nfl_player_id_2'].astype(str) + '_' + sub['step_pct'].astype(str)\n\ntest_helmets.drop('step_int', axis=1, inplace=True)\nsub.drop('step_int', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:05:01.775843Z","iopub.execute_input":"2022-12-17T15:05:01.776213Z","iopub.status.idle":"2022-12-17T15:05:02.034281Z","shell.execute_reply.started":"2022-12-17T15:05:01.776181Z","shell.execute_reply":"2022-12-17T15:05:02.033317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_helmets = test_helmets.groupby(['helmet_id','view'])[['left','width','top','height']]\\\n                             .mean()\\\n                             .reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:05:04.439226Z","iopub.execute_input":"2022-12-17T15:05:04.439808Z","iopub.status.idle":"2022-12-17T15:05:04.477994Z","shell.execute_reply.started":"2022-12-17T15:05:04.439764Z","shell.execute_reply":"2022-12-17T15:05:04.477082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_helmets['x1'] = test_helmets['left']\ntest_helmets['x2'] = test_helmets['x1'] + test_helmets['width']\ntest_helmets['y1'] = test_helmets['top']\ntest_helmets['y2'] = test_helmets['y1'] - test_helmets['height']\n\ntest_helmets['x_mid'] = test_helmets['left'] + (test_helmets['width']/2)\ntest_helmets['y_mid'] = test_helmets['top'] - (test_helmets['height']/2)\n\ntest_helmets.drop(['left','width','top','height'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:05:07.119748Z","iopub.execute_input":"2022-12-17T15:05:07.120125Z","iopub.status.idle":"2022-12-17T15:05:07.136277Z","shell.execute_reply.started":"2022-12-17T15:05:07.120093Z","shell.execute_reply":"2022-12-17T15:05:07.135189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sideline_player1 = test_helmets[test_helmets['view'] == 'Sideline'].drop('view', axis=1).copy()\nsideline_player2 = test_helmets[test_helmets['view'] == 'Sideline'].drop('view', axis=1).copy()\n\nendzone_player1 = test_helmets[test_helmets['view'] == 'Endzone'].drop('view', axis=1).copy()\nendzone_player2 = test_helmets[test_helmets['view'] == 'Endzone'].drop('view', axis=1).copy()\n\nsideline_player1.columns = ['helmet_id_1','sl_x1_1','sl_x2_1','sl_y1_1','sl_y2_1','sl_x_mid_1','sl_y_mid_1']\nsideline_player2.columns = ['helmet_id_2','sl_x1_2','sl_x2_2','sl_y1_2','sl_y2_2','sl_x_mid_2','sl_y_mid_2']\n\nendzone_player1.columns = ['helmet_id_1','ez_x1_1','ez_x2_1','ez_y1_1','ez_y2_1','ez_x_mid_1','ez_y_mid_1']\nendzone_player2.columns = ['helmet_id_2','ez_x1_2','ez_x2_2','ez_y1_2','ez_y2_2','ez_x_mid_2','ez_y_mid_2']","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:05:10.708719Z","iopub.execute_input":"2022-12-17T15:05:10.709090Z","iopub.status.idle":"2022-12-17T15:05:10.726385Z","shell.execute_reply.started":"2022-12-17T15:05:10.709058Z","shell.execute_reply":"2022-12-17T15:05:10.725545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_basetable = pd.merge(sub, sideline_player1, on=['helmet_id_1'], how='left')\ntest_basetable = pd.merge(test_basetable, sideline_player2, on=['helmet_id_2'], how='left')\ntest_basetable = pd.merge(test_basetable, endzone_player1, on=['helmet_id_1'], how='left')\ntest_basetable = pd.merge(test_basetable, endzone_player2, on=['helmet_id_2'], how='left')","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:05:14.014136Z","iopub.execute_input":"2022-12-17T15:05:14.015175Z","iopub.status.idle":"2022-12-17T15:05:14.136333Z","shell.execute_reply.started":"2022-12-17T15:05:14.015138Z","shell.execute_reply":"2022-12-17T15:05:14.135369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest_basetable['sideline_iou'] = test_basetable.apply(lambda x: get_iou(x, view='Sideline'), axis=1)\ntest_basetable['endzone_iou'] = test_basetable.apply(lambda x: get_iou(x, view='Endzone'), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:05:17.060294Z","iopub.execute_input":"2022-12-17T15:05:17.061259Z","iopub.status.idle":"2022-12-17T15:05:24.499560Z","shell.execute_reply.started":"2022-12-17T15:05:17.061222Z","shell.execute_reply":"2022-12-17T15:05:24.498459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_basetable['sideline_x1_diff'] = test_basetable['sl_x1_1'] - test_basetable['sl_x1_2']\ntest_basetable['sideline_x2_diff'] = test_basetable['sl_x2_1'] - test_basetable['sl_x2_2']\ntest_basetable['sideline_y1_diff'] = test_basetable['sl_y1_1'] - test_basetable['sl_y1_2']\ntest_basetable['sideline_y2_diff'] = test_basetable['sl_y2_1'] - test_basetable['sl_y2_2']\ntest_basetable['sideline_x1_x2_diff'] = test_basetable['sl_x1_1'] - test_basetable['sl_x2_2']\ntest_basetable['sideline_y1_y2_diff'] = test_basetable['sl_y1_1'] - test_basetable['sl_y2_2']\ntest_basetable['sideline_area'] = abs(test_basetable['sl_x2_1'] - test_basetable['sl_x1_1'])*abs(test_basetable['sl_y2_1'] - test_basetable['sl_y1_1'])\n\ntest_basetable['endzone_x1_diff'] = test_basetable['ez_x1_1'] - test_basetable['ez_x1_2']\ntest_basetable['endzone_x2_diff'] = test_basetable['ez_x2_1'] - test_basetable['ez_x2_2']\ntest_basetable['endzone_y1_diff'] = test_basetable['ez_y1_1'] - test_basetable['ez_y1_2']\ntest_basetable['endzone_y2_diff'] = test_basetable['ez_y2_1'] - test_basetable['ez_y2_2']\ntest_basetable['endzone_x1_x2_diff'] = test_basetable['ez_x1_1'] - test_basetable['ez_x2_2']\ntest_basetable['endzone_y1_y2_diff'] = test_basetable['ez_y1_1'] - test_basetable['ez_y2_2']\ntest_basetable['endzone_area'] = abs(test_basetable['ez_x2_1'] - test_basetable['ez_x1_1'])*abs(test_basetable['ez_y2_1'] - test_basetable['ez_y1_1'])\n\ntest_basetable['sideline_player_distance'] = np.sqrt((test_basetable['sl_x_mid_1']-test_basetable['sl_x_mid_2'])**2 + (test_basetable['sl_y_mid_1']-test_basetable['sl_y_mid_2'])**2)\ntest_basetable['endzone_player_distance'] = np.sqrt((test_basetable['ez_x_mid_1']-test_basetable['ez_x_mid_2'])**2 + (test_basetable['ez_y_mid_1']-test_basetable['ez_y_mid_2'])**2)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:05:24.501504Z","iopub.execute_input":"2022-12-17T15:05:24.502339Z","iopub.status.idle":"2022-12-17T15:05:24.531749Z","shell.execute_reply.started":"2022-12-17T15:05:24.502300Z","shell.execute_reply":"2022-12-17T15:05:24.530895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_basetable.drop(['contact_id','contact','id_split','game_play','step','nfl_player_id_1','nfl_player_id_2',\n                              'helmet_id_1','helmet_id_2'], axis=1)\nxgb_test = xgb.DMatrix(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:05:24.533106Z","iopub.execute_input":"2022-12-17T15:05:24.533545Z","iopub.status.idle":"2022-12-17T15:05:24.606866Z","shell.execute_reply.started":"2022-12-17T15:05:24.533510Z","shell.execute_reply":"2022-12-17T15:05:24.606072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds = [x for x in model.predict(xgb_test)]\ntest_labels = [1 if x > thresh else 0 for x in test_preds]","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:05:30.358156Z","iopub.execute_input":"2022-12-17T15:05:30.358830Z","iopub.status.idle":"2022-12-17T15:05:31.281157Z","shell.execute_reply.started":"2022-12-17T15:05:30.358793Z","shell.execute_reply":"2022-12-17T15:05:31.280202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['contact'] = test_labels\nsub = sub[['contact_id','contact']]\nsub.to_csv('submission.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-17T15:05:35.774090Z","iopub.execute_input":"2022-12-17T15:05:35.775009Z","iopub.status.idle":"2022-12-17T15:05:35.855088Z","shell.execute_reply.started":"2022-12-17T15:05:35.774953Z","shell.execute_reply":"2022-12-17T15:05:35.854196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}