{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nfrom pandas.plotting import scatter_matrix\nimport seaborn as sns\nfrom sklearn.preprocessing import StandardScaler\nimport plotly.express as px\nimport plotly.graph_objects as go\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nimport sys\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\n#Disable the warnings\nimport warnings\nwarnings.filterwarnings('ignore')\n\n#Disabling the plotly errors\nfrom plotly.offline import plot, iplot, init_notebook_mode\ninit_notebook_mode(connected=True)","metadata":{"_uuid":"bc2411a0-ffae-43ba-877e-a68f6022134c","_cell_guid":"745b3601-0d4d-4853-9d58-c7dfa538a3fe","collapsed":false,"jupyter":{"outputs_hidden":false},"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T14:39:38.816234Z","iopub.execute_input":"2022-01-06T14:39:38.818061Z","iopub.status.idle":"2022-01-06T14:39:42.064468Z","shell.execute_reply.started":"2022-01-06T14:39:38.817802Z","shell.execute_reply":"2022-01-06T14:39:42.063468Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players = pd.read_csv('../input/nfl-big-data-bowl-2022/players.csv')\ngames = pd.read_csv('../input/nfl-big-data-bowl-2022/games.csv')\nplays = pd.read_csv('../input/nfl-big-data-bowl-2022/plays.csv')\nscouting = pd.read_csv('../input/nfl-big-data-bowl-2022/PFFScoutingData.csv')","metadata":{"_uuid":"7d760346-3b1c-493c-ba22-259b0420d162","_cell_guid":"d6ac0b24-2979-4cec-89ac-a874696fd3eb","collapsed":false,"jupyter":{"outputs_hidden":false},"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T14:43:33.68459Z","iopub.execute_input":"2022-01-06T14:43:33.684967Z","iopub.status.idle":"2022-01-06T14:43:34.197809Z","shell.execute_reply.started":"2022-01-06T14:43:33.684925Z","shell.execute_reply":"2022-01-06T14:43:34.197111Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Players dataset\")\nplayers.info()\n\nprint(\"Games dataset\")\ngames.info()","metadata":{"_uuid":"b7c0951a-445f-4ee4-9a3d-e1056adbd66b","_cell_guid":"9da96d78-b3d8-4803-a7c0-42fb9fee8074","collapsed":false,"jupyter":{"outputs_hidden":false},"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-01-06T14:07:17.955163Z","iopub.execute_input":"2022-01-06T14:07:17.955495Z","iopub.status.idle":"2022-01-06T14:07:17.995075Z","shell.execute_reply.started":"2022-01-06T14:07:17.955453Z","shell.execute_reply":"2022-01-06T14:07:17.994068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T15:02:16.283015Z","iopub.execute_input":"2022-01-06T15:02:16.283381Z","iopub.status.idle":"2022-01-06T15:02:16.314037Z","shell.execute_reply.started":"2022-01-06T15:02:16.283343Z","shell.execute_reply":"2022-01-06T15:02:16.31317Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting Feet to Inches\ntemp = players[\"height\"].str.split(\"-\", n = 1, expand = True)\ntemp.columns = ['Feet', 'Inch']\nplayers[\"height_feet\"] = pd.to_numeric(temp[\"Feet\"]) #converting string to numeric values\nplayers[\"height_inch\"] = pd.to_numeric(temp[\"Inch\"])\nprint(\"The shape of the dataset after feature engineering is : \", players.shape)\nprint(players.sample(2))","metadata":{"_uuid":"8be1f917-a8e1-4c22-8bc9-10e69b0bb928","_cell_guid":"e9370465-be61-423e-b543-40d23d0433f2","collapsed":false,"jupyter":{"outputs_hidden":false},"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-01-06T15:01:31.78419Z","iopub.execute_input":"2022-01-06T15:01:31.784944Z","iopub.status.idle":"2022-01-06T15:01:31.816022Z","shell.execute_reply.started":"2022-01-06T15:01:31.784892Z","shell.execute_reply":"2022-01-06T15:01:31.814738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Thanks to @Sanjay V for the following cleanup codes, and additional data","metadata":{"_kg_hide-input":true,"_kg_hide-output":true}},{"cell_type":"code","source":"# Column - height_feet\nfor i in players[\"height_feet\"]:\n    if i > 7:\n        j = 0.0833333 * i\n        players[\"height_feet\"].replace({i : j}, inplace=True)\n        \n# Column - height_inch\nplayers[\"height_inch\"] = 0.0833333 * players[\"height_inch\"]\n#print(players.tail())\n\n#filling NA with '0'\nfor i in players[\"height_inch\"]:\n    players[\"height_inch\"] = players[\"height_inch\"].fillna(0)\n#print(players[\"height_inch\"])\n\n#bring it all together\nplayers[\"height_feet\"] = players[\"height_feet\"] + players[\"height_inch\"]\n\nprint(players[\"height_feet\"])","metadata":{"_uuid":"3d071d11-74bf-41bd-a545-0f9a701d6df5","_cell_guid":"b6083ed6-90ce-4cec-af1b-b7905d5d20a8","collapsed":false,"jupyter":{"outputs_hidden":false},"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-01-06T14:07:18.033706Z","iopub.execute_input":"2022-01-06T14:07:18.03401Z","iopub.status.idle":"2022-01-06T14:07:18.738336Z","shell.execute_reply.started":"2022-01-06T14:07:18.033959Z","shell.execute_reply":"2022-01-06T14:07:18.737244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Replacing the position abbreviations with full format\n\nplayers[\"Position\"].replace({\"WR\": \"Wide Receiver\", \"CB\": \"Corner Back\", \"DE\": \"Defensive End\", \"OLB\": \"Outside Linebacker\",\n                           \"TE\": \"Tight End\", \"T\": \"Tackle\", \"RB\": \"Running Back\", \"G\": \"Guard\", \"DT\": \"Defensive Tackle\",\n                           \"ILB\": \"Inside Linebacker\", \"FS\": \"Free Safety\", \"SS\": \"Strong Safety\", \"C\": \"Center\", \"K\": \"Kicker\",\n                           \"P\": \"Punter\", \"NT\": \"Nose Tackle\", \"LS\": \"Long Snapper\", \"LB\": \"Linebacker\", \"DB\": \"Defensive Back\",\n                           \"MLB\" : \"Middle Linebacker\", \"FB\": \"Full Back\", \"OT\": \"Offensive Tackle\", \"QB\": \"Quarter Back\",\n                           \"OG\": \"Offensive guard\", \"S\": \"Safety\", \"HB\": \"Half Back\"}, inplace=True)\nprint(players[\"Position\"])","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:18.740202Z","iopub.execute_input":"2022-01-06T14:07:18.74145Z","iopub.status.idle":"2022-01-06T14:07:18.759511Z","shell.execute_reply.started":"2022-01-06T14:07:18.741391Z","shell.execute_reply":"2022-01-06T14:07:18.758419Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"games[\"homeTeamAbbr\"].replace({\"ARI\": \"Arizona Cardinals\", \"KC\": \"Kansas City Chiefs\", \"TEN\": \"Tennessee Titans\", \"TB\": \"Tampa Bay Buccaneers\",\n                           \"SF\": \"San Francisco 49ers\", \"SEA\": \"Seattle Seahawks\", \"PIT\": \"Pittsburgh Steelers\", \"PHI\": \"Philadelphia Eagles\",\n                            \"NYJ\": \"New York Jets\", \"NYG\": \"New York Giants\", \"NO\": \"New Orleans Saints\", \"NE\": \"New England Patriots\", \n                             \"MIA\": \"Miami Dolphins\", \"ATL\": \"Atlanta Falcons\", \"LA\": \"Los Angeles Rams\", \"JAX\": \"Jacksonville Jaguars\", \n                             \"IND\": \"Indianapolis Colts\", \"BAL\": \"Baltimore Ravens\", \"BUF\": \"Buffalo Bills\", \"CAR\": \"Carolina Panthers\", \n                             \"CIN\": \"Cincinnati Bengals\", \"CLE\": \"Cleveland Browns\", \"DAL\": \"Dallas Cowboys\", \"DET\": \"Detroit Lions\", \n                             \"GB\": \"Green Bay Packers\", \"HOU\": \"Houston Texans\", \"WAS\": \"Washington Football Team\", \"DEN\": \"Denver Broncos\", \n                             \"MIN\": \"Minnesota Vikings\", \"CHI\": \"Chicago Bears\", \"LAC\": \"Los Angeles Chargers\", \"OAK\": \"Oakland Raiders\", \n                             \"LV\": \"Las Vegas Raiders\"}, inplace=True)\nprint(games[\"homeTeamAbbr\"])","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:18.760974Z","iopub.execute_input":"2022-01-06T14:07:18.761342Z","iopub.status.idle":"2022-01-06T14:07:18.776982Z","shell.execute_reply.started":"2022-01-06T14:07:18.761307Z","shell.execute_reply":"2022-01-06T14:07:18.776092Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Objective of this Competition\n\nDevelopment of these new methods to find the additional stats for special teams plays. \n\nThe effort put into finding the method can be adopted by the NFL for on air distribution, and it will help in improving America's most popular sports league.\n\nWhere to start? Competition evaluation metric is comprehensive, with opening for creativity.\n\n* Quantify special teams strategy. Which strategies yield the best results? What are other strategies that could be adopted?\n\n* Rank special teams players. How do they stack up with respect to one another?\n\n* Create a new special teams metric. \n\nBefore embarking on the new, check what exists already....\nhttps://nextgenstats.nfl.com/glossary","metadata":{}},{"cell_type":"code","source":"submit_table = go.Figure()\nheader = ['Sl_no','Features','Rankings','Remarks']\nsl_no = [1,2,3,4,5,6]\nFeatures = ['Best Team','Best Strategy','Strategies to be adopted','Team Players Ranking',\n            'Special metric','Penalty Losing Team']\nRankings = ['WAS : Washington Football Team','Punting','Return,Touchback and Muffed',\n            'Tress Way','Yet to be finalised','Las Vegas Raiders']\nRemarks = ['Washington Football Team had the best yards gained record in the seasons covered till 2020',\n           'Punting gains the maximum yards, while Kick-off comes close in next best strategy',\n          'Each strategy had multiple results based on the oponents.The most used were Return, Touchback & Muffed',\n          'Tress Way was best ranking player in terms of the yards gained in the games',\n          'More analysis is required by bringing in other NFL related datasets',\n          'Las Vegas Raiders have lost more yards due to penalties']\nsubmit_table.add_trace(go.Table(header_values=header,header_font_color='black',\n                              header_font_size=30,header_height=40,\n                              cells_values=[sl_no,Features,Rankings,Remarks],\n                              cells_font_size=15,cells_height=40,cells_align='center'))\nsubmit_table.update_layout(height = 800,width = 1000,title=\"Results Submission Table\")\nsubmit_table.show()","metadata":{"_uuid":"c908c650-4779-466f-9af5-db8889929f0b","_cell_guid":"dba9bb49-ef71-4b91-b67d-8fd2aa74ae6c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-01-06T15:58:33.071749Z","iopub.execute_input":"2022-01-06T15:58:33.072115Z","iopub.status.idle":"2022-01-06T15:58:33.097998Z","shell.execute_reply.started":"2022-01-06T15:58:33.072079Z","shell.execute_reply":"2022-01-06T15:58:33.096869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"How the above results have been concluded is shared below. Only the tables, and charts that matter have been kept for viewing. Other charts are hidden to meet the competition guidelines.","metadata":{}},{"cell_type":"code","source":"from plotly.subplots import make_subplots\n\nfig = make_subplots(rows = 1, cols= 2)\n\nfig.add_trace(go.Box(x=players.height,name='Height'),row=1,col=1)\nfig.add_trace(go.Box(x=players.weight,name='Weight'),row=1,col=2)\n\nfig.update_layout(title='Players Height & Weights')","metadata":{"_uuid":"759dbdae-4b0e-4fb2-ae3d-116a7be28a80","_cell_guid":"8f632706-444b-434d-8e85-206ae453a6d3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-01-06T14:07:18.778516Z","iopub.execute_input":"2022-01-06T14:07:18.779039Z","iopub.status.idle":"2022-01-06T14:07:19.050413Z","shell.execute_reply.started":"2022-01-06T14:07:18.778995Z","shell.execute_reply":"2022-01-06T14:07:19.049605Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"team_table = go.Figure()\nheader = ['Sl_no','Seasons','Games']\nsl_no = [1,2,3]\nseasons = games.season.unique()\ngame_played = games.groupby('season')['gameId'].count().values\n\nteam_table.add_trace(go.Table(header_values=header,header_font_color='black',\n                              header_font_size=30,header_height=40,\n                              cells_values=[sl_no,seasons,game_played],\n                              cells_font_size=20,cells_height=40))\nteam_table.update_layout(height = 500,width = 800,title=\"Games & Seasons\")\nteam_table.show()","metadata":{"_uuid":"c908c650-4779-466f-9af5-db8889929f0b","_cell_guid":"dba9bb49-ef71-4b91-b67d-8fd2aa74ae6c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-01-06T14:07:19.051905Z","iopub.execute_input":"2022-01-06T14:07:19.052363Z","iopub.status.idle":"2022-01-06T14:07:19.113895Z","shell.execute_reply.started":"2022-01-06T14:07:19.052322Z","shell.execute_reply":"2022-01-06T14:07:19.1131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Which team has the highest yards gained?","metadata":{}},{"cell_type":"code","source":"#Scores by each team in a game\nplay_result = pd.DataFrame()\nfor gameid in plays.gameId.unique():\n    temp = plays[(plays.gameId == gameid)].groupby('possessionTeam')['playResult'].mean().reset_index()\n    temp['gameId'] = gameid\n    #print(temp.shape[1])\n    play_result = play_result.append(temp)\n    \nplay_result['season'] = play_result.gameId.apply(lambda x: int(x/1000000))\nplay_result.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:19.117576Z","iopub.execute_input":"2022-01-06T14:07:19.118089Z","iopub.status.idle":"2022-01-06T14:07:21.273459Z","shell.execute_reply.started":"2022-01-06T14:07:19.118044Z","shell.execute_reply":"2022-01-06T14:07:21.272384Z"},"_kg_hide-output":true,"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Bar chart of the average \"yards gained\" by each team in the games in multiple seasons till date.\nteam_res = play_result.groupby(['possessionTeam','season'])['playResult'].mean().reset_index()\nteam_res.season = team_res.season.astype('category')\np_res = px.bar(data_frame=team_res,y='possessionTeam',x='playResult',color='season')\np_res.update_layout(barmode = 'stack',height = 800, width = 800, \n                    title = 'Play results of the Teams',\n                   yaxis = {'categoryorder' : 'total ascending'})\np_res.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:21.275172Z","iopub.execute_input":"2022-01-06T14:07:21.275432Z","iopub.status.idle":"2022-01-06T14:07:22.134575Z","shell.execute_reply.started":"2022-01-06T14:07:21.275401Z","shell.execute_reply":"2022-01-06T14:07:22.133829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Which strategy has the highest yards gained for the teams in average?","metadata":{}},{"cell_type":"code","source":"plays['season'] = plays.gameId.apply(lambda x:int(x/1000000))\nplay_tidy = plays.groupby(['season','specialTeamsPlayType','specialTeamsResult'])['playResult'].mean().reset_index()\nplay_type = px.bar(data_frame=play_tidy,x='playResult',y='specialTeamsPlayType',\n                   color ='specialTeamsResult',facet_col='season',facet_col_wrap=2)\nplay_type.update_layout(width = 1000,height = 1000,title = 'Play types & its results',\n                        barmode = 'group')\nplay_type.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:22.136287Z","iopub.execute_input":"2022-01-06T14:07:22.136847Z","iopub.status.idle":"2022-01-06T14:07:22.506538Z","shell.execute_reply.started":"2022-01-06T14:07:22.136777Z","shell.execute_reply":"2022-01-06T14:07:22.505721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Which teams are leading in the various play types?\nThis chart shows how the teams were implementing the startegies, and having the overall advantage.","metadata":{}},{"cell_type":"code","source":"play_team = plays.pivot_table(index=['specialTeamsResult','possessionTeam'],columns='specialTeamsPlayType',\n                              values='playResult',aggfunc='mean').reset_index()\nplay_team.fillna(0,inplace=True)\n\nteam_play = px.bar(data_frame=play_team,y='possessionTeam',x='Punt',color='specialTeamsResult')\nteam_play.update_layout(barmode='stack', yaxis={'categoryorder':'total ascending'},height = 1200,\n                       title = 'Yard gain distribution when using the Punt Playtype')\nteam_play.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:22.508013Z","iopub.execute_input":"2022-01-06T14:07:22.508905Z","iopub.status.idle":"2022-01-06T14:07:22.664498Z","shell.execute_reply.started":"2022-01-06T14:07:22.50885Z","shell.execute_reply":"2022-01-06T14:07:22.663673Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"field_goal = px.bar(data_frame=play_team,y='possessionTeam',x='Field Goal',color='specialTeamsResult')\nfield_goal.update_layout(barmode='stack', yaxis={'categoryorder':'total ascending'},\n                        height = 1200,title = 'Yard gain distribution when using the Field Goals Type')\nfield_goal.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:22.665826Z","iopub.execute_input":"2022-01-06T14:07:22.666235Z","iopub.status.idle":"2022-01-06T14:07:22.796229Z","shell.execute_reply.started":"2022-01-06T14:07:22.666196Z","shell.execute_reply":"2022-01-06T14:07:22.795395Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Kickoff = px.bar(data_frame=play_team,y='possessionTeam',x='Kickoff',color='specialTeamsResult')\nKickoff.update_layout(barmode='stack', yaxis={'categoryorder':'total ascending'},\n                        height = 1200,title = 'Yard gain distribution when using the Kickoff Playtype')\nKickoff.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:22.797673Z","iopub.execute_input":"2022-01-06T14:07:22.798596Z","iopub.status.idle":"2022-01-06T14:07:22.928224Z","shell.execute_reply.started":"2022-01-06T14:07:22.798544Z","shell.execute_reply":"2022-01-06T14:07:22.927176Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## The players in the game types, and their performance","metadata":{}},{"cell_type":"code","source":"player_id = players[['displayName','nflId']] #Just the name\nplays = pd.merge(left=player_id,right=plays,how='right',right_on='kickerId',left_on='nflId') #merging DF\nplays.drop('nflId',axis=1,inplace=True)#dropping duplicate Id\n#plays.info()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:22.929885Z","iopub.execute_input":"2022-01-06T14:07:22.93022Z","iopub.status.idle":"2022-01-06T14:07:22.992946Z","shell.execute_reply.started":"2022-01-06T14:07:22.930174Z","shell.execute_reply":"2022-01-06T14:07:22.991896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Which player had the best yards gained in the seasons till date. \nThis results include the 2021 season also.","metadata":{}},{"cell_type":"code","source":"#Connecting the Players to the results, and selecting the top kickers whose total results were above 10,000\nplay_act = plays.pivot_table(index=['displayName','season'],columns='specialTeamsResult',\n                              values='playResult',aggfunc='mean').reset_index()\nplay_act.fillna(0,inplace=True)\nplay_act['total_result'] = play_act.iloc[:, 2:].sum(axis=1)\nplay_act.sort_values(by='total_result',ascending=False,inplace=True)\nplay_act.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:22.99443Z","iopub.execute_input":"2022-01-06T14:07:22.995329Z","iopub.status.idle":"2022-01-06T14:07:23.049086Z","shell.execute_reply.started":"2022-01-06T14:07:22.995265Z","shell.execute_reply":"2022-01-06T14:07:23.04799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kicker = play_act[(play_act.total_result > 200)&\n        (play_act.season == 2020)]['displayName'].values\nplays[plays.displayName.isin(kicker)].head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:23.050471Z","iopub.execute_input":"2022-01-06T14:07:23.050738Z","iopub.status.idle":"2022-01-06T14:07:23.088832Z","shell.execute_reply.started":"2022-01-06T14:07:23.050705Z","shell.execute_reply":"2022-01-06T14:07:23.087729Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Which player has gained most yards in average every season?","metadata":{}},{"cell_type":"code","source":"kick_act = plays[plays.displayName.isin(kicker)]\nkick_act['season'] = kick_act.season.astype('str')\nkick_act_grp = kick_act.groupby(['displayName','season'])['playResult'].mean().reset_index()\nresult_fig = px.bar(data_frame=kick_act_grp,y='displayName',x='playResult',color='season')\nresult_fig.update_layout(yaxis={'categoryorder':'total descending'},\n                         title='Yard gain distribution by the player')\nresult_fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:23.090056Z","iopub.execute_input":"2022-01-06T14:07:23.090276Z","iopub.status.idle":"2022-01-06T14:07:23.195984Z","shell.execute_reply.started":"2022-01-06T14:07:23.090249Z","shell.execute_reply":"2022-01-06T14:07:23.194986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Which type penalty has lead to yards gained or lost","metadata":{"_kg_hide-output":true}},{"cell_type":"code","source":"#Lets check the penalties, which team has the most and its impact\n\nplays.penaltyYards.fillna(0,inplace=True)\n\npenalty_imp = plays.groupby('penaltyCodes')['penaltyYards'].mean().reset_index()\npenal = px.bar(data_frame=penalty_imp,x='penaltyCodes',y='penaltyYards')\npenal.update_layout(xaxis={'categoryorder' : 'total ascending'},\n                    title = 'Yard lost/gained distribution based on penalty types')\npenal.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:23.19761Z","iopub.execute_input":"2022-01-06T14:07:23.198095Z","iopub.status.idle":"2022-01-06T14:07:23.277273Z","shell.execute_reply.started":"2022-01-06T14:07:23.198059Z","shell.execute_reply":"2022-01-06T14:07:23.276203Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Seperating penalty jersey numbers by teams\nteam_jer_no = plays.penaltyJerseyNumbers.str.split(';',expand=True)\n#team_jer_no[0]","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:23.278819Z","iopub.execute_input":"2022-01-06T14:07:23.279032Z","iopub.status.idle":"2022-01-06T14:07:23.321663Z","shell.execute_reply.started":"2022-01-06T14:07:23.279005Z","shell.execute_reply":"2022-01-06T14:07:23.321055Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Seperating penalty Jersey number \njer_no = plays.penaltyJerseyNumbers.str.split(' ',expand=True,n=2)\njer_no.drop(2,inplace=True,axis=1)\njer_no.columns = ['Team','Id']\njer_no['penaltyCodes'] = plays.penaltyCodes\njer_no['penaltyYards'] = plays.penaltyYards\njer_no['teamjerseyCodes'] = team_jer_no[0] #taking the 1st penalty jersey code\njer_no.dropna(inplace=True)\njer_no.Id = jer_no.Id.str.split(';',expand=True)[0]\njer_no.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:23.32378Z","iopub.execute_input":"2022-01-06T14:07:23.324165Z","iopub.status.idle":"2022-01-06T14:07:23.529812Z","shell.execute_reply.started":"2022-01-06T14:07:23.32412Z","shell.execute_reply":"2022-01-06T14:07:23.528856Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Which teams had the most Penalty Yard","metadata":{}},{"cell_type":"code","source":"#Next to see, which team is being penalised\npenalty_team = jer_no.groupby('Team')['penaltyYards'].mean().reset_index()\npenal_team = px.bar(data_frame=penalty_team,x='Team',y='penaltyYards')\npenal_team.update_layout(xaxis={'categoryorder' : 'total ascending'},\n                        title = 'Yard gain/lost distribution based on team')\npenal_team.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:23.530901Z","iopub.execute_input":"2022-01-06T14:07:23.532064Z","iopub.status.idle":"2022-01-06T14:07:23.607305Z","shell.execute_reply.started":"2022-01-06T14:07:23.531998Z","shell.execute_reply":"2022-01-06T14:07:23.606344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Next to see, who was the reason behind the penalty yards?","metadata":{}},{"cell_type":"code","source":"penalty_id = jer_no.groupby('teamjerseyCodes')['penaltyYards'].sum().reset_index()\npenal_id = px.bar(data_frame=penalty_id,y='teamjerseyCodes',x='penaltyYards')\npenal_id.update_layout(height = 1000,yaxis={'categoryorder' : 'total ascending'},\n                      title = 'Yard gain/lost distribution based on player')\npenal_id.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:23.60868Z","iopub.execute_input":"2022-01-06T14:07:23.608944Z","iopub.status.idle":"2022-01-06T14:07:23.693693Z","shell.execute_reply.started":"2022-01-06T14:07:23.608912Z","shell.execute_reply":"2022-01-06T14:07:23.692587Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### How did the snap turned out for each play? ","metadata":{}},{"cell_type":"code","source":"scout_snap = scouting[~scouting.snapDetail.isna()]\nplay_snap = pd.DataFrame()\nfor gameid in scout_snap.gameId.unique():\n    play_id = scout_snap[scout_snap.gameId == gameid]['playId']\n    temp = plays[plays.playId.isin(play_id)&(plays.gameId == gameid)]\n    play_snap = play_snap.append(temp)\nscout_snap['displayName'] = play_snap['displayName']\nscout_snap['playResult'] = play_snap['playResult']\nscout_snap['season'] = play_snap['season']\nscout_snap.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:23.69569Z","iopub.execute_input":"2022-01-06T14:07:23.696021Z","iopub.status.idle":"2022-01-06T14:07:32.85531Z","shell.execute_reply.started":"2022-01-06T14:07:23.69598Z","shell.execute_reply":"2022-01-06T14:07:32.854259Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Recovering memory given to dataframes\nimport gc\ndel play_team,play_act,kick_act_grp,penalty_imp,penalty_team,penalty_id\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:32.857173Z","iopub.execute_input":"2022-01-06T14:07:32.857484Z","iopub.status.idle":"2022-01-06T14:07:33.004285Z","shell.execute_reply.started":"2022-01-06T14:07:32.857441Z","shell.execute_reply":"2022-01-06T14:07:33.003228Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"snap_time = px.box(data_frame=scout_snap,x='season',y='snapTime')\nsnap_time.update_layout(title='Snap Times over seasons',height = 600)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:33.006205Z","iopub.execute_input":"2022-01-06T14:07:33.006517Z","iopub.status.idle":"2022-01-06T14:07:33.130061Z","shell.execute_reply.started":"2022-01-06T14:07:33.006476Z","shell.execute_reply":"2022-01-06T14:07:33.129162Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hang_time = px.box(data_frame=scout_snap,x='season',y='hangTime')\nhang_time.update_layout(title='Hang Times over seasons',height = 600)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:33.13391Z","iopub.execute_input":"2022-01-06T14:07:33.134195Z","iopub.status.idle":"2022-01-06T14:07:33.232567Z","shell.execute_reply.started":"2022-01-06T14:07:33.134162Z","shell.execute_reply":"2022-01-06T14:07:33.231745Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oper_time = px.box(data_frame=scout_snap,x='season',y='operationTime')\noper_time.update_layout(title='operation Times over seasons',height = 500)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:33.234357Z","iopub.execute_input":"2022-01-06T14:07:33.234832Z","iopub.status.idle":"2022-01-06T14:07:33.329347Z","shell.execute_reply.started":"2022-01-06T14:07:33.234775Z","shell.execute_reply":"2022-01-06T14:07:33.328802Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### How did the snapDetail impact the yards gained?\nThe spread of the play result for the \"OK\" Snaps always dont gain yards. There is a probability of yards being lost even with good snap","metadata":{}},{"cell_type":"code","source":"scout_box = px.box(data_frame=scout_snap,x='playResult',color='snapDetail')\nscout_box.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:33.330327Z","iopub.execute_input":"2022-01-06T14:07:33.33128Z","iopub.status.idle":"2022-01-06T14:07:33.429572Z","shell.execute_reply.started":"2022-01-06T14:07:33.331239Z","shell.execute_reply":"2022-01-06T14:07:33.42878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### How did the players impact the Yards gained per snap in each season","metadata":{}},{"cell_type":"code","source":"play_box = scout_snap.groupby(['displayName','season'])['playResult'].mean().reset_index()\nplay_box.season = play_box.season.astype('category')\nscout_bar = px.bar(data_frame=play_box,x='playResult',y='displayName',animation_frame='season',\n                   category_orders={'season':['2018','2019','2020','2021']})\nscout_bar.update_layout(title='Impact of Players on the Snap results',height =1000,\n                       yaxis = {'categoryorder' : 'total ascending'})\nscout_bar.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:33.430823Z","iopub.execute_input":"2022-01-06T14:07:33.4317Z","iopub.status.idle":"2022-01-06T14:07:33.588486Z","shell.execute_reply.started":"2022-01-06T14:07:33.431665Z","shell.execute_reply":"2022-01-06T14:07:33.587878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()\ndel play_box","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:33.589509Z","iopub.execute_input":"2022-01-06T14:07:33.590286Z","iopub.status.idle":"2022-01-06T14:07:33.73397Z","shell.execute_reply.started":"2022-01-06T14:07:33.590243Z","shell.execute_reply":"2022-01-06T14:07:33.732731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### How is the track record of the players on the field? \n\nThere are 3 tracking datasets from 2018 to 2020 provided to us for analysis\n\nAll three datasets have the same number of columns, so essentially, all the parameters can be created as \na time-series over the 3 seasons. Even to predict, what to expect from each players in the coming season. \nThe time series data provided was cleaned using the helper functions, and the below datasets were created. ","metadata":{}},{"cell_type":"markdown","source":"#### The entire code can create condensed version of the tracking file\nperf_trac = pd.DataFrame()\n\nplay_trac = pd.DataFrame()\n\nfor reference in trac.gameId.unique():\n\n    print('Processing {}'.format(reference))\n    \n    temp_id = performance(reference)\n    \n    temp_play = play_perform(reference)\n    \n    print('completed {}'.format(reference))\n    \n    perf_trac = perf_trac.append(temp_id)\n    \n    play_trac = play_trac.append(temp_play)\n    \n#### The above code takes more than 1.5 hours to complete, due to multiple groupby operations on huge data \n\nSo I have ran the code, created the csv file. Below ","metadata":{"execution":{"iopub.status.busy":"2022-01-03T15:08:59.226545Z","iopub.execute_input":"2022-01-03T15:08:59.227415Z","iopub.status.idle":"2022-01-03T15:09:01.88302Z","shell.execute_reply.started":"2022-01-03T15:08:59.227375Z","shell.execute_reply":"2022-01-03T15:09:01.881286Z"},"_kg_hide-input":true}},{"cell_type":"markdown","source":"I am loading the same CSV files, to continue the visualisation","metadata":{"execution":{"iopub.status.busy":"2022-01-03T15:08:59.226545Z","iopub.execute_input":"2022-01-03T15:08:59.227415Z","iopub.status.idle":"2022-01-03T15:09:01.88302Z","shell.execute_reply.started":"2022-01-03T15:08:59.227375Z","shell.execute_reply":"2022-01-03T15:09:01.881286Z"},"_kg_hide-input":true}},{"cell_type":"code","source":"play_trac = pd.read_csv('../input/nfl-transformed-data/play_trac.csv',index_col=False)\nperf_trac = pd.read_csv('../input/nfl-transformed-data/perf_trac.csv',index_col=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:40:31.437839Z","iopub.execute_input":"2022-01-06T14:40:31.438203Z","iopub.status.idle":"2022-01-06T14:40:32.280236Z","shell.execute_reply.started":"2022-01-06T14:40:31.438167Z","shell.execute_reply":"2022-01-06T14:40:32.27916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The tracking of the player performance with respect each gameID and season. \n#The Distance, Speed and Acceleration is collected.\nplay_trac.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:42:11.223591Z","iopub.execute_input":"2022-01-06T14:42:11.22397Z","iopub.status.idle":"2022-01-06T14:42:11.248288Z","shell.execute_reply.started":"2022-01-06T14:42:11.223933Z","shell.execute_reply":"2022-01-06T14:42:11.247524Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The performance track of individual player alone is also collected. \nperf_trac.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:42:26.583447Z","iopub.execute_input":"2022-01-06T14:42:26.583871Z","iopub.status.idle":"2022-01-06T14:42:26.608987Z","shell.execute_reply.started":"2022-01-06T14:42:26.583827Z","shell.execute_reply":"2022-01-06T14:42:26.607556Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"box_perf = go.Figure()\npx.box(data_frame=perf_trac,x='dis',color='season')","metadata":{"execution":{"iopub.status.busy":"2022-01-06T16:26:06.310694Z","iopub.execute_input":"2022-01-06T16:26:06.311125Z","iopub.status.idle":"2022-01-06T16:26:06.613432Z","shell.execute_reply.started":"2022-01-06T16:26:06.311086Z","shell.execute_reply":"2022-01-06T16:26:06.612375Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Accelaration plot\npx.box(data_frame=perf_trac,x='a',color='season')","metadata":{"execution":{"iopub.status.busy":"2022-01-06T16:26:56.692229Z","iopub.execute_input":"2022-01-06T16:26:56.692633Z","iopub.status.idle":"2022-01-06T16:26:56.984018Z","shell.execute_reply.started":"2022-01-06T16:26:56.692593Z","shell.execute_reply":"2022-01-06T16:26:56.983093Z"},"_kg_hide-output":true,"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Speed plot\npx.box(data_frame=perf_trac,x='s',color='season')","metadata":{"execution":{"iopub.status.busy":"2022-01-06T16:27:34.108364Z","iopub.execute_input":"2022-01-06T16:27:34.108736Z","iopub.status.idle":"2022-01-06T16:27:34.386099Z","shell.execute_reply.started":"2022-01-06T16:27:34.108699Z","shell.execute_reply":"2022-01-06T16:27:34.384923Z"},"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Player Performance data\n#perf_trac.drop('Unnamed: 0',inplace=True,axis=1)\n#perf_trac.head()\n#play_trac.drop('Unnamed: 0',inplace=True,axis=1)\nplay_trac = pd.merge(left = players[['nflId','displayName']],\n                     left_on='nflId',right_on='nflId',right=play_trac,how='right')\nplay_trac.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:44:05.296516Z","iopub.execute_input":"2022-01-06T14:44:05.297663Z","iopub.status.idle":"2022-01-06T14:44:05.596438Z","shell.execute_reply.started":"2022-01-06T14:44:05.297591Z","shell.execute_reply":"2022-01-06T14:44:05.595446Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"play_trac.gameId = play_trac.gameId.astype('str')\nplay_trac.season = play_trac.season.astype('category')\nperf_trac.gameId = perf_trac.gameId.astype('str')\nperf_trac.season = perf_trac.season.astype('category')","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:46:33.430451Z","iopub.execute_input":"2022-01-06T14:46:33.430829Z","iopub.status.idle":"2022-01-06T14:46:33.5453Z","shell.execute_reply.started":"2022-01-06T14:46:33.430776Z","shell.execute_reply":"2022-01-06T14:46:33.54413Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\nBefore we start searching the haystack, lets decide what to search for.\n\n* Highest average Distance covered by the player in a Game\n\n* Highest Average speed and acceleration of the player in a Game\n\n* Number of events in a fixed time duration, say 1 minute(not calculated)\n\n* Average distance travelled between events(not calculated)\n\n* Does the Distance, Speed and Acceleration change, depending on being in Home or Away?(not calculated)\n\n* Do the players and their performance remain same across seasons? ","metadata":{"execution":{"iopub.status.busy":"2022-01-03T15:35:51.297333Z","iopub.execute_input":"2022-01-03T15:35:51.297689Z"}}},{"cell_type":"code","source":"dis_all = play_trac.groupby(['season','displayName'])['dis'].sum().reset_index()\nacc_all = play_trac.groupby(['season','displayName'])['a'].mean().reset_index()\nspd_all = play_trac.groupby(['season','displayName'])['s'].mean().reset_index()\ndis_all.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T16:33:48.623242Z","iopub.execute_input":"2022-01-06T16:33:48.623604Z","iopub.status.idle":"2022-01-06T16:33:48.936511Z","shell.execute_reply.started":"2022-01-06T16:33:48.623568Z","shell.execute_reply":"2022-01-06T16:33:48.935313Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dis_all.sort_values(by='dis',ascending=False,inplace=True)\ndis_all.loc[:,['displayName','dis']].head(5)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T16:32:28.544063Z","iopub.execute_input":"2022-01-06T16:32:28.544486Z","iopub.status.idle":"2022-01-06T16:32:29.685588Z","shell.execute_reply.started":"2022-01-06T16:32:28.544447Z","shell.execute_reply":"2022-01-06T16:32:29.684701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spd_all.sort_values(by='s',ascending=False,inplace=True)\nspd_all.loc[:,['displayName','s']].head(5)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T16:34:01.656714Z","iopub.execute_input":"2022-01-06T16:34:01.657125Z","iopub.status.idle":"2022-01-06T16:34:01.673846Z","shell.execute_reply.started":"2022-01-06T16:34:01.657086Z","shell.execute_reply":"2022-01-06T16:34:01.672551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc_all.sort_values(by='a',ascending=False,inplace=True)\nacc_all.loc[:,['displayName','a']].head(5)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T16:34:34.120572Z","iopub.execute_input":"2022-01-06T16:34:34.121365Z","iopub.status.idle":"2022-01-06T16:34:34.13732Z","shell.execute_reply.started":"2022-01-06T16:34:34.121319Z","shell.execute_reply":"2022-01-06T16:34:34.136095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Distance covered by a particular player in multiple games\nvelo_trac = play_trac[play_trac.nflId == 39470].groupby(['gameId','season'])['s'].mean().reset_index()\nvelo_fig = px.strip(data_frame=velo_trac,x='season',y='s',color='gameId')\nvelo_fig.update_layout(title='Average speed of player',yaxis={'categoryorder':'total ascending'})\nvelo_fig.update_yaxes(type = 'log')\nvelo_fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:54:23.763204Z","iopub.execute_input":"2022-01-06T14:54:23.764229Z","iopub.status.idle":"2022-01-06T14:54:24.044647Z","shell.execute_reply.started":"2022-01-06T14:54:23.764159Z","shell.execute_reply":"2022-01-06T14:54:24.043597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Distance covered by a particular player in multiple games\nacl_trac = play_trac[play_trac.nflId == 39470].groupby(['gameId','season'])['a'].mean().reset_index()\nacl_fig = px.strip(data_frame=acl_trac,x='season',y='a',color='gameId')\nacl_fig.update_layout(title='Average acceleration of player')\nacl_fig.update_yaxes(type = 'log')\nacl_fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:56:04.47483Z","iopub.execute_input":"2022-01-06T14:56:04.47585Z","iopub.status.idle":"2022-01-06T14:56:04.767012Z","shell.execute_reply.started":"2022-01-06T14:56:04.475782Z","shell.execute_reply":"2022-01-06T14:56:04.765816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Distance covered by a particular player in multiple games\ndis_trac = play_trac[play_trac.nflId == 39470].groupby(['gameId','season'])['dis'].sum().reset_index()\ndis_fig = px.strip(data_frame=dis_trac,x='season',y='dis',color='gameId')\ndis_fig.update_layout(title='Distance run by NFLId player',yaxis={'categoryorder':'total ascending'})\ndis_fig.update_yaxes(type = 'log')\ndis_fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:50:51.788783Z","iopub.execute_input":"2022-01-06T14:50:51.789148Z","iopub.status.idle":"2022-01-06T14:50:52.072896Z","shell.execute_reply.started":"2022-01-06T14:50:51.789111Z","shell.execute_reply":"2022-01-06T14:50:52.071759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### End of the Notebook.... The following markdown cells are helper functions, written for collecting the player tracking datasets","metadata":{}},{"cell_type":"markdown","source":"trac2018 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2018.csv')\ntrac2019 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2019.csv')\ntrac2020 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2020.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:07:33.735444Z","iopub.execute_input":"2022-01-06T14:07:33.735688Z","iopub.status.idle":"2022-01-06T14:09:58.1566Z","shell.execute_reply.started":"2022-01-06T14:07:33.735635Z","shell.execute_reply":"2022-01-06T14:09:58.155621Z"}}},{"cell_type":"markdown","source":"* Distance moved the entire game by the player\n* Average speed and speed distribution of the player\n* Time & Distance Interval between the events\n* Yards gained or lost as the play result\n* Variation of the above parameters from one game to other\n* Variation of the above parameters over multiple seasons\n","metadata":{}},{"cell_type":"markdown","source":"from dateutil import parser\ndef time_parser(session):\n    x = parser.parse(session)\n    return pd.Timestamp(x)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:09:58.160706Z","iopub.execute_input":"2022-01-06T14:09:58.160998Z","iopub.status.idle":"2022-01-06T14:09:58.167262Z","shell.execute_reply.started":"2022-01-06T14:09:58.160967Z","shell.execute_reply":"2022-01-06T14:09:58.16572Z"}}},{"cell_type":"markdown","source":"#bringing all the dataset into one variable\nprint(trac2018.shape[0],trac2019.shape[0],trac2020.shape[0])\ntrac = trac2018.append(trac2019)\ntrac = trac.append(trac2020)\nprint(trac.shape)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:09:58.169136Z","iopub.execute_input":"2022-01-06T14:09:58.169402Z","iopub.status.idle":"2022-01-06T14:10:18.033482Z","shell.execute_reply.started":"2022-01-06T14:09:58.169369Z","shell.execute_reply":"2022-01-06T14:10:18.032806Z"}}},{"cell_type":"markdown","source":"#Saving memory from the RAM\ngc.collect()\ndel trac2018,trac2019,trac2020","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:10:18.034569Z","iopub.execute_input":"2022-01-06T14:10:18.034994Z","iopub.status.idle":"2022-01-06T14:10:18.734279Z","shell.execute_reply.started":"2022-01-06T14:10:18.034955Z","shell.execute_reply":"2022-01-06T14:10:18.733298Z"}}},{"cell_type":"markdown","source":"#Attaching seasons to the data\ntrac['season'] = trac.gameId.apply(lambda x: int(x/1000000))","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:10:18.73598Z","iopub.execute_input":"2022-01-06T14:10:18.736289Z","iopub.status.idle":"2022-01-06T14:10:48.258312Z","shell.execute_reply.started":"2022-01-06T14:10:18.736249Z","shell.execute_reply":"2022-01-06T14:10:48.257371Z"}}},{"cell_type":"markdown","source":"### Lets get some facts about these collosal datasets, before we delve deeper for insights","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:16:37.021737Z","iopub.execute_input":"2022-01-03T10:16:37.022401Z","iopub.status.idle":"2022-01-03T10:16:37.418033Z","shell.execute_reply.started":"2022-01-03T10:16:37.022358Z","shell.execute_reply":"2022-01-03T10:16:37.417072Z"}}},{"cell_type":"markdown","source":"trac_fig = make_subplots(rows=3,cols=1,shared_xaxes=True,vertical_spacing=0.03,\n                         specs=[[{\"type\": \"table\"}],[{\"type\": \"table\"}],[{\"type\": \"table\"}]])\nheader = ['Sl_no','Features','values']\nsl_no = [1,2,3]\nFeatures = ['Games Covered','Players Covered','Data points per player']\n\ndef data_point(season):\n    game_covered = len(trac[trac.season == season].gameId.unique())\n    id_covered = len(trac[trac.season == season].nflId.unique())\n    data_points = int(trac[trac.season == season].shape[0]/id_covered)\n    return game_covered, id_covered, data_points\n\nvalue_list = []\nfor season in trac.season.unique():\n    value_list.append(data_point(season))\n\ntrac_fig.add_trace(go.Table(header_values=header,header_height=40,header_font_size=24,\n                            cells_values=[sl_no,Features,value_list[0]],cells_font_size=18),row=1,col=1)\ntrac_fig.add_trace(go.Table(header_values=header,header_height=40,header_font_size=24,\n                            cells_values=[sl_no,Features,value_list[1]],cells_font_size=18),row=2,col=1)\ntrac_fig.add_trace(go.Table(header_values=header,header_height=40,header_font_size=24,\n                            cells_values=[sl_no,Features,value_list[2]],cells_font_size=18),row=3,col=1)\ntrac_fig.update_layout(height = 1000,width = 800,title=\"Games, players & data_points\")\ntrac_fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:10:48.259863Z","iopub.execute_input":"2022-01-06T14:10:48.26017Z"}}},{"cell_type":"markdown","source":"### Couple of insights\n\n* The 2020 season has lesser games covered and even lesser players\n\n* Datapoints in 2020 season is also lesser per player","metadata":{}},{"cell_type":"markdown","source":"#Analyse for a single player across the entire 3 seasons, then use that pipeline to work on the remaing \n#players to get the necessary output from the helper function\n\n#Helper Function to get the NFL_Id related data\ndef performance (ref_id):\n    trac_perf = trac[trac.gameId == ref_id]\n    trac_perf.loc[:,'time'] = trac_perf.time.apply(lambda x: time_parser(x))\n    trac_perf.set_index('time',inplace=True)\n    agg_rules = {'x':'last', 'y':'last', 's':'max', 'a':'max', 'dis':'sum',\n                 'event':'unique', 'nflId':'last','displayName':'last'}\n    grouper = pd.Grouper(freq='60S')\n    trac_min = trac_perf.groupby([grouper,'nflId']).agg(agg_rules)\n    trac_min.drop('nflId',axis=1,inplace=True)\n    trac_min.reset_index(inplace=True)\n    trac_min['event_count'] = trac_min.event.apply(lambda x:len(x))\n    agg_rul2 = {'s':'max', 'a':'max', 'dis':'sum','event_count':'sum'}\n    trac_agg = trac_min.groupby('nflId')['dis','s','a','event_count'].agg(agg_rul2).reset_index()\n    trac_agg['gameId'] = ref_id\n    trac_agg['season'] = int(ref_id/1000000)\n    return trac_agg","metadata":{}},{"cell_type":"markdown","source":"#Helper Function to get the play_Id related data\ndef play_perform(ref_id):\n    trac_perf = trac[trac.gameId == ref_id]\n    trac_perf.loc[:,'time'] = trac_perf.time.apply(lambda x: time_parser(x))\n    trac_perf.set_index('time',inplace=True)\n    agg_rules = {'x':'last', 'y':'last', 's':'max', 'a':'max', 'dis':'sum',\n                 'event':'unique', 'displayName':'last','playId':'unique'}\n    grouper = pd.Grouper(freq='60S')\n    trac_min = trac_perf.groupby([grouper,'playId','nflId']).agg(agg_rules)\n    trac_min.drop('playId',axis=1,inplace=True)\n    trac_min.reset_index(inplace=True)\n    trac_min['event_count'] = trac_min.event.apply(lambda x:len(x))\n    agg_rul2 = {'s':'max', 'a':'max', 'dis':'sum','event_count':'sum'}\n    play_agg = trac_min.groupby(['playId','nflId'])['dis','s','a','event_count','nflId'].agg(agg_rul2).reset_index()\n    play_agg['gameId'] = ref_id\n    play_agg['season'] = int(ref_id/1000000)\n    return play_agg","metadata":{}},{"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":[]},{"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":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}