{"cells":[{"metadata":{"_uuid":"54c04a6ea87ee3ea2ffeae72f9bad8908b05ac72"},"cell_type":"markdown","source":"** NFL Data Analysis**\n\nThe **National Football League (NFL)** is a professional American football league consisting of 32 teams, divided equally between the National Football Conference (NFC) and the American Football Conference (AFC). The NFL is one of the four major professional sports leagues in North America, and the highest professional level of American football in the world.\n\n![NFL](https://cdn1.thecomeback.com/wp-content/uploads/sites/94/2018/08/10497682-832x447.jpg)\n\nThe NFL was formed in 1920 as the American Professional Football Association (APFA) before renaming itself the National Football League for the 1922 season. The NFL agreed to merge with the American Football League (AFL) in 1966, and the first Super Bowl was held at the end of that season; the merger was completed in 1970. Today, the NFL has the highest average attendance (67,591) of any professional sports league in the world and is the most popular sports league in the United States. The Super Bowl is among the biggest club sporting events in the world[6] and individual Super Bowl games account for many of the most watched television programs in American history, all occupying the Nielsen's Top 5 tally of the all-time most watched U.S. television broadcasts by 2015. The NFL's executive officer is the commissioner, who has broad authority in governing the league. The players in the league belong to the National Football League Players Association.\n\nThe team with the most NFL championships is the Green Bay Packers with thirteen (nine NFL titles before the Super Bowl era, and four Super Bowl championships afterwards); the team with the most Super Bowl championships is the Pittsburgh Steelers with six. The current NFL champions are the Philadelphia Eagles, who defeated the New England Patriots in Super Bowl LII, their first Super Bowl championship after winning three NFL titles before the Super Bowl era.\n\n\n**NFL Concussion**\n\nA concussion is a type of traumatic brain injury caused by a blow to the head. Reports show an increasing number of retired NFL players who have suffered concussions have developed memory and cognitive issues such as dementia, Alzheimer's, depression and chronic traumatic encephalopathy (CTE).\n\nIn September 2015, researchers with the Department of Veterans Affairs and Boston University announced that they had identified CTE in 96 percent of NFL players that they had examined and in 79 percent of all football players.\n\nNFL players suffered more concussions in 2017 than in each of the previous five years, according to data released Friday by the league. There were 281 reported concussions this season, including head injuries suffered in preseason games and practices.\n\nYou can learn more  [here](https://edition.cnn.com/2013/08/30/us/nfl-concussions-fast-facts/index.html).\n\n![](https://www.playsmartplaysafe.com/wp-content/uploads/2018/06/checklist-june-2018-final1-791x1024.png)\n\n![](https://www.playsmartplaysafe.com/wp-content/uploads/2018/06/9.8.18-concussion-protocol-1024x640.png)\n\nImprovements to the Concussion Protocol\n\nFor the 2018 season, the Head, Neck and Spine Committee made additional improvements to the Concussion Protocol:\n\n* Added a third UNC who will monitor the broadcast video and audio feeds of each game from the spotters’ booth, and notify on-field UNCs of possible head, neck or spine injuries.\n* Defined impact seizure and fencing responses as independent signs of potential loss of consciousness, representing “No-Go” criteria under the current Protocol. Players who display either of these signs at any time shall be removed from play and may not return to the game.\n* Required an evaluation for all players demonstrating gross motor instability (e.g., stumbling or falling to the ground when trying to stand) to determine the cause of the instability. If the team physician, in consultation with the sideline UNC, determines the instability to be neurologically caused, the player is designated a “No-Go” and may not return to play.\n* Officials, teammates, and coaching staffs have been instructed to take an injured player directly to a member of the medical team for appropriate evaluation, including a concussion assessment, if warranted.\n* Required all players who undergo any concussion evaluation on game day to have a follow-up evaluation conducted the following day by a member of the medical staff.\n\n\n**2018 Rule Changes**\n\n* 4-8-2 | Eliminates the requirement that a team who scores a touchdown at the end of regulation of a game to kick the extra point or go for two-point conversion if it would not affect the outcome of the game.\n* 6-1-3, 6-2-1 | Modifies rules for a free kick formation and for blocking on a free kick. \n* 8-1-3 | Changes standard for a catch.\n* 11-6-3 | Makes permanent the Playing Rule that changes the spot of the next snap after a touchback resulting from a free kick to the 25-yard line. \n* 12-2-8 | Makes lowering the head to initiate contact with the helmet a foul.\n* 12-5-1 | Makes the penalties for Illegal Batting and Kicking the same.\n* 15-2-2 | Authorizes the designated member of the Officiating department to instruct on-field game officials to disqualify a player for a flagrant non-football act when a foul for that act is called on the field.\n* 16-1-3 | Provides that in overtime, if the team that possesses the ball first scores a field goal on its initial possession and the second team loses possession by an interception or fumble, the down will be permitted to run to its conclusion, including awarding points scored by either team during the down\n\nIn the first section we will try to analyze the data and in the second section will propose the rules change.\n\n\n"},{"metadata":{"_uuid":"ece29730dc15fb97dc5680fc51c4169dcb3225cb"},"cell_type":"markdown","source":"**Exploring the Data**\n\nData exploration is the most important step in Data Analytics. So in the first section we explore the datasets and try to establish a coorelation between them such that the data makes more sense. \n\nLets get high level view of the data,\n![](https://storage.googleapis.com/kaggle-media/competitions/NFL%20player%20safety%20analytics/key_variables.jpg)\n\nWe will start with video review as it contains the concussion data."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":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)\n\nimport os\nprint(os.listdir(\"../input\"))\n\nfrom plotly import tools\nimport plotly.plotly as py\nfrom plotly.offline import init_notebook_mode, iplot\ninit_notebook_mode(connected=True)\nimport plotly.graph_objs as go\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport tqdm\nimport gc\nimport feather\n\n\nvideo_review = pd.read_csv('../input/video_review.csv')\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e6a8af07eb234484bf2f019aae4fb8576c97a14","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"game_review = pd.read_csv('../input/game_data.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0ca920b8152ac6c826033e387a5f1af6e5ac7d8e"},"cell_type":"markdown","source":"**Analysis of Concussions**"},{"metadata":{"trusted":true,"_uuid":"ebf641176c835d9700d5e14ec0e1ae2a481da9f1","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"player_role = pd.read_csv('../input/play_player_role_data.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a177e9f2e78452125b424e3b4c5a77916fd1dbe7","_kg_hide-input":true},"cell_type":"code","source":"# Player activity during primary injury causing event\ntemp = video_review[\"Player_Activity_Derived\"].value_counts()\nfig = {\n  \"data\": [\n    {\n      \"values\": temp.values,\n      \"labels\": temp.index,\n      \"domain\": {\"x\": [0, 1]},\n      \"hole\": .6,\n      \"type\": \"pie\"\n    },\n    \n    ],\n  \"layout\": {\n        \"title\":\"Player-Activity during primary injury causing event\",\n        \"annotations\": [\n            {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Player activity\",\n                \"x\": 0.5,\n                \"y\": 0.5\n            }\n            \n        ]\n    }\n}\niplot(fig, filename='donut')\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f1e832eaa225d3b8a429bdfd3adc9314114160a8"},"cell_type":"markdown","source":"From the above graph its quite evident that most of the player had concussion while Tackling or after getting Blocked.  "},{"metadata":{"trusted":true,"_uuid":"ae7987c84dcf04670609ec34ed44ca233cada751","_kg_hide-input":true},"cell_type":"code","source":"temp = video_review[\"Primary_Impact_Type\"].value_counts()\nfig = {\n  \"data\": [\n    {\n      \"values\": temp.values,\n      \"labels\": temp.index,\n      \"domain\": {\"x\": [0, 1]},\n      \"hole\": .6,\n      \"type\": \"pie\"\n    },\n    \n    ],\n  \"layout\": {\n        \"title\":\"Impacting source that caused the concussion\",\n        \"annotations\": [\n            {\n                \"font\": {\n                    \"size\": 17\n                },\n                \"showarrow\": False,\n                \"text\": \"Primary Impact type\",\n                \"x\": 0.5,\n                \"y\": 0.5\n            }\n            \n        ]\n    }\n}\niplot(fig, filename='donut')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6f0e14ead47a4f8d85c4f166bb16c3ab193f240f"},"cell_type":"markdown","source":"Most of these injuries were caused due Helmet-to-Body or Helmet-to-Helmet Contacts. These were the results of Helmet to Player high speed clashes that occurred during tackles or blocks."},{"metadata":{"trusted":true,"_uuid":"e7c05db07a16a0b75e5ccb4838d17308302fdbce","_kg_hide-input":true},"cell_type":"code","source":"temp = video_review[\"Primary_Partner_Activity_Derived\"].value_counts()\nfig = {\n  \"data\": [\n    {\n      \"values\": temp.values,\n      \"labels\": temp.index,\n      \"domain\": {\"x\": [0, 1]},\n      \"hole\": .6,\n      \"type\": \"pie\"\n    },\n    \n    ],\n  \"layout\": {\n        \"title\":\"Primary Partner Activity that caused the concussion\",\n        \"annotations\": [\n            {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Primary Partner Activity type\",\n                \"x\": 0.5,\n                \"y\": 0.5\n            }\n            \n        ]\n    }\n}\niplot(fig, filename='donut')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"69935e4d76f7ddc705c736d9fa723f0d50c31bd5"},"cell_type":"markdown","source":"From the above graph its quite evident that most of the player had concussion while the Partner was Tackling or after getting Blocked.  "},{"metadata":{"trusted":true,"_uuid":"5b2e234559e45d4f25f390d3348bce62c83bd792","_kg_hide-input":true},"cell_type":"code","source":"fig = plt.figure(figsize = (20,10))\nax1 = fig.add_subplot(2,2,1)\nax2 = fig.add_subplot(2,2,2)\nax3 = fig.add_subplot(2,2,3)\nax4 = fig.add_subplot(2,2,4)\ndf = pd.DataFrame(data=video_review)\nsns.heatmap(pd.crosstab(df.Player_Activity_Derived, df.Primary_Partner_Activity_Derived), annot=True, square=True, ax=ax1)\nsns.heatmap(pd.crosstab(df.Player_Activity_Derived, df.Primary_Impact_Type), annot=True, square=True, ax=ax2)\nsns.heatmap(pd.crosstab(df.Player_Activity_Derived, df.Friendly_Fire), annot=True, square=True, ax=ax3)\nsns.heatmap(pd.crosstab(df.Primary_Impact_Type, df.Friendly_Fire), annot=True, square=True, ax=ax4)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"60ff6b570db86b9e40aca54f440693344efc57d9"},"cell_type":"markdown","source":"**Analysis from the above data**\n\n* While player has blocked already the opponent and their primary partner still is trying to do the blocking its lead to 5(15%) of the concussions. .\n* While player is trying to tackle the opponent and their primary partner has already tackled it lead to 8(22%) of the concussions.\n* While player is trying to block the opponent and their primary partner has already blocked lead to 8 (22%) of the concussions.\n* While player has tackled the opponent and their primary partner is still tackling lead to 6 of the concussions.\n* Helmet to body contact while tackling to 8 (22%) of injuries.\n* Helmet to helmet contact while blocked to 4 (11%) of injuries.\n* Most of the injuries were non-friendly fires.\n* Most of the non-friendly injuries were Helmet-to-Helmet (43%)"},{"metadata":{"trusted":true,"_uuid":"a836f116706a18577fdb3f244dfc5ef0b56fa085","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"merged_csv = pd.merge (video_review,player_role)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"b120f3e67dbbb6250f040d2d9ce578456022de89"},"cell_type":"code","source":"player_punt_data = pd.read_csv('../input/player_punt_data.csv')\nplayer_punt_data = player_punt_data.sort_values('GSISID', ascending=False).drop_duplicates('GSISID').sort_index()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"553d084965e35a3c3c028f48ce315a5e77a3ab47"},"cell_type":"code","source":"merged_csv = pd.merge (merged_csv ,player_punt_data[['GSISID','Position']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"162f1099e948b071655d53780c9e97742d248546","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"del video_review,player_role,player_punt_data\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3fff22588af93860d56046cfc9f8f3a84aaa4b8a"},"cell_type":"markdown","source":"**Analysis of Player Roles and Positions**\n\nNow we will explore the **Player Role **  and  **Player Punt Data** to analyze which players receive more concussions. The NFL positions are explained as follows,\n![](http://www.sportsfy.com/football-positions.jpg)\n\n**Offensive Positions: ** \n\n* Center (C): The offensive player who snaps the ball to start a play. This player also blocks for the runner on a running play and protects the quarterback on a passing play.\n* Offensive Guard (OG): An offensive lineman who lines up next to the center. An offensive guard's role is to block for the runner on running plays and protect the quarterback on passing plays.\n* Offensive Tackle (OT): An offensive lineman who lines up outside of the offensive guard. An offensive tackle's role is to block for the runner on running plays and protect the quarterback on passing plays.\n* Wide Receiver (WR): An offensive player who often lines up away from the line of scrimmage, and whose primary responsibility is to catch the football on a passing play. On running plays, a wide receiver often blocks for the runner.\n* Tight End (TE): A tight end often lines up at the end of the offensive line. On a running play, the tight end blocks for the runner. On a passing play, the tight end often runs out and becomes a pass target.\n* Quarterback (QB): The quarterback usually lines up behind center and receives the snap. On running plays, the quarterback hands off to the runner. On passing plays, the quarterback throws the pass to a receiver. The quarterback may also gain yards by running.\n* Fullback (FB): A fullback lines up between the quarterback and the halfback, and his primary role is to back for the halfback in a running play and to protect the quarterback on a passing play.\n* Halfback (HB): Also called the tailback. A halfback is usually smaller and quicker than the fullback, and is the primary ball carrier on running plays. On passing plays, a halfback often becomes a passing target.\n* Running Back (RB) – Also known as the Halfback. This player does it all. Lining up either behind or beside the quarterback, he runs, he catches, he blocks and he’ll even throw a pass from time to time. A running back is normally a player who is a quick runner and thrives on contact.\n\n**Defensive Positions:**\n\n* Nose Tackle (NT): Also called a nose guard. A nose tackle is a defensive lineman who lines up directly across the opponent's center, commonly seen in a 3-4 defense. A nose tackle's primary role is to stop the run.\n* Defensive End (DE): A defensive lineman who lines up at the end of the defensive line. The primary role of the defensive end is to rush the quarterback.\n* Cornerback (CB): A defensive player whose primary responsibility is to defend against the pass.\n* Inside Linebacker (ILB): A defensive player who lines up behind the defensive line and has responsibility to stop plays that take place near the middle of the field.\n* Outside Linebacker (OLB): A linebacker who lines up behind the defensive line and has responsibility to stop plays that goes outside of the tackles. Outside linebackers sometimes rush the quarterback.\n* Strong Safety (SS): A defensive player who lines up on the strong side of the offense (the side where the tight end lines up), usually behind the linebackers. A strong safety supports the run defense on running plays and helps cornerbacks to defend the pass on passing plays.\n* Free Safety (FS): A defensive player who lines up behind the linebackers and the cornerbacks. A free safety should be a sure tackler as he is usually the last line of defense, and is expected to help the cornerback on deep passes.\n* Middle Linebacker (MLB): A linebacker who plays in the middle of the field behind the defensive line. Middle linebacker is a position in a 4-3 defense (not pictured). A middle linebacker is often the \"quarterback\" on defense, as he is the one who calls out defensive plays.\n* Defensive Tackle (DT): A defensive lineman who lines up on the inside of the defensive line in a 4-3 defense. The primary role of the defensive tackle is to stop the run."},{"metadata":{"trusted":true,"_uuid":"24e0074adc30318b6201904fd43496a95f54db2f","_kg_hide-input":true},"cell_type":"code","source":"temp = merged_csv[\"Role\"].value_counts()\ntempe = merged_csv[\"Position\"].value_counts()\nfig = {\n    \"data\": [\n    {\n      \"values\": temp.values,\n      \"labels\": temp.index,\n      \"domain\": {\"x\": [0, .48]},\n      \"hole\": .4,\n      \"type\": \"pie\"\n    },\n      {\n      \"values\": tempe.values,\n      \"labels\": tempe.index,\n      \"domain\": {\"x\": [.52, 1]},\n      \"hole\": .4,\n      \"type\": \"pie\"\n    },\n    \n    \n    ],\n  \"layout\": {\n        \"title\":\"Player Roles and Punt Positions\",\n        \"annotations\": [\n            {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Punt Role\",\n                 \"x\": 0.19,\n                \"y\": 0.5\n            },\n              {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Position\",\n                \"x\": 0.80,\n                \"y\": 0.5\n            }\n            \n        ]\n    }\n}\niplot(fig, filename='donut')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0467fe82fc49ff0832eb192a23b22af8b0ee1b86"},"cell_type":"markdown","source":"Most of the Players who were injured were Punt Returners, PLG and GL (Punt Role wise) and Tight End,Wide Receiver,Inside Line Breaker(Position Wise).\n\nA punt returner (PR) has the job of catching the ball after it is punted and to give his team good field position (or a touchdown if possible) by returning it. Before catching the punted ball, the returner must assess the situation on the field while the ball is still in the air.Punt returners sometimes also return kickoffs and usually play other positions, especially wide receiver, cornerback and running back, although sometimes as backups.\n\nTight End (TE) duties include blocking for both the quarterback and the running backs, but he can also run into the field and catch passes. Tight ends can catch like a receiver, but has the strength and size to dominate on the line.\n\nInside linebackers (ILB) are usually responsible for shadowing RBs, TEs and sometimes WRs; rushing the passer; and tackling ball carriers. Those who are linebackers are likely strong and fast."},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"0690087a032332fc9223f7d2707e6c35e0ac5742"},"cell_type":"code","source":"#temp = video_review[\"Primary_Partner_Activity_Derived\"].value_counts()\ntemp = merged_csv.groupby(['Role','Position']).size()\nfig = {\n  \"data\": [\n    {\n      \"values\": temp.values,\n      \"labels\": temp.index,\n      \"domain\": {\"x\": [0, 1]},\n      \"hole\": .6,\n      \"type\": \"pie\"\n    },\n    \n    ],\n  \"layout\": {\n        \"title\":\"Player Punt Role and Position Combination\",\n        \"annotations\": [\n            {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Position and Punt Role Combined\",\n                \"x\": 0.5,\n                \"y\": 0.5\n            }\n            \n        ]\n    }\n}\niplot(fig, filename='donut')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"01727e967f03658daeb8ce26842ded92ef962151"},"cell_type":"markdown","source":"This plot shows the coorelation between the Punt Roles and Player Positions."},{"metadata":{"trusted":true,"_uuid":"d2066ee56f1392089a1d9bfec783f6d52eeecd3f","_kg_hide-input":true,"_kg_hide-output":false,"scrolled":false},"cell_type":"code","source":"final_csv = pd.merge (merged_csv,game_review)\nplay_information = pd.read_csv('../input/play_information.csv')\nfinal_csv = pd.merge (final_csv ,play_information[['GameKey','Quarter','PlayID','Poss_Team','Score_Home_Visiting']],on=['GameKey','PlayID'],how='left')\nfinal_csv['home_poss'] = np.where(final_csv['HomeTeamCode'] == final_csv['Poss_Team'], 'Yes', 'No')  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"029968bc56ac45e9657fa6105d09c928962e050c","_kg_hide-input":true},"cell_type":"code","source":"score_away = []\nscore_home = []\nhome_win_loss = []\n\nfor item in final_csv['Score_Home_Visiting']:\n    scores = item.split('-')\n    temp =  int(scores[1])\n    temp2 = int(scores[0])\n    if(temp<temp2):\n        temp3 = \"Winning\"\n    elif(temp==temp2):\n        temp3 = \"Draw\"\n    else:\n        temp3 = \"Losing\"\n    score_away.append(temp)\n    score_home.append(temp2)\n    home_win_loss.append(temp3)\n\nfinal_csv['home_score'] = score_home  \nfinal_csv['visit_score'] = score_away\nfinal_csv['home_win_loss'] = home_win_loss","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62cd46e9864edab11acabeaad7f7244185a6c4c9","_kg_hide-input":true},"cell_type":"code","source":"poss_win_loss = []\nfor row in final_csv.iterrows():\n    items = row[1]\n    if items['Poss_Team'] == items['HomeTeamCode']:\n        if items['home_score'] < items['visit_score']:\n            poss_win_loss.append('Losing')\n        if items['home_score'] > items['visit_score']:\n            poss_win_loss.append('Winning')\n                \n    elif items['Poss_Team'] == items['VisitTeamCode']:\n        if items['visit_score'] < items['home_score']:\n            poss_win_loss.append('Losing')\n        if items['visit_score'] > items['home_score']:\n            poss_win_loss.append('Winning')\n                \n    if items['home_score'] == items['visit_score']:\n        poss_win_loss.append('Draw')\n    \nfinal_csv['Poss_Team_Status'] = poss_win_loss","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"759289eebef38f66197bad4171df157fd34dc0e0"},"cell_type":"markdown","source":"**Analysis of Game (Game Status and Timing)**\n\nIn this section we will analyze the Game Status and Game Types."},{"metadata":{"trusted":true,"_uuid":"a115ea96306b1757b733366c085c3cc6a8a7bbc7","_kg_hide-input":true},"cell_type":"code","source":"temp = final_csv[\"home_win_loss\"].value_counts()\ntempe = final_csv[\"Poss_Team_Status\"].value_counts()\nfig = {\n    \"data\": [\n    {\n      \"values\": temp.values,\n      \"labels\": temp.index,\n      \"domain\": {\"x\": [0, .48]},\n      \"hole\": .4,\n      \"type\": \"pie\"\n    },\n      {\n      \"values\": tempe.values,\n      \"labels\": tempe.index,\n      \"domain\": {\"x\": [.52, 1]},\n      \"hole\": .4,\n      \"type\": \"pie\"\n    },\n    \n    \n    ],\n  \"layout\": {\n        \"title\":\"Game Status during the time of Concussion\",\n        \"annotations\": [\n            {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Home Team Status\",\n                 \"x\": 0.145,\n                \"y\": 0.5\n            },\n              {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Poss Team Status\",\n                \"x\": 0.858,\n                \"y\": 0.5\n            }\n            \n        ]\n    }\n}\niplot(fig, filename='donut')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3c6cf0098e7f31b60a13b56bfdd1dc3f32427c5d"},"cell_type":"markdown","source":"Most of the concussions (59% approx) occured when the Possession Team was either Losing or Drawing. "},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"0a428f6dabccc1175a98e09560b82c764389a770","_kg_hide-input":true},"cell_type":"code","source":"temp = final_csv[\"Season_Type\"].value_counts()\ntempe = final_csv[\"Quarter\"].value_counts()\nfig = {\n  \"data\": [\n    {\n      \"values\": temp.values,\n      \"labels\": temp.index,\n      \"domain\": {\"x\": [0, .48]},\n      \"hole\": .6,\n      \"type\": \"pie\"\n    },\n      {\n      \"values\": tempe.values,\n      \"labels\": tempe.index,\n      \"domain\": {\"x\": [.52, 1]},\n      \"hole\": .4,\n      \"type\": \"pie\"\n    },\n    \n    ],\n  \"layout\": {\n        \"title\":\"Season and Game Quarter for Concussions\",\n        \"annotations\": [\n            {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Season\",\n                 \"x\": 0.170,\n                \"y\": 0.5\n            },\n              {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Game Quarter\",\n                \"x\": 0.828,\n                \"y\": 0.5\n            }\n            \n        ]\n    }\n}\niplot(fig, filename='donut')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"50a9ba8bf27eceb71f8f6533004ddbfc99a79ac9"},"cell_type":"markdown","source":"32.4 % concussion occurred in the Pre-season games while around 67.6% occurred in Regular Games. And NO concussions occured during the Post Season Games.\n\nWhile in a game, Quarter 3 experienced most number of concussions followed Quarter 2."},{"metadata":{"trusted":true,"_uuid":"51be651cf11d692849ab1380d6a2040e603aa7ce","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"del merged_csv,game_review,play_information\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"18d2c4dabe929e0d38a7ff20cd2648d5134e4df8"},"cell_type":"markdown","source":"**Analysis of Natural Conditions ( Turf , Weather and Temperature)**"},{"metadata":{"trusted":true,"_uuid":"fa6b4fdb3db7ea987cd631e3b685f2fc46bc14ef","_kg_hide-input":true},"cell_type":"code","source":"# Game Wise Analysis of Injuries \n\n# Turf Analysis \n\ntemp = final_csv[\"Turf\"].value_counts()\nfig = {\n  \"data\": [\n    {\n      \"values\": temp.values,\n      \"labels\": temp.index,\n      \"domain\": {\"x\": [0, 1]},\n      \"hole\": .6,\n      \"type\": \"pie\"\n    },\n    \n    ],\n  \"layout\": {\n        \"title\":\"In Which Turf most injuries occured ? \",\n        \"annotations\": [\n            {\n                \"font\": {\n                    \"size\": 17\n                },\n                \"showarrow\": False,\n                \"text\": \"Turf\",\n                \"x\": 0.5,\n                \"y\": 0.5\n            }\n            \n        ]\n    }\n}\niplot(fig, filename='donut')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"34d896f02ea3886a4f0e0d13399b4781b9bbd251"},"cell_type":"markdown","source":"Looks like, most of the injuries tend to occur on Natural Grass or Grass turfs. May be the Grass makes the players slip more and fumble , which cause most of the injuriies."},{"metadata":{"trusted":true,"_uuid":"857400dd2163234f9caae725877cba63c0fd1286","_kg_hide-input":true},"cell_type":"code","source":"# Game Wise Analysis of Injuries \n\n# Weather Analysi\ntemp = final_csv[\"OutdoorWeather\"].value_counts()\ntempe = final_csv[\"GameWeather\"].value_counts()\nfig = {\n  \"data\": [\n    {\n      \"values\": temp.values,\n      \"labels\": temp.index,\n      \"domain\": {\"x\": [0, .48]},\n      \"hole\": .4,\n      \"type\": \"pie\"\n    },\n      {\n      \"values\": tempe.values,\n      \"labels\": tempe.index,\n      \"domain\": {\"x\": [.52, 1]},\n      \"hole\": .4,\n      \"type\": \"pie\"\n    },\n    \n    \n    ],\n  \"layout\": {\n        \"title\":\"How was the weather during the Match ?\",\n        \"annotations\": [\n            {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Outdoor Weather\",\n                 \"x\": 0.135,\n                \"y\": 0.5\n            },\n              {\n                \"font\": {\n                    \"size\": 12\n                },\n                \"showarrow\": False,\n                \"text\": \"Game Weather\",\n                \"x\": 0.85,\n                \"y\": 0.5\n            }\n            \n        ]\n    }\n}\niplot(fig, filename='donut')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6e882b40f6348b1f97446402b7a799356937aa75"},"cell_type":"markdown","source":"Weather has nothing to do with concussions."},{"metadata":{"trusted":true,"_uuid":"c7bad820e9719b0b310a95ae4e1020eaf501d59c","_kg_hide-input":true},"cell_type":"code","source":"trace = {\"x\": final_csv[\"Stadium\"], \n          \"y\": final_csv[\"Temperature\"], \n          \"marker\": {\"size\": 12}, \n          \"mode\": \"markers\",  \n          \"type\": \"scatter\"\n}\n\n\ndata = [trace]\nlayout = {\"title\": \"Games and Temperatures\", \n          \"xaxis\": {\"title\": \"Stadiums\", }, \n          \"yaxis\": {\"title\": \"Temperature (in Farenheit)\"}}\n\nfig = go.Figure(data=data, layout=layout)\niplot(fig, filename='basic_dot-plot')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bb32b18b60dd9a6cfe178b4d2b9af89b4f1ad2bf"},"cell_type":"markdown","source":"So there is not much coorelation between temperature and concussions."},{"metadata":{"_uuid":"7e729b8644f11731907bc9c2ff6b846800a5511e"},"cell_type":"markdown","source":"**Home and Away Teams Analysis**\n\nLets visualize some co- relation between the Home and Away Teams,"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"0da0a87755b78062b0ac62f9475ab89e48ba376c"},"cell_type":"code","source":"trace0 = go.Scatter(\n    x=final_csv.Home_Team,\n    y=final_csv.Player_Activity_Derived,\n    mode='markers',\n    marker = dict(\n          color = 'rgb(17, 157, 255)',\n          size = 20,\n          line = dict(\n            color = 'rgb(231, 99, 250)',\n            width = 2\n          ))\n)\nlayout = dict(title='Home Team activity leading to concussion '\n)\nfig = go.Figure(data=[trace0], layout=layout)\niplot(fig, filename='bubblechart-color')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"428c4f4a47ef8b1db4e21136c1439a45822a7730"},"cell_type":"code","source":"trace0 = go.Scatter(\n    x=final_csv.Visit_Team,\n    y=final_csv.Player_Activity_Derived,\n    mode='markers',\n    marker = dict(\n          color = 'rgb(17, 157, 255)',\n          size = 20,\n          line = dict(\n            color = 'rgb(231, 99, 250)',\n            width = 2\n          ))\n)\nlayout = dict(\n            title='Visit Team activity leading to concussion '\n)\nfig = go.Figure(data=[trace0], layout=layout)\niplot(fig, filename='bubblechart-color')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"1c520125d9ff5544200e8d2a719f2cb21a119de7"},"cell_type":"code","source":"x = final_csv.Home_Team\ny = final_csv.Visit_Team\n\ndata = [\n    go.Histogram2d(\n        x=x,\n        y=y\n    )\n]\nlayout = dict(\n            title='Home Team vs Away Team and Concussions ',\n            xaxis=dict(\n                    title='Home Team'\n                        ),\n            yaxis=dict(\n                    title='Away Team',\n                        )\n)\nfig = go.Figure(data=data, layout=layout)\niplot(fig)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"83fe729a28e28b380fb54f43a52dc997d55c30fc"},"cell_type":"code","source":"del temp,tempe,x,y\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"18d8fada8619c30acdbe6918f3d12745912bd55e"},"cell_type":"markdown","source":"In this section, will make use of **NGS Data** to visualize the injuries in depth. NGS Data contains the each and every player movements during the play."},{"metadata":{"trusted":true,"_uuid":"f1eef62c019a425c94787e9054aa77aed97524a9","_kg_hide-input":true},"cell_type":"code","source":"def calculate_speeds(df, dt=None, SI=False):\n    data_selected = df[['Time', 'x','y']]\n    if SI==True:\n        data_selected.x = data_selected.x / 1.0936132983\n        data_selected.y = data_selected.y / 1.0936132983\n    # Might have used shift pd function ?\n    data_selected_diff = data_selected.diff()\n    if dt==None:\n        # Time is now a timedelta and need to be converted\n        data_selected_diff.Time = data_selected_diff.Time.apply(lambda x: (x.total_seconds()))\n        data_selected_diff['Speed'] = (data_selected_diff.x **2 + data_selected_diff.y **2).astype(np.float64).apply(np.sqrt) / data_selected_diff.Time\n    else:\n        # Need to be sure about the time step...\n        data_selected_diff['Speed'] = (data_selected_diff.x **2 + data_selected_diff.y **2).astype(np.float64).apply(np.sqrt) / dt\n    #data_selected_diff.rename(columns={'Time':'TimeDelta'}, inplace=True)\n    #return data_selected_diff\n    df['TimeDelta'] = data_selected_diff.Time\n    df['Speed'] = data_selected_diff.Speed\n    return df[1:]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"62ce606b5f06963e09ffe485f2e929868d8c0f88"},"cell_type":"markdown","source":"In the phase phase will only consider data from 2016 , will combine all the data from 2016, combine them , calcualte speeds and visualize. We ignore the NGS data from Post Season as there were no concussions in that period."},{"metadata":{"trusted":true,"_uuid":"d7e49ca73041e418401374fb844d1e98b724f1ce","_kg_hide-input":true},"cell_type":"code","source":"\ndtypes = {'Season_Year': 'int16',\n         'GameKey': 'int16',\n         'PlayID': 'int16',\n         'GSISID': 'float32',\n         'Time': 'str',\n         'x': 'float32',\n         'y': 'float32',\n         'dis': 'float32',\n         'o': 'float32',\n         'dir': 'float32',\n         'Event': 'str'}\n\ncol_names = list(dtypes.keys())\n\ndf_list = []\n\nbuffer = ['NGS-2017-pre.csv',\n             'NGS-2017-reg-wk1-6.csv',\n             'NGS-2017-reg-wk7-12.csv',\n             'NGS-2017-reg-wk13-17.csv',\n             'NGS-2017-post.csv']\nngs_files = ['NGS-2016-pre.csv',\n             'NGS-2016-reg-wk1-6.csv',\n             'NGS-2016-reg-wk7-12.csv','NGS-2016-reg-wk13-17.csv']\n\nfor i in tqdm.tqdm(ngs_files):\n    df = pd.read_csv(f'../input/'+i, usecols=col_names,dtype=dtypes)\n    date_format = '%Y-%m-%d %H:%M:%S.%f'\n    sortBy = ['Season_Year', 'GameKey', 'PlayID', 'GSISID', 'Time']\n    df.Time = pd.to_datetime(df.Time, format =date_format)\n    df.sort_values(sortBy, inplace=True)\n    df = calculate_speeds(df, SI=True)\n    df_list.append(df)\n    del df\n    gc.collect()\n\nngs = pd.concat(df_list)\n\ndel df_list\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c9343167078bc5b3b1c92c9998e04dd53515c1f","_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"#Converting everything to meters and speed to KMPH\nngs['x'] = ngs['x']/1.0936\nngs['y'] = ngs['y']/1.0936\nngs['dis'] = ngs['dis']/1.0936\nngs['Speed'] = ngs['Speed']* 3.6","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"1f8f99973f6f57c1d96b8aca8809318aa2856086","_kg_hide-input":true,"_kg_hide-output":false},"cell_type":"code","source":"ngs = ngs[ngs.replace([np.inf, -np.inf], np.nan).notnull().all(axis=1)] ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e89e270936b0c8504a35e017a696262444ffd498","_kg_hide-input":true},"cell_type":"code","source":"def remove_wrong_values(df, tested_columns=['Season_Year', 'GameKey', 'PlayID', 'GSISID', 'TimeDelta'], cutspeed=None):\n    dump = df.copy()\n    colums = dump.columns\n    mask = []\n    for col in tested_columns:\n        dump['shift_'+col] = dump[col].shift(-1)\n        mask.append(\"( dump['shift_\"+col+\"'] == dump['\"+col+\"'])\")\n    mask =eval(\" & \".join(mask))\n    # Keep results where next rows is equally space\n    dump = dump[mask]\n    dump = dump[colums]\n    if cutspeed!=None:\n        dump = dump[dump.Speed < cutspeed]\n    return dump","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00b630bd7ca47f3d5636743d7fe69b0d4b3c7a6d","_kg_hide-input":true},"cell_type":"code","source":"cut_speed=44 # World record 9,857232 m/s for NFL\nngs = remove_wrong_values(ngs, cutspeed=cut_speed)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ccd94bdcef4778bc0674760a72a2f6ce3ba621f","_kg_hide-input":true},"cell_type":"code","source":"video_review = pd.read_csv('../input/video_review.csv')\nfinal = pd.merge(final_csv,ngs,on=['Season_Year','GameKey','PlayID','GSISID'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"241616443d6ad995364c80095eb5fde9c19f5b29","_kg_hide-input":true},"cell_type":"code","source":"def load_layout():\n    \"\"\"\n    Returns a dict for a Football themed Plot.ly layout \n    \"\"\"\n    layout = dict(\n        title = \"Player Pitch Activity\",\n        plot_bgcolor='darkseagreen',\n        showlegend=True,\n        xaxis=dict(\n            autorange=False,\n            range=[0, 120],\n            showgrid=False,\n            zeroline=False,\n            showline=True,\n            linecolor='black',\n            linewidth=1,\n            mirror=True,\n            ticks='',\n            tickmode='array',\n            tickvals=[10,20, 30, 40, 50, 60, 70, 80, 90, 100, 110],\n            ticktext=['Goal', 10, 20, 30, 40, 50, 40, 30, 20, 10, 'Goal'],\n            showticklabels=True\n        ),\n        yaxis=dict(\n            title='',\n            autorange=False,\n            range=[-3.3,56.3],\n            showgrid=False,\n            zeroline=False,\n            showline=True,\n            linecolor='black',\n            linewidth=1,\n            mirror=True,\n            ticks='',\n            showticklabels=False\n        ),\n        shapes=[\n            dict(\n                type='line',\n                layer='below',\n                x0=0,\n                y0=0,\n                x1=120,\n                y1=0,\n                line=dict(\n                    color='white',\n                    width=2\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=0,\n                y0=53.3,\n                x1=120,\n                y1=53.3,\n                line=dict(\n                    color='white',\n                    width=2\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=10,\n                y0=0,\n                x1=10,\n                y1=53.3,\n                line=dict(\n                    color='white',\n                    width=10\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=20,\n                y0=0,\n                x1=20,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=30,\n                y0=0,\n                x1=30,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=40,\n                y0=0,\n                x1=40,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=50,\n                y0=0,\n                x1=50,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=60,\n                y0=0,\n                x1=60,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),dict(\n                type='line',\n                layer='below',\n                x0=70,\n                y0=0,\n                x1=70,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),dict(\n                type='line',\n                layer='below',\n                x0=80,\n                y0=0,\n                x1=80,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=90,\n                y0=0,\n                x1=90,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),dict(\n                type='line',\n                layer='below',\n                x0=100,\n                y0=0,\n                x1=100,\n                y1=53.3,\n                line=dict(\n                    color='white'\n                )\n            ),\n            dict(\n                type='line',\n                layer='below',\n                x0=110,\n                y0=0,\n                x1=110,\n                y1=53.3,\n                line=dict(\n                    color='white',\n                    width=10\n                )\n            )\n        ]\n    )\n    return layout","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c2909809d568fdbe2db068ca195432287f01666","_kg_hide-input":true},"cell_type":"code","source":"def plot_play(game_df, PlayID, player1=None, player2=None, custom_layout=False):\n    \"\"\"\n    Plots player movements on the field for a given game, play, and two players\n    \"\"\"\n    game_df = game_df[game_df.PlayID==PlayID]\n    finale = final[final.PlayID==PlayID]\n\n    GameKey=str(pd.unique(game_df.GameKey)[0])\n    traces=[]   \n    listb = []\n    \n    list1= list(game_df[game_df.GSISID==player1].Event)\n    list2= list(game_df[game_df.GSISID==player1].Speed)\n    lista = [\"Event: \"+str(list1[i]) +\" + Speed: \"+ str(list2[i]) for i in range(len(list1))]\n    if not lista:\n        lista.append(\"None\")\n    \n    list3= list(game_df[game_df.GSISID==player2].Event)\n    list4= list(game_df[game_df.GSISID==player2].Speed)\n    try:\n        listb = [\"Event: \"+str(list3[i]) +\" + Speed: \"+ str(list4[i]) for i in range(len(list2))]\n    except:\n        listb.append(\"None\")\n        \n    trace0 = go.Scatter(\n                x = game_df[game_df.GSISID==player1].x,\n                y = game_df[game_df.GSISID==player1].y,\n                name='Primary GSISID '+str(player1),\n                mode = 'lines+markers',\n                text = lista,\n                line = dict(width = 6,smoothing=1.1),\n                marker=dict(\n                size=12,\n                line = dict(\n                color= 'rgb(0,0,0)',\n                width = 1),\n                color = game_df[game_df.GSISID==player1].Speed, #set color equal to a variable\n                colorscale='Viridis',\n                colorbar=dict(\n                title='Primary Speed'\n                ),\n                showscale=True\n    )\n            )\n    trace1 = go.Scatter(\n                x = game_df[game_df.GSISID==player2].x,\n                y = game_df[game_df.GSISID==player2].y,\n                name='Partner GSISID '+str(player2),\n                text = listb,\n                line = dict(\n                width = 5),\n                mode = 'lines+markers',\n                marker=dict(\n                size=10,\n                line = dict(\n                color= 'rgb(0,0,0)',\n                width = 1),\n                color = game_df[game_df.GSISID==player2].Speed, #set color equal to a variable\n                colorscale='Portland',\n                colorbar=dict(title='Partner Speed', x =-0.14),\n                showscale= True\n                )\n            )\n    \n    layout = load_layout()\n    layout['title'] = 'Player Activity (Concussion) in GameKey ' + GameKey + ' : ' + str(pd.unique(finale.Home_Team)[0]) +' v/s ' + str(pd.unique(finale.Visit_Team)[0])\n    layout['legend'] = dict(orientation=\"h\")\n    data = [trace0,trace1]\n    fig = dict(data=data, layout=layout)\n    print(\" Play Information\")\n    print(\" Date :\" + str(pd.unique(finale.Game_Date)[0]))\n    print(\" Home Team :\" + str(pd.unique(finale.Home_Team)[0])+ \", Visiting Team : \" + str(pd.unique(finale.Visit_Team)[0]) )\n    print(\" Player Activity Derived :\" + str(pd.unique(finale.Player_Activity_Derived)[0])+ \", Primary Partner Activity Derived : \" + str(pd.unique(finale.Primary_Partner_Activity_Derived)[0]) )\n    print(\" Primary Impact Type :\" + str(pd.unique(finale.Primary_Impact_Type)[0])+ \", Punt Play Player Role : \" + str(pd.unique(finale.Role)[0]) + \", Player Position : \" + str(pd.unique(finale.Position)[0]))\n    print(\" Quarter of Play :\" + str(pd.unique(finale.Quarter)[0])+ \", Pocession Team : \" + str(pd.unique(finale.Poss_Team)[0]) + \", Score (Home-Visiting) : \" + str(pd.unique(finale.Score_Home_Visiting)[0]))\n    print(\" Home Team Status :\" + str(pd.unique(finale.home_win_loss)[0])+ \", Pocession Team Status : \" + str(pd.unique(finale.Poss_Team_Status)[0]) )\n    print(\" Max Speed :\" + str(game_df.Speed.max())+ \", Avg Speed :\" + str(game_df.Speed.mean()) )\n    print(\" Stadium :\" + str(pd.unique(finale.Stadium)[0])+ \", Turf : \" + str(pd.unique(finale.Turf)[0])+\", GameWeather :\" + str(pd.unique(finale.GameWeather)[0])+ \", Temperature : \" + str(pd.unique(finale.Temperature)[0])  )\n    del finale\n    #print(\"\\n\\n\\t\",play_description)\n    iplot(fig)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"31671e04ba1da0407e3ecc82a254785e24182b74"},"cell_type":"markdown","source":"**Players and their Partner Movements which caused Concussion - 2016**\n\nIn this section lets visualize , the player movements during their concussion time game by game. We will analyze all the games of Preseason , Regular Week 1-6 and few games for Regular Week 7-12. \n\nField Layout and Player positions for reference in case you are not aware,\n\n![](https://storage.googleapis.com/kaggle-media/competitions/NFL%20player%20safety%20analytics/punt_coverage.png)\n\n![](https://storage.googleapis.com/kaggle-media/competitions/NFL%20player%20safety%20analytics/punt_return.png)\n\nFootball Plotly Layout taken from [An-Indian-Analysing-American-Football](https://www.kaggle.com/piyush1912/an-indian-analysing-american-football/)\n\nLets start with some preseason games."},{"metadata":{"_uuid":"4acf197c9f389530ced8098beefaf663fc10ab6c"},"cell_type":"markdown","source":"**Pre-Season Game - 2016 - CHI vs DEN**\n\nP.O'Donnell punts 58 yards to DEN 11, Center-P.Scales. B.Addison to DEN 25 for 14 yards (K.Carey). PENALTY on DEN-S.Sulleyman, Illegal Block Above the Waist, 10 yards, enforced at DEN 25.\n"},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"dbae53bcd49bc9547eeedc94150239408dc15aa3","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=3129, player1=31057, player2=32482 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fdc085095bb0f6d178a53a94711551f9c8870dc7"},"cell_type":"markdown","source":"Now , let watch the original footage from NFL. \n\n*Note: If you are using Google Chrome , please enable unsecure HTTP Content in your Browser by clicking \"Load Unsafe Scripts\". Some of the official NFL Videos are having \"http://\"  (they don't have SSL Certificate) which cannot be displayed without permissions in Chrome.*"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"fbbce6611a9ef744bce942464f7233d45b95c970"},"cell_type":"code","source":"from IPython.display import HTML\n# Youtube\nHTML('<iframe width=\"950\" height=\"600\" src=\"http://a.video.nfl.com//films/vodzilla/153233/Kadeem_Carey_punt_return-Vwgfn5k9-20181119_152809972_5000k.mp4\" frameborder=\"0\" allowfullscreen></iframe>')\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7c346392c9afad80cad2795ceb3eeb5fd66631da"},"cell_type":"markdown","source":"We can see from the video K Carey recklessly tackles the opponent using his Helmet. Carey made a hard hit on special teams in the second half of the game against the Broncos and came off wobbling, prompting him to enter the concussion protocol."},{"metadata":{"_uuid":"2b1a14dec19ee52f6448043fcc3d462c6e8ad682"},"cell_type":"markdown","source":"**Pre-Season Game  - 2016 - TEN vs CAR**\n\nK.Redfern punts 36 yards to TEN 9, Center-J.Jansen, downed by CAR-B.Wegher. PENALTY on TEN-M.Huff, Illegal Blindside Block, 5 yards, enforced at TEN 9."},{"metadata":{"trusted":true,"_uuid":"e40a56a20633d831a314f536e535064903472618","scrolled":true,"_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=2587, player1=29343, player2=31059 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f53c343fe608001ff578017fa6e3c663aff9fc4"},"cell_type":"markdown","source":"**Pre-Season Game  - 2016 - WAS vs NYJ**\n\nL.Edwards punts 51 yards to WAS 27, Center-T.Purdum. J.Crowder MUFFS catch, touched at WAS 27, recovered by WAS-Q.Dunbar at WAS 11. Q.Dunbar to WAS 11 for no gain (M.Williams)."},{"metadata":{"trusted":true,"_uuid":"f824dd708d11d4bc826d1c0ee5563bd2e12e131d","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=538, player1=31023, player2=31941 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c8743070ee71b94e3d2b99c60fdf3d845c11d257"},"cell_type":"markdown","source":"**Pre-Season Game  - 2016 - NYJ vs NWG**\n\nB.Wing punts 44 yards to NYJ 10, Center-T.Ott. J.Ross to NYJ 38 for 28 yards (O.Darkwa; B.Goodson)."},{"metadata":{"trusted":true,"_uuid":"781045c09b8904ee3a5e8e6528a5bae1ecc6df33","_kg_hide-input":true,"scrolled":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=1212, player1=33121, player2=28249 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"57079fe2fda139b98d6e34f373cdafe4b96e71bb"},"cell_type":"markdown","source":"**Pre-Season Game  - 2016 - CAR vs PIT **\n\nBerry punts 45 yards to CAR 31, Center-G.Warren. D.Byrd to CAR 46 for 15 yards (S.Davis)."},{"metadata":{"trusted":true,"_uuid":"becc0b262faf921019c79034193ca7ad7a92f0cf","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=1045, player1=32444, player2=31756 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"06655122aa2b0151d4aab82827d394a741eddb8c"},"cell_type":"markdown","source":"**Regular Game  - 2016 - BUF vs SF **\n\n\nC.Schmidt punts 54 yards to SF 19, Center-G.Sanborn. J.Kerley to SF 31 for 12 yards (M.Gillislee)."},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"afe2bfaf2e35ed10fb3a23a9702c8d74ff337e74"},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=2342, player1=32410, player2=23259 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9c93b0fac70b7ad315972f71af2ccd41af9916c7"},"cell_type":"markdown","source":"**Regular Game  - 2016 - NO VS CAR **\n\nT.Morstead punts 54 yards to CAR 39, Center-J.Drescher. T.Ginn to 50 for 11 yards (D.Lasco)."},{"metadata":{"trusted":true,"_uuid":"2599fdf325c394e769fd3d9ad1e79b0562d42ec0","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=3663, player1=28128, player2=29629 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1b069135e0ed37cba9799ab1bff79c3490711aad"},"cell_type":"markdown","source":"**Regular Game  - 2016 - KC VS JAX **\n\nD.Colquitt punts 54 yards to JAX 31, Center-J.Winchester. B.Walters to JAX 39 for 8 yards (F.Zombo)"},{"metadata":{"trusted":true,"_uuid":"8310069a07d49ef1f866b8c022edbe2727bb514f","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=3509, player1=27595, player2=31950 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8486f308d8e5e2b71e3e4647906a29f2c5e0c5a4"},"cell_type":"markdown","source":"**Regular Game  - 2016 - IND VS TEN **\n\nB.Kern punts 46 yards to IND 37, Center-B.Brinkley. C.Rogers to IND 39 for 2 yards (W.Woodyard)."},{"metadata":{"trusted":true,"_uuid":"191f60afd3842d97b751eb1d2c4f29598be766af","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=3468, player1=28987, player2=31950)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"81b1a930eeec056f8155e5204f8050e1b3df3883"},"cell_type":"markdown","source":"**Regular Game  - 2016 - BLT VS  CIN**\n\nK.Huber punts 58 yards to BLT 14, Center-T.Ott. D.Hester MUFFS catch, and recovers at BLT 12. D.Hester pushed ob at BLT 27 for 15 yards (N.Vigil). CIN-C.Brown was injured during the play. "},{"metadata":{"trusted":true,"_uuid":"d4c39e68757a42321fdc1950e3219475a516bdc4","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=1976, player1=32214, player2=32807 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"146587e65773e99ba28898e8f4f90ba92d97f527"},"cell_type":"markdown","source":"Now, Lets see the original footage from the game, "},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"b4537c2362214d5326f20b787917c2606c7827e0"},"cell_type":"code","source":"HTML('<iframe width=\"950\" height=\"600\" src=\"https://nfl-vod.cdn.anvato.net/league/5691/18/11/25/284954/284954_75F12432BA90408C92660A696C1A12C8_181125_284954_huber_punt_3200.mp4\" frameborder=\"0\" allowfullscreen></iframe>')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c019304cd95ce1e7b68923d990220263b444a4aa"},"cell_type":"markdown","source":"C Brown suffers injury after a high speed Helmet to Helmet collision. Home Team was losing and resulted in desperate challenge on the opponent. "},{"metadata":{"trusted":true,"_uuid":"994512e40d8df26e7e19a0c6e334e26ec88de8b8"},"cell_type":"code","source":"finale = pd.merge(final_csv,ngs,on=['GSISID'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd5023e4baaf4f5b950e5f5fe37720562bb27e35","_kg_hide-input":true},"cell_type":"code","source":"import plotly.plotly as py\nimport plotly.graph_objs as go\ntemp = finale[\"Event\"].value_counts()\ntempe = final[\"Event\"].value_counts()\n\ntrace1 = go.Bar(\n    x=temp.index,\n    y=temp.values,\n    name='All Games'\n)\ntrace2 = go.Bar(\n    x=tempe.index,\n    y=tempe.values,\n    name='Concussion'\n)\n\ndata = [trace1, trace2]\nlayout = go.Layout(\n    barmode='group'\n)\n\nlayout = go.Layout(title='Events over the Games')\nfig = go.Figure(data=data, layout=layout)\n\niplot(fig, filename='grouped-bar')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"54baf282adeb7b9b6b78f0d8586400f6b5b4fe35"},"cell_type":"markdown","source":"**Analysis**\n\nOut of around 2659 punts over all games there were 18 punts involving concussions. Out of 1292 \"Punt Received\" , 14 resulted in concussions which is approx 1 concussion occured every 100 Punt Received. Where as in the event of 603 \"Fair Catches\" , there were 1 concussions. So , the rate was pretty less in case of fair catches."},{"metadata":{"_uuid":"f78de7b790274fdfe3517a43e30a97442e66bf40"},"cell_type":"markdown","source":"Lets analyze the speed of the players during these injury events,"},{"metadata":{"trusted":true,"_uuid":"f79bf3305d1dd0fc32d803563889f12394154284","_kg_hide-input":true},"cell_type":"code","source":"speed_during_punt = final.loc[final['Event'].isin(['punt'])]\nspeed_during_puntrec = final.loc[final['Event'].isin(['punt_received'])]\nspeed_during_tackle = final.loc[final['Event'].isin(['tackle'])]\nspeed_during_down = final.loc[final['Event'].isin(['punt_downed'])]\nspeed_during_fumble = final.loc[final['Event'].isin(['fumble'])]\nspeed_during_catch = final.loc[final['Event'].isin(['fair_catch'])]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"374b652527c3a2619b781146c2cec4b25d99b203","_kg_hide-input":true},"cell_type":"code","source":"trace0 = go.Box(\n    y=finale.Speed,\n    name = 'During Whole Game',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(165,42,42)',\n    ),\n)\n\ntrace1 = go.Box(\n    y=speed_during_punt.Speed,\n    name = 'During Punt',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(214,12,140)',\n    ),\n)\n\ntrace2 = go.Box(\n    y=speed_during_puntrec.Speed,\n    name = 'During Punt Rec',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(238,130,238)',\n    ),\n)\n\ntrace3 = go.Box(\n    y=speed_during_tackle.Speed,\n    name = 'During Tackle',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(46,139,87)',\n    ),\n)\n\n\ntrace4 = go.Box(\n    y=speed_during_down.Speed,\n    name = 'During Punt Downed',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(255,215,0)',\n    ),\n)\n\ntrace5 = go.Box(\n    y=speed_during_fumble.Speed,\n    name = 'During Fumble',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(0,191,255)',\n    ),\n)\n\ntrace6 = go.Box(\n    y=speed_during_catch.Speed,\n    name = 'During Fair Catch',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(176,48,96)',\n    ),\n)\n\nlayout = go.Layout(\n    width=900,\n    height=500,\n    yaxis=dict(\n        title='Speed of the Concussed Player',\n        zeroline=False\n    ),\n)\ndata = [trace0,trace1,trace2,trace3,trace4,trace5,trace6]\nlayout = go.Layout(title='Speed of the Players and Events 2016')\nfig= go.Figure(data=data, layout=layout)\niplot(fig, filename='alcohol-box-plot')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"903c9f3cfa2de49af2b6376a38893ebffe09d929"},"cell_type":"markdown","source":"We can observe most of the tackles occured in very high speeds and using Helmets."},{"metadata":{"trusted":true,"_uuid":"1ee040fcd318c8156fa9e0313e06c4bc19c5b818","_kg_hide-input":true},"cell_type":"code","source":"density_punt = finale.loc[finale['Event'].isin(['punt'])]\ndensity_punts = final.loc[final['Event'].isin(['punt'])]\ndensity_puntrec = finale.loc[finale['Event'].isin(['punt_received'])]\ndensity_puntrecs = final.loc[final['Event'].isin(['punt_received'])]\ndensity_tackle = final.loc[final['Event'].isin(['tackle'])]\ndensity_tackles = finale.loc[finale['Event'].isin(['tackle'])]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c7a2762592ba43f7d6f462c0bea5baa023edc86a"},"cell_type":"markdown","source":"Now lets visualize the player density of field during these specific events,"},{"metadata":{"trusted":true,"_uuid":"0c05312fb0860bdc2c0e4f6b995f337e98e41f38","_kg_hide-input":true,"scrolled":true},"cell_type":"code","source":"trace = go.Histogram2dContour(\n        x = density_punt.x,\n        y = density_punt.y\n)\n\ntrace0 = go.Scatter(\n    x = density_punts.x,\n    y = density_punts.y,\n    mode = 'markers',\n    name = 'Position of Players (Concussed) during Punts',\n    text = list(density_punts.Role),\n    marker = dict(\n        symbol='x',\n          color = 'rgb(25,25,112)',\n          size = 14)\n)\n\nlayout = load_layout()\nlayout['legend'] = dict(orientation=\"h\")\nlayout['plot_bgcolor'] = 'rgb(220,220,220)'\nlayout['title'] = 'Player Density on field during Punt 2016'\ndata = [trace,trace0]\nfig = dict(data=data, layout=layout)\niplot(fig, filename = \"Basic Histogram2dContour\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6b8de0d728333a4ebba96fb37afb786a7e7d7891","_kg_hide-input":true,"scrolled":true},"cell_type":"code","source":"trace = go.Histogram2dContour(\n        x = density_puntrec.x,\n        y = density_puntrec.y\n)\n\n\ntrace0 = go.Scatter(\n    x = density_puntrecs.x,\n    y = density_puntrecs.y,\n    mode = 'markers',\n    name = 'Position of Players (Concussed) during Punt Recs',\n    text = list(density_puntrecs.Role),\n    marker = dict(\n        symbol='x',\n          color = 'rgb(25,25,112)',\n          size = 14)\n)\nlayout = load_layout()\nlayout['plot_bgcolor'] = 'rgb(220,220,220)'\nlayout['legend'] = dict(orientation=\"h\")\nlayout['title'] = 'Player Density on field during Punt Rec 2016'\ndata = [trace,trace0]\nfig = dict(data=data, layout=layout)\niplot(fig, filename = \"Basic Histogram2dContour\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9a664db81db21a28425f4e97678f855701f5909","_kg_hide-input":true},"cell_type":"code","source":"trace = go.Scatter(\n    x = density_tackle.x,\n    y = density_tackle.y,\n    mode = 'markers',\n    name = 'Concussion',\n    text = list(density_tackle.Role),\n    marker = dict(\n        symbol='x',\n          color = 'rgb(255, 0, 0)',\n          size = 20)\n)\n\ntrace0 = go.Scatter(\n    x = density_tackles.x,\n    y = density_tackles.y,\n    mode = 'markers',\n    name = 'Normal',\n    marker = dict(\n          color = 'rgb(238,221,130)',\n          size = 10)\n)\n\nlayout = load_layout()\nlayout['title'] = 'Tackle Points on field during 2016'\ndata = [trace0,trace]\nfig = dict(data=data, layout=layout)\niplot(fig, filename='basic-scatter')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f0dc6b03f5a62407b51d294cd69ab6524e355f5a","_kg_hide-input":true},"cell_type":"code","source":"del density_punt,density_puntrec,ngs,final,finale\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d0ea19c70cff270a448719f8dd31ce4a8b489bbc","_kg_hide-input":true},"cell_type":"code","source":"del speed_during_punt,speed_during_puntrec,speed_during_tackle,speed_during_down,speed_during_fumble\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3594c8c9902ab1a2d0d4f1f87efd4b8898eb3675"},"cell_type":"markdown","source":"**Analysis**\n\n\n* Most of these events were full speed event.\n* Most of these tackles were made at very high speed.\n* Most of these tackles were near the end line.\n*  Most of these tackles were made while running back.\n"},{"metadata":{"_uuid":"80c7e42a038830d1da0de72b421943e4b0dd40bd"},"cell_type":"markdown","source":"**Now lets look at some of the concussions in 2017,**\n\nIn this section we will explore data "},{"metadata":{"trusted":true,"_uuid":"6527c5cd160d5e610f470e0f1c19fe306cec2fef","_kg_hide-input":true},"cell_type":"code","source":"dtypes = {'Season_Year': 'int16',\n         'GameKey': 'int16',\n         'PlayID': 'int16',\n         'GSISID': 'float32',\n         'Time': 'str',\n         'x': 'float32',\n         'y': 'float32',\n         'dis': 'float32',\n         'o': 'float32',\n         'dir': 'float32',\n         'Event': 'str'}\n\ncol_names = list(dtypes.keys())\n\ndf_list = []\n\nngs_files = ['NGS-2017-pre.csv',\n             'NGS-2017-reg-wk1-6.csv',\n             'NGS-2017-reg-wk7-12.csv','NGS-2017-reg-wk13-17.csv']\n\nfor i in tqdm.tqdm(ngs_files):\n    df = pd.read_csv(f'../input/'+i, usecols=col_names,dtype=dtypes)\n    date_format = '%Y-%m-%d %H:%M:%S.%f'\n    sortBy = ['Season_Year', 'GameKey', 'PlayID', 'GSISID', 'Time']\n    df.Time = pd.to_datetime(df.Time, format =date_format)\n    df.sort_values(sortBy, inplace=True)\n    df = calculate_speeds(df, SI=True)\n    df_list.append(df)\n    del df\n    gc.collect()\n\nngs = pd.concat(df_list)\n\ndel df_list\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"34068d1d7129fbd000b72396430aa9688c2b7952","_kg_hide-input":true},"cell_type":"code","source":"#Converting everything to meters and speed to KMPH\nngs['x'] = ngs['x']/1.0936\nngs['y'] = ngs['y']/1.0936\nngs['dis'] = ngs['dis']/1.0936\nngs['Speed'] = ngs['Speed']* 3.6","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f043d50342cab8eb1db23a0d1960db24a069a5b","_kg_hide-input":true},"cell_type":"code","source":"ngs = ngs[ngs.replace([np.inf, -np.inf], np.nan).notnull().all(axis=1)] ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0ce59f6b303ef2213de182248015892d210fbeaf","_kg_hide-input":true},"cell_type":"code","source":"def remove_wrong_values(df, tested_columns=['Season_Year', 'GameKey', 'PlayID', 'GSISID', 'TimeDelta'], cutspeed=None):\n    dump = df.copy()\n    colums = dump.columns\n    mask = []\n    for col in tested_columns:\n        dump['shift_'+col] = dump[col].shift(-1)\n        mask.append(\"( dump['shift_\"+col+\"'] == dump['\"+col+\"'])\")\n    mask =eval(\" & \".join(mask))\n    # Keep results where next rows is equally space\n    dump = dump[mask]\n    dump = dump[colums]\n    if cutspeed!=None:\n        dump = dump[dump.Speed < cutspeed]\n    return dump","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"889ac7fb1ace77deba399af2cb1cc82557d66e07","_kg_hide-output":true,"_kg_hide-input":true},"cell_type":"code","source":"cut_speed=44 # World record 9,857232 m/s for NFL\nngs = remove_wrong_values(ngs, cutspeed=cut_speed)\nngs.Speed.hist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66f0c5e5c17ebdaaff66d3ecbd1036a29a94a776","_kg_hide-input":true},"cell_type":"code","source":"video_review = pd.read_csv('../input/video_review.csv')\nfinal = pd.merge(final_csv,ngs,on=['Season_Year','GameKey','PlayID','GSISID'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"027fef914956e1b05c5ec45d727806969af04f8b"},"cell_type":"markdown","source":"**Players and their Partner Movements which caused Concussion - 2017**\n\nIn this section lets visualize , the player movements during their concussion time game by game. We will analyze all the games of Preseason , Regular Week 1-6 and few games for Regular Week 7-12. "},{"metadata":{"_uuid":"e364ecc7b88580542025bf8205125a0b52944ed8"},"cell_type":"markdown","source":"**Pre Season Game  - 2017 - MIA vs BLT**\n\nM.Haack punts 52 yards to BLT 25, Center-W.Chapman. B.Rainey to BLT 34 for 9 yards (D.Morgan). MIA-C.Pantale was injured during the play. His return is Questionable. "},{"metadata":{"trusted":true,"_uuid":"e74fd45386035fd014059409ee31e6ee66f065d1","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=3630, player1=30171, player2=29384 )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4d27d45b4ddd1e6194039abf3dde4c73bfef05c3","_kg_hide-input":true},"cell_type":"code","source":"HTML('<iframe width=\"950\" height=\"600\" src=\"http://a.video.nfl.com//films/vodzilla/153250/52_yard_Punt_by_Matt_Haack-ENsIvMyf-20181119_161418429_5000k.mp4\" frameborder=\"0\" allowfullscreen></iframe>')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"50eabb57fdbca61977f79451969091c8c1dbc670"},"cell_type":"markdown","source":"Another reckless challenge, The injured player C Pantale bulldozes the Partner player using his Helmet, to be exact C Pantale makes the contact with opponent at a speed of more than 25 KMPH ! "},{"metadata":{"_uuid":"9490a3cd584601d85cd4ae80196956a1171d6524"},"cell_type":"markdown","source":"**Pre Season Game - 2017 - WAS vs GB**\n\nJ.Vogel punts 43 yards to WAS 48, Center-D.Hart. K.Fuller to GB 40 for 12 yards (J.Hawkins)"},{"metadata":{"trusted":true,"_uuid":"1201d27fe2a4ca88c2963d23b1af3ce3922ced06","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=2764, player1=32323, player2=31930 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8a0b3722f2b66f4cd211a8aa43c1a52efa782b1f"},"cell_type":"markdown","source":"**Pre Season Game - 2017 - ATL vs JAX**\n\nB.Nortman punts 40 yards to ATL 25, Center-M.Overton. J.Hardy to ATL 32 for 7 yards (B.Brown). JAX-J.Harper was injured during the play.  PENALTY on ATL-J.Keyes, Offensive Holding, 10 yards, enforced at ATL 25."},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"3b218814eea285a321de359cc546e44e36e092d9","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=183, player1=33813, player2=33841 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bd617c5d3528af7663f4512dffc036892a3ab0c7"},"cell_type":"markdown","source":"**Pre Season Game - 2017 - KC vs TEN**\n\nB.Kern punts 61 yards to KC 24, Center-R.DiSalvo. J.Chesson for 76 yards, TOUCHDOWN. TEN-R.DiSalvo was injured during the play. "},{"metadata":{"trusted":true,"_uuid":"eddbe4d4a41e9777d82596e31da310beb4ec3b02","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=1088, player1=32615, player2=31999 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8bcaebb61635f0a39b8e95a504685eda86b1f668"},"cell_type":"markdown","source":"**Pre Season Game - 2017 - SF vs LAC**\n\nT.Baker punts 41 yards to SF 11, Center-M.Windt. D.Carter to SF 14 for 3 yards (J.Perry; D.Brown)."},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"772d0e18496100f69292eb95c682c894bffb8aa3","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=1526, player1=32894, player2=31763 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1c9ce231a385a9c9c6f217f885ed366a6296fe6e"},"cell_type":"markdown","source":"**Regular Game - 2017 - NE vs KC**\n\nD.Colquitt punts 36 yards to KC 47, Center-J.Winchester. D.Amendola to KC 44 for 3 yards (T.Smith). PENALTY on NE-B.Bolden, Running Into the Kicker, 5 yards, enforced at KC 11 - No Play."},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"3c515b1af41bb0f62a8d556773beb319c3ddbd1f","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=3312, player1=26035, player2=27442 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e4e0f6528f3ea3dc9a9c92f0cd4da1cf17d12abf"},"cell_type":"markdown","source":"**Regular Game - 2017 - DEN vs LAC**\n\nD.Kaser punts 59 yards to DEN 13, Center-M.Windt. I.McKenzie ran ob at DEN 44 for 31 yards."},{"metadata":{"trusted":true,"_uuid":"84401a1e69eb9ff56726ac455c8f28ba0211f337","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=1262, player1=33941, player2=27442 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7c09eac0ad1bd7b381c6ef014536b8f59531db31"},"cell_type":"markdown","source":"**Regular Game - 2017 - MIA vs NO**\n\nM.Haack punts 42 yards to NO 30, Center-J.Denney. T.Ginn ran ob at NO 39 for 9 yards (J.Denney)."},{"metadata":{"trusted":true,"_uuid":"286c81c7813513adf2c1ef291c84f99fa4404c56","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=2792, player1=33838, player2=31317 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"43ad14c9d71299e79d783e60b5849f3dd47f0dca"},"cell_type":"markdown","source":"**Regular Game - 2017 - OAK vs BLT**\n\nM.King punts 62 yards to BLT 15, Center-J.Condo. M.Campanaro to BLT 24 for 9 yards (J.Condo, J.Cowser)."},{"metadata":{"trusted":true,"_uuid":"37f236211044ca075100b01bb0ccdc65e8e7aea7","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=2072, player1=29492, player2=33445 )","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"de8ba382b92346735783fae4f29e87dc9067496d"},"cell_type":"markdown","source":"**Regular Game - 2017 - NYG vs KC**\n\nB.Wing punts 37 yards to KC 29, Center-Z.DeOssie. T.Hill pushed ob at KC 49 for 20 yards (N.Berhe). Penalty on KC-F.Zombo, Offensive Holding, declined. PENALTY on KC-T.Smith, Unnecessary Roughness, 15 yards, enforced at KC 33. Officially, a return for 4 yards."},{"metadata":{"trusted":true,"_uuid":"d0d8999f4d1a52a12002bd259a79cc817929d094","_kg_hide-input":true},"cell_type":"code","source":"plot_play(game_df=ngs, PlayID=1683, player1=32820, player2=25503 )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"91370dd5f89c2ca3617dc3f8382785160f74caf8","_kg_hide-input":true},"cell_type":"code","source":"HTML('<iframe width=\"950\" height=\"600\" src=\"http://a.video.nfl.com//films/vodzilla/153280/Wing_37_yard_punt-cPHvctKg-20181119_165941654_5000k.mp4\" frameborder=\"0\" allowfullscreen></iframe>')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a82d44c067c2c96b87330a04b96bd90b1d38e251"},"cell_type":"markdown","source":"Another unnecessary rough challenge using Helmet."},{"metadata":{"trusted":true,"_uuid":"5628a03317e6e510ad4a7102bb7021c5f5bb13a3"},"cell_type":"code","source":"finale = pd.merge(final_csv,ngs,on=['GSISID'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"efac3e8f46a92967f2bd36f61ef69907aa8431c5"},"cell_type":"markdown","source":"Lets, visualize the frequency of events and the concussion events"},{"metadata":{"_kg_hide-input":true,"trusted":true,"_uuid":"fcc74892ff957782c204730d73b520291355cf67"},"cell_type":"code","source":"import plotly.plotly as py\nimport plotly.graph_objs as go\n\ntemp = finale[\"Event\"].value_counts()\ntempe = final[\"Event\"].value_counts()\n\ntrace1 = go.Bar(\n    x=temp.index,\n    y=temp.values,\n    name='All Games'\n)\ntrace2 = go.Bar(\n    x=tempe.index,\n    y=tempe.values,\n    name='Concussion'\n)\n\ndata = [trace1, trace2]\nlayout = go.Layout(\n    barmode='group'\n)\n\nlayout = go.Layout(title='Events over the Games')\nfig = go.Figure(data=data, layout=layout)\n\niplot(fig, filename='grouped-bar')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d5f4eb2ba9a0f083f5b0821b015709e11ab9645c"},"cell_type":"markdown","source":"**Analysis**\n\nThe pattern remains similar like 2016 Season. Out of around 2181 punts over all games there were 18 punts involving concussions. Out of 1048 \"Punt Received\" , 16 resulted in concussions which is approx 1.6 concussion occured every 100 Punt Received. Where as in the event of 509 \"Fair Catches\" , there were only 2 concussions. So , data once again shows that fair catches are more safer."},{"metadata":{"_uuid":"88629c3ac121a9ea7ca409979888ca698389b0c3"},"cell_type":"markdown","source":"Lets analyze the speed of the players during these injury events,"},{"metadata":{"trusted":true,"_uuid":"660f9621b9e07d043b40fbabf85dc8c97348a844","_kg_hide-input":true},"cell_type":"code","source":"speed_during_punt = final.loc[final['Event'].isin(['punt'])]\nspeed_during_puntrec = final.loc[final['Event'].isin(['punt_received'])]\nspeed_during_tackle = final.loc[final['Event'].isin(['tackle'])]\nspeed_during_down = final.loc[final['Event'].isin(['punt_downed'])]\nspeed_during_fumble = final.loc[final['Event'].isin(['fumble'])]\nspeed_during_catch = final.loc[final['Event'].isin(['fair_catch'])]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"10954132c276a924acd28a9dc3cfdace31a7c9f5","_kg_hide-input":true},"cell_type":"code","source":"trace0 = go.Box(\n    y=finale.Speed,\n    name = 'During Whole Game',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(165,42,42)',\n    ),\n)\n\ntrace1 = go.Box(\n    y=speed_during_punt.Speed,\n    name = 'During Punt',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(214,12,140)',\n    ),\n)\n\ntrace2 = go.Box(\n    y=speed_during_puntrec.Speed,\n    name = 'During Punt Rec',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(138,43,226)',\n    ),\n)\n\ntrace3 = go.Box(\n    y=speed_during_tackle.Speed,\n    name = 'During Tackle',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(30,144,255)',\n    ),\n)\n\n\ntrace4 = go.Box(\n    y=speed_during_down.Speed,\n    name = 'During Punt Downed',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(214,179,140)',\n    ),\n)\n\ntrace5 = go.Box(\n    y=speed_during_fumble.Speed,\n    name = 'During Fumble',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(254,199,140)',\n    ),\n)\n\ntrace6 = go.Box(\n    y=speed_during_catch.Speed,\n    name = 'During Fair Catch',\n    boxpoints='all',\n    jitter=0.3,\n    marker = dict(\n        color = 'rgb(176,48,96)',\n    ),\n)\n\nlayout = go.Layout(\n    width=900,\n    height=500,\n    title = 'Speed , Concussion and Events',\n    yaxis=dict(\n        title='Speed of the Concussed Player 2017',\n        zeroline=False\n    ),\n)\ndata = [trace0,trace1,trace2,trace3,trace4,trace5,trace6]\nfig= go.Figure(data=data, layout=layout)\niplot(fig, filename='alcohol-box-plot')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0fd6c653e2c12b899895fd8be85071a36a4707d4"},"cell_type":"markdown","source":"**Analysis**\n\n\n* Most of these events were full speed event.\n* Most of these tackles were made at very high speed using their helmets.\n* Most of these tackles were near the end line.\n*  Most of these tackles were made while running back.\n"},{"metadata":{"trusted":true,"_uuid":"6a9f462e2a6dd533dc9868616a9c4a033e4a4d3a","_kg_hide-input":true},"cell_type":"code","source":"density_punt = finale.loc[finale['Event'].isin(['punt'])]\ndensity_punts = final.loc[final['Event'].isin(['punt'])]\ndensity_puntrec = finale.loc[finale['Event'].isin(['punt_received'])]\ndensity_puntrecs = final.loc[final['Event'].isin(['punt_received'])]\ndensity_tackle = final.loc[final['Event'].isin(['tackle'])]\ndensity_tackles = finale.loc[finale['Event'].isin(['tackle'])]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3c9bd3301ba5a8772054572f1dc1e02ec318f319","_kg_hide-input":true},"cell_type":"code","source":"trace = go.Histogram2dContour(\n        x = density_punt.x,\n        y = density_punt.y\n)\n\ntrace0 = go.Scatter(\n    x = density_punts.x,\n    y = density_punts.y,\n    mode = 'markers',\n    name = 'Position of Players (Concussed) during Punts',\n    text = list(density_punts.Role),\n    marker = dict(\n        symbol='x',\n          color = 'rgb(25,25,112)',\n          size = 14)\n)\n\nlayout = load_layout()\nlayout['legend'] = dict(orientation=\"h\")\nlayout['plot_bgcolor'] = 'rgb(220,220,220)'\nlayout['title'] = 'Player Density on field during Punt 2017'\ndata = [trace,trace0]\nfig = dict(data=data, layout=layout)\niplot(fig, filename = \"Basic Histogram2dContour\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f3dd0984f5c4048121b92b211467740e31cb0a16","_kg_hide-input":true},"cell_type":"code","source":"trace = go.Histogram2dContour(\n        x = density_puntrec.x,\n        y = density_puntrec.y\n)\n\n\ntrace0 = go.Scatter(\n    x = density_puntrecs.x,\n    y = density_puntrecs.y,\n    mode = 'markers',\n    name = 'Position of Players (Concussed) during Punt Recs',\n    text = list(density_puntrecs.Role),\n    marker = dict(\n        symbol='x',\n          color = 'rgb(25,25,112)',\n          size = 14)\n)\nlayout = load_layout()\nlayout['plot_bgcolor'] = 'rgb(220,220,220)'\nlayout['legend'] = dict(orientation=\"h\")\nlayout['title'] = 'Player Density on field during Punt Rec 2017'\ndata = [trace,trace0]\nfig = dict(data=data, layout=layout)\niplot(fig, filename = \"Basic Histogram2dContour\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"789ad368b8320fa2d30cedb43db56c5900522830"},"cell_type":"code","source":"trace = go.Scatter(\n    x = density_tackle.x,\n    y = density_tackle.y,\n    mode = 'markers',\n    name = 'Concussion',\n    text = list(density_tackle.Role),\n    marker = dict(\n        symbol='x',\n          color = 'rgb(255, 0, 0)',\n          size = 20)\n)\n\ntrace0 = go.Scatter(\n    x = density_tackles.x,\n    y = density_tackles.y,\n    mode = 'markers',\n    name = 'Normal',\n    marker = dict(\n          color = 'rgb(238,221,130)',\n          size = 10)\n)\n\nlayout = load_layout()\nlayout['title'] = 'Tackle Points on field during 2017'\ndata = [trace0,trace]\nfig = dict(data=data, layout=layout)\niplot(fig, filename='basic-scatter')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cb1116d0c083b721dac3029ac03fe49575c3356","_kg_hide-input":true},"cell_type":"code","source":"del density_punt,density_puntrec,ngs,final,finale,final_csv\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4384feb0b61617d2689103ed3aa8635b12c14588"},"cell_type":"markdown","source":"**Rule Changes:**\n\nHere in this section we will mention the rule changes that may reduce the number of concussions. \n\nLets **summarize **  all the  **observations** that were made in the above sections,\n\n*  Most of the player had concussion while Tackling or after getting Blocked.\n* Most of these injuries were caused due Helmet-to-Body or Helmet-to-Helmet Contacts.  \n* Helmet to body contact while tackling to 8 (22%) of injuries. Which were results of the injured player tackling the other player using their Helmets.\n* Most of the Players who were injured were Punt Returners, PLG and GL (Punt Role wise) and Tight End,Wide Receiver,Inside Line Breaker(Position Wise).\n* Most of the players who were concussed were in Offensive positions.\n* Most of the concussions (59% approx) occured when the Possession Team was either Losing or Drawing.\n* 32.4 % concussion occurred in the Pre-season games while around 67.6% occurred in Regular Games. And NO concussions occured during the Post Season Games.\n* While in a game, Quarter 3 experienced most number of concussions followed Quarter 2.\n* Most of the injuries tend to occur on Natural Grass or Grass turfs. \n* In 2016, Out of around 2659 punts (excluding Post Season) over all games there were 18 punts involving concussions. \n* In 2016, Out of 1292 \"Punt Received\" , 14 resulted in concussions which is approx 1 concussion occured every 100 Punt Received.\n* In 2016, Out of 603 \"Fair Catches\" , there were 1 concussions. So , the rate was pretty less in case of fair catches.\n* Most of these concussion events were full speed events.\n* Most of these tackles were made at very high speed, with player recklessly challenging the partner with the helmet.\n* Most of these tackles were near the end line.\n* Most of these tackles were made while running back.\n\n\n\nNow, Will suggest some rule changes based on these observations\n\n* **Bonus yards for fair catches.**\n\nFrom the observations it is quite evident that Fair Catches are more safer Punt Received, and thus Fair Catches must be encouraged. This may be achieved by awarding bonus yards.\n\n* **Penalty and Ejection for Roughness**\n\nIndividual analysis of lots of concussion footage showed that many of the players were involved in unecessary roughness. They used Helmet to tackle the opponent player. The 2018 Rule Change came up with  **\"lowering the head to initiate contact with the helmet a foul\".** But still its not enough. The foul should be backed up with Soccer like ejections. \n* Players receive “RED card” or ejection for direct helmet to helmet contact and a 10 yrd penalty. The roughness can be judged using any speed  sensors.\n* Players receive “YELLOW card” or ejection for direct helmet to body contact and a 5 yrd penalty. The roughness can be judged using any speed sensors.\n\nInternet of Things has reached to a great level and we have sensors for almost everything. So Sensors can be used to monitor the Helmet Use for tackling other players. \n\n\nOnce, the 2018 data is available we will have more insights and can analyze the impact of 2018 rule changes. \n\nVersion submitted for competition is Version 15. \n\n*Note : I knew nothing about NFL before this competetion, So,Thanks Kaggle and NFL.*\n\n\n"},{"metadata":{"trusted":true,"_uuid":"ae763fc0de037a7eded639eff0ccda32396c3d57"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}