{"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":"import pandas as pd\nimport matplotlib.pylab as plt\nimport seaborn as sns\nimport matplotlib.patches as patches\nimport pandas_profiling\nimport warnings\nimport numpy as np\nwarnings.filterwarnings('ignore')\n\nfrom time import time\n\nplaylist = pd.read_csv('../input/nfl-playing-surface-analytics/PlayList.csv')\ninjuries = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/InjuryRecord.csv')\ntracking = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/PlayerTrackData.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T21:27:06.819222Z","iopub.execute_input":"2023-03-08T21:27:06.819549Z","iopub.status.idle":"2023-03-08T21:28:23.932712Z","shell.execute_reply.started":"2023-03-08T21:27:06.819521Z","shell.execute_reply":"2023-03-08T21:28:23.931503Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# NFL Injury Analysis","metadata":{}},{"cell_type":"markdown","source":"The goal of this analysis is to understand player injuries based on specific variables related to plays and game conditions.","metadata":{}},{"cell_type":"code","source":"playlist.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T21:28:32.681276Z","iopub.execute_input":"2023-03-08T21:28:32.683255Z","iopub.status.idle":"2023-03-08T21:28:32.708565Z","shell.execute_reply.started":"2023-03-08T21:28:32.683173Z","shell.execute_reply":"2023-03-08T21:28:32.705174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T21:29:08.406170Z","iopub.execute_input":"2023-03-08T21:29:08.406580Z","iopub.status.idle":"2023-03-08T21:29:08.421303Z","shell.execute_reply.started":"2023-03-08T21:29:08.406523Z","shell.execute_reply":"2023-03-08T21:29:08.420292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Synthetic vs Natural Data Split","metadata":{}},{"cell_type":"code","source":"inj_natural = injuries[injuries.Surface == 'Natural']\ninj_turf = injuries[injuries.Surface == 'Synthetic']","metadata":{"execution":{"iopub.status.busy":"2023-03-08T21:55:04.795864Z","iopub.execute_input":"2023-03-08T21:55:04.796252Z","iopub.status.idle":"2023-03-08T21:55:04.802161Z","shell.execute_reply.started":"2023-03-08T21:55:04.796222Z","shell.execute_reply":"2023-03-08T21:55:04.801193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inj_natural.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T21:32:01.412101Z","iopub.execute_input":"2023-03-08T21:32:01.412435Z","iopub.status.idle":"2023-03-08T21:32:01.425640Z","shell.execute_reply.started":"2023-03-08T21:32:01.412406Z","shell.execute_reply":"2023-03-08T21:32:01.424612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inj_turf.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T21:32:30.867247Z","iopub.execute_input":"2023-03-08T21:32:30.867602Z","iopub.status.idle":"2023-03-08T21:32:30.880160Z","shell.execute_reply.started":"2023-03-08T21:32:30.867571Z","shell.execute_reply":"2023-03-08T21:32:30.879446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Visualize the field\n* Heatmaps of different types of injuries\n* Heatmap with player position on days missed on synthetic vs real turf\n* trends when connecting player injuries to he attributes of the play\n* Ultimately have to connect it to a surface factor\n* Build a logistic model that predicts probability of injury based on surface type\n* How early in the game do they get injured based on the surface\n* number of plays it takes for an injury based on position\n* number of days missed based on injury","metadata":{}},{"cell_type":"markdown","source":"# Data cleaning","metadata":{}},{"cell_type":"code","source":"inj_nat_play = pd.merge(inj_natural,\n                 playlist,\n                 on=['PlayerKey', 'GameID', 'PlayKey'],\n                       how='left')\ninj_tur_play = pd.merge(inj_turf,\n                 playlist,\n                 on=['PlayerKey', 'GameID', 'PlayKey'],\n                       how ='left')\n\ninj_play = pd.merge(injuries,\n                 playlist,\n                 on=['PlayerKey', 'GameID', 'PlayKey'],\n                       how ='left')\n\ninj_play_enc = pd.concat([inj_play,pd.get_dummies(inj_play['BodyPart'], prefix='part'), pd.get_dummies(inj_play['RosterPosition'], prefix='pos')],axis=1)\n\ninj_play.head()\n\n\n#put NAN play key into separate file then join with attributes","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:03:34.354521Z","iopub.execute_input":"2023-03-08T22:03:34.354924Z","iopub.status.idle":"2023-03-08T22:03:34.760125Z","shell.execute_reply.started":"2023-03-08T22:03:34.354886Z","shell.execute_reply":"2023-03-08T22:03:34.758992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries['DM_M1'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:05:58.931694Z","iopub.execute_input":"2023-03-08T22:05:58.932298Z","iopub.status.idle":"2023-03-08T22:05:58.941320Z","shell.execute_reply.started":"2023-03-08T22:05:58.932259Z","shell.execute_reply":"2023-03-08T22:05:58.940426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries['DM_M7'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:07:11.651358Z","iopub.execute_input":"2023-03-08T22:07:11.651786Z","iopub.status.idle":"2023-03-08T22:07:11.661825Z","shell.execute_reply.started":"2023-03-08T22:07:11.651748Z","shell.execute_reply":"2023-03-08T22:07:11.660748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries['DM_M28'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:10:03.871041Z","iopub.execute_input":"2023-03-08T22:10:03.871370Z","iopub.status.idle":"2023-03-08T22:10:03.879106Z","shell.execute_reply.started":"2023-03-08T22:10:03.871340Z","shell.execute_reply":"2023-03-08T22:10:03.878098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries['DM_M42'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:10:31.571123Z","iopub.execute_input":"2023-03-08T22:10:31.571756Z","iopub.status.idle":"2023-03-08T22:10:31.581266Z","shell.execute_reply.started":"2023-03-08T22:10:31.571712Z","shell.execute_reply":"2023-03-08T22:10:31.580038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playlist['StadiumType'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:11:12.095031Z","iopub.execute_input":"2023-03-08T22:11:12.095414Z","iopub.status.idle":"2023-03-08T22:11:12.115717Z","shell.execute_reply.started":"2023-03-08T22:11:12.095383Z","shell.execute_reply":"2023-03-08T22:11:12.114774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playlist['Weather'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:14:31.301606Z","iopub.execute_input":"2023-03-08T22:14:31.301946Z","iopub.status.idle":"2023-03-08T22:14:31.324551Z","shell.execute_reply.started":"2023-03-08T22:14:31.301917Z","shell.execute_reply":"2023-03-08T22:14:31.323220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m_inju_play=pd.merge(playlist,injuries,on='PlayKey',how='left')\nm_inju_play['Injury'] = m_inju_play['BodyPart'].apply(lambda x: 1 if x in ['Knee','Foot','Ankle']  else 0)\n\nm_inju_play_agg=m_inju_play[['StadiumType','FieldType','Weather','Injury', 'PlayType', 'Position', 'PlayerDay', 'Temperature']]\n#Change weather to 1 and 0\nm_inju_play_agg['Weather']=m_inju_play_agg.Weather.isin(['rain','Rain','Snow','snow'])\nm_inju_play_agg['Weather'] = m_inju_play_agg['Weather'].apply(lambda x: 1 if x== True  else 0)\n\nm_inju_play_agg['StadiumType']=m_inju_play_agg.StadiumType.isin(['Outdoor','Oudoor','Open','Ourdoor','Out','open'])\nm_inju_play_agg['StadiumType'] = m_inju_play_agg['StadiumType'].apply(lambda x: 1 if x== True  else 0)\n\nm_inju_play_agg['FieldType']=m_inju_play_agg.FieldType.isin(['Natural'])\nm_inju_play_agg['FieldType'] = m_inju_play_agg['FieldType'].apply(lambda x: 1 if x== True  else 0)\n\nm_inju_play_agg.PlayType = pd.Categorical(m_inju_play_agg.PlayType)\nm_inju_play_agg['PlayType_c'] = m_inju_play_agg.PlayType.cat.codes\n\nm_inju_play_agg.Position = pd.Categorical(m_inju_play_agg.Position)\nm_inju_play_agg['Position_c'] = m_inju_play_agg.Position.cat.codes\n\n#Now we are going to multiplicate the StadiumType Variable with the Weather because the weather doesnt matter if the match is played on a closed stadium\n\nm_inju_play_agg['Multiplication']=m_inju_play_agg['StadiumType']*m_inju_play_agg['Weather']","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:47:18.171240Z","iopub.execute_input":"2023-03-08T22:47:18.171612Z","iopub.status.idle":"2023-03-08T22:47:18.959105Z","shell.execute_reply.started":"2023-03-08T22:47:18.171581Z","shell.execute_reply":"2023-03-08T22:47:18.958041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m_inju_play_agg.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:47:23.379917Z","iopub.execute_input":"2023-03-08T22:47:23.380268Z","iopub.status.idle":"2023-03-08T22:47:23.393591Z","shell.execute_reply.started":"2023-03-08T22:47:23.380238Z","shell.execute_reply":"2023-03-08T22:47:23.392515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"markdown","source":"# Body Part","metadata":{}},{"cell_type":"code","source":"sns.countplot(data = inj_play, x='BodyPart', hue = 'Surface')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:21:51.435792Z","iopub.execute_input":"2023-03-08T22:21:51.436168Z","iopub.status.idle":"2023-03-08T22:21:51.643048Z","shell.execute_reply.started":"2023-03-08T22:21:51.436129Z","shell.execute_reply":"2023-03-08T22:21:51.641871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Roster Position","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nsns.countplot(data=inj_play, x='RosterPosition', hue='Surface')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:24:05.448228Z","iopub.execute_input":"2023-03-08T22:24:05.448571Z","iopub.status.idle":"2023-03-08T22:24:05.679722Z","shell.execute_reply.started":"2023-03-08T22:24:05.448542Z","shell.execute_reply":"2023-03-08T22:24:05.678987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nsns.countplot(data=inj_play, x='PositionGroup', hue='Surface')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:25:47.034676Z","iopub.execute_input":"2023-03-08T22:25:47.035038Z","iopub.status.idle":"2023-03-08T22:25:47.255562Z","shell.execute_reply.started":"2023-03-08T22:25:47.035002Z","shell.execute_reply":"2023-03-08T22:25:47.254893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nsns.countplot(data=inj_play, x='PlayType', hue='Surface')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:27:17.149312Z","iopub.execute_input":"2023-03-08T22:27:17.149721Z","iopub.status.idle":"2023-03-08T22:27:17.385765Z","shell.execute_reply.started":"2023-03-08T22:27:17.149683Z","shell.execute_reply":"2023-03-08T22:27:17.384863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nsns.countplot(data=inj_play, x='DM_M7', hue='Surface')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:28:25.002512Z","iopub.execute_input":"2023-03-08T22:28:25.002864Z","iopub.status.idle":"2023-03-08T22:28:25.191524Z","shell.execute_reply.started":"2023-03-08T22:28:25.002824Z","shell.execute_reply":"2023-03-08T22:28:25.190538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nsns.countplot(data=inj_play, x='DM_M28', hue='Surface')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:30:02.801874Z","iopub.execute_input":"2023-03-08T22:30:02.802255Z","iopub.status.idle":"2023-03-08T22:30:02.980161Z","shell.execute_reply.started":"2023-03-08T22:30:02.802225Z","shell.execute_reply":"2023-03-08T22:30:02.979174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nsns.countplot(data=inj_play, x='DM_M42', hue='Surface')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:31:00.073910Z","iopub.execute_input":"2023-03-08T22:31:00.074329Z","iopub.status.idle":"2023-03-08T22:31:00.252997Z","shell.execute_reply.started":"2023-03-08T22:31:00.074292Z","shell.execute_reply":"2023-03-08T22:31:00.252096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nsns.countplot(data=inj_play, x='Weather', hue='Surface')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:31:50.862253Z","iopub.execute_input":"2023-03-08T22:31:50.862580Z","iopub.status.idle":"2023-03-08T22:31:51.242730Z","shell.execute_reply.started":"2023-03-08T22:31:50.862553Z","shell.execute_reply":"2023-03-08T22:31:51.241681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nsns.countplot(data=inj_play, x='StadiumType', hue = 'Surface')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:33:50.908651Z","iopub.execute_input":"2023-03-08T22:33:50.909011Z","iopub.status.idle":"2023-03-08T22:33:51.232775Z","shell.execute_reply.started":"2023-03-08T22:33:50.908972Z","shell.execute_reply":"2023-03-08T22:33:51.231855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Are there certain stadiums where they are occurng more than others?\n\nWhat body parts are being injured by position? and by surface?","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(16,6))\nsns.countplot(data=inj_play, x='PositionGroup', hue='BodyPart')","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:38:44.067554Z","iopub.execute_input":"2023-03-08T22:38:44.067907Z","iopub.status.idle":"2023-03-08T22:38:44.356781Z","shell.execute_reply.started":"2023-03-08T22:38:44.067874Z","shell.execute_reply":"2023-03-08T22:38:44.355879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inj_play[inj_play['BodyPart']=='Toes']","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:40:32.906368Z","iopub.execute_input":"2023-03-08T22:40:32.906745Z","iopub.status.idle":"2023-03-08T22:40:32.932673Z","shell.execute_reply.started":"2023-03-08T22:40:32.906707Z","shell.execute_reply":"2023-03-08T22:40:32.931384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries[injuries['BodyPart']=='Toes']","metadata":{"execution":{"iopub.status.busy":"2023-03-08T22:41:39.345744Z","iopub.execute_input":"2023-03-08T22:41:39.346097Z","iopub.status.idle":"2023-03-08T22:41:39.359222Z","shell.execute_reply.started":"2023-03-08T22:41:39.346068Z","shell.execute_reply":"2023-03-08T22:41:39.358460Z"},"trusted":true},"execution_count":null,"outputs":[]}]}