{"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)\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\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-23T02:43:42.735522Z","iopub.execute_input":"2021-11-23T02:43:42.735883Z","iopub.status.idle":"2021-11-23T02:43:42.767714Z","shell.execute_reply.started":"2021-11-23T02:43:42.735775Z","shell.execute_reply":"2021-11-23T02:43:42.766785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import necessary library\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# https://www.rookieroad.com/football/101/special-teams/","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:42.769557Z","iopub.execute_input":"2021-11-23T02:43:42.769798Z","iopub.status.idle":"2021-11-23T02:43:43.570829Z","shell.execute_reply.started":"2021-11-23T02:43:42.769768Z","shell.execute_reply":"2021-11-23T02:43:43.570105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading data tables\ngames = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/games.csv')\nplays = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/plays.csv')\n#players = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/players.csv')\n#tracking2018 = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/tracking2018.csv')\n#tracking2019 = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/tracking2019.csv')\n#tracking2020 = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/tracking2020.csv')\nscouting = pd.read_csv('/kaggle/input/nfl-big-data-bowl-2022/PFFScoutingData.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:43.572171Z","iopub.execute_input":"2021-11-23T02:43:43.572657Z","iopub.status.idle":"2021-11-23T02:43:43.777384Z","shell.execute_reply.started":"2021-11-23T02:43:43.572612Z","shell.execute_reply":"2021-11-23T02:43:43.776696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Choosing games related to last 3 SuperBowl Winners\ngames_p1 = games.set_index('homeTeamAbbr')\ngames_p2 = games.set_index('visitorTeamAbbr')\nwinners = ['TB','KC','NE']\ng1 = games_p1.loc[winners]\ng2 = games_p2.loc[winners]\ng1 = (g1.reset_index())#.set_index('gameId')\ng2 = (g2.reset_index())#.set_index('gameId')\n\ng = pd.concat([g1,g2]).drop_duplicates()\ng = g.set_index('gameId')\n#g.index.is_unique\n\np1 = plays.set_index('gameId')\npw = (p1.loc[list(g.index)]).reset_index()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:43.778670Z","iopub.execute_input":"2021-11-23T02:43:43.779123Z","iopub.status.idle":"2021-11-23T02:43:43.823470Z","shell.execute_reply.started":"2021-11-23T02:43:43.779092Z","shell.execute_reply":"2021-11-23T02:43:43.822888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = (pw.loc[:,['possessionTeam','specialTeamsPlayType']]).set_index('possessionTeam')\n#Extra Point, Field Goal, Kickoff or Punt\n","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:43.825361Z","iopub.execute_input":"2021-11-23T02:43:43.825770Z","iopub.status.idle":"2021-11-23T02:43:43.831166Z","shell.execute_reply.started":"2021-11-23T02:43:43.825740Z","shell.execute_reply":"2021-11-23T02:43:43.830625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.DataFrame([list((df.loc['TB',:]).value_counts().sort_index(ascending=True)),\n                    list((df.loc['KC',:]).value_counts().sort_index(ascending=True)),\n                    list((df.loc['NE',:]).value_counts().sort_index(ascending=True))\n                    ],\n                    index = ['TB','KC','NE'], \n                    columns = ['Extra Point', 'Field Goal', 'Kickoff', 'Punt'])\n","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:43.832325Z","iopub.execute_input":"2021-11-23T02:43:43.832700Z","iopub.status.idle":"2021-11-23T02:43:43.853637Z","shell.execute_reply.started":"2021-11-23T02:43:43.832672Z","shell.execute_reply":"2021-11-23T02:43:43.852970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Checking SpecialTeam Play Type for Last 3 SuperBowl Winners\nfig, axes = plt.subplots(2,1)\ndf1.plot.bar(ax = axes[0], alpha = 0.5)\n\n# Shrink current axis by 20%\nbox = axes[0].get_position()\naxes[0].set_position([box.x0, box.y0, box.width * 0.8, box.height])\n\n# Put a legend to the right of the current axis\naxes[0].legend(loc='center left', bbox_to_anchor=(1, 0.5))\n\n\n### Plot stacked version of the distribution\ndf1.plot.barh(ax = axes[1], stacked = True, alpha = 0.5)\n# Shrink current axis by 20%\nbox1 = axes[1].get_position()\naxes[1].set_position([box1.x0, box1.y0, box1.width * 0.8, box1.height * 0.9])\n\n# Put a legend to the right of the current axis\naxes[1].legend(loc='center left', bbox_to_anchor=(1, 0.5))\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:43.854622Z","iopub.execute_input":"2021-11-23T02:43:43.854819Z","iopub.status.idle":"2021-11-23T02:43:44.360942Z","shell.execute_reply.started":"2021-11-23T02:43:43.854793Z","shell.execute_reply":"2021-11-23T02:43:44.360027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Understand percentage of return yards over kick length for SuperBowl Winners in \"Return\" playResult\ndf2 = pw.loc[:,['gameId','playId','possessionTeam','specialTeamsPlayType','specialTeamsResult','kickLength','kickReturnYardage']]\ndf2 = df2[df2['specialTeamsResult'] == 'Return']\ndf2 = df2.set_index('gameId')\n#df2.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:44.362309Z","iopub.execute_input":"2021-11-23T02:43:44.362923Z","iopub.status.idle":"2021-11-23T02:43:44.372640Z","shell.execute_reply.started":"2021-11-23T02:43:44.362876Z","shell.execute_reply":"2021-11-23T02:43:44.371801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"games_p = games.loc[:,['gameId','homeTeamAbbr','visitorTeamAbbr']]\ngames_p = games_p.set_index('gameId')\n#games_p.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:44.374069Z","iopub.execute_input":"2021-11-23T02:43:44.374563Z","iopub.status.idle":"2021-11-23T02:43:44.388827Z","shell.execute_reply.started":"2021-11-23T02:43:44.374516Z","shell.execute_reply":"2021-11-23T02:43:44.387710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3 = (pd.merge(df2, games_p, how='left',left_index=True,right_index=True)).reset_index()\n#df3.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:44.390015Z","iopub.execute_input":"2021-11-23T02:43:44.390696Z","iopub.status.idle":"2021-11-23T02:43:44.403770Z","shell.execute_reply.started":"2021-11-23T02:43:44.390662Z","shell.execute_reply":"2021-11-23T02:43:44.402889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tampa Bay Buccaneers\ndf4 = df3[(df3['homeTeamAbbr']=='TB') | (df3['visitorTeamAbbr']=='TB')]\ndf4 = df4[df4['possessionTeam'] != 'TB']\ndf4['Ratio'] = df4['kickReturnYardage']*100/df4['kickLength']\n#df4['Ratio']\n\n# Kansas City Chiefs\ndf5 = df3[(df3['homeTeamAbbr']=='KC') | (df3['visitorTeamAbbr']=='KC')]\ndf5 = df5[df5['possessionTeam'] != 'KC']\ndf5['Ratio'] = df5['kickReturnYardage']*100/df5['kickLength']\n#df4['Ratio']\n\n# New Englad Patriots\ndf6 = df3[(df3['homeTeamAbbr']=='NE') | (df3['visitorTeamAbbr']=='NE')]\ndf6 = df6[df6['possessionTeam'] != 'NE']\ndf6['Ratio'] = df6['kickReturnYardage']*100/df6['kickLength']\n#df4['Ratio']\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:44.404785Z","iopub.execute_input":"2021-11-23T02:43:44.405350Z","iopub.status.idle":"2021-11-23T02:43:44.420576Z","shell.execute_reply.started":"2021-11-23T02:43:44.405316Z","shell.execute_reply":"2021-11-23T02:43:44.419910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(3,1, sharex = True)\n\naxes[0].set_title('Tampa Bay Buccaneers')\naxes[0].set_xlim([0, 100])\nsns.histplot(df4['Ratio'], bins=range(1, 110, 3), ax = axes[0])\n\naxes[1].set_title('Kansas City Chiefs')\naxes[1].set_xlim([0, 100])\nsns.histplot(df5['Ratio'], bins=range(1, 110, 3), ax = axes[1])\n\naxes[2].set_title('New Englad Patriots')\naxes[2].set_xlim([0, 100])\nsns.histplot(df6['Ratio'], bins=range(1, 110, 3), ax = axes[2])","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:44.421871Z","iopub.execute_input":"2021-11-23T02:43:44.422511Z","iopub.status.idle":"2021-11-23T02:43:45.022128Z","shell.execute_reply.started":"2021-11-23T02:43:44.422481Z","shell.execute_reply":"2021-11-23T02:43:45.021297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Checking correlation between return ratio and other variables\nscouting_p = scouting.loc[:,['gameId','playId','operationTime','hangTime',\n                             'kickType','kickDirectionIntended','kickDirectionActual',\n                             'returnDirectionIntended','returnDirectionActual','kickoffReturnFormation']]\nscouting_p = scouting_p.set_index(['gameId','playId'])","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:45.023383Z","iopub.execute_input":"2021-11-23T02:43:45.023625Z","iopub.status.idle":"2021-11-23T02:43:45.037021Z","shell.execute_reply.started":"2021-11-23T02:43:45.023595Z","shell.execute_reply":"2021-11-23T02:43:45.036181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tampa Bay Buccaneers\ndf4 = df4.set_index(['gameId','playId'])\ndf7 = (pd.merge(df4, scouting_p, how='left',left_index=True,right_index=True)).reset_index()\ndf4 = df4.reset_index()\ndf7 = df7[df7['hangTime'].notnull()]\n\n# Kansas City Chiefs\ndf5 = df5.set_index(['gameId','playId'])\ndf8 = (pd.merge(df5, scouting_p, how='left',left_index=True,right_index=True)).reset_index()\ndf5 = df5.reset_index()\ndf8 = df8[df8['hangTime'].notnull()]\n\n# New Englad Patriots\ndf6 = df6.set_index(['gameId','playId'])\ndf9 = (pd.merge(df6, scouting_p, how='left',left_index=True,right_index=True)).reset_index()\ndf6 = df6.reset_index()\ndf9 = df9[df9['hangTime'].notnull()]\n","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:43:45.039268Z","iopub.execute_input":"2021-11-23T02:43:45.039492Z","iopub.status.idle":"2021-11-23T02:43:45.084763Z","shell.execute_reply.started":"2021-11-23T02:43:45.039465Z","shell.execute_reply":"2021-11-23T02:43:45.083905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1,1, sharex = True)\nsns.regplot(x= 'hangTime', y= 'Ratio', data = df7)\nsns.regplot(x= 'hangTime', y= 'Ratio', data = df8)\nsns.regplot(x= 'hangTime', y= 'Ratio', data = df9)","metadata":{"execution":{"iopub.status.busy":"2021-11-23T02:52:59.584884Z","iopub.execute_input":"2021-11-23T02:52:59.585165Z","iopub.status.idle":"2021-11-23T02:53:00.096559Z","shell.execute_reply.started":"2021-11-23T02:52:59.585134Z","shell.execute_reply":"2021-11-23T02:53:00.095541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(3,1, sharex = True)#, sharey = True)\nsns.regplot(x= 'hangTime', y= 'Ratio', data = df7, ax = axes[0])\nsns.regplot(x= 'hangTime', y= 'Ratio', data = df8, ax = axes[1])\nsns.regplot(x= 'hangTime', y= 'Ratio', data = df9, ax = axes[2])","metadata":{"execution":{"iopub.status.busy":"2021-11-23T03:01:12.226227Z","iopub.execute_input":"2021-11-23T03:01:12.226748Z","iopub.status.idle":"2021-11-23T03:01:12.993972Z","shell.execute_reply.started":"2021-11-23T03:01:12.226710Z","shell.execute_reply":"2021-11-23T03:01:12.993089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}