{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# ref : https://www.kaggle.com/robikscube/nfl-1st-and-future-analytics-intro\n# For tracking the player route\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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Distribution of surface natural vs syntetic\nplaylist = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/PlayList.csv')\n#playlist.FieldType\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# check the PlayerTrack of player\nplayerTrack = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/PlayerTrackData.csv')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# What is percentage  of playing in natural vs synthetic turf\n# natural surface percentage is 59.87%  (149723 is 59.87% of 250095)\n# synthetic surface percentage is 40.13%  (100372 is 40.13% of 250095)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"playlist.groupby('FieldType').count()['StadiumType'].sum()\nplaylist.groupby('StadiumType')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pylab  as plt\nimport seaborn as sns\nsns.set_style('whitegrid')\nplaylist.groupby('FieldType').count() \\\n            .sort_values('StadiumType') \\\n            .plot(kind ='bar', figsize=(15,5), title='distribution of natural surf vs sythetic')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# check the InjuryRecord of player\ninjlist = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/InjuryRecord.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot the distibution of injury occured on bodypart on a given surface\ninjlist.groupby(['BodyPart','Surface']) \\\n            .count() \\\n            .unstack('BodyPart')['PlayKey'] \\\n            .T.sort_values('Natural').T \\\n            .sort_values('Surface') \\\n            .plot(kind ='bar', figsize=(15,5), title='Distibution of injury occured on bodypart on a given surface')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injlist","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# As you can see from above plot.\n# 01. Occurance of  knee injury for a player will be same wether he plays on natural or synthetic turf\n# 02. Occurance of  Ankel  injury for a player will be less on Natural turf as compared to synthetic turf \n# 03. Occurance of Foot injury for a player will be More on Natural turf as compared to synthetic turf ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injlist.groupby('PlayerKey').nunique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inj_player = injlist.merge(playlist)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inj_player.groupby(['RosterPosition','Surface']) \\\n            .count()['PlayerKey'] \\\n            .sort_values() \\\n            .plot(kind ='bar', figsize=(15,5), title='Injury occured at a given position by surface')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# As you can see from above plot.\n# 01. Occurance of  injury at linebacker position is more on natural or synthetic turf\n# 02. Followed by Occurance of  injury at Wide position is more on synthetic turf than natural turf\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inj_player","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inj_player.groupby('Surface').count()['PlayerKey'] \\\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inj_player.groupby(['RosterPosition','BodyPart']) \\\n            .count() \\\n            .unstack('BodyPart')['PlayerKey'] \\\n            .T.apply(lambda x: x / x.sum()) \\\n            .sort_values('BodyPart').T.sort_values('Ankle', ascending=False) \\\n            .plot(kind ='bar', figsize=(15,5), title='Injury occured on a bodypart at a position')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# As you can see from above plot.\n# 01. Occurance of Ankle njury at a position Comeback, Knee injurya while Running Back is more\n# 02. Followed by Occurance of  Ankle injury at offensive Line man, Wide position, linebscker is more, Occurane of Knee injury at position safet and line back are equal\n","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":false,"_kg_hide-output":false,"trusted":true},"cell_type":"code","source":"import matplotlib.patches as patches\n# ref https://www.kaggle.com/robikscube/nfl-big-data-bowl-plotting-player-position\ndef create_football_field(linenumbers=True,\n                          endzones=True,\n                          highlight_line=False,\n                          highlight_line_number=50,\n                          highlighted_name='Line of Scrimmage',\n                          fifty_is_los=False,\n                          figsize=(12, 6.33)):\n    \"\"\"\n    Function that plots the football field for viewing plays.\n    Allows for showing or hiding endzones.\n    \"\"\"\n    rect = patches.Rectangle((0, 0), 120, 53.3, linewidth=0.1,\n                             edgecolor='r', facecolor='darkgreen', zorder=0)\n\n    fig, ax = plt.subplots(1, figsize=figsize)\n    ax.add_patch(rect)\n\n    plt.plot([10, 10, 10, 20, 20, 30, 30, 40, 40, 50, 50, 60, 60, 70, 70, 80,\n              80, 90, 90, 100, 100, 110, 110, 120, 0, 0, 120, 120],\n             [0, 0, 53.3, 53.3, 0, 0, 53.3, 53.3, 0, 0, 53.3, 53.3, 0, 0, 53.3,\n              53.3, 0, 0, 53.3, 53.3, 0, 0, 53.3, 53.3, 53.3, 0, 0, 53.3],\n             color='white')\n    if fifty_is_los:\n        plt.plot([60, 60], [0, 53.3], color='gold')\n        plt.text(62, 50, '<- Player Yardline at Snap', color='gold')\n    # Endzones\n    if endzones:\n        ez1 = patches.Rectangle((0, 0), 10, 53.3,\n                                linewidth=0.1,\n                                edgecolor='r',\n                                facecolor='blue',\n                                alpha=0.2,\n                                zorder=0)\n        ez2 = patches.Rectangle((110, 0), 120, 53.3,\n                                linewidth=0.1,\n                                edgecolor='r',\n                                facecolor='blue',\n                                alpha=0.2,\n                                zorder=0)\n        ax.add_patch(ez1)\n        ax.add_patch(ez2)\n    plt.xlim(0, 120)\n    plt.ylim(-5, 58.3)\n    plt.axis('off')\n    if linenumbers:\n        for x in range(20, 110, 10):\n            numb = x\n            if x > 50:\n                numb = 120 - x\n            plt.text(x, 5, str(numb - 10),\n                     horizontalalignment='center',\n                     fontsize=20,  # fontname='Arial',\n                     color='white')\n            plt.text(x - 0.95, 53.3 - 5, str(numb - 10),\n                     horizontalalignment='center',\n                     fontsize=20,  # fontname='Arial',\n                     color='white', rotation=180)\n    if endzones:\n        hash_range = range(11, 110)\n    else:\n        hash_range = range(1, 120)\n\n    for x in hash_range:\n        ax.plot([x, x], [0.4, 0.7], color='white')\n        ax.plot([x, x], [53.0, 52.5], color='white')\n        ax.plot([x, x], [22.91, 23.57], color='white')\n        ax.plot([x, x], [29.73, 30.39], color='white')\n\n    if highlight_line:\n        hl = highlight_line_number + 10\n        plt.plot([hl, hl], [0, 53.3], color='yellow')\n        plt.text(hl + 2, 50, '<- {}'.format(highlighted_name),\n                 color='yellow')\n    return fig, ax","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Loop through all 99 inj plays\ninj_play_list = injlist['PlayKey'].tolist()\nfig, ax = create_football_field()\nfor playkey, inj_play in playerTrack.query('PlayKey in @inj_play_list').groupby(['time']):\n    inj_play.plot(kind='scatter', x='x', y='y', ax=ax, color='orange', alpha=0.2)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"playerTrack","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pylab  as plt\nplayerTrack.query('PlayKey in @inj_play_list')['s'].plot(kind='hist',\n                                                 title='Distribution of player Speed injured',\n                                                 figsize=(15, 5), bins=30)\n","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}