{"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":"![ooops](https://billswire.usatoday.com/wp-content/uploads/sites/65/2021/01/USATSI_15479143.jpg?w=1000&h=600&crop=1)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-02T10:01:37.200863Z","iopub.execute_input":"2022-01-02T10:01:37.201215Z","iopub.status.idle":"2022-01-02T10:01:37.205496Z","shell.execute_reply.started":"2022-01-02T10:01:37.201169Z","shell.execute_reply":"2022-01-02T10:01:37.204873Z"}}},{"cell_type":"markdown","source":"We want to estimate ability to catch the ball (for opponent receiver)\n\n# I. Collect the data","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-01-04T10:46:18.570363Z","iopub.execute_input":"2022-01-04T10:46:18.570779Z","iopub.status.idle":"2022-01-04T10:46:18.600646Z","shell.execute_reply.started":"2022-01-04T10:46:18.570679Z","shell.execute_reply":"2022-01-04T10:46:18.599912Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read income data\ngame = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/games.csv')\nplayer = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/players.csv')\nplay = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/plays.csv')\n\ntracking2018 = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/tracking2018.csv')\ntracking2019 = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/tracking2019.csv')\ntracking2020 = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/tracking2020.csv')\n\ntracking = tracking2018\ntracking.append(tracking2019)\ntracking.append(tracking2020)\n\nPFF = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/PFFScoutingData.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-04T10:46:18.601980Z","iopub.execute_input":"2022-01-04T10:46:18.602584Z","iopub.status.idle":"2022-01-04T10:48:44.773886Z","shell.execute_reply.started":"2022-01-04T10:46:18.602544Z","shell.execute_reply":"2022-01-04T10:48:44.772784Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# II. Determine player\nWho was the nearest to ball","metadata":{"execution":{"iopub.status.busy":"2022-01-02T10:03:42.238285Z","iopub.execute_input":"2022-01-02T10:03:42.238591Z","iopub.status.idle":"2022-01-02T10:03:59.244788Z","shell.execute_reply.started":"2022-01-02T10:03:42.23855Z","shell.execute_reply":"2022-01-02T10:03:59.243898Z"}}},{"cell_type":"code","source":"import math\npunt = pd.merge(play[play.specialTeamsPlayType=='Punt'],\n                 game,\n                 how = \"inner\", on = 'gameId')\nkickoff = pd.merge(play[play.specialTeamsPlayType=='Kickoff'],\n                 game,\n                 how = \"inner\", on = 'gameId')\n\n# function to determine nearest receiver\ndef nearest_to_ball(_x,_y,_team,_gameId,_playId):\n    global tracking\n    subplay = tracking[(tracking.playId ==_playId) & (tracking.gameId ==_gameId)\n                       &(tracking.team==_team)]\n    subplay = subplay.reset_index()\n    min_dist=170**2\n    playerId=0\n    for i in range(len(subplay)):\n        if ((subplay['x'][i]-_x)**2+(subplay['y'][i]-_y)**2)<min_dist:\n            min_dist= ((subplay['x'][i]-_x)**2+(subplay['y'][i]-_y)**2)\n            playerId= subplay['nflId'][i]\n    return playerId","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-01-04T10:48:44.775179Z","iopub.execute_input":"2022-01-04T10:48:44.775405Z","iopub.status.idle":"2022-01-04T10:48:44.846745Z","shell.execute_reply.started":"2022-01-04T10:48:44.775370Z","shell.execute_reply":"2022-01-04T10:48:44.845762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#punt muffed\npunt_muffed = punt[punt.specialTeamsResult == 'Muffed']\npunt_muffed = pd.merge(punt_muffed[['gameId', 'playId', 'possessionTeam','homeTeamAbbr','visitorTeamAbbr']],\n                  tracking[(tracking.team == 'football') & (tracking.event=='punt_muffed')],\n                  how = 'inner', on = ['gameId','playId']                 \n                 )\npunt_muffed = punt_muffed[['gameId', 'playId', 'possessionTeam','homeTeamAbbr','visitorTeamAbbr','x','y']]\n\n#kickoff muffed\nkickoff_muffed = kickoff[kickoff.specialTeamsResult == 'Muffed']\nkickoff_muffed = pd.merge(kickoff_muffed[['gameId', 'playId', 'possessionTeam','homeTeamAbbr','visitorTeamAbbr']],\n                  tracking[(tracking.team == 'football') & (tracking.event=='punt_muffed')],\n                  how = 'inner', on = ['gameId','playId']                 \n                 )\nkickoff_muffed = kickoff_muffed[['gameId', 'playId', 'possessionTeam','homeTeamAbbr','visitorTeamAbbr','x','y']]","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-01-04T10:48:44.848326Z","iopub.execute_input":"2022-01-04T10:48:44.849346Z","iopub.status.idle":"2022-01-04T10:48:52.218834Z","shell.execute_reply.started":"2022-01-04T10:48:44.849297Z","shell.execute_reply":"2022-01-04T10:48:52.218078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Blunders on punt","metadata":{}},{"cell_type":"code","source":"nearest_nflid = []\nfor i in range(len(punt_muffed)):\n    if punt_muffed['possessionTeam'][i]==punt_muffed['visitorTeamAbbr'][i]:\n        nearest_nflid.append(nearest_to_ball(punt_muffed['x'][i],punt_muffed['y'][i],\n                                             \"home\",punt_muffed['gameId'][i],punt_muffed['playId'][i]))\n    elif punt_muffed['possessionTeam'][i]==punt_muffed['homeTeamAbbr'][i]:\n        nearest_nflid.append(nearest_to_ball(punt_muffed['x'][i],punt_muffed['y'][i],\n                                             \"away\",punt_muffed['gameId'][i],punt_muffed['playId'][i]))\n    else:\n        pass\np_cnt = np.ones(len(punt_muffed))\nk_cnt = np.zeros(len(punt_muffed))\npunt_muffed = pd.concat([punt_muffed,\n                    pd.DataFrame(nearest_nflid, columns = ['nflId']),\n                    pd.DataFrame(p_cnt,columns=['punt_cnt']),\n                    pd.DataFrame(k_cnt, columns=['kickoff_cnt'])\n                   ],\n                  axis=1)\npunt_muffed.head(5)\n","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-01-04T10:48:52.221046Z","iopub.execute_input":"2022-01-04T10:48:52.221756Z","iopub.status.idle":"2022-01-04T10:50:34.481328Z","shell.execute_reply.started":"2022-01-04T10:48:52.221713Z","shell.execute_reply":"2022-01-04T10:50:34.480398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Blunders on kickoff","metadata":{}},{"cell_type":"code","source":"nearest_nflid = []\nfor i in range(len(kickoff_muffed)):\n    if kickoff_muffed['possessionTeam'][i]==kickoff_muffed['visitorTeamAbbr'][i]:\n        nearest_nflid.append(nearest_to_ball(kickoff_muffed['x'][i],kickoff_muffed['y'][i],\n                                             \"home\",kickoff_muffed['gameId'][i],kickoff_muffed['playId'][i]))\n    elif kickoff_muffed['possessionTeam'][i]==kickoff_muffed['homeTeamAbbr'][i]:\n        nearest_nflid.append(nearest_to_ball(kickoff_muffed['x'][i],kickoff_muffed['y'][i],\n                                             \"away\",kickoff_muffed['gameId'][i],kickoff_muffed['playId'][i]))\n    else:\n        pass\np_cnt = np.zeros(len(kickoff_muffed))\nk_cnt = np.ones(len(kickoff_muffed))\nkickoff_muffed = pd.concat([kickoff_muffed,\n                    pd.DataFrame(nearest_nflid, columns = ['nflId']),\n                    pd.DataFrame(p_cnt,columns=['punt_cnt']),\n                    pd.DataFrame(k_cnt, columns=['kickoff_cnt'])\n                   ],\n                  axis=1)\nkickoff_muffed.head(5)","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-01-04T10:50:34.482319Z","iopub.execute_input":"2022-01-04T10:50:34.482556Z","iopub.status.idle":"2022-01-04T10:50:38.675127Z","shell.execute_reply.started":"2022-01-04T10:50:34.482529Z","shell.execute_reply":"2022-01-04T10:50:38.674198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"muffed = punt_muffed\nmuffed.append(kickoff_muffed)\n\nmuffed['total'] = muffed['punt_cnt']+muffed['kickoff_cnt']","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-01-04T10:50:38.676271Z","iopub.execute_input":"2022-01-04T10:50:38.676546Z","iopub.status.idle":"2022-01-04T10:50:38.684313Z","shell.execute_reply.started":"2022-01-04T10:50:38.676515Z","shell.execute_reply":"2022-01-04T10:50:38.683457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# III. Results","metadata":{}},{"cell_type":"code","source":"pp = pd.merge(muffed, player, how='inner', on ='nflId')\npp= pp[['nflId','punt_cnt','kickoff_cnt','total','displayName','Position']]\n\nimport seaborn as sns\nax = sns.displot(data=pp[['Position']],x=\"Position\", binwidth=5, \n                 #hue=\"specialTeamsResult\", \n                 multiple=\"stack\")\nax.set(title='Muffed by position')","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-01-04T10:50:38.685674Z","iopub.execute_input":"2022-01-04T10:50:38.686343Z","iopub.status.idle":"2022-01-04T10:50:40.163677Z","shell.execute_reply.started":"2022-01-04T10:50:38.686306Z","shell.execute_reply":"2022-01-04T10:50:40.162753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pp[['displayName','total']].groupby(['displayName']).sum('total').sort_values(by = 'total', ascending=False)","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-01-04T10:50:40.165075Z","iopub.execute_input":"2022-01-04T10:50:40.165439Z","iopub.status.idle":"2022-01-04T10:50:40.189093Z","shell.execute_reply.started":"2022-01-04T10:50:40.165379Z","shell.execute_reply":"2022-01-04T10:50:40.188073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"May be there is a weak link.","metadata":{"execution":{"iopub.status.busy":"2022-01-02T10:15:35.036035Z","iopub.execute_input":"2022-01-02T10:15:35.036405Z","iopub.status.idle":"2022-01-02T10:15:35.04069Z","shell.execute_reply.started":"2022-01-02T10:15:35.036361Z","shell.execute_reply":"2022-01-02T10:15:35.039795Z"}}}]}