{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## ***NFL ANALYTICS***\n\nIn this challenge, We have to investigate the relationship between the playing surface and the injury and performance of National Football League (NFL) athletes and to examine factors that may contribute to lower extremity injuries.\n\n\nIn the NFL, 12 stadiums have fields with synthetic turf. Recent investigations of lower limb injuries among football athletes have indicated significantly higher injury rates on synthetic turf compared to natural turf for whatever reasons. \n\nIn this notebook I will only investigate the chances of injuries to be occured to any typed player at any given positon.\n\nSo, Lets start. ","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#Importing Libraries to expose data insights\n\n#data analysis and wrangling\nimport pandas as pd\nimport numpy as np\nimport random as rnd\nimport math\n\n# visualization\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport plotly.graph_objs as go\nimport plotly.tools as tls\nimport plotly.offline as py","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_Rec = pd.read_csv('../input/nfl-playing-surface-analytics/InjuryRecord.csv')\nPlayList = pd.read_csv('../input/nfl-playing-surface-analytics/PlayList.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_Rec.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## **Explaination Of Above Data:**\n\nBodypart: Injured Body Part (Knee, Ankle, Foot, etc.)\n\nSurface: Playing surface area at the time of injury.\n\nDM_M1: One hot encoding, indicates 1 or more days missed due to injury\n\nDM_M7: One hot encoding, indicates 7 or more days missed due to injury\n\nDM_M28: One hot encoding, indicates 28 or more days missed due to injury\n\nDM_M42: One hot encoding, indicates 42 or more days missed due to injury","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"PlayList.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## **Explaination Of Above Data:**\n\n\nRosterPosition: Player's roster position i.e. Running Back\n\nPlayerDay: Time line of players participation in games\n\nPlayer Game: Uniquely identifies the player's game\n\nStadium Type: open, close, outdoor, etc\n\nField type: Natural or synthetic\n\nTemperature: On field temperature at start of the game. Not provided in dome/indoor stadiums.\n\nWeather: Weather conditions\n\nPlaytype: categorical description of play type.\n\nPlayerGamePlay: running count of plays a player has participated during the game\n\nPosition: Player position for eg. RB,QB,DE etc.\n\nPositionGroup: Player position group for the position held during the play. \n","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"### Lets gain some insights from data through beautiful visuals","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"df = Injury_Rec.groupby('BodyPart').agg({'PlayerKey': ['count']})\ndf = df.sort_values(('PlayerKey', 'count'), ascending=True).sort_index()\ndata = go.Bar(x=df.index, y=df['PlayerKey']['count'].sort_values(),marker_color=\"mediumPurple\",name='counts')\n\nlayout = go.Layout(dict(title = \"Which Body Part got Injured frequently ?\",\n                        xaxis = dict(title = \"Injured BodyParts\",\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Purple\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = 'Counts',\n                                     color=\"Purple\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_Rec.groupby('Surface').count()['PlayerKey'].sort_values().plot(kind='barh', figsize=(15, 5), title='Surface on games are played',color='mediumPurple')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_Rec.groupby(['BodyPart','Surface']).count() \\\n.unstack('BodyPart')['PlayerKey'].T.sort_values('Natural')\\\n.T.sort_values('Ankle').plot(kind='bar', figsize=(15, 5), title='Body Parts Injured on respected surfaces')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_Rec.groupby(['DM_M1','DM_M7','DM_M28','DM_M42']).count()['PlayerKey'].sort_values().plot(kind='bar', figsize=(15, 5), title='Players Recovery',color='mediumPurple')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Injury_Rec.groupby(['DM_M1','DM_M7','DM_M28','DM_M42','BodyPart','Surface']).count() \\\n.unstack('BodyPart')['PlayerKey']\\\n.plot(kind='bar', figsize=(15, 5), title='Games missed by a player due to injury ')\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## **Observations**\n\n- 57 matches were played in synthetic field while others on natural field.\n\n- Out of 105 injuries, Toes injury with 48 numbers have been the highest amongst them all.\n\n- Ankle and toes injuries in synthetic field increases drastically.\n\n- No heel injury in synthetic field recorded.\n\n- Knee injuries remains almost same on both the fields.\n\n- Foot injury got decreased in sythetic field.\n","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Thats all for InjuryRecord dataset\nPlayList.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = PlayList[['PlayKey','PlayType']].drop_duplicates()\ndf = df.groupby('PlayType').agg({'PlayKey': ['count']})\ndf = df.sort_values(('PlayKey', 'count'), ascending=True).sort_index()\ndata = go.Bar(x=df.index, y=df['PlayKey']['count'],marker_color=\"cornflowerblue\",name='counts')\n\nlayout = go.Layout(dict(title = \"Most frequent player types\",\n                        xaxis = dict(title = \"Player Type\",\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = 'Counts',\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Lets merge both of them to get insights\ninj_detailed_merged = Injury_Rec.merge(PlayList)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inj_detailed_merged.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = inj_detailed_merged.groupby('RosterPosition').agg({'PlayerKey': ['count']})\ndf = df.sort_values(('PlayerKey', 'count'), ascending=True).sort_index()\ndata = go.Bar(x=df.index, y=df['PlayerKey']['count'],marker_color=\"cornflowerblue\",name='counts')\n\nlayout = go.Layout(dict(title = \"Most frequent postions of the players\",\n                        xaxis = dict(title = \"Player Positions\",\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = 'Counts',\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = inj_detailed_merged.groupby('PlayType').agg({'PlayerKey': ['count']})\ndf = df.sort_values(('PlayerKey', 'count'), ascending=True).sort_index()\ndata = go.Bar(x=df.index, y=df['PlayerKey']['count'],marker_color=\"cornflowerblue\",name='counts')\n\nlayout = go.Layout(dict(title = \"Most frequent player types\",\n                        xaxis = dict(title = \"Player Positions\",\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = 'Counts',\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### What are the main factors to cause injuries ??:","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"ds = inj_detailed_merged.groupby(['RosterPosition','BodyPart']).count() \\\n.unstack('BodyPart')['PlayerKey']\\\n.T.apply(lambda x: x / x.sum())\\\n.sort_values('BodyPart').T.sort_values('Ankle', ascending=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = go.Bar(x=ds['Ankle'], y=ds.index,marker_color=\"blue\",name='counts',orientation = 'h')\n\nlayout = go.Layout(dict(title = \"Most frequent player positions having Ankle Injury\",\n                        xaxis = dict(title = \"Count\",\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = 'Player Postions',\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = go.Bar(x=ds['Foot'], y=ds.index,marker_color=\"orange\",name='counts',orientation = 'h')\n\nlayout = go.Layout(dict(title = \"Most frequent player positions having Foot Injury\",\n                        xaxis = dict(title = 'Counts',\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = \"Player Positions\",\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = go.Bar(x=ds['Knee'], y=ds.index,marker_color=\"green\",name='counts',orientation = 'h')\n\nlayout = go.Layout(dict(title = \"Most frequent player positions having Knee Injury\",\n                        xaxis = dict(title = 'Counts',\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = \"Player Positions\",\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Lets get a merged perspective of all of these factors\n\nds.plot(kind='barh', figsize=(15, 5), title='Most frequent body parts injuries in different positions',stacked=True)\n\nplt.show() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 'PlayType' & 'BodyPart'\n\ndf = inj_detailed_merged.groupby(['PlayType','BodyPart']).count() \\\n.unstack('BodyPart')['PlayerKey']\\\n.T.apply(lambda x: x / x.sum())\\\n.sort_values('BodyPart').T.sort_values('Ankle', ascending=False)\\","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = go.Bar(x=df['Ankle'], y=df.index,marker_color=\"blue\",name='counts',orientation = 'h')\n\nlayout = go.Layout(dict(title = \"Most frequent player types having Ankle Injury\",\n                        xaxis = dict(title = \"Count\",\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = 'Player Postions',\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = go.Bar(x=df['Foot'], y=df.index,marker_color=\"orange\",name='counts',orientation = 'h')\n\nlayout = go.Layout(dict(title = \"Most frequent player types having Foot Injury\",\n                        xaxis = dict(title = 'Counts',\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = \"Player Positions\",\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = go.Bar(x=df['Knee'], y=df.index,marker_color=\"green\",name='counts',orientation = 'h')\n\nlayout = go.Layout(dict(title = \"Most frequent player types having Knee Injury\",\n                        xaxis = dict(title = 'Counts',\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = \"Player Positions\",\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Lets get a merged perspective of all of these factors\n\ninj_detailed_merged.groupby(['PlayType','BodyPart']).count() \\\n.unstack('BodyPart')['PlayerKey']\\\n.T.apply(lambda x: x / x.sum())\\\n.sort_values('BodyPart').T.sort_values('Ankle', ascending=False)\\\n.plot(kind='barh', figsize=(15, 5), title='Frequency of different injuries on different playtypes',stacked=True)\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 'RosterPosition' & 'Surface'\n\ndf = inj_detailed_merged.groupby(['RosterPosition','Surface']).count() \\\n.unstack('Surface')['PlayerKey']\\\n.T.apply(lambda x: x / x.sum())\\\n.sort_values('Surface').T.sort_values('Natural', ascending=False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = go.Bar(x=df['Natural'], y=df.index,marker_color=\"blue\",name='counts',orientation = 'h')\n\nlayout = go.Layout(dict(title = \"Most frequent player positions in Natural Field Type\",\n                        xaxis = dict(title = \"Count\",\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = 'Field Type',\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = go.Bar(x=df['Synthetic'], y=df.index,marker_color=\"orange\",name='counts',orientation = 'h')\n\nlayout = go.Layout(dict(title = \"Most frequent player positions in Synthetic Field Type\",\n                        xaxis = dict(title = 'Counts',\n                                     #marker = list(color = \"rgb(195, 195, 195)\"),\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,),\n                        yaxis = dict(title = \"Player Positions\",\n                                     color=\"Black\",\n                                     showgrid=True,\n                                     zeroline=True,\n                                     showline=True,)))\n\npy.iplot(dict(data=data, layout=layout))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Lets get a merged perspective of all of these factors\ninj_detailed_merged.groupby(['RosterPosition','Surface']).count() \\\n.unstack('Surface')['PlayerKey']\\\n.T.apply(lambda x: x / x.sum())\\\n.sort_values('Surface').T.sort_values('Natural', ascending=False)\\\n.plot(kind='barh', figsize=(15, 5), title='Players on different positions on different surfaces',stacked=True)\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Observation & Conclusion:\n\n- No ankle injury to tightend positioned players, Highest recorded to cornerback positioned players.\n\n- No foot injuries to \"Running back\", \"Safety\" and \"line backer\" while highest ankle injuries to tight end positioned players.\n\n- \"Running back\", \"Safety\" and \"line backer\" have highest knee injuries recorded.\n\n- No ankle injuries to \"putNotReturned\", \"Kickoff\" and \"KickOff not returned\" type players while highest ankle injuries to \"punt return type\" player.\n\n- \"PutNotReturned\" type players have highest foot injuries.\n\n- Knee injuries have been recorded most to \"kick off\" and \"kick off return\" typed players.","execution_count":null}],"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":4}