{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pylab as plt\nimport seaborn as sns\nimport matplotlib.patches as patches\nsns.set_style(\"whitegrid\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data:\nThere are three files provided in the dataset, as described below:\n\n1. **Injury Record:** The injury record file in .csv format contains information on 105 lower-limb injuries that occurred during regular season games over the two seasons. Injuries can be linked to specific records in a player history using the PlayerKey, GameID, and PlayKey fields.\n\n2. **Play List:** – The play list file contains the details for the 267,005 player-plays that make up the dataset. Each play is indexed by PlayerKey, GameID, and PlayKey fields. Details about the game and play include the player’s assigned roster position, stadium type, field type, weather, play type, position for the play, and position group.\n\n3. **Player Track Data:** player level data that describes the location, orientation, speed, and direction of each player during a play recorded at 10 Hz (i.e. 10 observations recorded per second).\n"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Read the input files\nplaylist = pd.read_csv('../input/nfl-playing-surface-analytics/PlayList.csv')\ninj = pd.read_csv('../input/nfl-playing-surface-analytics/InjuryRecord.csv')\ntrk = pd.read_csv('../input/nfl-playing-surface-analytics/PlayerTrackData.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Injury Data\nFirst lets look at the injury data. It's a fairly small file with only 105 injury plays shown. I notice that many of the rows for the injury plays do not show `PlayerKey`, `GameId`, etc. I'm not sure if this is a bug or intentially done.\n\n- PlayerKey, GameId, PlayKey\n- BodyPart\n- Surface\n- DM_M1, DM_M7, DM_28, DM_42 - One hot encoding the number of days missed for injury"},{"metadata":{"trusted":true},"cell_type":"code","source":"inj.groupby('Surface').count()['PlayerKey'] \\\n    .sort_values() \\\n    .plot(kind='barh', figsize=(15, 5), title='Count of injuries by Field Surface', color='orange')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"# injuries_encoded = inj[['BodyPart', 'Surface','DM_M1','DM_M7','DM_M28','DM_M42']]\n# body_parts_map = {'Heel': 2, 'Foot': 3, 'Toes': 4, 'Ankle': 1, 'Knee': 0}\n# surface_map = {'Synthetic': 0, 'Natural': 1}\n# injuries_encoded['BodyPart'] = injuries_encoded['BodyPart'].map(body_parts_map)\n# injuries_encoded['Surface'] = injuries_encoded['Surface'].map(surface_map)\n# coefs = np.corrcoef(injuries_encoded, rowvar=False)\n\n# from matplotlib import cm as cm\n# fig = plt.figure()\n# ax1 = fig.add_subplot(111)\n# cmap = cm.get_cmap('jet', 30)\n# cax = ax1.imshow(coefs, interpolation=\"nearest\", cmap=cmap)\n# fig.colorbar(cax, ticks=[.75,.8,.85,.90,.95,1])\n# ax1.grid(True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(1, 2)\n# inj[['BodyPart', 'Surface','DM_M1','DM_M7','DM_M28','DM_M42']]\nfig_1 = inj[inj['Surface'] == 'Natural'].groupby('BodyPart').count()['PlayerKey'] \\\n    .sort_values() \\\n    .plot(kind='bar', figsize=(15, 5), title='Count of injuries by Body Part - Natural', ax=axes[0])\nfig_2 = inj[inj['Surface'] == 'Synthetic'].groupby('BodyPart').count()['PlayerKey'] \\\n    .sort_values() \\\n    .plot(kind='bar', figsize=(15, 5), title='Count of injuries by Body Part - Synthetic', ax=axes[1])\n# plt.plot(fig_1, fig_2)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Distribution of injured body parts against the surface type. We can see that ankles is more highly represented on synthetic turf."},{"metadata":{"trusted":true},"cell_type":"code","source":"def encode_missed_days(row):\n    missed_days = ['DM_M1','DM_M7','DM_M28','DM_M42']\n    if row['DM_M42']:\n        return 42\n    if row['DM_M28']:\n        return 28\n    if row['DM_M7']:\n        return 7\n    if row['DM_M1']:\n        return 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ankle_injuries = inj[inj['BodyPart'] == 'Ankle']\nankle_injuries['DaysMissed'] = ankle_injuries.apply(lambda x: encode_missed_days(x), axis=1)\nfig, axes = plt.subplots(1, 2)\n# inj[['BodyPart', 'Surface','DM_M1','DM_M7','DM_M28','DM_M42']]\nfig_1 = ankle_injuries[ankle_injuries['Surface'] == 'Natural'].groupby('DaysMissed').count()['PlayerKey'] \\\n    .sort_values() \\\n    .plot(kind='bar', figsize=(15, 5), title='Count of ankle injuries by days missed - Natural', ax=axes[0])\nfig_2 = ankle_injuries[ankle_injuries['Surface'] == 'Synthetic'].groupby('DaysMissed').count()['PlayerKey'] \\\n    .sort_values() \\\n    .plot(kind='bar', figsize=(15, 5), title='Count of ankle injuries by days missed - Synthetic', ax=axes[1])\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"The number of days missed due to an ankle injury at the top end is way more represented on synthetic turf than natural turf.\n\nWe can see that synthetic turf is causing ankle injuries at a higher rate and that those ankle injuries are causing more damage (harder to recover from). More exploration to follow..."},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}