{"cells":[{"metadata":{"id":"_iluP95ds0tw","colab_type":"text"},"cell_type":"markdown","source":"# **Variety in PlayType can be a factor for injury**"},{"metadata":{"id":"vO0La_brtGai","colab_type":"text"},"cell_type":"markdown","source":"## **Objective**\n\nCharacterize any differences in player movement between the playing surfaces and to identify specific variables (e.g., field surface, weather, position, play type, etc.) that may influence player movement and the risk of injury.\n\nEvaluation tab:\nhttps://www.kaggle.com/c/nfl-playing-surface-analytics/overview/evaluation"},{"metadata":{"id":"14WbHFZrtI2q","colab_type":"text"},"cell_type":"markdown","source":"## **Libraries**"},{"metadata":{"id":"x33134LstFKs","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nimport gc\nimport matplotlib.patches as patches\nimport time\npd.options.mode.chained_assignment = None","execution_count":null,"outputs":[]},{"metadata":{"id":"1TsXL84OtO64","colab_type":"text"},"cell_type":"markdown","source":"## **Load Data**"},{"metadata":{"id":"F4tJ_6GutMvB","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"# Reduce memory and read csv fies\ndef reduce_mem_usage(df):\n    \"\"\" iterate through all the columns of a dataframe and modify the data type\n        to reduce memory usage.        \n    \"\"\"\n    start_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n    \n    for col in df.columns:\n        col_type = df[col].dtype\n        \n        if col_type != object:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)\n\n    end_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n    \n    return df\n\n\ndef import_data(file):\n    \"\"\"create a dataframe and optimize its memory usage\"\"\"\n    df = pd.read_csv(file, parse_dates=True, keep_date_col=True)\n    df = reduce_mem_usage(df)\n    return df","execution_count":null,"outputs":[]},{"metadata":{"id":"m0Ln018KtVK8","colab_type":"code","outputId":"57dafef9-77bd-40f8-ce56-073e0512ee88","colab":{"base_uri":"https://localhost:8080/","height":269},"trusted":true},"cell_type":"code","source":"print('-' * 80)\nprint('injury_record')\ninjury_record = import_data('../input/nfl-playing-surface-analytics/InjuryRecord.csv')\n\nprint('-' * 80)\nprint('player_data')\nplayer_data = import_data('../input/nfl-playing-surface-analytics/PlayerTrackData.csv')\n\nprint('-' * 80)\nprint('play_list')\nplay_list = import_data('../input/nfl-playing-surface-analytics/PlayList.csv')","execution_count":null,"outputs":[]},{"metadata":{"id":"rrmsfNFItiNA","colab_type":"text"},"cell_type":"markdown","source":"## **Data cleaning**"},{"metadata":{"id":"6KdMxxk_5hpJ","colab_type":"text"},"cell_type":"markdown","source":"45 Play Keys in play data doesn't have player track data"},{"metadata":{"id":"ucDI-S5D5gpI","colab_type":"code","outputId":"20e36f21-2cae-48f6-993b-2fc1003d17d3","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"play_list[~play_list.PlayKey.isin(player_data.PlayKey.unique())].PlayKey.count()","execution_count":null,"outputs":[]},{"metadata":{"id":"f8BUgcJSWO5L","colab_type":"code","outputId":"a5d5eb8e-dcf4-43a9-ccb4-7712f6a0e0ba","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"injury_record[injury_record.PlayKey.isin(player_data.PlayKey.unique())].PlayKey.count()","execution_count":null,"outputs":[]},{"metadata":{"id":"otWQbtRMtmfS","colab_type":"text"},"cell_type":"markdown","source":"### **Injury record**"},{"metadata":{"id":"NvUUjJZwxhbp","colab_type":"text"},"cell_type":"markdown","source":"Analysis is based on player track data.So **drop 28 rows in which play key is empty**"},{"metadata":{"id":"KVZA_IeptjCO","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"#injury_record.dropna(subset = ['PlayKey'],inplace =True)","execution_count":null,"outputs":[]},{"metadata":{"id":"-6s34Ni0-1Mn","colab_type":"text"},"cell_type":"markdown","source":"### **Convert one hot injury duration to Catagories for easier visualizations**"},{"metadata":{"id":"sOxbxgWN-vvG","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"def catagorize_injury_duration(DM_M1,DM_M7 ,DM_M28 ,DM_M42):\n    \n    if (DM_M42 == 1):\n        return '6 or more weeks'\n    elif(DM_M28 == 1): \n        return '4 - 6 weeks'\n    elif(DM_M7 == 1):\n        return '1 - 3 weeks'\n    elif(DM_M1 == 1):\n        return 'less than a week'","execution_count":null,"outputs":[]},{"metadata":{"id":"OGWlrmt5-41T","colab_type":"code","outputId":"3778ca01-61de-456b-f802-1e2784ce0a65","colab":{"base_uri":"https://localhost:8080/","height":195},"trusted":true},"cell_type":"code","source":"injury_record['InjuryDuration'] = injury_record.apply(lambda X:catagorize_injury_duration(X.DM_M1,X.DM_M7 ,X.DM_M28 ,X.DM_M42) ,axis =1 )\n\ninjury_record.drop(columns=['DM_M1','DM_M7' ,'DM_M28' ,'DM_M42'],inplace =True)\n\ninjury_record.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"ocWxIERx9tQI","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"id":"ckRKabkTuDDo","colab_type":"text"},"cell_type":"markdown","source":"## **Weather**"},{"metadata":{"id":"35fFZ3pmsz_c","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"rain = ['30% Chance of Rain', 'Rainy', 'Rain Chance 40%', 'Showers', 'Cloudy, 50% change of rain', 'Rain likely, temps in low 40s.',\n          'Cloudy with periods of rain, thunder possible. Winds shifting to WNW, 10-20 mph.',\n          'Scattered Showers', 'Cloudy, Rain', 'Rain shower', 'Light Rain', 'Rain']\n\novercast = ['Party Cloudy', 'Cloudy, chance of rain',\n              'Coudy', \n              'Cloudy and cold', 'Cloudy, fog started developing in 2nd quarter',\n              'Partly Clouidy', 'Mostly Coudy', 'Cloudy and Cool',\n              'cloudy', 'Partly cloudy', 'Overcast', 'Hazy', 'Mostly cloudy', 'Mostly Cloudy',\n              'Partly Cloudy', 'Cloudy']\n\nclear = ['Partly clear', 'Sunny and clear', 'Sun & clouds', 'Clear and Sunny',\n           'Sunny and cold', 'Sunny Skies', 'Clear and Cool', 'Clear and sunny',\n           'Sunny, highs to upper 80s', 'Mostly Sunny Skies', 'Cold',\n           'Clear and warm', 'Sunny and warm', 'Clear and cold', 'Mostly sunny',\n           'T: 51; H: 55; W: NW 10 mph', 'Clear Skies', 'Clear skies', 'Partly sunny',\n           'Fair', 'Partly Sunny', 'Mostly Sunny', 'Clear', 'Sunny']\n\nsnow = ['Cloudy, light snow accumulating 1-3\"', 'Heavy lake effect snow', 'Snow']\n\nnone = ['N/A Indoor', 'Indoors', 'Indoor', 'N/A (Indoors)', 'Controlled Climate']\n","execution_count":null,"outputs":[]},{"metadata":{"id":"Kilg6KlUuaxQ","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"play_list['Weather'] = np.where(play_list.Weather.isin(rain), 'rain' , \\\n         np.where(play_list.Weather.isin(overcast) , 'overcast',\\\n          np.where(play_list.Weather.isin(clear) , 'clear' ,\\\n           np.where(play_list.Weather.isin(snow) ,'snow',\\\n            np.where(play_list.Weather.isin(none),'indoors','unknown')))))","execution_count":null,"outputs":[]},{"metadata":{"id":"0PXLk9by20WM","colab_type":"text"},"cell_type":"markdown","source":"## **Roof**"},{"metadata":{"id":"NJwSXH6S3Oue","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"opened = ['Outdoor', 'Outdoors', 'Cloudy', 'Heinz Field', \n              'Outdor', 'Ourdoor', 'Outside', 'Outddors', \n              'Outdoor Retr Roof-Open', 'Oudoor', 'Bowl','Indoor, Open Roof', 'Open', 'Retr. Roof-Open', 'Retr. Roof - Open','Domed, Open', 'Domed, open']\n\nclosed = ['Indoors', 'Indoor', 'Indoor, Roof Closed', 'Indoor, Roof Closed',\n                   'Retr. Roof-Closed', 'Retr. Roof - Closed', 'Retr. Roof Closed','Dome', 'Domed, closed', 'Closed Dome', 'Domed', 'Dome, closed']","execution_count":null,"outputs":[]},{"metadata":{"id":"kU3F8j4m41TE","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"play_list['roof'] = np.where(play_list.StadiumType.isin(opened), 'open' , \\\n         np.where(play_list.StadiumType.isin(closed) , 'closed','unknown'))\n\nplay_list.drop(columns = ['StadiumType'],inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"id":"cBRUepdUAwhH","colab_type":"text"},"cell_type":"markdown","source":"## **Feature engineering**"},{"metadata":{"id":"1Fh-MZjFN8rt","colab_type":"text"},"cell_type":"markdown","source":"## **Distance**"},{"metadata":{"id":"K4nv3go0QO7S","colab_type":"text"},"cell_type":"markdown","source":"### Distance per play"},{"metadata":{"id":"BhHS0ewvQNrS","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"play_distance = player_data.groupby('PlayKey')['dis'].sum(axis =1).reset_index().rename(columns={'dis':'play_dis'})","execution_count":null,"outputs":[]},{"metadata":{"id":"fKw3sIidO6Gh","colab_type":"code","outputId":"74fc7269-429b-4412-9199-3997200ff9ec","colab":{"base_uri":"https://localhost:8080/","height":195},"trusted":true},"cell_type":"code","source":"play_distance.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"PGt22FdYSOWt","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"play_list = play_list.merge( play_distance , on = 'PlayKey' , how ='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del play_distance\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"id":"wzoBxQGqSYlf","colab_type":"text"},"cell_type":"markdown","source":"### Distance per game"},{"metadata":{"id":"QzhhdJwwSixt","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"game_distance = play_list.groupby('GameID')['play_dis'].sum(axis =1).reset_index().rename(columns={'play_dis':'game_dis'})","execution_count":null,"outputs":[]},{"metadata":{"id":"qACNgmJfTFrW","colab_type":"code","outputId":"97737330-7013-4627-c37f-b0717ee7080a","colab":{"base_uri":"https://localhost:8080/","height":195},"trusted":true},"cell_type":"code","source":"game_distance.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"ZkhiuWGTTH13","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"play_list = play_list.merge( game_distance , on = 'GameID' , how ='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del game_distance\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"id":"_dcB-H1ATNqk","colab_type":"text"},"cell_type":"markdown","source":"### Distance per player"},{"metadata":{"id":"bMEwKVZeTdQ8","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"player_distance = play_list.groupby('PlayerKey')['play_dis'].sum(axis =1).reset_index().rename(columns={'play_dis':'player_dis'})","execution_count":null,"outputs":[]},{"metadata":{"id":"Rtz8sLEtV8sJ","colab_type":"code","outputId":"353b62d1-7523-4632-af46-9c1e6f98ca62","colab":{"base_uri":"https://localhost:8080/","height":195},"trusted":true},"cell_type":"code","source":"player_distance.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"gqLwLM4RWCcF","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"play_list = play_list.merge( player_distance , on = 'PlayerKey' , how ='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del player_distance\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"id":"v8Qm2T3w1DW2","colab_type":"text"},"cell_type":"markdown","source":"## **Time**\n\n*Time per play*"},{"metadata":{"id":"Y0HwCUET1PO1","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"time_data = player_data.groupby('PlayKey')['time'].apply(lambda x:x.iloc[-1]).reset_index()","execution_count":null,"outputs":[]},{"metadata":{"id":"N6pEmiaA1UOb","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"play_list = play_list.merge( time_data , on = 'PlayKey' , how ='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del time_data\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"id":"rlcOp9Vi1BlU","colab_type":"code","outputId":"87fba25e-41eb-4315-d188-f6978033a819","colab":{"base_uri":"https://localhost:8080/","height":336},"trusted":true},"cell_type":"code","source":"play_list.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"cPGmhSwI54jM","colab_type":"text"},"cell_type":"markdown","source":"Time data not available for 45 play keys which doesn't have player track data"},{"metadata":{"id":"WAMaJWp-6f3n","colab_type":"text"},"cell_type":"markdown","source":"**Acceleration**"},{"metadata":{"id":"s-40LD1QmHU0","colab_type":"code","outputId":"4a16b8fe-fecf-4882-b2af-0f236e4ce01c","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"import time\nstart_time = time.time()\nrows_list = []\nfor k, v in  player_data.groupby('PlayKey'):\n    #coords = v[['x', 'y']].to_numpy()\n    dists = (v['s'] -v['s'].shift(1)) / (v['time'] - v['time'].shift(1))\n    dists.iloc[0] = 0\n    rows_list.extend(dists)\n\nprint(time.time() - start_time)","execution_count":null,"outputs":[]},{"metadata":{"id":"8rbC6hvA7V4c","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"acceleration = pd.Series(rows_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del rows_list\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"id":"puTAcuS57l7d","colab_type":"code","outputId":"e1fb39fb-66ed-4025-8570-3ecb419ac992","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"acceleration.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"N8X2dSIsmLfb","colab_type":"text"},"cell_type":"markdown","source":"There are values in acceleration which are 'infinity' and 'NaN'. On below situations and subsitute with '0'\n\n\n\n*   infinity (1/0)(Change in speed / no change in time)\n*   NaN (0/0) (No change in speed / no change in time)\n\n\n\n\n\n"},{"metadata":{"id":"IMFKnespnepj","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"acceleration = acceleration.replace([np.inf, -np.inf], np.nan)","execution_count":null,"outputs":[]},{"metadata":{"id":"Cm4CkFCYn1j3","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"acceleration.fillna(0 , inplace = True)","execution_count":null,"outputs":[]},{"metadata":{"id":"Jiv8soEl5rAJ","colab_type":"code","outputId":"81ddcacc-fe28-4d8e-85ce-e0d8d8d8feee","colab":{"base_uri":"https://localhost:8080/","height":34},"trusted":true},"cell_type":"code","source":"acceleration.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"id":"vHMxoAjKonw7","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"player_data['a'] = acceleration","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del acceleration\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"id":"qKZ0CRmhpGWM","colab_type":"code","outputId":"af12762c-f4f0-4d8d-c666-8ed08c148037","colab":{"base_uri":"https://localhost:8080/","height":195},"trusted":true},"cell_type":"code","source":"player_data.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"PS21zhMFjMzS","colab_type":"text"},"cell_type":"markdown","source":"## **Pitch location**\n"},{"metadata":{"id":"nMLS2YuP2ImC","colab_type":"text"},"cell_type":"markdown","source":"Devide the pitch into Middle and wide on y -axis: Identify difference between Middle and wide player movements\n\n\n\n*   *Lower Wide* (0-17.76 yards)\n*   *Middle* (17.77-35.6 yards)\n*   *Upper wide* (35.7 -53.5 yards)\n\n\n\n"},{"metadata":{"id":"dq_Tgm9cBA6v","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"3883f6ae-3c04-431a-aed9-04da097396d1","trusted":true},"cell_type":"code","source":"#start = time.time()\n#player_data['PlayLoc'] = np.where((player_data.y >= 17.7) & (player_data.y <= 35.6) ,'Middle' , 'Wide' )\n#print('time',time.time() - start)","execution_count":null,"outputs":[]},{"metadata":{"id":"ZT-90PxL2puu","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"#start = time.time()\n#player_data['PlayLoc'] = np.where((player_data.x < 40) ,'Left' , np.where(player_data.x > 80 , 'Right' ,'Middle') )\n#print('time',time.time() - start)","execution_count":null,"outputs":[]},{"metadata":{"id":"BU78-s48rz-W","colab_type":"text"},"cell_type":"markdown","source":"## **Merge datsets**"},{"metadata":{"id":"CSXXRfH0r815","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"inj_merge = injury_record.merge(play_list , on = 'PlayKey' , how ='left')\n\ninj_merge = inj_merge.merge(player_data , on='PlayKey' , how = 'left')","execution_count":null,"outputs":[]},{"metadata":{"id":"UC4KTnTHpnOR","colab_type":"text"},"cell_type":"markdown","source":"# **EDA**"},{"metadata":{"id":"vZn609KBUteu","colab_type":"text"},"cell_type":"markdown","source":"## **Player movements in injured Plays**"},{"metadata":{"id":"p8wS9FfpVbkN","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":1000},"outputId":"ed79de15-1f2c-4ca9-e4d4-13d23de21141","trusted":true},"cell_type":"code","source":"fig, axes = plt.subplots(2,1)\nfig.set_size_inches(12, 19)\n\nfig.suptitle(' Player movement in injured Plays', fontsize=16)\n\n\n#Injury locations- Natural\nnatural_inj_loc = inj_merge.groupby('Surface').get_group('Natural')\nsns.kdeplot(natural_inj_loc[\"x\"].dropna(), natural_inj_loc[\"y\"].dropna(),ax=axes[0] ,shade = \"True\", color = 'red').set_title('Natural')\n\n#Injury locations- Synthetic\nSynthetic_inj_loc = inj_merge.groupby('Surface').get_group('Synthetic')\n\nsns.kdeplot(Synthetic_inj_loc[\"x\"].dropna(), Synthetic_inj_loc[\"y\"].dropna(),ax=axes[1] ,shade = \"True\", color = 'red').set_title('Synthetic')\n\n#axes[0].xlim(0, 120)\n#axes[0].ylim(0, 53.3)\n\n\n#axes[1].xlim(0, 120)\n#axes[1].ylim(0, 53.3)\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"fV6mU_Vzrmvq","colab_type":"text"},"cell_type":"markdown","source":"## **Number of plays per surface**"},{"metadata":{"id":"jomKpT5v5e0z","colab_type":"code","outputId":"41c82ca4-2662-4972-d621-b598562ec66f","colab":{"base_uri":"https://localhost:8080/","height":67},"trusted":true},"cell_type":"code","source":"play_list.FieldType.value_counts() / play_list.FieldType.value_counts().sum() * 100","execution_count":null,"outputs":[]},{"metadata":{"id":"xuFVWzyYrDT0","colab_type":"code","outputId":"b61e7a3a-e1c8-41d0-cdc3-4f41981506db","colab":{"base_uri":"https://localhost:8080/","height":458},"trusted":true},"cell_type":"code","source":"play_list.FieldType.value_counts().plot(kind = 'bar',figsize = (15,7),rot = 0,sort_columns = True,title = 'Number of Plays per surface').set(xlabel=\"Number of plays per surface\", ylabel=\"Pitch region\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"xNoL72Du4-fZ","colab_type":"text"},"cell_type":"markdown","source":"## **Number of injuries per surface**"},{"metadata":{"id":"nieES9S45Ae2","colab_type":"code","outputId":"bf222424-6ac8-453a-b72b-52b5c4aa600f","colab":{"base_uri":"https://localhost:8080/","height":458},"trusted":true},"cell_type":"code","source":"#Number of injuries per surface\n\ninjury_record.Surface.value_counts().plot(kind ='bar',rot = 0,sort_columns = True,figsize = (15,7),title = 'Number of injuries per Surface').set(xlabel=\"Surface\", ylabel=\"Number of injuries\")\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"UBdSLT4E6qFk","colab_type":"code","outputId":"bae21146-47c3-4638-d3e5-89b9e54db9b1","colab":{"base_uri":"https://localhost:8080/","height":67},"trusted":true},"cell_type":"code","source":"injury_record.Surface.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"vuAF6Mol5XmS","colab_type":"text"},"cell_type":"markdown","source":"### *46,799‬(17.52 %) more plays in natural, but 8 more injuries in synthetic, indicates that its more likely to have an injury on synthetic*"},{"metadata":{"id":"5QYvtWi9wk1_","colab_type":"text"},"cell_type":"markdown","source":"### Number of injuries per body part"},{"metadata":{"id":"3C6WlszewbVI","colab_type":"code","outputId":"e4459f14-fd94-4209-a133-7de737219ff8","colab":{"base_uri":"https://localhost:8080/","height":458},"trusted":true},"cell_type":"code","source":"#Number of injuries per body part\n\ninjury_record.BodyPart.value_counts().plot(kind ='bar',rot = 0,sort_columns = True,figsize = (15,7), title = 'Number of injuries per body part').set(xlabel=\"Body part\", ylabel=\"Number of injuries\")\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"QzZTcRe84YLq","colab_type":"text"},"cell_type":"markdown","source":"## Body part by surface"},{"metadata":{"id":"cNTmD1EZ1TtZ","colab_type":"code","outputId":"8fb686d6-2bbe-43d0-d8ac-4cb6a68be026","colab":{"base_uri":"https://localhost:8080/","height":458},"trusted":true},"cell_type":"code","source":"ax =injury_record.groupby(['Surface','BodyPart']).count().unstack('BodyPart')['PlayerKey'].plot(kind='bar', rot = 0,sort_columns = True,figsize=(15, 7),alpha =0.8, title='Body Part by Surface')\nfor p in ax.patches:\n    ax.annotate(str(p.get_height()), (p.get_x() * 1.0, p.get_height() * 1.015))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"g1KuL0kI8w90","colab_type":"text"},"cell_type":"markdown","source":"### **Ankle** - *8 more in synthetic. Why?*\n### **Knee** - *Although both surface have 24 injuries,17.52% more plays in natural indicate knee injuries are more likely in synthetic as well*\n\n### **Toes** - *5 out of 6 in synthetic!*"},{"metadata":{"id":"TcV7UoseDR72","colab_type":"text"},"cell_type":"markdown","source":"## **Length of injury**:\n\nIts difficult to conclude Length of injury as it requires the context such as\n\n\n*   how injury prone a player is?\n*   previous records\n*   how exactly the injury occured?   \n\n"},{"metadata":{"id":"6T2HahrwCCCI","colab_type":"code","outputId":"5135c247-463c-4d4e-e0a0-31d06c847f85","colab":{"base_uri":"https://localhost:8080/","height":458},"trusted":true},"cell_type":"code","source":"injury_record.groupby('Surface')['InjuryDuration'].value_counts().unstack('InjuryDuration').plot(kind='bar',  rot = 0,sort_columns = True,figsize=(15, 7), alpha =0.8,title='Injury duration per surface').set(ylabel = 'Count')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"sWpwRKx2Rs8r","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"inj_play = injury_record.merge(play_list , on = ['PlayerKey','GameID','PlayKey'],how = 'left')","execution_count":null,"outputs":[]},{"metadata":{"id":"1DGqnhpNSbJN","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"def fill_unkown(game_id,column):\n    \n    if game_id in play_list[play_list[column] !='Unknown'].GameID.values:\n        return play_list[play_list.GameID == game_id][column].values[0]\n    else:\n        return 'Unknown'","execution_count":null,"outputs":[]},{"metadata":{"id":"o2DYmZrdScQE","colab_type":"code","outputId":"835723b6-fe97-4f76-9927-f410ed750f43","colab":{"base_uri":"https://localhost:8080/","height":70},"trusted":true},"cell_type":"code","source":"handle_cols = ['roof','Temperature','Weather','RosterPosition','PlayerDay','PlayerGame','Position', 'PositionGroup']\nfor col in handle_cols:\n    inj_play[col].loc[inj_play[col].isna()] = inj_play[['GameID',col]].loc[inj_play[col].isna()]\\\n                                                                               .apply(lambda x:fill_unkown(x['GameID'],col),axis =1)","execution_count":null,"outputs":[]},{"metadata":{"id":"vYltLO1wTesf","colab_type":"code","outputId":"54213a1b-37f2-4139-891c-faa8733d4eb8","colab":{"base_uri":"https://localhost:8080/","height":1000},"trusted":true},"cell_type":"code","source":"fig,axes = plt.subplots(2,1,figsize = (15,17))\nfig.suptitle('Play type - injury vs non injury Plays', fontsize=16)\n\ninj_play.PlayType.value_counts().sort_values().plot(kind ='barh',ax = axes[0],title = 'Injury').set(xlabel=\"Number of Plays\", ylabel=\"Play type\")\nplay_list[~play_list.PlayKey.isin(injury_record.PlayKey)].PlayType.value_counts().sort_values().plot(kind ='barh',ax = axes[1],title = 'No injury').set(xlabel=\"Number of plays\", ylabel=\"Play Type\")\nplt.subplots_adjust(hspace=0.35)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"eYXz925rAm86","colab_type":"text"},"cell_type":"markdown","source":"## **Players who play at Quarter back and kicker did not get any injury in 2 seasons**"},{"metadata":{"id":"QHdC3Wudwwbq","colab_type":"code","outputId":"22baa493-fdb2-4691-fc9a-646f0cefbe91","colab":{"base_uri":"https://localhost:8080/","height":1000},"trusted":true},"cell_type":"code","source":"fig,axes = plt.subplots(2,1,figsize = (15,17))\nfig.suptitle('Roster position - injury vs non injury Plays', fontsize=16)\n\ninj_play.RosterPosition.value_counts().sort_values().plot(kind ='barh',ax = axes[0],title = 'Injury').set(xlabel=\"Number of Plays\", ylabel=\"Play type\")\nplay_list[~play_list.PlayKey.isin(injury_record.PlayKey)].RosterPosition.value_counts().sort_values().plot(kind ='barh',ax = axes[1],title = 'No injury').set(xlabel=\"Number of plays\", ylabel=\"Play Type\")\nplt.subplots_adjust(hspace=0.35)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"3_gpUMOuAqK8","colab_type":"text"},"cell_type":"markdown","source":"## **Corner back is one of the least injured position in natural while its the 2nd most injured position in synthetic with 4000 less plays**\n\nNatural - 2 injuries out of 16800 plays at cornerback\n\nSynthetic - 11 injuries out of 12187 plays at cornerback"},{"metadata":{"id":"ZCph5Caa5jMz","colab_type":"code","outputId":"c94088e2-4496-4b1b-ba8f-925de63a2a72","colab":{"base_uri":"https://localhost:8080/","height":1000},"trusted":true},"cell_type":"code","source":"fig,axes = plt.subplots(2,1,figsize = (15,17))\nfig.suptitle('Roster position difference between surface - injury vs non injury Plays', fontsize=16)\n\ninj_play.groupby('Surface')['RosterPosition'].value_counts().unstack().plot(kind ='bar',rot =0 ,ax =axes[0],sort_columns = True, title = 'Injured',legend = False)\n\nplay_list[~play_list.PlayKey.isin(injury_record.PlayKey)][~play_list.RosterPosition.isin(['Quarterback','Kicker'])].groupby('FieldType')['RosterPosition'].value_counts().unstack().plot(kind ='bar',sort_columns = True,rot = 0,ax = axes[1],title = 'No injury',legend = False)\nplt.subplots_adjust(hspace=0.35)\nhandles, labels = axes[1].get_legend_handles_labels()\nfig.legend(handles, labels, loc='right')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"1spkMlGe3NrH","colab_type":"text"},"cell_type":"markdown","source":"## **Cornerbacks with 7 ankle injuries in synthetic and none in Natural**"},{"metadata":{"id":"AXsLFHqU8IHz","colab_type":"code","outputId":"3e6efcef-d131-4ef5-9c15-787cc2d27090","colab":{"base_uri":"https://localhost:8080/","height":924},"trusted":true},"cell_type":"code","source":"fig,axes = plt.subplots(2,1,figsize = (20,14))\nfig.suptitle('Roster Position', fontsize=16)\n\ninj_play[(inj_play.Surface == 'Natural') & (inj_play.BodyPart != 'Heel')].groupby('RosterPosition')['BodyPart'].value_counts().unstack().plot(kind ='bar',rot =0 ,ax = axes[0],sort_columns = True, title = 'Natural',legend = False)\ninj_play[(inj_play.Surface == 'Synthetic') & (inj_play.BodyPart != 'Heel')].groupby('RosterPosition')['BodyPart'].value_counts().unstack().plot(kind ='bar',rot =0 ,ax = axes[1],sort_columns = True, title = 'Synthetic',legend = False)\n\nplt.subplots_adjust(hspace=0.35)\nhandles, labels = axes[1].get_legend_handles_labels()\nfig.legend(handles, labels, loc='right')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"40iqOYckaq4I","colab_type":"text"},"cell_type":"markdown","source":"## **Higher the variety in play greater the chance of injury. For example, quarternack only involved in pass and rush , never got any injury**"},{"metadata":{"id":"N1wE3t7SB9-G","colab_type":"code","outputId":"c5f161fa-94cd-4fe5-ce89-3fa216a6b904","colab":{"base_uri":"https://localhost:8080/","height":842},"trusted":true},"cell_type":"code","source":"play_list.groupby('RosterPosition')['PlayType'].value_counts().unstack().plot(kind ='barh',rot =0 ,stacked =True,figsize = (20,14),sort_columns = True, title = 'Synthetic')","execution_count":null,"outputs":[]},{"metadata":{"id":"vmYagHlXE-hR","colab_type":"text"},"cell_type":"markdown","source":"## **Events**"},{"metadata":{"id":"EFLXVVTO9D25","colab_type":"text"},"cell_type":"markdown","source":"## **Cornerbacks in Synthetic are involved higher number of events compared to Natural which supports the logic of 'Higher the variety of Plays and events involved higher the chance of injury'** "},{"metadata":{"id":"2c65uc08IjLY","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":302},"outputId":"2a1292fb-8ba9-47c6-e9d8-5bff03b0de87","trusted":true},"cell_type":"code","source":"inj_merge[(inj_merge.RosterPosition == 'Cornerback') & (inj_merge.Surface == 'Synthetic')]['event'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"NrcwZg5fJHYx","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":134},"outputId":"61efe1a6-bca6-4025-f69f-94fa1d784b21","trusted":true},"cell_type":"code","source":"inj_merge[(inj_merge.RosterPosition == 'Cornerback') & (inj_merge.Surface == 'Natural')]['event'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"zQR_sO4Ab_d8","colab_type":"text"},"cell_type":"markdown","source":"## Weather"},{"metadata":{"id":"gBxmpp9xcBNJ","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":445},"outputId":"623ac747-2418-46e9-9710-d90b21c9e2be","trusted":true},"cell_type":"code","source":"inj_play.Weather.value_counts().plot(kind ='bar' ,rot =0,figsize =(15,7))","execution_count":null,"outputs":[]},{"metadata":{"id":"ZdI7vKcmdbmB","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":445},"outputId":"4a71364f-8773-493e-b96f-8fc3f77b274d","trusted":true},"cell_type":"code","source":"play_list.Weather.value_counts().plot(kind ='bar' ,rot =0,figsize =(15,7))","execution_count":null,"outputs":[]},{"metadata":{"id":"pwSa5h5PcTz2","colab_type":"text"},"cell_type":"markdown","source":"### Clear weather has less number of plays but more injuries"},{"metadata":{"id":"r3J-rEpHciRI","colab_type":"text"},"cell_type":"markdown","source":"### **Corner backs have been anomaly so far.Lets check corner back player movement difference between Synthetic and Natural**"},{"metadata":{"id":"yDTGn2lbHK7Y","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"#Utility functions\n\ndef plt_var(var , label,nat_playkey , syn_playkey):\n  fig,axes = plt.subplots(1,2,figsize = (20,7))\n  fig.suptitle(label, fontsize=16)\n\n\n  sns.distplot(player_data[player_data.PlayKey.isin(nat_playkey)][var] , ax = axes[0]).set_title('Natural')\n\n  axes[0].set(xlabel=label, ylabel='density')\n\n  sns.distplot(player_data[player_data.PlayKey.isin(syn_playkey)][var] , ax = axes[1]).set_title('Synthetic')\n\n  axes[1].set(xlabel=label, ylabel='density')\n\n  plt.subplots_adjust(wspace=0.35)\n  plt.show()\n\ndef plt_std_var(var ,title,label, nat_playkey , syn_playkey):\n  fig,axes = plt.subplots(1,2,figsize = (20,7))\n  fig.suptitle(title, fontsize=16)\n\n\n  sns.distplot(player_data[player_data.PlayKey.isin(nat_playkey)].groupby('PlayKey')[var].std() , ax = axes[0]).set_title('Natural')\n\n  axes[0].set(xlabel=label, ylabel='density')\n\n  sns.distplot(player_data[player_data.PlayKey.isin(syn_playkey)].groupby('PlayKey')[var].std() , ax = axes[1]).set_title('Synthetic')\n\n  axes[1].set(xlabel=label, ylabel='density')\n\n  plt.subplots_adjust(wspace=0.35)\n  plt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"id":"3Nk7oLMRdWNl","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"cb_nat_playkey = play_list[(play_list.RosterPosition == 'Cornerback') & (play_list.FieldType == 'Natural')].PlayKey\ncb_syn_playkey = play_list[(play_list.RosterPosition == 'Cornerback') & (play_list.FieldType == 'Synthetic')].PlayKey","execution_count":null,"outputs":[]},{"metadata":{"id":"QC2eHOcGXw4U","colab_type":"text"},"cell_type":"markdown","source":"## Speed"},{"metadata":{"id":"I-va8GIjXzrD","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":493},"outputId":"8b794396-8afe-4f12-eeaf-5b25790b0ab1","trusted":true},"cell_type":"code","source":"plt_var('s' , 'Speed',cb_nat_playkey , cb_syn_playkey)","execution_count":null,"outputs":[]},{"metadata":{"id":"KurlfgV9AtxH","colab_type":"text"},"cell_type":"markdown","source":"## Variation in speed( Measure using Standard deviation)"},{"metadata":{"id":"Bdumz_LpADVP","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":493},"outputId":"96a4538c-1f0c-41e2-fadb-5e0b9d79a249","trusted":true},"cell_type":"code","source":"plt_std_var('s' , 'Variation in speed','Speed',cb_nat_playkey , cb_syn_playkey)","execution_count":null,"outputs":[]},{"metadata":{"id":"xA8Aa7J7CjoM","colab_type":"text"},"cell_type":"markdown","source":"## *Acceleration*"},{"metadata":{"id":"rVmoZ_tEFSp8","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":493},"outputId":"d4085710-604d-4ed7-e5ae-acaa579e4127","trusted":true},"cell_type":"code","source":"plt_var('a' , 'Acceleration',cb_nat_playkey , cb_syn_playkey)","execution_count":null,"outputs":[]},{"metadata":{"id":"TFfmieB0JOJc","colab_type":"text"},"cell_type":"markdown","source":"## Variation in acceleration"},{"metadata":{"id":"PTJWWu-LJn8p","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":493},"outputId":"88cb1627-810f-4c53-a390-e12657842789","trusted":true},"cell_type":"code","source":"plt_std_var('a' , 'Variation in acceleration','Acceleration',cb_nat_playkey , cb_syn_playkey)","execution_count":null,"outputs":[]},{"metadata":{"id":"VztNPuf4JsGo","colab_type":"text"},"cell_type":"markdown","source":"## Orientation"},{"metadata":{"id":"Q9ecvoU0JvAD","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":493},"outputId":"b0541263-78bb-496f-913b-9cf2c1c01ed0","trusted":true},"cell_type":"code","source":"plt_var('o' , 'Orientation',cb_nat_playkey , cb_syn_playkey)","execution_count":null,"outputs":[]},{"metadata":{"id":"WH_H7mpeJ0sX","colab_type":"text"},"cell_type":"markdown","source":"## Variation in Orientation"},{"metadata":{"id":"EX02fs59J8qL","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":493},"outputId":"c2be37b0-7ccc-4508-cc52-281115a55153","trusted":true},"cell_type":"code","source":"plt_std_var('o' , 'Variation in Orientation','Orientation',cb_nat_playkey , cb_syn_playkey)","execution_count":null,"outputs":[]},{"metadata":{"id":"QxZfBmgAKHHj","colab_type":"text"},"cell_type":"markdown","source":"### There isn't any noticable difference in player movement between Natural and Synthetic for cornerbacks . We could conclude , incase of Cornerback injury difference its the Variation of Play types and events that led to injury"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"NFL - Pitch location based injury analysis.ipynb","provenance":[],"collapsed_sections":[],"machine_shape":"hm"},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":1}