{"cells":[{"metadata":{},"cell_type":"markdown","source":"## First and Future - Analytics"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport os","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# read in the files separately, all at once cause a kernel failure\ninjury_record = pd.read_csv(\"../input/nfl-playing-surface-analytics/InjuryRecord.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_record.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_counts = injury_record['BodyPart'].value_counts()\ninjury_counts.plot.bar()\nplt.xlabel('Body Part')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# compare body parts vs surface\ninjury_surface = pd.crosstab(injury_record['BodyPart'], injury_record['Surface'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_surface.plot.bar()\nplt.legend(title='Surface', loc = 'center')\nplt.title('Surface vs Body Part Injured')\nplt.show()\n# plt.savefig('surf_body.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"play_list = pd.read_csv(\"../input/nfl-playing-surface-analytics/PlayList.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_record.groupby('Surface').count()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"play_list['Weather'] = play_list['Weather'].str.lower()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"play_list['GameID'].nunique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# id weather conditions by game, drop duplicates so it's clean to join to the injury record data\ngame_weather = play_list[['GameID','Weather']]\ngame_weather = game_weather.drop_duplicates(['GameID','Weather'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# match player key and position, drop duplicates to join to injury record data\nplayer_positions = play_list[['PlayerKey','RosterPosition']]\nplayer_positions = player_positions.drop_duplicates('PlayerKey')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# same as above for stadium type\ngame_environment = play_list[['GameID','StadiumType']]\ngame_environment = game_environment.drop_duplicates(['GameID','StadiumType'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# merge the data into injury record\ninjury_record = pd.merge(injury_record, game_environment, on = 'GameID', how = 'left')\ninjury_record = pd.merge(injury_record, player_positions, on = 'PlayerKey', how = 'left')\ninjury_record = pd.merge(injury_record, game_weather, on = 'GameID', how = 'left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_record","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_record['Weather'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Outdoors','Outdoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Open','Outdoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Outddors','Outdoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Oudoor','Outdoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Open Roof','Outdoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Retr. Roof - Open','Outdoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Heinz Field','Outdoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Retr. Roof - Outdoor','Outdoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Indoors','Indoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Dome','Indoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Retr. Roof-Closed','Indoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Domed, closed','Indoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Retr. Roof - Closed','Indoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Indoor, Roof Closed','Indoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Indoord, closed','Indoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Closed Indoor','Indoor')\ninjury_record['StadiumType'] = injury_record['StadiumType'].str.replace('Indoor, Outdoor Roof','Indoor')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_record['Weather'] = injury_record['Weather'].astype(str)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# function to create wet variable\ndef conditions(string):\n        if \"rain\" in string:\n            return 1\n        else:\n            return 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_record['Wet'] = injury_record['Weather'].apply(conditions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_record[injury_record['StadiumType'].isnull()]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# look at injuries with wet turf\npd.crosstab(injury_record['Wet'], injury_record['Surface'])","execution_count":null,"outputs":[]},{"metadata":{"scrolled":true,"trusted":true},"cell_type":"code","source":"wet_injuries = pd.crosstab(injury_record['Wet'], injury_record['Surface'])\nwet_injuries.plot.bar()\nplt.legend(title='Surface', loc='upper right')\nplt.xlabel('No Rain vs Rain')\nplt.title('Wet vs Surface where Injuries Occur')\n#plt.show()\nplt.savefig('wet_surface_injuries.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pd.crosstab(injury_record['RosterPosition'], injury_record['Surface']).plot.bar()\nplt.legend(title='Surface', loc='center')\nplt.xlabel('Roster Position')\nplt.title('Roster Position vs Surface Type Injury Breakdown')\n#plt.show()\nplt.savefig('surface_position.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# clean up the play_list DF for stadium type, indoor/outdoor\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Outdoors','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Open','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Outddors','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Oudoor','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Open Roof','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Retr. Roof - Open','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Heinz Field','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Retr. Roof - Outdoor','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Indoors','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Dome','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Retr. Roof-Closed','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Domed, closed','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Retr. Roof - Closed','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Indoor, Roof Closed','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Indoord, closed','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Closed Indoor','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Indoord','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Outdoor Retr Roof-Outdoor','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Ourdoor','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Retr. Roof-Outdoor','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Indoor, closed','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Indoord, Outdoor','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Indoord, open','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Retr. Roof Closed','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Outdor','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Outside','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Retractable Roof','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Bowl','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Indoor, Outdoor Roof','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Indoor, Outdoor','Indoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Indoor, open','Outdoor')\nplay_list['StadiumType'] = play_list['StadiumType'].str.replace('Cloudy','Outdoor')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# there are enough observations that we can model after dropping nulls\nplay_list['StadiumType'].dropna(inplace=True)\nplay_list['StadiumType'].unique()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Game Injury Model - Predicting if a player will be injured in a game"},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl = pd.concat([play_list,pd.get_dummies(play_list['RosterPosition'], prefix='Pos')],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injured_gameid = list(injury_record['GameID'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def cond_injured(df):\n    if (df['GameID'] in injured_gameid):\n        return 1\n    else:\n        return 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl['Injured'] = play_list.apply(cond_injured, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl = mod1_data_pl.drop_duplicates(['GameID'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl = mod1_data_pl.drop(columns=['PlayKey','RosterPosition','PlayType','PlayerGamePlay','Position',\n                                          'PositionGroup','Pos_Kicker',])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create a list to identify wet turf\npl_wet = ['rain','showers','snow','scattered showers','light rain','cloudy, rain','rainy','cloudy, light snow accumulating 1-2\"',\n         'cloudy with periods of rain, thunder possible. winds shifting to wnw, 10-20 mph.','rain shower']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# for mapping for a model - new variable\nturfs = {'Natural':0, 'Synthetic':1}\nin_out = {'Outdoor':0, 'Indoor':1}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# function to create wet variable\ndef conditions(df):\n    if (df['Weather'] in pl_wet) & (df['StadiumType'] == 'Outdoor'):\n        return 1\n    else:\n        return 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl['Wet'] = mod1_data_pl.apply(conditions, axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl['FieldType'] = mod1_data_pl['FieldType'].map(turfs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl['StadiumType'] = mod1_data_pl['StadiumType'].map(in_out)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# these instances of injured players can be retained based on null values; weather is null but stadium is indoor\nkeep = ['36621-13','36696-24','40051-13','43826-7','47382-3']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl.loc[(mod1_data_pl['Weather'].isnull()) &  (mod1_data_pl['GameID'].isin(keep)), 'Weather'] = 'ok'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"drops_weather_stadiumtype = mod1_data_pl[(mod1_data_pl['Weather'].isnull()) | (mod1_data_pl['StadiumType'].isnull())].index","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl.drop(drops_weather_stadiumtype, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod1_data_pl = mod1_data_pl.drop(columns=['PlayerKey','GameID','Weather'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# correlation of the dataset\ncorr1 = mod1_data_pl.corr()\ncorr1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create the X and y data for train_test_split\nX = mod1_data_pl[[\n 'PlayerDay',\n 'PlayerGame',\n 'StadiumType',\n 'FieldType',\n 'Temperature',\n 'Pos_Cornerback',\n 'Pos_Defensive Lineman',\n 'Pos_Linebacker',\n 'Pos_Offensive Lineman',\n 'Pos_Quarterback',\n 'Pos_Running Back',\n 'Pos_Safety',\n 'Pos_Tight End',\n 'Pos_Wide Receiver',\n    'Wet']].to_numpy(copy = True)\ny = mod1_data_pl['Injured'].to_numpy(copy = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Splitting the dataset into the Training set and Test set\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 0.20, random_state = 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fitting Random Forest to the Trng set\nfrom sklearn.ensemble import RandomForestClassifier\n# Create Classifier - entropy is ideal criterion\nclassifier = RandomForestClassifier(n_estimators = 150, max_depth = 10, criterion = 'entropy', random_state = 0)\nclassifier.fit(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Apply K-fold cross validation \nfrom sklearn.model_selection import cross_val_score\nrf_scores = cross_val_score(classifier, X_train, y_train, cv=10)\nrf_scores.mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Predictions\ny_pred = classifier.predict(X_test) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Make the confusion matrix - evaluate the model\nfrom sklearn.metrics import confusion_matrix\ncm = confusion_matrix(y_test, y_pred) \ncm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"importances = classifier.feature_importances_\nimportances","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"According to the RF model, field type (turf or natural) is the 4th most important feature in the model. Day, game, and temperature have more influence over an injury occurring in a game according to the random forest classifier. In general, the model does not predict well but helps understand the relationship of surface on the risk of being injured."},{"metadata":{},"cell_type":"markdown","source":"\n## Access the tracking data"},{"metadata":{"trusted":true},"cell_type":"code","source":"# due to the size of the tracking data file, the dask package is utilized\nimport dask.dataframe as dd\ntrack_data = dd.read_csv('../input/nfl-playing-surface-analytics/PlayerTrackData.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"track_data = track_data.drop('event', axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"track_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"td = track_data.groupby('PlayKey').max().compute() - track_data.groupby('PlayKey').min().compute()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"distances = track_data.groupby('PlayKey').dis.sum().compute()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mn_speed = track_data.groupby('PlayKey').s.mean().compute()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"max_speed = track_data.groupby('PlayKey').s.max().compute()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"max_speed.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"td = td.drop(['dis','s'], axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"td = pd.merge(td, distances, on = 'PlayKey', how = 'left')\ntd = pd.merge(td, mn_speed, on = 'PlayKey', how = 'left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"td['speed_time_ratio'] = td['s']/td['time']\ntd['distance_time_ratio'] = td['dis']/td['time']\ntd['mean_speed'] = td['s']\ntd = td.drop('s', axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"td.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Merging the track data with play list"},{"metadata":{"trusted":true},"cell_type":"code","source":"play_and_injury_data = pd.merge(play_list, injury_record, how='left', on='PlayKey')\nplay_and_injury_data.drop(['PlayerKey_y', 'GameID_y', 'Weather_y', 'RosterPosition_y', 'StadiumType_y'], axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import math\ndef encode_injured_players(string):\n    if (pd.isnull(string)):\n        return 0\n    else:\n        return 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"play_and_injury_data['Injured'] = play_and_injury_data['BodyPart'].apply(encode_injured_players)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# function to create wet variable\ndef conditions(string):\n        if \"rain\" in string:\n            return 1\n        else:\n            return 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"play_and_injury_data['Weather_x'] = play_and_injury_data['Weather_x'].astype(str)\nplay_and_injury_data['Wet'] = play_and_injury_data['Weather_x'].apply(conditions)\nplay_and_injury_data = play_and_injury_data[play_and_injury_data['PlayType'] != '0']\nplay_and_injury_data = play_and_injury_data.rename(columns={'PlayerKey_x': 'PlayerKey', \n                                     'GameID_x': 'GameID', \n                                     'RosterPosition_x':'RosterPosition', \n                                     'StadiumType_x': 'StadiumType', \n                                     'Weather_x': 'Weather'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"play_and_injury_data.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mn_player_plays = play_and_injury_data.groupby('PlayerKey').mean()\nmn_player_plays['PlayerGamePlay'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_data_pl = play_and_injury_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_data_pl['FieldType'] = mod2_data_pl['FieldType'].map(turfs)\nmod2_data_pl['StadiumType'] = mod2_data_pl['StadiumType'].map(in_out)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_data_pl['StadiumType'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"drops_weather_stadiumtype2 = mod2_data_pl[(mod2_data_pl['Weather'].isnull()) | (mod2_data_pl['StadiumType'].isnull())].index","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_data_pl.drop(drops_weather_stadiumtype2, inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# merge tracking data onto mod2_data_pl\nmod2_data_pl = pd.merge(mod2_data_pl, td, on = 'PlayKey', how = 'left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_data_pl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['Injured'], mod2_data_pl['mean_speed'])\nplt.title('Mean Speed vs Injured')\nplt.xlabel('Not injured vs injured')\nplt.show()\n#plt.savefig('mnspeed_injured.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"scrolled":true,"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['Injured'], mod2_data_pl['x'])\nplt.title('X range (max-min) vs Injured')\nplt.xlabel('Not injured vs injured')\nplt.show()\n#plt.savefig('xrange_injured.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['Injured'], mod2_data_pl['y'])\nplt.title('Y range (max-min) vs Injured')\nplt.xlabel('Not injured vs injured')\nplt.show()\n#plt.savefig('yrange_injured.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['Injured'], mod2_data_pl['time'])\nplt.title('Time range (max-min) vs Injured')\nplt.xlabel('Not injured vs injured')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['FieldType'], mod2_data_pl['mean_speed'])\nplt.title('Average Speed vs Field Type')\nplt.xlabel('Natural vs Turf')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['FieldType'], mod2_data_pl['x'])\nplt.title('X range (max-min) vs Field Type')\nplt.xlabel('Natural vs Turf')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['FieldType'], mod2_data_pl['y'])\nplt.title('Y range (max-min) vs Field Type')\nplt.xlabel('Natural vs Turf')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['FieldType'], mod2_data_pl['time'])\nplt.title('Time range (max-min) vs Field Type')\nplt.xlabel('Natural vs Turf')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_injured = mod2_data_pl[mod2_data_pl['Injured'] == 1]\nmod2_notinjured = mod2_data_pl[mod2_data_pl['Injured'] == 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_injured['mean_speed'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_notinjured['mean_speed'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_injured_turf = mod2_injured[mod2_data_pl['FieldType'] == 1]\nmod2_injured_grass = mod2_injured[mod2_data_pl['FieldType'] == 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_injured_turf['mean_speed'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_injured_grass['mean_speed'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_injured['FieldType'], mod2_injured['mean_speed'])\nplt.title('Mean speed vs Field Type - Injured Only')\nplt.xlabel('Natural vs Turf')\nplt.show()\n#plt.savefig('mean_spd_field_type.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_injured['FieldType'], mod2_injured['x'])\nplt.title('X range (max-min) vs Field Type - Injured Only')\nplt.xlabel('Natural vs Turf')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_injured['FieldType'], mod2_injured['y'])\nplt.title('Y range (max-min) vs Field Type - Injured Only')\nplt.xlabel('Natural vs Turf')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['FieldType'], mod2_data_pl['dis'])\nplt.title('Distance vs Field Type')\nplt.xlabel('Natural vs Turf')\nplt.show()\n#plt.savefig('distance_turf.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['Injured'], mod2_data_pl['dis'])\nplt.title('Distance vs Injured')\nplt.xlabel('Not-injured vs Injured')\nplt.show()\n#plt.savefig('distance_injured.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['Injured'], mod2_data_pl['speed_time_ratio'])\nplt.title('Speed_time_ratio vs Injured')\nplt.xlabel('Not-injured vs Injured')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['FieldType'], mod2_data_pl['speed_time_ratio'])\nplt.title('Speed_time_ratio vs FieldType')\nplt.xlabel('Natural vs Turf')\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"turf = mod2_data_pl[mod2_data_pl['FieldType'] == 1]\nturf['speed_time_ratio'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"grass = mod2_data_pl[mod2_data_pl['FieldType'] == 0]\ngrass['speed_time_ratio'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"grass['mean_speed'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"turf['mean_speed'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.distplot(turf['mean_speed'], hist = False, kde = True,\n                 kde_kws = {'shade': True, 'linewidth': 3})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.distplot(grass['mean_speed'], hist = False, kde = True,\n                 kde_kws = {'shade': True, 'linewidth': 3})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"grass['x'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"grass['y'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"turf['x'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"turf['y'].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['FieldType'], mod2_data_pl['distance_time_ratio'])\nplt.title('Distance_time_ratio vs FieldType')\nplt.xlabel('Natural vs Turf')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.boxplot(mod2_data_pl['Injured'], mod2_data_pl['distance_time_ratio'])\nplt.title('Distance_time_ratio vs Injured')\nplt.xlabel('Not-injured vs Injured')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_plays = play_and_injury_data[play_and_injury_data['Injured'] == 1]\npd.crosstab(injury_plays['PlayType'], injury_plays['FieldType']).plot.bar()\nplt.legend(title='Surface', loc = 'upper right')\nplt.title('Play Type by Field Type Where Injuries Occurred')\nplt.show()\n#plt.savefig('play_type_injuries.jpg', bbox_inches='tight')\n# note - a previous plot was included where more injured instances occurred - the data was modified to capture\n# only playkeys where injuries occur in this version","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Noticed all temperatures with -999 degrees were indoor stadiums that are temp controlled\nplay_and_injury_data['Temperature'].replace(-999, value=0, inplace=True)\ninjury_plays = play_and_injury_data[play_and_injury_data['Injured'] == 1]\ninjury_plays['Temperature'].hist(grid=False, bins=10)\nplt.title('Injury Count by Temperature')\nplt.xlabel('Temperature')\nplt.ylabel('Count')\nplt.show()\n#plt.savefig('temp_injuries.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_plays['PlayerGamePlay'].hist(grid=False, bins=10)\nplt.xlabel('Plays Played')\nplt.ylabel('Count')\nplt.title('Injury Count by Plays Played')\nplt.show()\n#plt.savefig('play_cnt_injuries.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injured_grass = grass[grass['Injured'] == 1]\ninjured_grass['PlayerGamePlay'].hist(grid=False, bins=10)\nplt.xlabel('Plays Played')\nplt.ylabel('Count')\nplt.title('Injury Count by Plays Played on Natural Surface')\nplt.show()\n#plt.savefig('play_cnt_injuries_nat.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injured_turf = turf[turf['Injured'] == 1]\ninjured_turf['PlayerGamePlay'].hist(grid=False, bins=10)\nplt.xlabel('Plays Played')\nplt.ylabel('Count')\nplt.title('Injury Count by Plays Played on Synthetic Surface')\nplt.show()\n#plt.savefig('play_cnt_injuries_turf.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_data_pl = pd.merge(mod2_data_pl, max_speed, on = 'PlayKey', how = 'left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mod2_data_pl['max_speed_play'] = mod2_data_pl['s']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"max_spd_injured = mod2_data_pl[mod2_data_pl['Injured'] == 1]\nmax_spd_notinjured = mod2_data_pl[mod2_data_pl['Injured'] == 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"max_spd_injured['max_speed_play'].hist(grid=False, bins=10)\nplt.xlabel('Max Speed')\nplt.ylabel('Count')\nplt.title('Max Speed Injured Players - yards per second')\nplt.show()\n#plt.savefig('maxspeedinjured.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"max_spd_notinjured['max_speed_play'].hist(grid=False, bins=100)\nplt.xlim(left = 0)\nplt.xlim(right = 10)\nplt.xlabel('Max Speed')\nplt.ylabel('Count')\nplt.title('Max Speed Not Injured - yards per second')\nplt.show()\n#plt.savefig('maxspeednotinjured.jpg', bbox_inches='tight')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.3"}},"nbformat":4,"nbformat_minor":1}