{"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":"# Survival Analysis & NFL Injuries","metadata":{}},{"cell_type":"markdown","source":"This notebook was used for the 2019 NFL 1st and Future - Analytics competition. The goal was to understand player injuries based on specific variables related to plays and game conditions. I tried some more traditional survival analysis models as well as some basic ML models. Enjoy!","metadata":{}},{"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# Load the data files\nplaylist = pd.read_csv('../input/nfl-playing-surface-analytics/PlayList.csv')\ninjuries = pd.read_csv('../input/nfl-playing-surface-analytics/InjuryRecord.csv')\ntracking = pd.read_csv('../input/nfl-playing-surface-analytics/PlayerTrackData.csv', nrows=int(1e6)) # load only a fraction of the data","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-31T13:15:51.999887Z","iopub.execute_input":"2022-07-31T13:15:52.000702Z","iopub.status.idle":"2022-07-31T13:15:56.880570Z","shell.execute_reply.started":"2022-07-31T13:15:52.000627Z","shell.execute_reply":"2022-07-31T13:15:56.879537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playlist.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:56.883082Z","iopub.execute_input":"2022-07-31T13:15:56.883540Z","iopub.status.idle":"2022-07-31T13:15:56.916639Z","shell.execute_reply.started":"2022-07-31T13:15:56.883463Z","shell.execute_reply":"2022-07-31T13:15:56.915806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:56.918286Z","iopub.execute_input":"2022-07-31T13:15:56.918545Z","iopub.status.idle":"2022-07-31T13:15:56.931879Z","shell.execute_reply.started":"2022-07-31T13:15:56.918495Z","shell.execute_reply":"2022-07-31T13:15:56.930959Z"},"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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2022-07-31T13:15:56.933669Z","iopub.execute_input":"2022-07-31T13:15:56.934000Z","iopub.status.idle":"2022-07-31T13:15:56.944414Z","shell.execute_reply.started":"2022-07-31T13:15:56.933950Z","shell.execute_reply":"2022-07-31T13:15:56.943285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inj_natural.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:56.947420Z","iopub.execute_input":"2022-07-31T13:15:56.947747Z","iopub.status.idle":"2022-07-31T13:15:56.965539Z","shell.execute_reply.started":"2022-07-31T13:15:56.947628Z","shell.execute_reply":"2022-07-31T13:15:56.964796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inj_turf.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:56.967736Z","iopub.execute_input":"2022-07-31T13:15:56.967955Z","iopub.status.idle":"2022-07-31T13:15:56.982495Z","shell.execute_reply.started":"2022-07-31T13:15:56.967924Z","shell.execute_reply":"2022-07-31T13:15:56.981870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ideas\n\nVisualize the field\n\nHeatmaps of different types of injuries\n\nHeatmaps with player position on days missed on synthetic vs real turf\n\nTrends when connecting Player injuries to the attributes of the play\n\nUltimately have to connect it to a surface factor \n\nBuild a logistic model that predicts probability of injury based on surface type.\n\nHow early in the game do they get injured based on the surface. \n\nNumber of plays it takes for an injury based on position. \n\nNumber of days missed based on injury\n\n# Data Cleaning\n","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\n","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:56.983646Z","iopub.execute_input":"2022-07-31T13:15:56.984060Z","iopub.status.idle":"2022-07-31T13:15:57.579128Z","shell.execute_reply.started":"2022-07-31T13:15:56.984019Z","shell.execute_reply":"2022-07-31T13:15:57.578037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries['DM_M1'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:57.580519Z","iopub.execute_input":"2022-07-31T13:15:57.580877Z","iopub.status.idle":"2022-07-31T13:15:57.588064Z","shell.execute_reply.started":"2022-07-31T13:15:57.580712Z","shell.execute_reply":"2022-07-31T13:15:57.587289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries['DM_M7'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:57.589158Z","iopub.execute_input":"2022-07-31T13:15:57.589408Z","iopub.status.idle":"2022-07-31T13:15:57.600097Z","shell.execute_reply.started":"2022-07-31T13:15:57.589370Z","shell.execute_reply":"2022-07-31T13:15:57.599230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries['DM_M28'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:57.601125Z","iopub.execute_input":"2022-07-31T13:15:57.601365Z","iopub.status.idle":"2022-07-31T13:15:57.613235Z","shell.execute_reply.started":"2022-07-31T13:15:57.601321Z","shell.execute_reply":"2022-07-31T13:15:57.612368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries['DM_M42'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:57.614412Z","iopub.execute_input":"2022-07-31T13:15:57.614761Z","iopub.status.idle":"2022-07-31T13:15:57.625062Z","shell.execute_reply.started":"2022-07-31T13:15:57.614726Z","shell.execute_reply":"2022-07-31T13:15:57.624502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lots of values that need to be cleaned\nplaylist['StadiumType'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:57.626283Z","iopub.execute_input":"2022-07-31T13:15:57.626617Z","iopub.status.idle":"2022-07-31T13:15:57.658022Z","shell.execute_reply.started":"2022-07-31T13:15:57.626580Z","shell.execute_reply":"2022-07-31T13:15:57.657293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playlist['Weather'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:57.659048Z","iopub.execute_input":"2022-07-31T13:15:57.659462Z","iopub.status.idle":"2022-07-31T13:15:57.691207Z","shell.execute_reply.started":"2022-07-31T13:15:57.659415Z","shell.execute_reply":"2022-07-31T13:15:57.690653Z"},"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":"2022-07-31T13:15:57.692094Z","iopub.execute_input":"2022-07-31T13:15:57.692408Z","iopub.status.idle":"2022-07-31T13:15:58.606694Z","shell.execute_reply.started":"2022-07-31T13:15:57.692373Z","shell.execute_reply":"2022-07-31T13:15:58.605762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m_inju_play_agg.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:58.607700Z","iopub.execute_input":"2022-07-31T13:15:58.608038Z","iopub.status.idle":"2022-07-31T13:15:58.625126Z","shell.execute_reply.started":"2022-07-31T13:15:58.608003Z","shell.execute_reply":"2022-07-31T13:15:58.624209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA\n\n## BodyPart","metadata":{}},{"cell_type":"code","source":"sns.countplot(data=inj_play, x='BodyPart', hue= 'Surface')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:58.626291Z","iopub.execute_input":"2022-07-31T13:15:58.626650Z","iopub.status.idle":"2022-07-31T13:15:58.952848Z","shell.execute_reply.started":"2022-07-31T13:15:58.626485Z","shell.execute_reply":"2022-07-31T13:15:58.951832Z"},"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":"2022-07-31T13:15:58.954396Z","iopub.execute_input":"2022-07-31T13:15:58.954900Z","iopub.status.idle":"2022-07-31T13:15:59.372623Z","shell.execute_reply.started":"2022-07-31T13:15:58.954837Z","shell.execute_reply":"2022-07-31T13:15:59.371708Z"},"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":"2022-07-31T13:15:59.374263Z","iopub.execute_input":"2022-07-31T13:15:59.374792Z","iopub.status.idle":"2022-07-31T13:15:59.625797Z","shell.execute_reply.started":"2022-07-31T13:15:59.374736Z","shell.execute_reply":"2022-07-31T13:15:59.624996Z"},"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":"2022-07-31T13:15:59.629123Z","iopub.execute_input":"2022-07-31T13:15:59.629400Z","iopub.status.idle":"2022-07-31T13:15:59.912607Z","shell.execute_reply.started":"2022-07-31T13:15:59.629348Z","shell.execute_reply":"2022-07-31T13:15:59.911687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6, 6))\nsns.countplot(data=inj_play, x='DM_M7', hue= 'Surface')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:15:59.914219Z","iopub.execute_input":"2022-07-31T13:15:59.914718Z","iopub.status.idle":"2022-07-31T13:16:00.089643Z","shell.execute_reply.started":"2022-07-31T13:15:59.914651Z","shell.execute_reply":"2022-07-31T13:16:00.088917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6, 6))\nsns.countplot(data=inj_play, x='DM_M28', hue= 'Surface')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:16:00.091070Z","iopub.execute_input":"2022-07-31T13:16:00.091664Z","iopub.status.idle":"2022-07-31T13:16:00.265921Z","shell.execute_reply.started":"2022-07-31T13:16:00.091610Z","shell.execute_reply":"2022-07-31T13:16:00.265257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6, 6))\nsns.countplot(data=inj_play, x='DM_M42', hue= 'Surface')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:16:00.267382Z","iopub.execute_input":"2022-07-31T13:16:00.267937Z","iopub.status.idle":"2022-07-31T13:16:00.450868Z","shell.execute_reply.started":"2022-07-31T13:16:00.267881Z","shell.execute_reply":"2022-07-31T13:16:00.450061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(40, 6))\nsns.countplot(data=inj_play, x='Weather', hue= 'Surface')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:16:00.454172Z","iopub.execute_input":"2022-07-31T13:16:00.454470Z","iopub.status.idle":"2022-07-31T13:16:00.963537Z","shell.execute_reply.started":"2022-07-31T13:16:00.454419Z","shell.execute_reply":"2022-07-31T13:16:00.962668Z"},"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":"2022-07-31T13:16:00.964997Z","iopub.execute_input":"2022-07-31T13:16:00.965355Z","iopub.status.idle":"2022-07-31T13:16:01.336586Z","shell.execute_reply.started":"2022-07-31T13:16:00.965296Z","shell.execute_reply":"2022-07-31T13:16:01.335627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Are there certain stadiums where they are occuring more than others?\n\nWhat body parts are being injured by position? And by surface?","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20, 6))\nsns.countplot(data=inj_play, x='PositionGroup', hue= 'BodyPart')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:16:01.339933Z","iopub.execute_input":"2022-07-31T13:16:01.340189Z","iopub.status.idle":"2022-07-31T13:16:01.750709Z","shell.execute_reply.started":"2022-07-31T13:16:01.340139Z","shell.execute_reply":"2022-07-31T13:16:01.750032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inj_play[inj_play['BodyPart'] == 'Toes']","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:16:01.752001Z","iopub.execute_input":"2022-07-31T13:16:01.752548Z","iopub.status.idle":"2022-07-31T13:16:01.781836Z","shell.execute_reply.started":"2022-07-31T13:16:01.752489Z","shell.execute_reply":"2022-07-31T13:16:01.781035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"injuries[injuries['BodyPart'] == 'Toes']","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:16:01.785804Z","iopub.execute_input":"2022-07-31T13:16:01.787783Z","iopub.status.idle":"2022-07-31T13:16:01.807672Z","shell.execute_reply.started":"2022-07-31T13:16:01.787714Z","shell.execute_reply":"2022-07-31T13:16:01.806435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Changes in movement pattern across the surface. ","metadata":{}},{"cell_type":"markdown","source":"Number of injuries across days and player game. ","metadata":{}},{"cell_type":"markdown","source":"Model the time until injury and see if this changes across field types, use a hazard rate model and see if the rate changes. ","metadata":{}},{"cell_type":"markdown","source":"# Hazard Rate Model\n\nFor further details on the concept and definitions of Hazard Rate Models refer to this blog post.\n\nhttps://towardsdatascience.com/survival-analysis-intuition-implementation-in-python-504fde4fcf8e\n\n\n## Kaplan-Meier Estimate","metadata":{}},{"cell_type":"code","source":"!pip install lifelines","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:18:44.320071Z","iopub.execute_input":"2022-07-31T13:18:44.320425Z","iopub.status.idle":"2022-07-31T13:19:39.375403Z","shell.execute_reply.started":"2022-07-31T13:18:44.320364Z","shell.execute_reply":"2022-07-31T13:19:39.374345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lifelines import KaplanMeierFitter\n\n##Natural Turf\n## Example Data \ndurations = list(range(int(max(inj_nat_play['PlayerDay'].dropna()))+1))\nevent_observed = [1 if dur in inj_nat_play['PlayerDay'] else 0 for dur in durations]\n\n## create a kmf object\nkmf = KaplanMeierFitter() \n\n## Fit the data into the model\nkmf.fit(durations, event_observed,label='Natural')\n\n## Create an estimate\na1 = kmf.plot(ci_show=False) ## ci_show is meant for Confidence interval, since our data set is too tiny, thus i am not showing it.","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:19:41.732910Z","iopub.execute_input":"2022-07-31T13:19:41.733356Z","iopub.status.idle":"2022-07-31T13:19:42.132432Z","shell.execute_reply.started":"2022-07-31T13:19:41.733195Z","shell.execute_reply":"2022-07-31T13:19:42.131367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Artificial Turf\n## Example Data \ndurations = list(range(int(max(inj_tur_play['PlayerDay'].dropna()))+1))\nevent_observed = [1 if dur in inj_tur_play['PlayerDay'] else 0 for dur in durations]\n\n\n## Fit the data into the model\nkmf.fit(durations, event_observed,label='Artificial')\n\n## Create an estimate\nkmf.plot() ## ci_show is meant for Confidence interval, since our data set is too tiny, thus i am not showing it.","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:20:29.588213Z","iopub.execute_input":"2022-07-31T13:20:29.588527Z","iopub.status.idle":"2022-07-31T13:20:29.924865Z","shell.execute_reply.started":"2022-07-31T13:20:29.588479Z","shell.execute_reply":"2022-07-31T13:20:29.923562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Cox Proportional Hazard Model","metadata":{}},{"cell_type":"code","source":"df_dummy = pd.get_dummies(inj_play, drop_first=True)\ndf_dummy.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:20:38.689778Z","iopub.execute_input":"2022-07-31T13:20:38.690074Z","iopub.status.idle":"2022-07-31T13:20:38.735712Z","shell.execute_reply.started":"2022-07-31T13:20:38.690026Z","shell.execute_reply":"2022-07-31T13:20:38.734740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lifelines import CoxPHFitter","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:20:42.177996Z","iopub.execute_input":"2022-07-31T13:20:42.178333Z","iopub.status.idle":"2022-07-31T13:20:42.184398Z","shell.execute_reply.started":"2022-07-31T13:20:42.178271Z","shell.execute_reply":"2022-07-31T13:20:42.183717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummy = pd.Series(playlist[['PlayerKey', 'GameID', 'PlayKey']].itertuples(index=False, name=None)).isin(injuries[['PlayerKey', 'GameID', 'PlayKey']].itertuples(index=False, name=None))*1","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:20:44.806268Z","iopub.execute_input":"2022-07-31T13:20:44.806758Z","iopub.status.idle":"2022-07-31T13:20:45.000414Z","shell.execute_reply.started":"2022-07-31T13:20:44.806690Z","shell.execute_reply":"2022-07-31T13:20:44.999446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playlist['inj_ind'] = dummy","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:20:46.425472Z","iopub.execute_input":"2022-07-31T13:20:46.425765Z","iopub.status.idle":"2022-07-31T13:20:46.431327Z","shell.execute_reply.started":"2022-07-31T13:20:46.425717Z","shell.execute_reply":"2022-07-31T13:20:46.430326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playlist.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:20:48.111398Z","iopub.execute_input":"2022-07-31T13:20:48.111886Z","iopub.status.idle":"2022-07-31T13:20:48.128723Z","shell.execute_reply.started":"2022-07-31T13:20:48.111822Z","shell.execute_reply":"2022-07-31T13:20:48.127895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playlist.StadiumType = pd.Categorical(playlist.StadiumType)\nplaylist['StadiumType_c'] = playlist.StadiumType.cat.codes\nplaylist.FieldType = pd.Categorical(playlist.FieldType)\nplaylist['FieldType_c'] = playlist.FieldType.cat.codes\nplaylist.Weather = pd.Categorical(playlist.Weather)\nplaylist['Weather_c'] = playlist.Weather.cat.codes\nplaylist.PlayType = pd.Categorical(playlist.PlayType)\nplaylist['PlayType_c'] = playlist.PlayType.cat.codes\nplaylist.Position = pd.Categorical(playlist.Position)\nplaylist['Position_c'] = playlist.Position.cat.codes","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:20:50.970066Z","iopub.execute_input":"2022-07-31T13:20:50.970596Z","iopub.status.idle":"2022-07-31T13:20:51.127280Z","shell.execute_reply.started":"2022-07-31T13:20:50.970344Z","shell.execute_reply":"2022-07-31T13:20:51.126315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playlist.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:20:54.039411Z","iopub.execute_input":"2022-07-31T13:20:54.039715Z","iopub.status.idle":"2022-07-31T13:20:54.063336Z","shell.execute_reply.started":"2022-07-31T13:20:54.039666Z","shell.execute_reply":"2022-07-31T13:20:54.062462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Using Cox Proportional Hazards model\n#features = ['FieldType_c', 'Temperature', 'StadiumType_c', 'PlayerGame', 'Weather_c', 'PlayType_c', 'Position_c', 'PlayerDay', 'inj_ind']\nfeatures = ['Injury','Multiplication', 'FieldType', 'Temperature', 'StadiumType', 'PlayerDay','PlayType_c','Position_c' ]\nX = m_inju_play_agg[features]\ncph = CoxPHFitter()   ## Instantiate the class to create a cph object\n#cph.fit(X_train, 'PlayerDay', event_col='inj_ind')   ## Fit the data to train the model\ncph.fit(X, 'PlayerDay', event_col='Injury')\ncph.print_summary() ","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:20:57.772394Z","iopub.execute_input":"2022-07-31T13:20:57.772889Z","iopub.status.idle":"2022-07-31T13:20:59.971619Z","shell.execute_reply.started":"2022-07-31T13:20:57.772655Z","shell.execute_reply":"2022-07-31T13:20:59.970624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Logistic Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\n\nfeatures = ['Multiplication', 'FieldType', 'Temperature', 'StadiumType', 'PlayerDay','PlayType_c','Position_c' ]\nX = m_inju_play_agg[features]\ny = m_inju_play_agg['Injury']\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=42)\n\nclf = LogisticRegression(random_state=0).fit(X_train, y_train)\ny_pred_log = clf.predict(X_test)\nclf.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:21:03.672805Z","iopub.execute_input":"2022-07-31T13:21:03.673129Z","iopub.status.idle":"2022-07-31T13:21:05.888925Z","shell.execute_reply.started":"2022-07-31T13:21:03.673078Z","shell.execute_reply":"2022-07-31T13:21:05.887503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.coef_","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:21:08.637388Z","iopub.execute_input":"2022-07-31T13:21:08.637706Z","iopub.status.idle":"2022-07-31T13:21:08.643930Z","shell.execute_reply.started":"2022-07-31T13:21:08.637650Z","shell.execute_reply":"2022-07-31T13:21:08.643191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\ncnf_matrix = metrics.confusion_matrix(y_test, y_pred_log)\n#cnf_matrix\n\nclass_names=[0,1] # name  of classes\nfig, ax = plt.subplots()\ntick_marks = np.arange(len(class_names))\nplt.xticks(tick_marks, class_names)\nplt.yticks(tick_marks, class_names)\n# create heatmap\nsns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap=\"YlGnBu\" ,fmt='g')\nax.xaxis.set_label_position(\"top\")\nplt.tight_layout()\nplt.title('Confusion matrix', y=1.1)\nplt.ylabel('Actual label')\nplt.xlabel('Predicted label')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:21:10.323293Z","iopub.execute_input":"2022-07-31T13:21:10.323855Z","iopub.status.idle":"2022-07-31T13:21:10.592581Z","shell.execute_reply.started":"2022-07-31T13:21:10.323807Z","shell.execute_reply":"2022-07-31T13:21:10.591710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Random Forest","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:22:28.127447Z","iopub.execute_input":"2022-07-31T13:22:28.127749Z","iopub.status.idle":"2022-07-31T13:22:28.228879Z","shell.execute_reply.started":"2022-07-31T13:22:28.127710Z","shell.execute_reply":"2022-07-31T13:22:28.228040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = ['Multiplication', 'FieldType', 'Temperature', 'StadiumType', 'PlayerDay','PlayType_c','Position_c' ]\nX = m_inju_play_agg[features]\ny = m_inju_play_agg['Injury']\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=42)\n\nclf = RandomForestClassifier(random_state=0, n_estimators=100).fit(X_train, y_train)\ny_pred_rf = clf.predict(X_test)\nclf.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:22:39.388537Z","iopub.execute_input":"2022-07-31T13:22:39.389079Z","iopub.status.idle":"2022-07-31T13:22:48.869641Z","shell.execute_reply.started":"2022-07-31T13:22:39.389012Z","shell.execute_reply":"2022-07-31T13:22:48.868841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnf_matrix = metrics.confusion_matrix(y_test, y_pred_rf)\nfig, ax = plt.subplots()\ntick_marks = np.arange(len(class_names))\nplt.xticks(tick_marks, class_names)\nplt.yticks(tick_marks, class_names)\n# create heatmap\nsns.heatmap(pd.DataFrame(cnf_matrix), annot=True, cmap=\"YlGnBu\" ,fmt='g')\nax.xaxis.set_label_position(\"top\")\nplt.tight_layout()\nplt.title('Confusion matrix', y=1.1)\nplt.ylabel('Actual label')\nplt.xlabel('Predicted label')","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:23:27.531132Z","iopub.execute_input":"2022-07-31T13:23:27.531479Z","iopub.status.idle":"2022-07-31T13:23:27.921674Z","shell.execute_reply.started":"2022-07-31T13:23:27.531416Z","shell.execute_reply":"2022-07-31T13:23:27.920608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importances = clf.feature_importances_","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:23:50.754794Z","iopub.execute_input":"2022-07-31T13:23:50.755401Z","iopub.status.idle":"2022-07-31T13:23:50.767271Z","shell.execute_reply.started":"2022-07-31T13:23:50.755350Z","shell.execute_reply":"2022-07-31T13:23:50.766289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forest_importances = pd.Series(importances, index=features)\nstd = np.std([tree.feature_importances_ for tree in clf.estimators_], axis=0)\n\nfig, ax = plt.subplots()\nforest_importances.plot.bar(yerr=std, ax=ax)\nax.set_title(\"Feature importances using MDI\")\nax.set_ylabel(\"Mean decrease in impurity\")\nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T13:25:28.254896Z","iopub.execute_input":"2022-07-31T13:25:28.255213Z","iopub.status.idle":"2022-07-31T13:25:28.683257Z","shell.execute_reply.started":"2022-07-31T13:25:28.255154Z","shell.execute_reply":"2022-07-31T13:25:28.682269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The most impactful variable in this subset seems to be 'PlayerDay' which seems to be the particular time of season. ","metadata":{}}]}