{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n# import plotting\nimport scipy.stats as ss\n\n# import machine learning\nfrom sklearn.model_selection import StratifiedKFold\nfrom imblearn.over_sampling import RandomOverSampler, SMOTE\nimport xgboost as xgb\nfrom sklearn.metrics import accuracy_score, confusion_matrix, cohen_kappa_score\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"","_uuid":"","trusted":true},"cell_type":"code","source":"\ninjury = pd.read_csv(\"../input/nfl-playing-surface-analytics/InjuryRecord.csv\")\nplay = pd.read_csv(\"../input/nfl-playing-surface-analytics/PlayList.csv\")\ntrack = pd.read_csv(\"../input/nfl-playing-surface-analytics/PlayerTrackData.csv\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Injury Record: The injury record file in .csv format contains information on 105 lower-limb injuries that occurred during regular season games over the two seasons. Injuries can be linked to specific records in a player history using the PlayerKey, GameID, and PlayKey fields.\n\n['PlayerKey', 'GameID', 'PlayKey', 'BodyPart', 'Surface', 'DM_M1',\n     'DM_M7', 'DM_M28', 'DM_M42']\nPlay List: – The play list file contains the details for the 267,005 player-plays that make up the dataset. Each play is indexed by PlayerKey, GameID, and PlayKey fields. Details about the game and play include the player’s assigned roster position, stadium type, field type, weather, play type, position for the play, and position group.\n\n['PlayerKey', 'GameID', 'PlayKey', 'RosterPosition', 'PlayerDay',\n     'PlayerGame', 'StadiumType', 'FieldType', 'Temperature', 'Weather',\n     'PlayType', 'PlayerGamePlay', 'Position', 'PositionGroup']\nPlayer Track Data: player level data that describes the location, orientation, speed, and direction of each player during a play recorded at 10 Hz (i.e. 10 observations recorded per second).\n['PlayKey', 'time', 'event', 'x', 'y', 'dir', 'dis', 'o', 's']"},{"metadata":{"trusted":true},"cell_type":"code","source":"injury.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x=\"BodyPart\", data=injury, kind=\"count\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x=\"BodyPart\", data=injury,hue='Surface', kind=\"count\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x=\"Surface\", data=injury)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury['injury_type']=injury[['DM_M1','DM_M7','DM_M28','DM_M42']].sum(axis=1)\ndef change_vals(i):\n    if i==1:\n        return 'Light Injury'\n    elif i==2:\n        return 'Medium Injury'\n    elif i==3:\n        return 'Almost Serious Injury'\n    else:\n        return 'Serious Injury'\ninjury['injury_type']=injury['injury_type'].apply(change_vals)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x=\"Surface\", data=injury,hue='BodyPart', kind=\"count\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x=\"Surface\", data=injury,hue='injury_type', kind=\"count\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"body_part=injury['BodyPart'].unique().tolist()\nfig=plt.figure(figsize=(15,3))\nfor i in range(len(body_part)-1):\n    ax=fig.add_subplot(1,5,i+1)\n    c=injury[(injury['BodyPart']==body_part[i])&(injury['Surface']=='Synthetic')]['injury_type'].value_counts(normalize=True)*100\n    c_lst=c.tolist()\n    ax.bar(c.index,c_lst,width=0.4)\n    ax.set_title('{} in Synthetic'.format(body_part[i]))\n    ax.set_ylabel('')\n    xlabels=[i for i in c.index]\n    ax.set_xticklabels(xlabels, rotation=90)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury.BodyPart.value_counts()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury.DM_M1.value_counts()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x=\"DM_M1\", hue='BodyPart', data=injury, kind=\"count\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury.DM_M7.value_counts()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x=\"DM_M7\", hue='BodyPart', data=injury[(injury.DM_M7==1)], kind=\"count\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#                 how='left')\nInjury_games_play = injury.merge(play,\n                  on='PlayKey',\n                  how='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_games_play.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_games_play['Weather'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_games_play.isnull().sum()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_games_play = Injury_games_play[pd.notnull(Injury_games_play['PlayKey'])]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_games_play.tail()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_games_play.columns\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x=\"BodyPart\", hue='StadiumType', data=Injury_games_play, kind=\"count\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_games_play=Injury_games_play.dropna()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data=Injury_games_play[['BodyPart', 'Surface', 'RosterPosition', 'PlayerDay', 'PlayerGame', 'StadiumType', 'FieldType',\n       'Temperature', 'Weather', 'PlayType', 'PlayerGamePlay', 'Position',\n       'PositionGroup']]\n\nax = sns.pairplot(data, hue='BodyPart', kind=\"reg\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**now for player data**"},{"metadata":{"trusted":true},"cell_type":"code","source":"chart=sns.catplot(x=\"RosterPosition\", data=play, kind=\"count\")\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"chart=sns.catplot(x=\"PlayType\", data=play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play = injury.merge(play,\n                  on='PlayKey',\n                  how='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play['Weather'].unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play['Weather'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# def merge(data):\n#     cold=0\n#     warm=0\n#     for i in   injury_games_play['Weather'].tolist():\n#         if 'cloud' in i:\n#             cold+=1\n#         else:\n#             warm+=1\n# print(merge(injury_games_play))\ninjury_games_play['Weather'].fillna('Cloudy',inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# def merge(data):\n#     for i in data.Weather.values:\n#         if 'Cloud' in ''.join(i):\n#             data['weather_type']='cold'\n#         else:\n#             data['weather_type']='warm'\n# merge(injury_games_play)  \n# injury_games_play['weather_type'].value_counts()\n# injury_games_play.Weather.values\nfrom collections import Counter\ncold=0;warm=0\nfor i in list(Counter(injury_games_play.Weather.values).keys()):\n    if 'cloud' in i.replace('','').lower():\n        cold+=Counter(injury_games_play.Weather.values)[i]\n    else:\n        warm+=Counter(injury_games_play.Weather.values)[i]\ncold,warm        \n    \n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"players tend to get hurt 61 times whenevr word 'cloud' is mentioned in weather and 44 times when its not . so this observation agrees with the scientific fact that the colder or cloudy the weather the more muscles are in contracted state and hence the risk of getting hurt is comparatively high."},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x=\"BodyPart\", hue='Weather', data=injury_games_play, kind=\"count\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"definitely higher injuries in cold weather "},{"metadata":{"trusted":true},"cell_type":"code","source":"chart = sns.catplot(x=\"injury_type\", hue='Weather', data=injury_games_play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"more evidence towards higher risk of injury in cold weather .."},{"metadata":{"trusted":true},"cell_type":"code","source":"chart = sns.catplot(x=\"injury_type\", hue='Surface', data=injury_games_play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"more serious injuries on synthetic turf . now lets examine the position data "},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play.Position.value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"according to wikipeida following are the position in the football :-\nOffensive (interior) line\n\nThe offensive line (on left, in orange jerseys) consists of a center (with ball in hand ready to snap) with two guards on either side, and two tackles.\nThe offensive line is primarily responsible for blocking. During normal play, offensive linemen do not handle the ball (aside from the snap from center), unless the ball is fumbled by a ball carrier, a pass is deflected, or when a player who is normally an offensive lineman takes a different position on the field. The offensive line consists of:\n**\nCenter (C)**\nThe center is the player who begins the play from scrimmage by snapping the ball to the quarterback. As the name suggests, the center usually plays in the middle of the offensive line, though some teams may employ an unbalanced line where the center is offset to one side. Like all offensive linemen, the center has the responsibility to block defensive players. The center often also has the responsibility to call out blocking assignments and make last second adjustments depending on the defensive alignment.\n**Offensive guard (OG)**\nTwo guards line up directly on either side of the center. Like all interior linemen, their function is to block on both running and passing plays. On some plays, rather than blocking straight ahead, a guard will \"pull\", whereby the guard comes out of their position in line to lead block for a ball carrier, on plays known as \"traps\" (for inside runs), \"sweeps\" (for outside runs), and \"screens\" (for passing plays). In such cases, the guard is referred to as a \"pulling guard\".\n\nThe six backs and receivers are those that line up outside or behind the offensive line. There are four main positions in this set of players:\n\n**Wide receiver (WR)**\n\nA wide receiver (No. 87, in white) begins a play in the flanker position\nWide receivers are pass-catching specialists. Their main job is to run pass routes and get open for passes, although they are occasionally called on to block. Wide receivers generally line up split \"wide\" near the sidelines at the start of the play. Wide receivers, like running backs, come in different varieties depending on exactly where they line up. A wide receiver who is directly on the line of scrimmage is called a \"split end\", and is counted among the seven required players on the line of scrimmage. A wide receiver who lines up behind the line (and thus counts as one of the four backs) is called the \"flanker\". A wide receiver who lines up between the outermost wide receiver and the offensive line is said to be \"in the slot\" and is called the \"slot receiver\".\n\n**Tight end (TE)**\nTight ends play on either side of, and directly next to, the tackles. Tight ends are considered hybrid players, something between a wide receiver and an offensive lineman. Because they play next to the other offensive linemen, they are frequently called on to block, especially on running plays. However, because they are eligible receivers, they may also catch passes. The position known as the \"H-back\" is a tight end who lines up behind the line of scrimmage, and is thus counted as one of the four \"backs\", but otherwise their role is similar to that of other tight ends.\nDepending on the style of offense the coaches have designed, the game situation, and the relative skill sets of the players, teams may run formations which contain any number of running backs, wide receivers, and tight ends, so long as the mandated \"four backs and seven on the line\" rule is followed. For many years, the standard set consisted of the quarterback, two running backs (a tailback/halfback and a fullback), two wide receivers (a flanker and a split end) and a tight end. Modern teams show a wide variety of formations, from a \"full house\" formation with three running backs, two tight ends, and no wide receivers, to \"spread\" formations featuring four or five wide receivers, sometimes without any running backs. The \"I formation\" is one of the most common.\n\nDefense\nThe defensive team or defense is the team that begins a play from scrimmage not in possession of the ball. The objective of the defensive team is to prevent the other team from scoring. The defense accomplishes this by forcing the offense to turn the ball over, either by preventing them from achieving a first down and forcing a punt, forcing the offense to fumble or throw an interception, or more rarely, forcing a turnover on downs.\n\nUnlike the offensive team, the rules do not restrict the defensive team into certain positions. A defensive player may line up anywhere on his side of the line of scrimmage and perform any legal action. Over time, however, defensive roles have become defined into three main sets of players, and several individual positions.\n\nDefensive line\n\nThe four defensive linemen (in red) have their hands on the ground in a \"three point stance\"\nLike their offensive counterparts, defensive linemen (also called rushers) line up directly on the line of scrimmage, close to the ball. There are two positions usually considered part of the defensive line:\n\n**Defensive tackle (DT);**\nSometimes called a \"defensive guard\", defensive tackles play at the center of the defensive line. Their function is to rush the passer and stop running plays directed at the middle of the line of scrimmage. The most interior defensive tackle who sometimes lines up directly across from the ball (and therefore is almost nose-to-nose with the offense's center) is often called a \"nose tackle\", alternately \"nose guard\" or \"middle guard\". The nose tackle is most common in the 3-4 defense. Most defensive sets have one or two defensive tackles. If one employs a second defensive tackle, sometimes called an \"under tackle\", they are usually a bit faster than the nose tackle.\nDefensive end (DE)\nThe two defensive ends play next to the defensive tackles, at the edges of the defensive line. Their function is to attack the passer or stop offensive runs to the outer edges of the line of scrimmage (most often referred to as \"containment\"). The faster of the two is usually placed on the right side of the defensive line (quarterback's left) because that is a right-handed quarterback's blind side.\nOften, a defensive lineman will have their hands on the ground, in a three- or four-point stance before the ball is snapped; this distinguishes their pre-snap stance from a linebacker, who begins in a two-point stance (i.e. without a hand touching the ground).\n\nLinebackers\n\nThis defense (in white) is in a base 4-3 set. Just behind the four defensive linemen (whose hands are on the ground) are three linebackers (Nos. 55, 3 and 16), and further back are two safeties (numbers 24 and 44).\nLinebackers play behind the defensive line and perform various duties depending on the situation, including rushing the passer, covering receivers, and defending against the run.\n\n**Middle linebacker (MLB)**\nSometimes called the \"inside linebacker\" (especially in a 3-4 defense), and known colloquially as the \"Mike\" linebacker, the middle linebacker is often known as the \"quarterback of the defense\", as they are frequently the primary defensive play callers and must react to a wide variety of situations. Middle linebackers must be capable of stopping running backs who make it past the defensive line, covering pass plays over the middle, and rushing the quarterback on blitz plays.\n**Outside linebacker (OLB)**\nOutside linebackers are given different names depending on their role and the philosophy of the team. Some teams keep their outside linebackers on the same side of the field at all times. Some teams define them by their role; as playing either \"strongside\" (SLB) or \"weakside\" (WLB). The strongside, or \"Sam\", linebacker lines up on the same side as the offensive tight end and often is responsible for covering the tight end or running back on pass plays. The weakside, or \"Will\", linebacker lines up on the side of the offensive line without a tight end, and is often used to rush, or blitz the quarterback, or may need to cover a running back on pass plays.\nDefensive backs\n\nCornerback Tory James gets a read on the offense just prior to the start of play\nDefensive backs, also known as the \"secondary\", play either behind the linebackers or set to the outside, near the sidelines. Defensive backs are primarily used to defend against pass plays. Defensive backs also act as the last line of defense on running plays and need to be able to make open field tackles, especially when the ball carrier has gotten past the other defenders. A normal complement of defensive backs includes two cornerbacks and two safeties, though specialty defensive backs (nickelbacks and dime backs) can be brought in in place of linebackers and defensive linemen, when there is a need to cover additional pass receivers.\n\n**Cornerback (CB)**\nCornerbacks attempt to prevent successful passes by either swatting the airborne ball away from the receiver or by catching the pass themselves. In rushing situations, their job is to contain the runner, either by directing them back to the middle of the field to be tackled, by tackling them themselves or by forcing them out of bounds.\nSafety (S)\nThe safeties are the last line of defense (farthest from the line of scrimmage) and usually help the corners with deep-pass coverage. The strong safety (SS) is usually the larger and stronger of the two, providing extra protection against run plays by standing closer to the line of scrimmage, usually on the strong (tight end) side of the field. The free safety (FS) is usually the smaller and faster of the two, and is usually the deepest player on the defense, providing help on long pass plays.\nNickelback and dimeback\nIn certain formations, the defense may remove a linebacker or a defensive lineman to bring in extra pass coverage in the form of extra defensive backs. A formation with five defensive backs is often called a \"nickel\" formation, and the fifth (extra) defensive back is called a \"nickelback\" after the U.S. nickel coin, a five-cent piece. By extension, a formation with a sixth defensive back (dimeback) is called a \"dime package\", a 10-cent dime coin being two nickels (nickelbacks). Rarely, a team may employ seven or eight defensive backs on certain plays.\nDefensive formations are often known by a numerical code indicating the number of players at each position. The two most common formations are the 3–4 defense and the 4–3 defense, where the first number refers to the number of defensive linemen, and the second number refers to the number of linebackers (the number of defensive backs can be inferred, since there must be eleven players on the field). Thus, a 3–4 defense consists of three defensive linemen (usually a nose tackle and two defensive ends), four linebackers, and four defensive backs (two cornerbacks, a strong safety, and a free safety)"},{"metadata":{},"cell_type":"markdown","source":"**players who have more to do with the ball passing and catching have a higher chance of getting hurt than the players who's job is to defend . interesting ! **"},{"metadata":{"trusted":true},"cell_type":"code","source":"chart = sns.catplot(x=\"Position\", hue='Surface', data=injury_games_play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"chart = sns.catplot(x=\"Position\", hue='injury_type', data=injury_games_play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play = Injury_games_play[pd.notnull(Injury_games_play['PlayKey'])]\nsns.catplot(x=\"BodyPart\", hue='StadiumType', data=Injury_games_play, kind=\"count\")\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"now lets try to examine the PlayType and its impact on the injury.."},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play.PlayType.unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_games_play.PlayType.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"chart = sns.catplot(x=\"PlayType\", hue='injury_type', data=injury_games_play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"seems like players are most likely to get injured during passing and rush . interersting .."},{"metadata":{"trusted":true},"cell_type":"code","source":"chart = sns.catplot(x=\"PlayType\", hue='BodyPart', data=injury_games_play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"right off the bat we can see that the chance for the foot injury is really less . But while doing punt,pass n kicoff the knee is most likely to get injured only in the case of rush the ankle injury is high ."},{"metadata":{"trusted":true},"cell_type":"code","source":"chart = sns.catplot(x=\"DM_M1\", hue='injury_type', data=injury_games_play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"we can see that when player comes back after one holiday the chances for the medium injury is the highest , this is intuitive since longer the ring rust more likely it is that the player gets injured ."},{"metadata":{"trusted":true},"cell_type":"code","source":"chart = sns.catplot(x=\"DM_M7\", hue='injury_type', data=injury_games_play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"chart = sns.catplot(x=\"DM_M28\", hue='injury_type', data=injury_games_play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"chart = sns.catplot(x=\"DM_M42\", hue='injury_type', data=injury_games_play, kind=\"count\")\n\nchart.set_xticklabels(rotation=45)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"in all the cases when the players absense was more than 7 days the chance of serious injury has increased significantly .."},{"metadata":{"trusted":true},"cell_type":"code","source":"data=injury_games_play[['BodyPart', 'Surface', 'RosterPosition', 'PlayerDay', 'PlayerGame', 'StadiumType', 'FieldType',\n       'Temperature', 'Weather', 'PlayType', 'PlayerGamePlay', 'Position',\n       'PositionGroup']]\n\nax = sns.pairplot(data, hue='BodyPart', kind=\"reg\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.heatmap(injury.corr())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.heatmap(injury_games_play.corr())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"track.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"track['event'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# injury_games_play_track = injury_games_play.merge(track,\n#                   on='PlayKey',\n#                   how='left')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create a dataframe with game-level information\ngame_df = play[['GameID', 'StadiumType', 'FieldType', 'Weather', 'Temperature']].drop_duplicates().reset_index().drop(columns=['index'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def clean_weather(row):\n    cloudy = ['Cloudy 50% change of rain', 'Hazy', 'Cloudy.', 'Overcast', 'Mostly Cloudy',\n          'Cloudy, fog started developing in 2nd quarter', 'Partly Cloudy',\n          'Mostly cloudy', 'Rain Chance 40%',' Partly cloudy', 'Party Cloudy',\n          'Rain likely, temps in low 40s', 'Partly Clouidy', 'Cloudy, 50% change of rain','Mostly Coudy', '10% Chance of Rain',\n          'Cloudy, chance of rain', '30% Chance of Rain', 'Cloudy, light snow accumulating 1-3\"',\n          'cloudy', 'Coudy', 'Cloudy with periods of rain, thunder possible. Winds shifting to WNW, 10-20 mph.',\n         'Cloudy fog started developing in 2nd quarter', 'Cloudy light snow accumulating 1-3\"',\n         'Cloudywith periods of rain, thunder possible. Winds shifting to WNW, 10-20 mph.',\n         'Cloudy 50% change of rain', 'Cloudy and cold',\n       'Cloudy and Cool', 'Partly cloudy']\n    \n    clear = ['Clear, Windy',' Clear to Cloudy', 'Clear, highs to upper 80s',\n             'Clear and clear','Partly sunny',\n             'Clear, Windy', 'Clear skies', 'Sunny', 'Partly Sunny', 'Mostly Sunny', 'Clear Skies',\n             'Sunny Skies', 'Partly clear', 'Fair', 'Sunny, highs to upper 80s', 'Sun & clouds', 'Mostly sunny','Sunny, Windy',\n             'Mostly Sunny Skies', 'Clear and Sunny', 'Clear and sunny','Clear to Partly Cloudy', 'Clear Skies',\n            'Clear and cold', 'Clear and warm', 'Clear and Cool', 'Sunny and cold', 'Sunny and warm', 'Sunny and clear']\n    \n    rainy = ['Rainy', 'Scattered Showers', 'Showers', 'Cloudy Rain', 'Light Rain', 'Rain shower', 'Rain likely, temps in low 40s.', 'Cloudy, Rain']\n    \n    snow = ['Heavy lake effect snow']\n    \n    indoor = ['Controlled Climate', 'Indoors', 'N/A Indoor', 'N/A (Indoors)']\n        \n    if row.Weather in cloudy:\n        return 'Cloudy'\n    \n    if row.Weather in indoor:\n        return 'Indoor'\n    \n    if row.Weather in clear:\n        return 'Clear'\n    \n    if row.Weather in rainy:\n        return 'Rain'\n    \n    if row.Weather in snow:\n        return 'Snow'\n      \n    if row.Weather in ['Cloudy.', 'Heat Index 95', 'Cold']:\n        return np.nan\n    \n    return row.Weather\n\ndef clean_stadiumtype(row):\n    if row.StadiumType in ['Bowl', 'Heinz Field', 'Cloudy']:\n        return np.nan\n    else:\n        return row.StadiumType\n\ndef clean_play_df(play_df):\n    play_df_cleaned = play_df.copy()\n    \n    # clean StadiumType\n    play_df_cleaned['StadiumType'] = play_df_cleaned['StadiumType'].str.replace(r'Oudoor|Outdoors|Ourdoor|Outddors|Outdor|Outside', 'Outdoor')\n    play_df_cleaned['StadiumType'] = play_df_cleaned['StadiumType'].str.replace(r'Indoors|Indoor, Roof Closed|Indoor, Open Roof', 'Indoor')\n    play_df_cleaned['StadiumType'] = play_df_cleaned['StadiumType'].str.replace(r'Closed Dome|Domed, closed|Domed, Open|Domed, open|Dome, closed|Domed', 'Dome')\n    play_df_cleaned['StadiumType'] = play_df_cleaned['StadiumType'].str.replace(r'Retr. Roof-Closed|Outdoor Retr Roof-Open|Retr. Roof - Closed|Retr. Roof-Open|Retr. Roof - Open|Retr. Roof Closed', 'Retractable Roof')\n    play_df_cleaned['StadiumType'] = play_df_cleaned.apply(lambda row: clean_stadiumtype(row), axis=1)\n    \n    # clean Weather\n    play_df_cleaned['Weather'] = play_df_cleaned.apply(lambda row: clean_weather(row), axis=1)\n    \n    return play_df_cleaned","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"play_df_cleaned = clean_play_df(play)\ngame_df_cleaned = play_df_cleaned[['GameID', 'StadiumType', 'FieldType', 'Weather', 'Temperature']].drop_duplicates().reset_index().drop(columns=['index'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"player_data_df = play_df_cleaned[['PlayerKey', 'RosterPosition', 'PlayerGamePlay', 'Position', 'PositionGroup']]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax=sns.countplot(x=\"RosterPosition\", data=player_data_df)\nax.set_xticklabels(ax.get_xticklabels(),rotation=30)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax=sns.countplot(x=\"PositionGroup\", data=player_data_df)\nax.set_xticklabels(ax.get_xticklabels(),rotation=30)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax=sns.countplot(x=\"Position\", data=player_data_df)\nax.set_xticklabels(ax.get_xticklabels(),rotation=30)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax=sns.countplot(x=\"PlayerGamePlay\", data=player_data_df)\nax.set_xticklabels(ax.get_xticklabels(),rotation=60)\n\nplt.gcf().set_size_inches(16, 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def visualize_play(play_df_cleaned):\n    plt.style.use('ggplot')\n    fig, ax = plt.subplots(1,1,figsize=(15,5))\n    \n    plt.bar(play_df_cleaned.PlayType.value_counts().keys().values, play_df_cleaned.PlayType.value_counts().values)\n    plt.xticks(range(len(play_df_cleaned.PlayType.value_counts().keys().values)), play_df_cleaned.PlayType.value_counts().keys().values, rotation=20)\n    add_value_labels(ax, spacing=5)\n    plt.title('Play-Level Exploration: PlayType', fontsize=16)\n    \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"visualize_play(play_df_cleaned)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def visualize_field_heatmap(player_df, xbins=13, ybins=6, annotate = False):\n    # create a grid\n    x = np.linspace(0, 120, xbins)\n    y = np.linspace(0, 53, ybins)\n    \n    # initialize heatmap\n    hmap = np.zeros((xbins,ybins))\n    \n    for i in range(xbins-1):\n        for j in range(ybins-1):\n            hmap[i,j] = len(player_df[(player_df.x >= x[i]) & (player_df.x <= x[i+1]) & (player_df.y >= y[j]) & (player_df.y <= y[j+1])])\n            \n    fig = plt.figure(figsize=(10,5))\n    ax = sns.heatmap(np.transpose(hmap), annot=annotate, fmt = '.0f')\n    plt.title('Field Heatmap \\n the most visited areas of the field are highlighted')\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"visualize_field_heatmap(track)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# joined cleaned games dataset and injury dataset\ngame_injury_df = injury.set_index('GameID').join(game_df_cleaned.set_index('GameID'), how = 'outer')\n\n# fill null values for the injury columns with zeros\ngame_injury_df['DM_M1'] = game_injury_df['DM_M1'].fillna(0).astype(int)\ngame_injury_df['DM_M7'] = game_injury_df['DM_M7'].fillna(0).astype(int)\ngame_injury_df['DM_M28'] = game_injury_df['DM_M28'].fillna(0).astype(int)\ngame_injury_df['DM_M42'] = game_injury_df['DM_M42'].fillna(0).astype(int)\n\ngame_injury_df.DM_M1 = game_injury_df.DM_M1 - game_injury_df.DM_M7\ngame_injury_df.DM_M7 = game_injury_df.DM_M7 - game_injury_df.DM_M28\ngame_injury_df.DM_M28 = game_injury_df.DM_M28 - game_injury_df.DM_M42\n\n# introduce a column with a flag indicating an injury\ngame_injury_df['Injury'] = game_injury_df['DM_M1'] + game_injury_df['DM_M7'] + game_injury_df['DM_M28'] + game_injury_df['DM_M42']\n\n# drop duplicated surface column\ngame_injury_df = game_injury_df.drop(columns=['Surface'])\n\n# drop play-level features just for now\ngame_injury_df = game_injury_df.drop(columns=['PlayerKey', 'PlayKey'])\n\n# create dummy variables\ngame_injury_df_dummies = pd.get_dummies(game_injury_df, dummy_na = True, drop_first = True).drop(columns=['FieldType_nan'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"corr_df = game_injury_df_dummies[['Temperature', 'StadiumType_Indoor', 'StadiumType_Open', 'StadiumType_Outdoor', 'StadiumType_Retractable Roof',\n                                 'FieldType_Synthetic', 'Weather_Cloudy', 'Weather_Rain', 'Weather_Snow', 'Injury']].corr()\n\nfig = plt.figure(figsize=(10,7))\nsns.heatmap(corr_df, annot=True, cmap=sns.diverging_palette(220, 20, as_cmap=True))\nplt.title('Correlation Heatmap')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(10,5))\nplt.boxplot([game_injury_df[game_injury_df.Injury == 0].Temperature.values, game_injury_df[game_injury_df.Injury == 1].Temperature.values], vert = False)\nplt.title('Temperature Distribution')\nplt.yticks([1,2], ['No Injury', 'Injury'])\nplt.xlim(0,100)\nplt.xlabel('Temperature')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def player_games_timeline(player_key, play_df, injury_df):\n    '''\n    Function to plot the player's timeline\n    '''\n    player_games = play_df[play_df.PlayerKey == player_key][['GameID', 'PlayKey', 'PlayerDay', 'PlayerGame']]\n    \n    # plot timeline for the player\n    plt.figure(figsize=(20,5))\n    plt.title('Player Games Timeline \\n PlayerKey: ' + str(player_key))  \n    plt.plot(player_games.PlayerDay.unique(), np.zeros(len(player_games.PlayerDay.unique())), color='#00c2c7')\n    plt.scatter(player_games.PlayerDay.unique(), np.zeros(len(player_games.PlayerDay.unique())), s=100, color='#00c2c7', label='games')\n    \n    # add games with injury\n    injured_players = injury_df.PlayerKey.unique()\n    if player_key in injured_players:\n        injury_games = injury_df[injury_df.PlayerKey == player_key].GameID.values\n        injury_days = player_games[player_games.GameID.isin(injury_games)].PlayerDay.unique()\n        \n        plt.scatter(injury_days, np.zeros(len(injury_days)), s=100, color='#e01e5a', label='injury')\n    \n    plt.legend()\n    plt.xlabel('days')\n    plt.yticks([])\n    plt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"player_games_timeline(26624, play, injury)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# joined cleaned games dataset and injury dataset\nplay_injury_df = injury.dropna(subset=['PlayKey']).set_index('PlayKey').join(play_df_cleaned.set_index('PlayKey'), how = 'outer', lsuffix='_left', rsuffix='_right')\n\n# fill null values for the injury columns with zeros\nplay_injury_df['DM_M1'] = play_injury_df['DM_M1'].fillna(0).astype(int)\nplay_injury_df['DM_M7'] = play_injury_df['DM_M7'].fillna(0).astype(int)\nplay_injury_df['DM_M28'] = play_injury_df['DM_M28'].fillna(0).astype(int)\nplay_injury_df['DM_M42'] = play_injury_df['DM_M42'].fillna(0).astype(int)\n\n# introduce a column with a flag indicating an injury\nplay_injury_df.DM_M1 = play_injury_df.DM_M1 - play_injury_df.DM_M7\nplay_injury_df.DM_M7 = play_injury_df.DM_M7 - play_injury_df.DM_M28\nplay_injury_df.DM_M28 = play_injury_df.DM_M28 - play_injury_df.DM_M42\n\nplay_injury_df['Injury'] = play_injury_df['DM_M1'] + play_injury_df['DM_M7'] + play_injury_df['DM_M28']+ play_injury_df['DM_M42']\n\n# drop duplicated surface column\nplay_injury_df = play_injury_df.drop(columns=['Surface'])\n\n# create dummy variables\nplay_injury_df_dummies = pd.get_dummies(play_injury_df, columns = ['PlayType', 'PositionGroup'], dummy_na = True, drop_first = True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# reset index \nfeatures_df = play_injury_df.copy().reset_index()\n# drop the irrelevant columns\nfeatures_df = features_df.drop(columns=['PlayerKey_left','GameID_left','BodyPart', 'PlayKey', 'PlayerKey_right', 'GameID_right', 'DM_M1', 'DM_M7', 'DM_M28', 'DM_M42'])\n# convert into dummies\nfeatures_df = pd.get_dummies(features_df, dummy_na = False, drop_first = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# split into X and y\ny = features_df['Injury']\nX = features_df.drop(columns=['Injury'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"skf = StratifiedKFold(n_splits=2)\n\nfor train_index, test_index in skf.split(X, y):\n    X_train, X_test = X.values[train_index, :], X.values[test_index, :]\n    y_train, y_test = y[train_index], y[test_index]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\nmodel=RandomForestClassifier()\n                      \nmodel.fit(X_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"since the dataset is small ,classifier will always predict no injury so accuracy won't be a good parameter to judge model performnce so we will use auc roc and then feature importances..."},{"metadata":{"trusted":true},"cell_type":"code","source":"y_prob = model.predict_proba(X_test)[:,1] # This will give you positive class prediction probabilities  \ny_pred = np.where(y_prob > 0.5, 1, 0) # This will threshold the probabilities to give class predictions.\nmodel.score(X_test, y_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import roc_curve, auc\nfalse_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_prob)\nroc_auc = auc(false_positive_rate, true_positive_rate)\nroc_auc","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# extract the feature importances from XGBoost model\nfeature_importances = model.feature_importances_\nfeature_importances = pd.DataFrame(feature_importances, index = X.columns).reset_index().rename(columns={'index':'feature', 0:'importance'}).sort_values(by=['importance'], ascending=False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# plot as a bar chart\nplt.figure(figsize=(20,7))\nplt.bar(range(len(feature_importances)), feature_importances.importance.values)\nplt.xticks(range(len(feature_importances)), feature_importances.feature.values, rotation=90)\n\nplt.title('Feature importances')\nplt.xlabel('features')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Motion Analysis"},{"metadata":{"trusted":true},"cell_type":"code","source":"dfs=[track,injury_games_play]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from functools import reduce\ndf_final = reduce(lambda left,right: pd.merge(left,right,on='PlayKey'), dfs)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final.s.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = sns.distplot(df_final['s'])\nax.set_xticklabels(ax.get_xticklabels(),rotation=30)\n\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final.groupby('PlayKey')[['s']].max() \\\n    .sort_values('s', ascending=False) \\\n    .query('s != 0').head(10) \\\n    .plot(kind='barh', figsize=(15, 5), title='Top 10 Plays by Max Player Speed')\nplt.show()\n\ndf_final.groupby('PlayKey')[['s']].max() \\\n    .sort_values('s', ascending=True) \\\n    .query('s != 0').head(10) \\\n    .plot(kind='barh', figsize=(15, 5), title='Bottom 10 Plays by Min Player Speed')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax=sns.scatterplot(x=\"Temperature\", y=\"s\", data=df_final)\nax.set_xticklabels(ax.get_xticklabels(),rotation=30)\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax=sns.scatterplot(x=\"Temperature\", y=\"s\", hue = 'injury_type',data=df_final)\nax.set_xticklabels(ax.get_xticklabels(),rotation=30)\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from scipy.stats.stats import pearsonr\n\nax=sns.jointplot(x=\"Temperature\", y=\"s\", data=df_final,stat_func=pearsonr);\n# ax.set_xticklabels(ax.get_xticklabels(),rotation=30)\nplt.gcf().set_size_inches(16, 8)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"pearson corelation suggests there is indeed a corelation between speed and temperature"},{"metadata":{},"cell_type":"markdown","source":"references :- https://www.kaggle.com/aleksandradeis/nfl-injury-analysis,\n                https://www.kaggle.com/chandraroy/nfl-analytics"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}