{"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":"# NFL Big Data Bowl 2022 - Version-2\n\n---","metadata":{}},{"cell_type":"markdown","source":"**Problem Statement:**\n\n* Before National Football League (NFL) coaches celebrate a big W, they strategize ways to improve field position and score points. \n* Both of these objectives receive significant contributions from special teams plays, which consist of punts, kickoffs, field goals and extra points. \n* These play types take on important roles in a game’s final score—so much so that coaches say they're a third of the game. \n* Yet special teams remain an understudied part of American football, with an opportunity for data science to offer better ways to understand its impact.\n\n**Note:** In this competition, we need to quantify what happens on special teams plays. We might have to create a new special teams metric, quantify team or individual strategies, rank players, or even something we haven’t considered.\n\n---","metadata":{}},{"cell_type":"markdown","source":"**Dataset Involves:**\n\nThe 2022 Big Data Bowl data contains Next Gen Stats player tracking, play, game, player, and PFF scouting data for all 2018-2020 Special Teams plays. \n\n* **Game data:** The games.csv contains the teams playing in each game. The key variable is **gameId**.\n\n* **Play data:** The plays.csv file contains play-level information from each game. The key variables are **gameId** and **playId**.\n\n* **Player data:** The players.csv file contains player-level information from players that participated in any of the tracking data files. The key variable is **nflId**.\n\n* **Tracking data:** Files tracking[season].csv contain player tracking data from season [season]. The key variables are **gameId**, **playId**, and **nflId**.\n\n* **PFF Scouting data:** The PFFScoutingData.csv file contains play-level scouting information for each game. The key variables are **gameId** and **playId**.\n\n---","metadata":{}},{"cell_type":"markdown","source":"**Importing Libraries:**\n\n* To get started we will use Python for data pre-processing and data analysis.\n\n* Import python libraries as necessary to get started for data load and later import other libraries as needed\n\n---\n","metadata":{}},{"cell_type":"code","source":"# Importing package numpys (For Numerical Python)\nimport numpy as np \n# Importing for data analysis\nimport pandas as pd \n# module finds all the pathnames matching a specified pattern\nimport glob \n# module provides a portable way of using operating system dependent functionality\nimport os \n # Importing pyplot interface using matplotlib\nimport matplotlib.pyplot as plt \n# Importing seaborn library for interactive visualization\nimport seaborn as sns \n# Importing WordCloud for text data visualization\nfrom wordcloud import WordCloud\n# Importing matplotlib for plots\nimport matplotlib\n# Importing datetime for using datetime\nfrom datetime import datetime\n# Importing plotly for interactive plots\nimport plotly.express as px","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:10:35.410042Z","iopub.execute_input":"2021-12-11T11:10:35.410524Z","iopub.status.idle":"2021-12-11T11:10:37.964922Z","shell.execute_reply.started":"2021-12-11T11:10:35.410441Z","shell.execute_reply":"2021-12-11T11:10:37.964081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"# Loading dataset games.csv\ngame_data = pd.read_csv('../input/nfl-big-data-bowl-2022/games.csv')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:10:37.966511Z","iopub.execute_input":"2021-12-11T11:10:37.966752Z","iopub.status.idle":"2021-12-11T11:10:37.987817Z","shell.execute_reply.started":"2021-12-11T11:10:37.966719Z","shell.execute_reply":"2021-12-11T11:10:37.986780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert gameDate from object type to datetime type\ngame_data['gameDate']= pd.to_datetime(game_data['gameDate'])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:10:37.989308Z","iopub.execute_input":"2021-12-11T11:10:37.989634Z","iopub.status.idle":"2021-12-11T11:10:38.009626Z","shell.execute_reply.started":"2021-12-11T11:10:37.989592Z","shell.execute_reply":"2021-12-11T11:10:38.008979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"# Loading dataset plays.csv\nplay_data = pd.read_csv('../input/nfl-big-data-bowl-2022/plays.csv')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:10:38.011775Z","iopub.execute_input":"2021-12-11T11:10:38.012319Z","iopub.status.idle":"2021-12-11T11:10:38.165229Z","shell.execute_reply.started":"2021-12-11T11:10:38.012274Z","shell.execute_reply":"2021-12-11T11:10:38.164349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"\n","metadata":{}},{"cell_type":"code","source":"# Loading dataset players.csv\nplayer_data = pd.read_csv('../input/nfl-big-data-bowl-2022/players.csv')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:10:38.166921Z","iopub.execute_input":"2021-12-11T11:10:38.167254Z","iopub.status.idle":"2021-12-11T11:10:38.187003Z","shell.execute_reply.started":"2021-12-11T11:10:38.167213Z","shell.execute_reply":"2021-12-11T11:10:38.186077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert gameDate from object type to datetime type\nplayer_data['birthDate']= pd.to_datetime(player_data['birthDate'])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:10:38.188138Z","iopub.execute_input":"2021-12-11T11:10:38.188370Z","iopub.status.idle":"2021-12-11T11:10:38.195355Z","shell.execute_reply.started":"2021-12-11T11:10:38.188342Z","shell.execute_reply":"2021-12-11T11:10:38.194372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"# Loading dataset tracking2018.csv\ntracking2018_data = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2018.csv')\n\n# Loading dataset tracking2019.csv\ntracking2019_data = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2019.csv')\n\n# Loading dataset tracking2020.csv\ntracking2020_data = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2020.csv')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:10:38.196689Z","iopub.execute_input":"2021-12-11T11:10:38.197059Z","iopub.status.idle":"2021-12-11T11:12:28.865355Z","shell.execute_reply.started":"2021-12-11T11:10:38.197029Z","shell.execute_reply":"2021-12-11T11:12:28.864084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Note:** Using sample of tracking2020, tracking 2019 and tracking 2018 season dataset respectively for EDA","metadata":{}},{"cell_type":"code","source":"# create sample of tracking dataset for EDA (For faster execution and to avoid out of memory issue for notebook)\ntracking_data_sample = pd.concat([tracking2020_data.sample(n=100000, random_state=1),tracking2019_data.sample(n=100000, random_state=1),tracking2018_data.sample(n=100000, random_state=1)])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:28.867115Z","iopub.execute_input":"2021-12-11T11:12:28.867472Z","iopub.status.idle":"2021-12-11T11:12:31.521527Z","shell.execute_reply.started":"2021-12-11T11:12:28.867429Z","shell.execute_reply":"2021-12-11T11:12:31.520670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reset index after appending all three dataset\ntracking_data_sample.reset_index(inplace=True)\n# get quick summary of new dataset sample\ntracking_data_sample.head()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:31.523141Z","iopub.execute_input":"2021-12-11T11:12:31.523474Z","iopub.status.idle":"2021-12-11T11:12:31.556620Z","shell.execute_reply.started":"2021-12-11T11:12:31.523435Z","shell.execute_reply":"2021-12-11T11:12:31.556012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"code","source":"# Loading dataset PFFScoutingData.csv\nPFFScouting_data = pd.read_csv('../input/nfl-big-data-bowl-2022/PFFScoutingData.csv')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:31.558772Z","iopub.execute_input":"2021-12-11T11:12:31.559051Z","iopub.status.idle":"2021-12-11T11:12:31.655129Z","shell.execute_reply.started":"2021-12-11T11:12:31.559024Z","shell.execute_reply":"2021-12-11T11:12:31.654231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}},{"cell_type":"markdown","source":"---\n# Data Definition/Description of NFL 2022 data\n\n---","metadata":{}},{"cell_type":"markdown","source":"Note: Merge various dataset to arrive at combined sample dataset for EDA","metadata":{}},{"cell_type":"markdown","source":"---\n**Merge game and play dataset - game_play_data**\n\n---","metadata":{}},{"cell_type":"code","source":"# Merge of game and play dataset using key as gameId\ngame_play_data = pd.merge(game_data, play_data, how=\"inner\", on=[\"gameId\"])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:31.656597Z","iopub.execute_input":"2021-12-11T11:12:31.657374Z","iopub.status.idle":"2021-12-11T11:12:31.698731Z","shell.execute_reply.started":"2021-12-11T11:12:31.657332Z","shell.execute_reply":"2021-12-11T11:12:31.698046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n**Merge player and tracking_data_sample dataset- player_tracking_data**\n\n---","metadata":{}},{"cell_type":"code","source":"# Merge of player and tracking_data_sample season dataset using key as nflId\nplayer_tracking_data = pd.merge(player_data,tracking_data_sample, how=\"inner\", on=[\"nflId\"])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:31.700031Z","iopub.execute_input":"2021-12-11T11:12:31.700534Z","iopub.status.idle":"2021-12-11T11:12:32.310219Z","shell.execute_reply.started":"2021-12-11T11:12:31.700482Z","shell.execute_reply":"2021-12-11T11:12:32.309294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n**Merge game_play_data and PFFScouting dataset- game_play_scouting_data**\n\n---","metadata":{}},{"cell_type":"code","source":"# Merge of game_play_data and PFFScouting_data dataset using key as gameId and playId\ngame_play_scouting_data = pd.merge(game_play_data, PFFScouting_data, how=\"inner\", left_on=[\"gameId\",\"playId\"], right_on=[\"gameId\",\"playId\"])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:32.311382Z","iopub.execute_input":"2021-12-11T11:12:32.311633Z","iopub.status.idle":"2021-12-11T11:12:32.342681Z","shell.execute_reply.started":"2021-12-11T11:12:32.311607Z","shell.execute_reply":"2021-12-11T11:12:32.341857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n**Merge game_play_scouting and player_tracking dataset- nfl2022_data_sample**\n\n---","metadata":{}},{"cell_type":"code","source":"# Merge of game_play_scouting_data and player_tracking_data dataset using key as gameId and playId\nnfl2022_data_sample = pd.merge(game_play_scouting_data, player_tracking_data, how=\"inner\", left_on=[\"gameId\",\"playId\"], right_on=[\"gameId\",\"playId\"])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:32.343791Z","iopub.execute_input":"2021-12-11T11:12:32.344023Z","iopub.status.idle":"2021-12-11T11:12:33.077674Z","shell.execute_reply.started":"2021-12-11T11:12:32.343997Z","shell.execute_reply":"2021-12-11T11:12:33.076705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nfl2022_data_sample.rename(columns={'displayName_x':'displayName'}, inplace=True)\nnfl2022_data_sample.drop(columns=['displayName_y'], axis=1, inplace=True)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:33.079272Z","iopub.execute_input":"2021-12-11T11:12:33.079520Z","iopub.status.idle":"2021-12-11T11:12:33.354274Z","shell.execute_reply.started":"2021-12-11T11:12:33.079492Z","shell.execute_reply":"2021-12-11T11:12:33.353547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get shape of dataframe\nprint('Shape of nfl2022_data_sample dataset is:', nfl2022_data_sample.shape)\n\n# print summary of dataframe\nnfl2022_data_sample.info()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:33.355535Z","iopub.execute_input":"2021-12-11T11:12:33.355778Z","iopub.status.idle":"2021-12-11T11:12:33.865265Z","shell.execute_reply.started":"2021-12-11T11:12:33.355750Z","shell.execute_reply":"2021-12-11T11:12:33.864077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print first 5 rows of dataframe\nnfl2022_data_sample.head()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:33.866707Z","iopub.execute_input":"2021-12-11T11:12:33.867243Z","iopub.status.idle":"2021-12-11T11:12:33.898713Z","shell.execute_reply.started":"2021-12-11T11:12:33.867196Z","shell.execute_reply":"2021-12-11T11:12:33.897710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Impute kickLength missing value with median value\nnfl2022_data_sample['kickLength'].fillna(nfl2022_data_sample['kickLength'].median(), inplace=True)\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:33.900238Z","iopub.execute_input":"2021-12-11T11:12:33.900548Z","iopub.status.idle":"2021-12-11T11:12:33.909031Z","shell.execute_reply.started":"2021-12-11T11:12:33.900510Z","shell.execute_reply":"2021-12-11T11:12:33.908065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Impute kickReturnYardage missing value with median value\nnfl2022_data_sample['kickReturnYardage'].fillna(nfl2022_data_sample['kickReturnYardage'].median(), inplace=True)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:33.912193Z","iopub.execute_input":"2021-12-11T11:12:33.912481Z","iopub.status.idle":"2021-12-11T11:12:33.921594Z","shell.execute_reply.started":"2021-12-11T11:12:33.912451Z","shell.execute_reply":"2021-12-11T11:12:33.920764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Impute collegeName missing value with value as \"Missing\"\nnfl2022_data_sample['collegeName'].fillna(\"Missing\", inplace=True)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:33.923145Z","iopub.execute_input":"2021-12-11T11:12:33.923369Z","iopub.status.idle":"2021-12-11T11:12:33.945658Z","shell.execute_reply.started":"2021-12-11T11:12:33.923343Z","shell.execute_reply":"2021-12-11T11:12:33.944814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print summary of dataframe\nnfl2022_data_sample.info()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:33.947152Z","iopub.execute_input":"2021-12-11T11:12:33.947432Z","iopub.status.idle":"2021-12-11T11:12:34.454591Z","shell.execute_reply.started":"2021-12-11T11:12:33.947401Z","shell.execute_reply":"2021-12-11T11:12:34.453740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n# Data Analysis/EDA of NFL 2022 data\n\n---","metadata":{}},{"cell_type":"markdown","source":"---\n**Q: What is distribution of Season and Team with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"#distribution count for specialTeamsPlayType with respect to team and season\nsns.catplot(data=nfl2022_data_sample, x=\"specialTeamsPlayType\", col=\"team\", hue=\"season\", kind=\"count\", height=8, aspect=0.8, palette=\"rainbow\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:34.455627Z","iopub.execute_input":"2021-12-11T11:12:34.455954Z","iopub.status.idle":"2021-12-11T11:12:38.582429Z","shell.execute_reply.started":"2021-12-11T11:12:34.455911Z","shell.execute_reply":"2021-12-11T11:12:38.581582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Above plot shows that every season has similar pattern for Kickoff, Punt, Field Goal and Extra Point with respect to Special Teams Play Type. Number of Kickoff is higher in Season 2020 and Number of Punt was higher in Season 2018.","metadata":{}},{"cell_type":"markdown","source":"---\n**Q: What is distribution of Season with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"# distribution of season with respect to specialTeamsPlayType\nfig = px.sunburst(nfl2022_data_sample, path=['season', 'specialTeamsPlayType'], color='specialTeamsPlayType', hover_data=['season'])\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:38.583682Z","iopub.execute_input":"2021-12-11T11:12:38.583957Z","iopub.status.idle":"2021-12-11T11:12:43.251969Z","shell.execute_reply.started":"2021-12-11T11:12:38.583914Z","shell.execute_reply":"2021-12-11T11:12:43.251107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Above plot shows that every season has similar distribution for Special Teams Play Type as expected.","metadata":{}},{"cell_type":"markdown","source":"---\n**Q: What is distribution of Team with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"# distribution of team with respect to specialTeamsPlayType\nfig = px.sunburst(nfl2022_data_sample, path=['team', 'specialTeamsPlayType'], color='specialTeamsPlayType', hover_data=['team'])\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:43.253272Z","iopub.execute_input":"2021-12-11T11:12:43.253501Z","iopub.status.idle":"2021-12-11T11:12:46.881219Z","shell.execute_reply.started":"2021-12-11T11:12:43.253472Z","shell.execute_reply":"2021-12-11T11:12:46.880278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Above plot shows that for both away and home team, Special Teams Play Type has similar distribution with Number of Punt higher compared to Number of Kickoff.","metadata":{}},{"cell_type":"markdown","source":"---\n**Q: What is distribution of Play Direction with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"# distribution of playDirection with respect to specialTeamsPlayType\nfig = px.sunburst(nfl2022_data_sample, path=['playDirection', 'specialTeamsPlayType'], color='specialTeamsPlayType', hover_data=['playDirection'])\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:46.882619Z","iopub.execute_input":"2021-12-11T11:12:46.882845Z","iopub.status.idle":"2021-12-11T11:12:50.456636Z","shell.execute_reply.started":"2021-12-11T11:12:46.882820Z","shell.execute_reply":"2021-12-11T11:12:50.455827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Above plot shows that for both play direction right and left, Special Teams Play Type has similar distribution with Number of Punt higher compared to Number of Kickoff. Number of Punt is higher for right Play Direction compared to left Play Direction in this sample data.","metadata":{}},{"cell_type":"markdown","source":"---\n**Q: What is distribution of Special Teams Play Type with respect to Special Teams Result?**\n\n\n---","metadata":{}},{"cell_type":"code","source":"# distribution of specialTeamsPlayType with respect to specialTeamsResult\nfig = px.sunburst(nfl2022_data_sample, path=['specialTeamsPlayType', 'specialTeamsResult'], color='specialTeamsPlayType', hover_data=['specialTeamsPlayType'])\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:50.458043Z","iopub.execute_input":"2021-12-11T11:12:50.458828Z","iopub.status.idle":"2021-12-11T11:12:54.039481Z","shell.execute_reply.started":"2021-12-11T11:12:50.458787Z","shell.execute_reply":"2021-12-11T11:12:54.038660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Above plot shows that Kickoff has majority datapoint of Special Teams Play Result as Return and Touchback, Punt has majority datapoint of Special Teams Play Result as Return, Fair Catch and Downed, Field Goal has majority datapoint of Special Teams Play Result as Kick Attempt Good followed by Kick Attempt No Good with Extra Point has majority datapoint of Special Teams Play Result as Kick Attempt Good in this sample data.","metadata":{}},{"cell_type":"markdown","source":"---\n**Q: What is the distribution for Kick Type and Kick Length with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"#distribution of kickType, kickLength with respect to specialTeamsPlayType\nsns.catplot(data=nfl2022_data_sample, x=\"kickType\", y=\"kickLength\", hue=\"specialTeamsPlayType\", height=8, aspect=0.8)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:12:54.040634Z","iopub.execute_input":"2021-12-11T11:12:54.040852Z","iopub.status.idle":"2021-12-11T11:13:02.837632Z","shell.execute_reply.started":"2021-12-11T11:12:54.040826Z","shell.execute_reply":"2021-12-11T11:13:02.836617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"D: Deep, N: Normal - standard punt style, A: Nose down or Aussie-style punts, Q: Squib, P: Pooch kick, F: Flat, O: Obvious Onside, K: Free Kick, R: Rugby style punt, S: Surprise Onside, B: Deep Direct OOB\n\nAbove plot shows that majority of Kickoff resulted in kickType as Deep, Squib, Pooch Kick, Flat, Obvious Onside and Free Kick. And majority of Punt resulted in kickType as Normal and Nose down with some Rugby style punt in this sample data.","metadata":{}},{"cell_type":"markdown","source":"---\n**Q: What is the distribution for Kick Type and Play Result with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"#distribution of kickType, playResult with respect to specialTeamsPlayType\nsns.catplot(data=nfl2022_data_sample, x=\"kickType\", y=\"playResult\", hue=\"specialTeamsPlayType\", height=8, aspect=0.8)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-11T11:14:04.747224Z","iopub.execute_input":"2021-12-11T11:14:04.748181Z","iopub.status.idle":"2021-12-11T11:14:13.468908Z","shell.execute_reply.started":"2021-12-11T11:14:04.748138Z","shell.execute_reply":"2021-12-11T11:14:13.468043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"D: Deep, N: Normal - standard punt style, A: Nose down or Aussie-style punts, Q: Squib, P: Pooch kick, F: Flat, O: Obvious Onside, K: Free Kick, R: Rugby style punt, S: Surprise Onside, B: Deep Direct OOB\n\nAbove plot shows that majority of Kickoff resulted in kickType as Deep, Squib, Pooch Kick, Flat, Obvious Onside and Free Kick. And majority of Punt resulted in kickType as Normal and Nose down with some Rugby style punt in this sample data.","metadata":{}},{"cell_type":"markdown","source":"---\n**Q:What is the distribution of Special Teams Result and Special Teams Play Type with respect to Kick Length?**\n\n---","metadata":{}},{"cell_type":"code","source":"# get specialTeamsResult and specialTeamsPlayType sorted by kickLength\nkickLength = nfl2022_data_sample.groupby(by = ['specialTeamsPlayType','specialTeamsResult'], as_index = False)['kickLength'].agg('median').sort_values(by ='kickLength', ascending = False)\n# set plot figure size\nfigure = plt.figure(figsize = [15, 10])\n# plot comparison of specialTeamsResult\nsns.barplot(data = kickLength[0:12],x = 'kickLength', y ='specialTeamsResult', hue='specialTeamsPlayType',palette='winter')\n# set plot label\nplt.xlabel(xlabel = 'Kick Length')\nplt.ylabel(ylabel = 'Special Teams Result')\nplt.grid(b = True, axis = 'x')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:13:04.879481Z","iopub.execute_input":"2021-12-11T11:13:04.880615Z","iopub.status.idle":"2021-12-11T11:13:05.485620Z","shell.execute_reply.started":"2021-12-11T11:13:04.880551Z","shell.execute_reply":"2021-12-11T11:13:05.484515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n**Q:What is the distribution of Special Teams Result and Special Teams Play Type with respect to Play Result?**\n\n---","metadata":{}},{"cell_type":"code","source":"# get specialTeamsResult and specialTeamsPlayType sorted by playResult\nplayResult = nfl2022_data_sample.groupby(by = ['specialTeamsPlayType','specialTeamsResult'], as_index = False)['playResult'].agg('mean').sort_values(by ='playResult', ascending = False)\n# set plot figure size\nfigure = plt.figure(figsize = [15, 10])\n# plot comparison of specialTeamsResult\nsns.barplot(data = playResult[0:12], x = 'playResult', y ='specialTeamsResult',hue='specialTeamsPlayType',palette='summer')\n# set plot label\nplt.xlabel(xlabel = 'Play Result')\nplt.ylabel(ylabel = 'Special Teams Result')\nplt.grid(b = True, axis = 'x')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:13:05.487648Z","iopub.execute_input":"2021-12-11T11:13:05.488814Z","iopub.status.idle":"2021-12-11T11:13:05.915541Z","shell.execute_reply.started":"2021-12-11T11:13:05.488745Z","shell.execute_reply":"2021-12-11T11:13:05.914673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n**Q: What is the distribution for Kick Length and Play Result with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"# distribution of kickLength and playResult with respect to specialTeamsPlayType\nfig = px.scatter(nfl2022_data_sample, x=\"playResult\", y=\"kickLength\",size ='absoluteYardlineNumber',color=\"specialTeamsPlayType\",\n                 hover_name=\"specialTeamsPlayType\", log_x=True, size_max=10)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-11T11:29:36.370993Z","iopub.execute_input":"2021-12-11T11:29:36.371324Z","iopub.status.idle":"2021-12-11T11:29:38.356843Z","shell.execute_reply.started":"2021-12-11T11:29:36.371291Z","shell.execute_reply":"2021-12-11T11:29:38.354544Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n# Summary\n---\n\n**EDA Involves:**\n* Generate actionable, practical, and novel insights from player tracking data that corresponds to special teams play. \n\nPotential topics for EDA:\n\n* Create a new special teams metric. The winning algorithm from the 2020 Big Data Bowl has been adopted by the NFL/NFL Network for on air distribution, and we are hopeful that there could be a new stat for special teams plays that could come from this year’s competition.\n* Quantify special teams strategy. Special teams’ coaches are among the most creative and innovative in the league. Compare/contrast how each team game plans. Which strategies yield the best results? What are other strategies that could be adopted?\n* Rank special teams players. Each team employs a variety of players (including longsnappers, kickers, punters, and other utility special teams players). How do they stack up with respect to one another?\n---","metadata":{}},{"cell_type":"markdown","source":"**Highlights:**\n\nNote: Click/Touch on any category (Kickoff, Punt, Field Goal or Extra Point) in plot to drill down further.","metadata":{}},{"cell_type":"markdown","source":"---\n**Q: What are the Game Characteristics for top 5 Play Result with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"# get distribution of specialTeamsPlayType,specialTeamsResult,season,homeTeamAbbr and visitorTeamAbbr with respect to playResult\nkickoff = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Kickoff\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','season', 'team','homeTeamAbbr','visitorTeamAbbr'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\npunt = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Punt\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','season', 'team','homeTeamAbbr','visitorTeamAbbr'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\nfieldGoal = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Field Goal\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','season', 'team','homeTeamAbbr','visitorTeamAbbr'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\nextraPoint = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Extra Point\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','season', 'team','homeTeamAbbr','visitorTeamAbbr'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\n# combine each specialTeamsPlayType aggregation in single dataframe\ntopGameCharacteristicsbyPlayResult = pd.concat([kickoff[:5],punt[:5],fieldGoal[:5],extraPoint[:5]])\n# plot distribution of top 5 game characteristics\nfig = px.sunburst(topGameCharacteristicsbyPlayResult, path=['specialTeamsPlayType','specialTeamsResult','season', 'team','homeTeamAbbr','visitorTeamAbbr','playResult'], color='specialTeamsPlayType', hover_data=['specialTeamsPlayType'])\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:13:05.917277Z","iopub.execute_input":"2021-12-11T11:13:05.917725Z","iopub.status.idle":"2021-12-11T11:13:06.538296Z","shell.execute_reply.started":"2021-12-11T11:13:05.917690Z","shell.execute_reply":"2021-12-11T11:13:06.536867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# table view display\ntopGameCharacteristicsbyPlayResult","metadata":{"_kg_hide-input":true,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2021-12-11T11:13:06.539740Z","iopub.execute_input":"2021-12-11T11:13:06.540365Z","iopub.status.idle":"2021-12-11T11:13:06.556179Z","shell.execute_reply.started":"2021-12-11T11:13:06.540328Z","shell.execute_reply":"2021-12-11T11:13:06.554884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n**Q: What are the Player Characteristics for top 5 Play Result with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"# get distribution of specialTeamsPlayType, specialTeamsResult, displayName,collegeName,height, weight and position with respect to playResult\nkickoff = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Kickoff\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','displayName','collegeName','height','weight','position'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\npunt = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Punt\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','displayName','collegeName','height','weight','position'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\nfieldGoal = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Field Goal\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','displayName','collegeName','height','weight','position'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\nextraPoint = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Extra Point\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','displayName','collegeName','height','weight','position'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\n# combine each specialTeamsPlayType aggregation in single dataframe\ntopPlayerCharacteristicsbyPlayResult = pd.concat([kickoff[:5],punt[:5],fieldGoal[:5],extraPoint[:5]])\n# plot distribution of top 5 player characteristics\nfig = px.sunburst(topPlayerCharacteristicsbyPlayResult, path=['specialTeamsPlayType','specialTeamsResult','displayName','collegeName', 'height','weight','position','playResult'], color='specialTeamsPlayType', hover_data=['specialTeamsPlayType'])\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:13:06.557902Z","iopub.execute_input":"2021-12-11T11:13:06.558267Z","iopub.status.idle":"2021-12-11T11:13:07.326807Z","shell.execute_reply.started":"2021-12-11T11:13:06.558224Z","shell.execute_reply":"2021-12-11T11:13:07.325601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# table view display\ntopPlayerCharacteristicsbyPlayResult","metadata":{"_kg_hide-input":true,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2021-12-11T11:13:07.329111Z","iopub.execute_input":"2021-12-11T11:13:07.329489Z","iopub.status.idle":"2021-12-11T11:13:07.346993Z","shell.execute_reply.started":"2021-12-11T11:13:07.329444Z","shell.execute_reply":"2021-12-11T11:13:07.345488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n**Q: What are the Player Tracking Characteristics for top 5 Play Result with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"# get distribution of specialTeamsPlayType, specialTeamsResult, displayName,x,y,s and a with respect to playResult\nkickoff = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Kickoff\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','displayName','x','y','s','a'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\npunt = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Punt\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','displayName','x','y','s','a'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\nfieldGoal = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Field Goal\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','displayName','x','y','s','a'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\nextraPoint = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Extra Point\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','displayName','x','y','s','a'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\n# combine each specialTeamsPlayType aggregation in single dataframe\ntopPlayerTrackingCharacteristicsbyPlayResult = pd.concat([kickoff[:5],punt[:5],fieldGoal[:5],extraPoint[:5]])\n# plot distribution of top 5 player tracking characteristics\nfig = px.sunburst(topPlayerTrackingCharacteristicsbyPlayResult, path=['specialTeamsPlayType','specialTeamsResult','displayName','x','y','s','a','playResult'], color='specialTeamsPlayType', hover_data=['specialTeamsPlayType'])\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-11T11:49:09.800534Z","iopub.execute_input":"2021-12-11T11:49:09.800842Z","iopub.status.idle":"2021-12-11T11:49:10.765215Z","shell.execute_reply.started":"2021-12-11T11:49:09.800814Z","shell.execute_reply":"2021-12-11T11:49:10.764142Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# table view display\ntopPlayerTrackingCharacteristicsbyPlayResult","metadata":{"execution":{"iopub.status.busy":"2021-12-11T11:47:56.242156Z","iopub.execute_input":"2021-12-11T11:47:56.242732Z","iopub.status.idle":"2021-12-11T11:47:56.263035Z","shell.execute_reply.started":"2021-12-11T11:47:56.242685Z","shell.execute_reply":"2021-12-11T11:47:56.262043Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n**Q: What are the Play Characteristics for top 5 play result with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"# get distribution of specialTeamsPlayType, specialTeamsResult, possessionTeam, penaltyYards, playDescription with respect to playResult\nkickoff = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Kickoff\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','possessionTeam','penaltyYards','playDescription'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\npunt = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Punt\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','possessionTeam','penaltyYards','playDescription'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\nfieldGoal = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Field Goal\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','possessionTeam','penaltyYards','playDescription'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\nextraPoint = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Extra Point\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','possessionTeam','penaltyYards','playDescription'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\n# combine each specialTeamsPlayType aggregation in single dataframe\ntopPlayCharacteristicsbyPlayResult = pd.concat([kickoff[:5],punt[:5],fieldGoal[:5],extraPoint[:5]])\n# plot distribution of top 5 play characteristics\nfig = px.sunburst(topPlayCharacteristicsbyPlayResult, path=['specialTeamsPlayType', 'specialTeamsResult','possessionTeam','penaltyYards','playResult','playDescription'], color='specialTeamsPlayType', hover_data=['specialTeamsPlayType'])\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:13:07.349274Z","iopub.execute_input":"2021-12-11T11:13:07.349755Z","iopub.status.idle":"2021-12-11T11:13:07.991117Z","shell.execute_reply.started":"2021-12-11T11:13:07.349721Z","shell.execute_reply":"2021-12-11T11:13:07.989934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# table view display\ntopPlayCharacteristicsbyPlayResult","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:13:07.993226Z","iopub.execute_input":"2021-12-11T11:13:07.993522Z","iopub.status.idle":"2021-12-11T11:13:08.013228Z","shell.execute_reply.started":"2021-12-11T11:13:07.993493Z","shell.execute_reply":"2021-12-11T11:13:08.012035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n**Q: What are the Play Tracking Characteristics for top 5 play result with respect to Special Teams Play Type?**\n\n---","metadata":{}},{"cell_type":"code","source":"# get distribution of specialTeamsPlayType, specialTeamsResult, team, playDirection, event with respect to playResult\nkickoff = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Kickoff\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','team','playDirection','event'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\npunt = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Punt\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','team','playDirection','event'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\nfieldGoal = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Field Goal\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','team','playDirection','event'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\nextraPoint = nfl2022_data_sample[nfl2022_data_sample.specialTeamsPlayType.isin([\"Extra Point\"])].groupby(by =['specialTeamsPlayType','specialTeamsResult','team','playDirection','event'], as_index = False)['playResult'].agg('max').sort_values(by ='playResult', ascending = False)\n# combine each specialTeamsPlayType aggregation in single dataframe\ntopPlayTrackingCharacteristicsbyPlayResult = pd.concat([kickoff[:5],punt[:5],fieldGoal[:5],extraPoint[:5]])\n# plot distribution of top 5 play tracking characteristics\nfig = px.sunburst(topPlayTrackingCharacteristicsbyPlayResult, path=['specialTeamsPlayType', 'specialTeamsResult','team','playDirection','event', 'playResult'], color='specialTeamsPlayType', hover_data=['specialTeamsPlayType'])\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:43:52.185287Z","iopub.execute_input":"2021-12-11T11:43:52.185583Z","iopub.status.idle":"2021-12-11T11:43:52.731810Z","shell.execute_reply.started":"2021-12-11T11:43:52.185548Z","shell.execute_reply":"2021-12-11T11:43:52.730848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# table view display\ntopPlayTrackingCharacteristicsbyPlayResult","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:43:59.991753Z","iopub.execute_input":"2021-12-11T11:43:59.992577Z","iopub.status.idle":"2021-12-11T11:44:00.006607Z","shell.execute_reply.started":"2021-12-11T11:43:59.992535Z","shell.execute_reply":"2021-12-11T11:44:00.005602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n**Thank you and Happy Learning.**\n\n---","metadata":{}},{"cell_type":"code","source":"thank_you_str=\"Thanks,Happy Learning,Collaboration,Thankyou,Keep Learning\"\n# create WordCloud with converted string\nwordcloud = WordCloud(width = 1000, height = 500, random_state=1, background_color='white', collocations=True).generate(thank_you_str)\nplt.figure(figsize=(20, 20))\nplt.imshow(wordcloud) \nplt.axis(\"off\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-12-11T11:13:08.015152Z","iopub.execute_input":"2021-12-11T11:13:08.015503Z","iopub.status.idle":"2021-12-11T11:13:08.659657Z","shell.execute_reply.started":"2021-12-11T11:13:08.015460Z","shell.execute_reply":"2021-12-11T11:13:08.658771Z"},"trusted":true},"execution_count":null,"outputs":[]}]}