{"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":"<h1><center>NFL Big Data Bowl 2022</center></h1>\n\n<center><img src=\"https://deadline.com/wp-content/uploads/2021/01/NFL-ball.jpg?crop=0px%2C33px%2C1226px%2C687px&resize=681%2C383\"></center>","metadata":{}},{"cell_type":"markdown","source":"### PLEASE UPVOTE if you like this notebook. It will keep me motivated to update my notebook.","metadata":{}},{"cell_type":"markdown","source":"#### What are you trying to do in this notebook ?\n\nIn this notebook I will predict how many yards a team will gain on a rushing play in an NFL regular season game. I will loop through a series of rushing plays; for each play. I'll use the information that is provided in this notebook and it will predict how many yards the team will gain on the play as a cumulative probability distribution. Once I'll make that prediction, I can move on to the next rushing play.\n\n#### Why are you trying it ?\n\nThis competition is different from most Kaggle Competitions in that:\n\n* In this kaggle notebook I may not other data sources, GPU, or internet access.\n* This is a two-stage competition. In Stage One I can edit my Notebooks and improve my model, where Public Leaderboard scores are based on my predictions on rushing play.\n* The purpose of this competition is to control the flow of information that is to ensure the future data to make predictions for the current rushing play.","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2021-12-02T04:52:31.474344Z","iopub.execute_input":"2021-12-02T04:52:31.474813Z","iopub.status.idle":"2021-12-02T04:52:31.485756Z","shell.execute_reply.started":"2021-12-02T04:52:31.474770Z","shell.execute_reply":"2021-12-02T04:52:31.484805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Linear Algebra, Data Manipulation\nimport numpy as np\nimport pandas as pd\n\n# Plots\nimport seaborn as sns\npal_hls = sns.hls_palette(100, l=.3, s=.8).as_hex() # Seaborn Color Palette for colors\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nsns.set_style('darkgrid')\n\n# Interactive Plots\nimport plotly.express as px\nfrom plotly.offline import init_notebook_mode\nimport plotly.graph_objects as go\ninit_notebook_mode()\n\n# Ignore Warnings\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:31.487671Z","iopub.execute_input":"2021-12-02T04:52:31.488148Z","iopub.status.idle":"2021-12-02T04:52:31.571038Z","shell.execute_reply.started":"2021-12-02T04:52:31.488117Z","shell.execute_reply":"2021-12-02T04:52:31.569900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"games = pd.read_csv(\"../input/nfl-big-data-bowl-2022/games.csv\")\ngames","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:31.572578Z","iopub.execute_input":"2021-12-02T04:52:31.573242Z","iopub.status.idle":"2021-12-02T04:52:31.603907Z","shell.execute_reply.started":"2021-12-02T04:52:31.573184Z","shell.execute_reply":"2021-12-02T04:52:31.603021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"games[\"month\"] = games[\"gameDate\"].apply(lambda x: int(x.split(\"/\")[0]))\ngames[\"day\"] = games[\"gameDate\"].apply(lambda x: int(x.split(\"/\")[1]))\ngames[\"hour\"] = games[\"gameTimeEastern\"].apply(lambda x: int(x.split(\":\")[0]))","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:31.605648Z","iopub.execute_input":"2021-12-02T04:52:31.605859Z","iopub.status.idle":"2021-12-02T04:52:31.617009Z","shell.execute_reply.started":"2021-12-02T04:52:31.605835Z","shell.execute_reply":"2021-12-02T04:52:31.616266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotlybar(data, column, title):\n    df = pd.DataFrame()\n\n    df[column] = data[column].value_counts().index # Gets all the unique seasons\n    df[\"gamesCount\"] = data[column].value_counts().values # Gets the gamesCount for those unique seasons\n    df[\"percent\"] = round(df.gamesCount / len(data) * 100, 1) # The Percent of games played that season\n\n    # Bar plot using plotly bar plot\n    fig = px.bar(df, \n                y='gamesCount',\n                x=column,\n                text='percent',\n                height=600, width=950,\n                title=f\"<b>{title}</b>\", color=pal_hls[:len(df)])\n    fig.update_traces(texttemplate='%{text}%', textposition='outside') # The text at the top of the bar plot\n    fig.update_layout(\n        xaxis = dict(dtick = 1),\n        showlegend=False) # Fixing the problem with the x ticks here\n    fig.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:31.618277Z","iopub.execute_input":"2021-12-02T04:52:31.618833Z","iopub.status.idle":"2021-12-02T04:52:31.626930Z","shell.execute_reply.started":"2021-12-02T04:52:31.618797Z","shell.execute_reply":"2021-12-02T04:52:31.625900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(games, \"season\", \"Number of Games Played Every Season\")\n","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:31.628414Z","iopub.execute_input":"2021-12-02T04:52:31.629280Z","iopub.status.idle":"2021-12-02T04:52:31.734109Z","shell.execute_reply.started":"2021-12-02T04:52:31.629228Z","shell.execute_reply":"2021-12-02T04:52:31.733322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"title_font_dict = {\"fontsize\": 16, \"fontweight\": \"bold\"}\n\nplt.figure(figsize=(10, 5), dpi=100)\nsns.countplot(x='season', data=games, hue='month', lw=2)\nplt.title('Games in Year/Season', fontdict=title_font_dict)\nplt.legend([\"January\", \"September\", \"October\", \"November\", \"December\"],\n               loc='center right', bbox_to_anchor=(1.2, 0.5), ncol=1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:31.735273Z","iopub.execute_input":"2021-12-02T04:52:31.735508Z","iopub.status.idle":"2021-12-02T04:52:32.021169Z","shell.execute_reply.started":"2021-12-02T04:52:31.735463Z","shell.execute_reply":"2021-12-02T04:52:32.020194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(games, \"month\", \"Number of Games Played Every Month\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:32.022293Z","iopub.execute_input":"2021-12-02T04:52:32.022517Z","iopub.status.idle":"2021-12-02T04:52:32.102442Z","shell.execute_reply.started":"2021-12-02T04:52:32.022490Z","shell.execute_reply":"2021-12-02T04:52:32.101545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(games, \"day\", \"Number of Games Held Every Day\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:32.103849Z","iopub.execute_input":"2021-12-02T04:52:32.104452Z","iopub.status.idle":"2021-12-02T04:52:32.281177Z","shell.execute_reply.started":"2021-12-02T04:52:32.104403Z","shell.execute_reply":"2021-12-02T04:52:32.280395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(games, \"gameTimeEastern\", \"Number of Games played on different gameTimeEastern\")\n","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:32.283649Z","iopub.execute_input":"2021-12-02T04:52:32.283863Z","iopub.status.idle":"2021-12-02T04:52:32.408681Z","shell.execute_reply.started":"2021-12-02T04:52:32.283838Z","shell.execute_reply":"2021-12-02T04:52:32.407951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(games, \"hour\", \"Number of Games played on different gameTimeEastern(ignoring minutes)\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:32.409865Z","iopub.execute_input":"2021-12-02T04:52:32.410084Z","iopub.status.idle":"2021-12-02T04:52:32.510730Z","shell.execute_reply.started":"2021-12-02T04:52:32.410058Z","shell.execute_reply":"2021-12-02T04:52:32.509858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(games, \"week\", \"Number of Games Held Every week\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:32.511913Z","iopub.execute_input":"2021-12-02T04:52:32.512132Z","iopub.status.idle":"2021-12-02T04:52:32.644555Z","shell.execute_reply.started":"2021-12-02T04:52:32.512104Z","shell.execute_reply":"2021-12-02T04:52:32.643797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players = pd.read_csv('../input/nfl-big-data-bowl-2022/players.csv')\nplayers","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:32.645817Z","iopub.execute_input":"2021-12-02T04:52:32.646009Z","iopub.status.idle":"2021-12-02T04:52:32.681973Z","shell.execute_reply.started":"2021-12-02T04:52:32.645984Z","shell.execute_reply":"2021-12-02T04:52:32.681070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players_heights = players[\"height\"] # Get the Height data from DataFrame\nplayers_heights = players_heights.apply(lambda x: x.split(\"-\")) # Split the heights by hyphen (\"-\")\n\n# Convert Heights to Centimeters and add them to DataFrame\nplayers[\"height\"] = players_heights.apply(lambda x: int(x[0]) * 12 + int(x[1]) if len(x) == 2 else int(x[0])) * 2.54\n\n# Convert Weights to Kilograms and them to DataFrame\nplayers[\"weight\"] = round(players.weight * 0.453592, 2)\n\nplayers","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:32.683310Z","iopub.execute_input":"2021-12-02T04:52:32.683573Z","iopub.status.idle":"2021-12-02T04:52:32.708875Z","shell.execute_reply.started":"2021-12-02T04:52:32.683542Z","shell.execute_reply":"2021-12-02T04:52:32.708002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players[\"birthYear\"] = 0\nplayers[\"birthMonth\"] = 0","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:32.710152Z","iopub.execute_input":"2021-12-02T04:52:32.710943Z","iopub.status.idle":"2021-12-02T04:52:32.716850Z","shell.execute_reply.started":"2021-12-02T04:52:32.710897Z","shell.execute_reply":"2021-12-02T04:52:32.716247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#There are NA values in birthDate so that we should drop them\nplayers.dropna(subset=[\"birthDate\"], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:32.717767Z","iopub.execute_input":"2021-12-02T04:52:32.718389Z","iopub.status.idle":"2021-12-02T04:52:32.731854Z","shell.execute_reply.started":"2021-12-02T04:52:32.718357Z","shell.execute_reply":"2021-12-02T04:52:32.730984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, row in players.iterrows():\n    if len(row['birthDate'].split('/')) == 3: # 05/17/1994 \n        players.loc[idx, 'birthYear'] = row['birthDate'].split('/')[2]\n        players.loc[idx, 'birthMonth'] = row['birthDate'].split('/')[0]\n        \n    elif len(row['birthDate'].split('-')) == 3: # 1995-05-05\n        players.loc[idx, 'birthYear'] = row['birthDate'].split('-')[0]\n        players.loc[idx, 'birthMonth'] = row['birthDate'].split('-')[1]","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:32.733032Z","iopub.execute_input":"2021-12-02T04:52:32.733461Z","iopub.status.idle":"2021-12-02T04:52:34.280723Z","shell.execute_reply.started":"2021-12-02T04:52:32.733429Z","shell.execute_reply":"2021-12-02T04:52:34.280016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:34.281825Z","iopub.execute_input":"2021-12-02T04:52:34.282730Z","iopub.status.idle":"2021-12-02T04:52:34.301097Z","shell.execute_reply.started":"2021-12-02T04:52:34.282695Z","shell.execute_reply":"2021-12-02T04:52:34.300443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(20, 15), dpi=80)\n\nbirth_year_dist = players.birthYear.value_counts().sort_index()\nax1 = fig.add_subplot(221)\nsns.barplot(x=birth_year_dist.index, y=birth_year_dist.values, ax=ax1)\nax1.tick_params(axis='x', rotation=45)\nax1.set_title(\"Birth Year Distribution\", fontdict=title_font_dict)\n\nbirth_month_dist = players.birthMonth.value_counts().sort_index()\nax2 = fig.add_subplot(222)\nsns.barplot(x=birth_month_dist.index, y=birth_month_dist.values, ax=ax2)\nax2.set_title(\"Birth Month Distribution\", fontdict=title_font_dict)\n\nax3 = fig.add_subplot(223)\nsns.histplot(players.weight, ax=ax3)\nax3.set_title(\"Weight(Kg) Distribution\", fontdict=title_font_dict)\n\nax4 = fig.add_subplot(224)\nsns.histplot(players.height, ax=ax4, bins=10)\nax4.set_title(\"Height(cm) Distribution\", fontdict=title_font_dict);","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:34.302621Z","iopub.execute_input":"2021-12-02T04:52:34.303510Z","iopub.status.idle":"2021-12-02T04:52:35.644225Z","shell.execute_reply.started":"2021-12-02T04:52:34.303412Z","shell.execute_reply":"2021-12-02T04:52:35.643390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(20, 12), dpi=80)\ntitle_font_dict = {\"fontsize\": 16, \"fontweight\": \"bold\"}\n\nax1 = fig.add_subplot(211)\nsns.boxplot(players.weight, ax=ax1, width=0.5)\nax1.set_title(\"Weight(Kg) Distribution\", fontdict=title_font_dict)\n\nax2 = fig.add_subplot(212)\nsns.boxplot(players.height, ax=ax2, width=0.5)\nax2.set_title(\"Height(cm) Distribution\", fontdict=title_font_dict);","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:35.645791Z","iopub.execute_input":"2021-12-02T04:52:35.646264Z","iopub.status.idle":"2021-12-02T04:52:36.434441Z","shell.execute_reply.started":"2021-12-02T04:52:35.646218Z","shell.execute_reply":"2021-12-02T04:52:36.433533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Youngest Player Born in: {players.birthYear.max()}\")\nprint(f\"Oldest Player Born in: {players.birthYear.min()}\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:36.435612Z","iopub.execute_input":"2021-12-02T04:52:36.435920Z","iopub.status.idle":"2021-12-02T04:52:36.442580Z","shell.execute_reply.started":"2021-12-02T04:52:36.435890Z","shell.execute_reply":"2021-12-02T04:52:36.441652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 6), dpi=100)\nsns.regplot(x=players.weight, y=players.height, line_kws={\"color\": \"orange\"})\nplt.title(\"Player Weight(Kg) vs Player Height(cm)\");","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:36.443773Z","iopub.execute_input":"2021-12-02T04:52:36.444367Z","iopub.status.idle":"2021-12-02T04:52:36.961740Z","shell.execute_reply.started":"2021-12-02T04:52:36.444334Z","shell.execute_reply":"2021-12-02T04:52:36.960785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Seeing Which college has given the most number of players\n\ncollege_count = players.collegeName.value_counts()[:25]\n\nfig = px.bar(x=college_count.index, y=college_count.values,\n             labels={\"x\":\"College Name\", \"y\":\"Count\"}, title=\"<b>Players College Count</b>\")\nfig.update_layout(\n  width=1300,\n  height=500,\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:36.963253Z","iopub.execute_input":"2021-12-02T04:52:36.963566Z","iopub.status.idle":"2021-12-02T04:52:37.024389Z","shell.execute_reply.started":"2021-12-02T04:52:36.963531Z","shell.execute_reply":"2021-12-02T04:52:37.023559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"position_count = players.Position.value_counts()\n\nfig = px.bar(x=position_count.index, y=position_count.values,\n             labels={\"x\":\"Position\", \"y\":\"Count\"}, title=\"<b>Players Position Count</b>\")\nfig.update_layout(\n  width=1300,\n  height=500,\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:37.026134Z","iopub.execute_input":"2021-12-02T04:52:37.026710Z","iopub.status.idle":"2021-12-02T04:52:37.083732Z","shell.execute_reply.started":"2021-12-02T04:52:37.026667Z","shell.execute_reply":"2021-12-02T04:52:37.083047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays = pd.read_csv(\"../input/nfl-big-data-bowl-2022/plays.csv\")\nplays","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:37.084848Z","iopub.execute_input":"2021-12-02T04:52:37.085567Z","iopub.status.idle":"2021-12-02T04:52:37.242852Z","shell.execute_reply.started":"2021-12-02T04:52:37.085534Z","shell.execute_reply":"2021-12-02T04:52:37.242187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 12))\nsns.heatmap(plays.corr(), annot=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:37.244179Z","iopub.execute_input":"2021-12-02T04:52:37.244385Z","iopub.status.idle":"2021-12-02T04:52:38.757991Z","shell.execute_reply.started":"2021-12-02T04:52:37.244359Z","shell.execute_reply":"2021-12-02T04:52:38.757291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays.info()","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:38.759157Z","iopub.execute_input":"2021-12-02T04:52:38.759945Z","iopub.status.idle":"2021-12-02T04:52:38.783958Z","shell.execute_reply.started":"2021-12-02T04:52:38.759904Z","shell.execute_reply":"2021-12-02T04:52:38.782996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(plays, \"quarter\", \"Number of Plays every quater\")\n","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:38.787853Z","iopub.execute_input":"2021-12-02T04:52:38.788183Z","iopub.status.idle":"2021-12-02T04:52:38.871898Z","shell.execute_reply.started":"2021-12-02T04:52:38.788149Z","shell.execute_reply":"2021-12-02T04:52:38.871216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(plays, \"down\", \"Number of Plays every down\")\n","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:38.873235Z","iopub.execute_input":"2021-12-02T04:52:38.873445Z","iopub.status.idle":"2021-12-02T04:52:38.953326Z","shell.execute_reply.started":"2021-12-02T04:52:38.873419Z","shell.execute_reply":"2021-12-02T04:52:38.952680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(plays, \"yardsToGo\", \"Number of Plays every yardsToGo\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:38.954453Z","iopub.execute_input":"2021-12-02T04:52:38.954830Z","iopub.status.idle":"2021-12-02T04:52:39.166831Z","shell.execute_reply.started":"2021-12-02T04:52:38.954799Z","shell.execute_reply":"2021-12-02T04:52:39.166036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(plays, \"specialTeamsPlayType\", \"Number of Playes per specialTeamsPlayType\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:39.168237Z","iopub.execute_input":"2021-12-02T04:52:39.168460Z","iopub.status.idle":"2021-12-02T04:52:39.242181Z","shell.execute_reply.started":"2021-12-02T04:52:39.168431Z","shell.execute_reply":"2021-12-02T04:52:39.241625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotlybar(plays, \"possessionTeam\", \"Number of Playes per possessionTeam\")\n","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:39.243172Z","iopub.execute_input":"2021-12-02T04:52:39.243512Z","iopub.status.idle":"2021-12-02T04:52:39.437028Z","shell.execute_reply.started":"2021-12-02T04:52:39.243464Z","shell.execute_reply":"2021-12-02T04:52:39.436194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 12), dpi=100)\n\nax1 = plt.subplot(211)\nsns.histplot(plays.kickLength, ax=ax1)\nax1.set_title(\"Kick Length Distribution\", fontdict=title_font_dict)\nax1.set_xticks(np.arange(0, 85, 10));\n\nax2 = plt.subplot(212)\nsns.boxplot(plays.kickLength, ax=ax2, width=0.5)\nax2.set_title(\"Kick Length Distribution\", fontdict=title_font_dict)\nax2.set_xticks(np.arange(0, 85, 10));","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:39.438155Z","iopub.execute_input":"2021-12-02T04:52:39.438369Z","iopub.status.idle":"2021-12-02T04:52:39.978710Z","shell.execute_reply.started":"2021-12-02T04:52:39.438342Z","shell.execute_reply":"2021-12-02T04:52:39.978014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns = [\"quarter\", \"down\", \"preSnapHomeScore\", \"preSnapVisitorScore\", \"kickReturnYardage\", \"kickLength\", \"yardsToGo\", \"kickBlockerId\",\n           \"yardlineNumber\", \"penaltyYards\", \"playResult\"]\n\nplays_temp = plays[columns]\nplays_temp","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:39.979919Z","iopub.execute_input":"2021-12-02T04:52:39.980274Z","iopub.status.idle":"2021-12-02T04:52:40.003716Z","shell.execute_reply.started":"2021-12-02T04:52:39.980225Z","shell.execute_reply":"2021-12-02T04:52:40.002678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 8), dpi=80)\nsns.heatmap(plays_temp.corr(), annot=True)\nplt.title(\"Correlation Heat Map\", fontdict=title_font_dict);","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:40.005165Z","iopub.execute_input":"2021-12-02T04:52:40.005490Z","iopub.status.idle":"2021-12-02T04:52:40.987626Z","shell.execute_reply.started":"2021-12-02T04:52:40.005432Z","shell.execute_reply":"2021-12-02T04:52:40.986691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 6), dpi=100)\n\nax1 = plt.subplot(121)\nsns.boxplot(\n    x = plays_temp.quarter,\n    y = plays_temp.preSnapHomeScore,\n    ax=ax1\n)\nax1.set_title(\"quarter vs preSnapHomeScore\", fontdict=title_font_dict)\n\nax2 = plt.subplot(122)\nsns.boxplot(\n    x = plays_temp.quarter,\n    y = plays_temp.preSnapVisitorScore,\n    ax=ax2\n)\nax2.set_title(\"quarter vs preSnapVisitorScore\", fontdict=title_font_dict);","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:40.988777Z","iopub.execute_input":"2021-12-02T04:52:40.988995Z","iopub.status.idle":"2021-12-02T04:52:41.504122Z","shell.execute_reply.started":"2021-12-02T04:52:40.988969Z","shell.execute_reply":"2021-12-02T04:52:41.503176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 6), dpi=100)\n\nax1 = plt.subplot(121)\nsns.boxplot(\n    x = plays_temp.down,\n    y = plays_temp.kickReturnYardage,\n    ax=ax1\n)\nax1.set_title(\"down vs kickReturnYardage\", fontdict=title_font_dict)\n\nax2 = plt.subplot(122)\nsns.boxplot(\n    x = plays_temp.down,\n    y = plays_temp.kickLength,\n    ax=ax2\n)\nax2.set_title(\"down vs kickLength\", fontdict=title_font_dict);\n","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:41.505718Z","iopub.execute_input":"2021-12-02T04:52:41.506371Z","iopub.status.idle":"2021-12-02T04:52:41.965446Z","shell.execute_reply.started":"2021-12-02T04:52:41.506319Z","shell.execute_reply":"2021-12-02T04:52:41.964652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(25, 8))\n\nax1 = plt.subplot(131)\nsns.boxplot(\n    x = plays_temp.yardsToGo,\n    y = plays_temp.kickReturnYardage,\n    ax=ax1\n)\nax1.set_title(\"yardsToGo vs kickReturnYardage\", fontdict=title_font_dict)\n\nax2 = plt.subplot(132)\nsns.boxplot(\n    x = plays_temp.yardsToGo,\n    y = plays_temp.kickLength,\n    ax=ax2\n)\nax2.set_title(\"yardsToGo vs kickLength\", fontdict=title_font_dict);\n\nax3 = plt.subplot(133)\nsns.boxplot(\n    x = plays_temp.yardsToGo,\n    y = plays_temp.kickBlockerId,\n    ax=ax3\n)\nax3.set_title(\"yardsToGo vs kickBlockerId\", fontdict=title_font_dict);","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:41.966640Z","iopub.execute_input":"2021-12-02T04:52:41.966854Z","iopub.status.idle":"2021-12-02T04:52:44.563642Z","shell.execute_reply.started":"2021-12-02T04:52:41.966827Z","shell.execute_reply":"2021-12-02T04:52:44.562713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 10))\n\nax1 = plt.subplot(121)\nsns.regplot(\n    x = plays_temp.yardlineNumber,\n    y = plays_temp.playResult,\n    ax=ax1, line_kws={\"color\": \"orange\"}, scatter_kws={\"color\": \"#396EB0\"},\n)\nax1.set_title(\"yardlineNumber vs playResult\", fontdict=title_font_dict)\n\nax2 = plt.subplot(122)\nsns.regplot(\n    x = plays_temp.yardlineNumber,\n    y = plays_temp.kickLength,\n    ax=ax2, line_kws={\"color\": \"#396EB0\"}, scatter_kws={\"color\": \"orange\"},\n    marker=\"+\"\n)\nax2.set_title(\"yardlineNumber vs kickLength\", fontdict=title_font_dict);","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:44.565643Z","iopub.execute_input":"2021-12-02T04:52:44.566123Z","iopub.status.idle":"2021-12-02T04:52:46.788174Z","shell.execute_reply.started":"2021-12-02T04:52:44.566078Z","shell.execute_reply":"2021-12-02T04:52:46.787337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 10))\n\nax1 = plt.subplot(121)\nsns.regplot(\n    x = plays_temp.kickLength,\n    y = plays_temp.kickReturnYardage,\n    ax=ax1, line_kws={\"color\": \"orange\"}, scatter_kws={\"color\": \"#396EB0\"},\n)\nax1.set_title(\"kickLength vs kickReturnYardage\", fontdict=title_font_dict)\n\nax2 = plt.subplot(122)\nsns.regplot(\n    x = plays_temp.kickLength,\n    y = plays_temp.playResult,\n    ax=ax2, line_kws={\"color\": \"#396EB0\"}, scatter_kws={\"color\": \"orange\"},\n    marker=\"+\"\n)\nax2.set_title(\"kickLength vs playResult\", fontdict=title_font_dict);","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:46.789800Z","iopub.execute_input":"2021-12-02T04:52:46.790312Z","iopub.status.idle":"2021-12-02T04:52:48.368051Z","shell.execute_reply.started":"2021-12-02T04:52:46.790260Z","shell.execute_reply":"2021-12-02T04:52:48.367228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2020 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2020.csv')\ntracking2020.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:52:48.369641Z","iopub.execute_input":"2021-12-02T04:52:48.370199Z","iopub.status.idle":"2021-12-02T04:53:20.553957Z","shell.execute_reply.started":"2021-12-02T04:52:48.370155Z","shell.execute_reply":"2021-12-02T04:53:20.553095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Downloading and Saving Field Image for BackGround\nimport urllib.request\nurllib.request.urlretrieve(\"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/AmFBfield.svg/1200px-AmFBfield.svg.png\", \"field.png\")\n\nfrom PIL import Image\n#set a local image as a background\nimage_filename = 'field.png'\nplotly_logo = Image.open(image_filename)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:20.555107Z","iopub.execute_input":"2021-12-02T04:53:20.555335Z","iopub.status.idle":"2021-12-02T04:53:21.640154Z","shell.execute_reply.started":"2021-12-02T04:53:20.555307Z","shell.execute_reply":"2021-12-02T04:53:21.639080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_tracking_data(gameId, playId=False, position=False):\n    team_names = [\"home\", \"away\", \"football\"]\n    colors = [\"#396EB0\", \"#116530\", \"red\"]\n    \n    if playId:\n        temp_tracking_df = tracking2020.query(f'gameId == {gameId} and playId == {playId}')\n        teams = 3\n    elif position:\n        temp_tracking_df = tracking2020.query(f'gameId == {gameId} and position == \"{position}\"')\n        teams = 2\n        \n    fig = go.Figure()\n    \n    for i in range (teams):\n        fig.add_trace(go.Scatter(\n            x = temp_tracking_df.query(f\"team == '{team_names[i]}'\").x,\n            y = temp_tracking_df.query(f\"team == '{team_names[i]}'\").y,\n            mode = \"markers\",\n            name=team_names[i],\n            marker=dict(color=colors[i])\n        ))\n        \n    if playId:\n        title = f\"<b>Tracking Data - gameId = {gameId} and playId = {playId}</b>\"\n    elif position:\n        title = f\"<b>Tracking Data - gameId = {gameId} and position = {position}</b>\"\n        \n    fig.update_layout(\n        template=\"plotly_white\",\n        autosize=False,\n        width=1000,\n        height=550,\n        images= [dict(source=plotly_logo,\n                      xref=\"paper\", yref=\"paper\",x=0, y=1, sizex=1, sizey=1,\n                      layer=\"below\", opacity=0.5)],\n        xaxis=dict(showgrid=False),\n        yaxis=dict(showgrid=False),\n        title=title,\n        xaxis_title=\"x\",\n        yaxis_title=\"y\",\n    )\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:21.641804Z","iopub.execute_input":"2021-12-02T04:53:21.642138Z","iopub.status.idle":"2021-12-02T04:53:21.652803Z","shell.execute_reply.started":"2021-12-02T04:53:21.642094Z","shell.execute_reply":"2021-12-02T04:53:21.651834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_tracking_data(gameId=2020092707, playId=1186)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:21.654354Z","iopub.execute_input":"2021-12-02T04:53:21.654865Z","iopub.status.idle":"2021-12-02T04:53:21.849540Z","shell.execute_reply.started":"2021-12-02T04:53:21.654819Z","shell.execute_reply":"2021-12-02T04:53:21.848443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_tracking_data(gameId=2020102600, playId=250)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:21.850982Z","iopub.execute_input":"2021-12-02T04:53:21.851283Z","iopub.status.idle":"2021-12-02T04:53:22.007176Z","shell.execute_reply.started":"2021-12-02T04:53:21.851238Z","shell.execute_reply":"2021-12-02T04:53:22.006535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_tracking_data(gameId=2021010307, playId=2460)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:22.008327Z","iopub.execute_input":"2021-12-02T04:53:22.008680Z","iopub.status.idle":"2021-12-02T04:53:22.209348Z","shell.execute_reply.started":"2021-12-02T04:53:22.008642Z","shell.execute_reply":"2021-12-02T04:53:22.208085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_tracking_df = tracking2020.query(f'gameId == 2021010307 and position == \"RB\"')","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:22.210748Z","iopub.execute_input":"2021-12-02T04:53:22.211370Z","iopub.status.idle":"2021-12-02T04:53:22.538341Z","shell.execute_reply.started":"2021-12-02T04:53:22.211325Z","shell.execute_reply":"2021-12-02T04:53:22.537444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_tracking_data(gameId=2020102508, position=\"CB\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:22.539966Z","iopub.execute_input":"2021-12-02T04:53:22.540274Z","iopub.status.idle":"2021-12-02T04:53:22.915487Z","shell.execute_reply.started":"2021-12-02T04:53:22.540235Z","shell.execute_reply":"2021-12-02T04:53:22.914486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_tracking_data(gameId=2020101112, position=\"LB\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:22.916721Z","iopub.execute_input":"2021-12-02T04:53:22.916974Z","iopub.status.idle":"2021-12-02T04:53:23.304035Z","shell.execute_reply.started":"2021-12-02T04:53:22.916943Z","shell.execute_reply":"2021-12-02T04:53:23.303067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_tracking_data(gameId=2020102507, position=\"RB\")","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:23.305342Z","iopub.execute_input":"2021-12-02T04:53:23.306213Z","iopub.status.idle":"2021-12-02T04:53:23.690896Z","shell.execute_reply.started":"2021-12-02T04:53:23.306159Z","shell.execute_reply":"2021-12-02T04:53:23.690002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\n\ndef animated_game(gameId, playId, speed):\n    temp_tracking_df = tracking2020[tracking2020['gameId'] == gameId]\n\n    temp_tracking_query = (tracking2020['gameId'] == gameId) & (tracking2020['playId'] == playId)\n\n    temp_tracking_df = (\n        tracking2020[temp_tracking_query][['x', 'y', 'time', 'nflId', 'team', 'displayName']]\n        .fillna(0.)\n        .sort_values(['team', 'time'])\n    )\n\n    fig = px.scatter(\n        temp_tracking_df,\n        x='x',\n        y='y',\n        animation_frame='time',\n        color='team',\n        animation_group=\"nflId\",\n        hover_name=\"displayName\"\n    )\n    fig.update_traces(marker=dict(size=12,line=dict(width=2,color='DarkSlateGrey')),\n                      selector=dict(mode='markers'))\n    fig.update_layout(\n        template=\"plotly_white\",\n        autosize=False,\n        width=1000,\n        height=600,\n        title=f'Animation Every Players in the play {playId} of Game {gameId}',\n        images= [dict(source=plotly_logo,\n                      xref=\"paper\", yref=\"paper\",x=0, y=1, sizex=1, sizey=1,\n                      layer=\"below\", opacity=0.5)],\n        xaxis=dict(showgrid=False),\n        yaxis=dict(showgrid=False)\n    )\n    fig.layout.updatemenus[0].buttons[0].args[1][\"frame\"][\"duration\"] = speed\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:23.692134Z","iopub.execute_input":"2021-12-02T04:53:23.692340Z","iopub.status.idle":"2021-12-02T04:53:23.701498Z","shell.execute_reply.started":"2021-12-02T04:53:23.692314Z","shell.execute_reply":"2021-12-02T04:53:23.700414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gameId = 2021010300\nplayId = 40\nspeed = 60\n\nanimated_game(gameId, playId, speed)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:23.702784Z","iopub.execute_input":"2021-12-02T04:53:23.702999Z","iopub.status.idle":"2021-12-02T04:53:24.479286Z","shell.execute_reply.started":"2021-12-02T04:53:23.702973Z","shell.execute_reply":"2021-12-02T04:53:24.478527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gameId = 2020111510\nplayId = 1987\nspeed = 60\n\nanimated_game(gameId, playId, speed)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:24.480508Z","iopub.execute_input":"2021-12-02T04:53:24.480705Z","iopub.status.idle":"2021-12-02T04:53:25.261338Z","shell.execute_reply.started":"2021-12-02T04:53:24.480680Z","shell.execute_reply":"2021-12-02T04:53:25.260480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gameId = 2020121302\nplayId = 1320\nspeed = 60\n\nanimated_game(gameId, playId, speed)","metadata":{"execution":{"iopub.status.busy":"2021-12-02T04:53:25.262657Z","iopub.execute_input":"2021-12-02T04:53:25.262904Z","iopub.status.idle":"2021-12-02T04:53:26.652845Z","shell.execute_reply.started":"2021-12-02T04:53:25.262874Z","shell.execute_reply":"2021-12-02T04:53:26.651680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Are the proposed findings actionable?\n#### Is this a way of looking at tracking data that is novel?\n\nMy notebook is to generate actionable, practical, and novel insights from player tracking data that corresponds to special teams play. \n\nThere are several points but are not limited to:\n\n* Create a new special teams metric. \n* Quantify special teams strategy. \n* Rank special teams players. \nSubmissions that examine one idea more thoroughly are preferred versus those that examine several ideas somewhat thoroughly.\n\n\n#### Is this project creative?\n\nWell, this competition has both creative and analytical properties.","metadata":{}}]}