{"cells":[{"metadata":{},"cell_type":"markdown","source":"Focus:\n\nThe objective of the analysis is to identify the effect of playing the NFL games in Synthetic and Natural Surface. By analysing the given datasets, it is understood that the rate of injuries occurred in Synthetic Surface is higher than the Natural Surface. The challenge is to identify the  facts from other aspects like weather, position, player speed, direction, orientation  along with Surface attribute.\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"play_list = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/PlayList.csv')\nplay_list.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_record = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/InjuryRecord.csv')\ninjury_record.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"player_track_record = pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/PlayerTrackData.csv')\nplayer_track_record.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nprint(play_list.shape,injury_record.shape,player_track_record.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#lower limb injury's count based on Playerkey\nimport matplotlib                  \nimport matplotlib.pyplot as plt\nimport seaborn as sns  \nlower_inj=injury_record.groupby('BodyPart').count()['PlayerKey']\nprint(lower_inj)\ninjury_record.groupby('BodyPart')['PlayerKey'].count().plot(kind='bar', figsize = (10,8), title='Count of Injuries based on body parts')\nplt.xticks(rotation=100)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Injuries Count"},{"metadata":{"trusted":true},"cell_type":"code","source":"count_inj=injury_record.groupby(['Surface']).count()['PlayerKey']\nprint(count_inj)\ninjury_record.groupby(['Surface']).count()['PlayerKey'].plot(kind='bar', figsize = (5,5), title='Count of Injuries based on Surface')\nplt.xticks(rotation=100)\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inj_pivot = injury_record.pivot_table(index='BodyPart', columns='Surface', aggfunc='size',fill_value=0)\ninj_pivot.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Visualising the pivot table data in a bar chart\ninj_pivot.plot(kind='barh', figsize=[10,5], stacked=False, colormap='autumn')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Merging both Player and Injury list together based on 'Playkey' "},{"metadata":{"trusted":true},"cell_type":"code","source":"\nplay_inj_together = injury_record.merge(play_list, on='PlayKey')\nplay_inj_together.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#play_inj_together.isnull().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"play_inj_together['PlayKey'].nunique()\n#play_inj_together['PlayKey'].value_counts()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#play_list['Position'].value_counts()\nplay_list['Position'].value_counts().plot(kind='bar', figsize=(10,5), color='orange', title='Position of All Players')\n#play_list.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#play_inj_together['Position'].nunique()\nplay_inj_together['Position'].value_counts().plot(kind='bar', figsize=(10,5), color='orange', title='Position of Injured Players')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nplay_inj_pivot = play_inj_together.pivot_table(index=['Position','Surface'], columns='BodyPart', aggfunc='size',fill_value=0)\nplay_inj_pivot.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nplay_inj_pivot.plot(kind='barh', figsize=[15,15],stacked=True)\nplt.title('Injuries based on Position and Surface')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Above analysis shows 'Wide Receiver' position has three type of injuries occured in a Synthetic surface followed by the position 'CB' (Synthetic) which has maximum count than the positions 'OLB','WR'(Natural Surface). "},{"metadata":{"trusted":true},"cell_type":"code","source":"play_inj_together.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Injuries occurred in Natural Surface when the weather varies from sunny to cloudy.\nplay_inj_stad_pivot = play_inj_together[play_inj_together['Surface'] == 'Natural']['Weather'].value_counts()\n#play_inj_stad_pivot\nplay_inj_stad_pivot.plot(kind='bar', figsize = (10,5))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#displaying the Injuries happened in Synthetic Surface when the weather varies from sunny, cloudy and mostly sunny.\nplay_inj_stad_pivot1 = play_inj_together[play_inj_together['Surface'] == 'Synthetic']['Weather'].value_counts()\n#play_inj_stad_pivot\nplay_inj_stad_pivot1.plot(kind='bar', figsize = (10,5), color ='orange')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Analysing the attributes Surface, Weather and StadiumType together to visualise the facts. \n#To analyse whether the characteristics of stadium and weather having impact on the surface and injuries.\ncomb_result = play_inj_together.pivot_table(index=['StadiumType','Weather'], columns='Surface', aggfunc='size',fill_value=0)\ncomb_result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"comb_result.plot(kind='bar', figsize=[15,15], stacked=True, colormap='autumn')\nplt.ylabel('Number of Injuries')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#player_track_record.columns\nplayer_track_record.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#To visualise the speed of all the players during the game.\nx=player_track_record['s']\nimport pandas as pd\ny = pd.Series(x)\nax = sns.distplot(y,hist=True,color='brown')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Querying the player track record dataset with the help of 'Playkey' attribute of Injury dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plotting the players movement (speed) during the game. The value displayed in the graph has its mean, \n#minimum and maximum speed of the injured players. One can visualise the number of the injuries have been diminished\n#when the speed increases.\n\ninjury_rec=injury_record['PlayKey'].tolist()\nplayer_track_record.query('PlayKey in @injury_rec')['s'].plot.hist(bins=30)\nplt.axvline(player_track_record.query('PlayKey in @injury_rec')['s'].mean(),label='mean',color='green')\n#The below given script shows the minimum and maximum speed of players who got injured. \nplt.axvline(player_track_record.query('PlayKey in @injury_rec')['s'].max(), label='max_speed',color='Orange')\nplt.axvline(player_track_record.query('PlayKey in @injury_rec')['s'].min(), label='min_speed',color='brown')\nplt.legend()\nplt.xlabel('Speed')\nplt.ylabel('Frequency')\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#The graph shows the maximum speed of players who got injured both in Natural and Synthetic Surface.\ninjury_Nat=injury_record[injury_record['Surface'] == 'Natural']['PlayKey'].tolist()\ninjury_Syn=injury_record[injury_record['Surface'] == 'Synthetic']['PlayKey'].tolist()\nplayer_track_record.query('PlayKey in @injury_Nat')['s'].plot.hist(bins=30,color='blue', label='Natural')\nplayer_track_record.query('PlayKey in @injury_Syn')['s'].plot.hist(bins=30,color='orange', label='Synthetic',)\nplt.axvline(player_track_record.query('PlayKey in @injury_Nat')['s'].max(), label='max_speed_N',color='green')\nplt.axvline(player_track_record.query('PlayKey in @injury_Syn')['s'].max(), label='max_speed_S',color='red')\nplt.legend()\nplt.xlabel('Speed')\nplt.ylabel('Frequency')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"player_track_record.query('PlayKey in @injury_Nat')['s'].plot.hist(bins=30,color='blue', label='Natural')\nplayer_track_record.query('PlayKey in @injury_Syn')['s'].plot.hist(bins=30,color='orange', label='Synthetic',)\nplt.axvline(player_track_record.query('PlayKey in @injury_Nat')['s'].skew(),color='red', label='Natural_sk')\nplt.axvline(player_track_record.query('PlayKey in @injury_Syn')['s'].skew(),color='green', label='Synthetic_sk')\nplt.xlabel('Speed')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"player_track_record.query('PlayKey in @injury_Nat')['dir'].plot.hist(bins=30,color='blue', label='Natural')\nplayer_track_record.query('PlayKey in @injury_Syn')['dir'].plot.hist(bins=30,color='brown', label='Synthetic',)\nplt.axvline(player_track_record.query('PlayKey in @injury_Nat')['dir'].skew(),color='yellow')\nplt.axvline(player_track_record.query('PlayKey in @injury_Syn')['dir'].skew(),color='green')\nplt.legend()\nplt.xlabel('direction')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"player_track_record.query('PlayKey in @injury_Nat')['o'].plot.hist(bins=30,color='blue', label='Natural')\nplayer_track_record.query('PlayKey in @injury_Syn')['o'].plot.hist(bins=30,color='brown', label='Synthetic',)\nplt.axvline(player_track_record.query('PlayKey in @injury_Nat')['o'].skew(),color='yellow')\nplt.axvline(player_track_record.query('PlayKey in @injury_Syn')['o'].skew(),color='green')\nplt.legend()\nplt.xlabel('O')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Analysis Continues..........."}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}