{"cells":[{"metadata":{},"cell_type":"markdown","source":"<h1>NFL 1st and future- Analytics</h1>\n<img src='https://1ycbx02rgnsa1i87hd1i7v1r-wpengine.netdna-ssl.com/wp-content/uploads/2019/01/nfl.png'>\n<h2>About NFL</h2>\n<p>The National Football League is America's most popular sports league, comprised of 32 franchises that compete each year to win the Super Bowl, the world's biggest annual sporting event. Founded in 1920, the NFL developed the model for the successful modern sports league, including national and international distribution, extensive revenue sharing, competitive excellence, and strong franchises across the country.\n\nThe NFL is committed to advancing progress in the diagnosis, prevention and treatment of sports-related injuries. The NFL's ongoing health and safety efforts include support for independent medical research and engineering advancements and a commitment to work to better protect players and make the game safer, including enhancements to medical protocols and improvements to how our game is taught and played.\n\nAs more is learned, the league evaluates and changes rules to evolve the game and try to improve protections for players. Since 2002 alone, the NFL has made 50 rules changes intended to eliminate potentially dangerous tactics and reduce the risk of injuries.\n\nFor more information about the NFL's health and safety efforts, please visit <a href='www.PlaySmartPlaySafe.com'>the NFL's health and safety efforts</a></p>"},{"metadata":{},"cell_type":"markdown","source":"<h2>The Challenge</h2>\n<p>In 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 with natural turf (Mack et al., 2018; Loughran et al., 2019). In conjunction with the epidemiologic investigations, biomechanical studies of football cleat-surface interactions have shown that synthetic turf surfaces do not release cleats as readily as natural turf and may contribute to the incidence of non-contact lower limb injuries (Kent et al., 2015). Given these differences in cleat-turf interactions, it has yet to be determined whether player movement patterns and other measures of player performance differ across playing surfaces and how these may contribute to the incidence of lower limb injury.\n\nNow, the NFL is challenging Kagglers to help them examine the effects that playing on synthetic turf versus natural turf can have on player movements and the factors that may contribute to lower extremity injuries. NFL player tracking, also known as Next Gen Stats, is the capture of real time location data, speed and acceleration for every player, every play on every inch of the field. As part of this challenge, the NFL has provided full player tracking of on-field position for 250 players over two regular season schedules. One hundred of the athletes in the study data set sustained one or more injuries during the study period that were identified as a non-contact injury of a type that may have turf interaction as a contributing factor to injury. The remaining 150 athletes serve as a representative sample of the larger NFL population that did not sustain a non-contact lower-limb injury during the study period. Details of the surface type and environmental parameters that may influence performance and outcome are also provided.</p>"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"injury_rec=pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/InjuryRecord.csv')\nplayer_trackdata=pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/PlayerTrackData.csv')\nplay=pd.read_csv('/kaggle/input/nfl-playing-surface-analytics/PlayList.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h2>EDA of Injury Record Data</h2>\n<p>first We are going to ask ourselves a questions and answer it from the datasets</p>\n<ul>\n    <li>Making a pie chart of the fields to Know which field has the greater percentage</li>\n    <li>I will determine the severity of the injury through the number of missing days in Injury record dataset</li>\n    <li>Making a pie chart of injury type</li>\n    <li>Comparing between the injuries that happened in Synthetic field and natural field</li>\n    <li>Comparing between the injury-types that happened in Synthetic field and natural field</li>\n    <li>let's see the injury type in each body part in synthetic and natural field</li>\n<ul>"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.style.use('fivethirtyeight')\nsns.set_style('whitegrid')\ninjury_rec['Surface'].value_counts().plot.pie(autopct='%.1f%%',\n                                              shadow=True)\nplt.title('Comparing injuries in different fields')\nplt.ylabel('')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_rec['injury_type']=injury_rec[['DM_M1','DM_M7','DM_M28','DM_M42']].sum(axis=1)\ndef change_vals(i):\n    if i==1:\n        return 'Light Injury'\n    elif i==2:\n        return 'Medium Injury'\n    elif i==3:\n        return 'Almost Serious Injury'\n    else:\n        return 'Serious Injury'\ninjury_rec['injury_type']=injury_rec['injury_type'].apply(change_vals)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injury_rec['injury_type'].value_counts(normalize=True).plot.pie(autopct='%.1f%%',\n                                                                shadow=True,\n                                                               colors=['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'])\nplt.title('Injury-Types of NFL players')\nplt.ylabel('')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<p>from the chart above we can see that most of injuries takes 7 days to return to games</p> "},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x='Surface',hue='BodyPart',data=injury_rec)\nplt.legend(loc='best')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<p>In Synthetic Surface there are alot of ankle injuries and toes in comparison with Natural Surface, there are no heel injuries in Synthetic, equally knee injuries</p>"},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(x='Surface',hue='injury_type',data=injury_rec)\nplt.legend(loc='best')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<p>we can see that there are alot of injuries happened in synthetic are Serious and almost serious comparing with natural</p>"},{"metadata":{},"cell_type":"markdown","source":"<h3> In Synthetic Surface</h3>"},{"metadata":{"trusted":true},"cell_type":"code","source":"body_part=injury_rec['BodyPart'].unique().tolist()\nfig=plt.figure(figsize=(15,3))\nfor i in range(len(body_part)-1):\n    ax=fig.add_subplot(1,5,i+1)\n    c=injury_rec[(injury_rec['BodyPart']==body_part[i])&(injury_rec['Surface']=='Synthetic')]['injury_type'].value_counts(normalize=True)*100\n    c_lst=c.tolist()\n    ax.bar(c.index,c_lst,width=0.4)\n    ax.set_title('{} in Synthetic'.format(body_part[i]))\n    ax.set_ylabel('')\n    xlabels=[i for i in c.index]\n    ax.set_xticklabels(xlabels, rotation=90)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h3>In Natural Surface</h3>"},{"metadata":{"trusted":true},"cell_type":"code","source":"body_part=injury_rec['BodyPart'].unique().tolist()\nfig=plt.figure(figsize=(15,3))\nfor i in range(len(body_part)):\n    ax=fig.add_subplot(1,5,i+1)\n    c=injury_rec[(injury_rec['BodyPart']==body_part[i])&(injury_rec['Surface']=='Natural')]['injury_type'].value_counts(normalize=True)*100\n    c_lst=c.tolist()\n    ax.bar(c.index,c_lst,width=0.4,color='red')\n    ax.set_title('{} in Natural'.format(body_part[i]))\n    ax.set_ylabel('')\n    xlabels=[i for i in c.index]\n    ax.set_xticklabels(xlabels, rotation=90)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h2>EDA of Play List data</h2>\n<ul>\n    <li>Use the playkey to See the Temperature, Weather, position of players who have been \n    injured</li>\n    <li>Examine which position more exposed to injuries</li>\n    <li>Examine which position more exposed to injuries</li>\n    <li>Box plot of temperature</li>\n    <li>let's see if the weather is effective </li>\n</ul>"},{"metadata":{"trusted":true},"cell_type":"code","source":"injured_full_data=injury_rec.merge(play)\nlen(injured_full_data)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<p>77 link between injury record data and playlist data</p>"},{"metadata":{"trusted":true},"cell_type":"code","source":"injured_full_data['PlayType'].value_counts().plot.barh(figsize=(15,5))\nplt.title('play-type of injured players')\nplt.xlabel('frequency')\nplt.ylabel('play-type')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<p>most of injuries happen in Pass and Rush</p>"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(16,5))\nplt.subplot(1,2,1)\ninjured_full_data['Position'].value_counts().plot.barh()\nplt.title('Position of injured players')\nplt.xlabel('frequency')\nplt.ylabel('')\nplt.subplot(1,2,2)\nplay['Position'].value_counts().plot.barh()\nplt.title('Position of all players')\nplt.xlabel('frequency')\nplt.ylabel('')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<p>there are positions not exist in injured players, (WR,OLB,CB) positions are the most positions exposed to injury</p>  "},{"metadata":{"trusted":true},"cell_type":"code","source":"injured_full_data['Temperature'].plot.kde(label='injured players')\nplay['Temperature'].plot.kde(color='red',label='all players')\nplt.axvline(injured_full_data['Temperature'].mean(),c='green',label='inj_mean')\nplt.axvline(play['Temperature'].mean(),c='yellow',label='all_mean')\nplt.legend(loc='best')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"injured_full_data['Weather'].value_counts().plot.barh(figsize=(15,10))\nplt.title('Weather')\nplt.xlabel('frequency')\nplt.ylabel('Weather-type')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<p>most of injuries happened in (Cloudy,Partly Cloudy,Sunny)</p>"},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(6,16))\nplt.subplot(2,1,1)\ninjured_full_data[injured_full_data['Surface']=='Synthetic']['Weather'].value_counts().plot.barh(figsize=(15,10),color='red')\nplt.title('Weather in Synthetic')\nplt.ylabel('')\nplt.subplot(2,1,2)\ninjured_full_data[injured_full_data['Surface']=='Natural']['Weather'].value_counts().plot.barh(figsize=(15,10))\nplt.title('Weather in Natural')\nplt.xlabel('frequency')\nplt.ylabel('')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h2>EDA of Player Track-data</h2>"},{"metadata":{"trusted":true},"cell_type":"code","source":"player_trackdata['s'].plot.hist(bins=20)\nplt.axvline(player_trackdata['s'].mean(),color='red',label='mean')\nplt.axvline(player_trackdata['s'].median(),color='green',label='median')\nplt.axvline(player_trackdata['s'].std(),color='blue',label='std')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h3>Plot the distribution of speed of injured players</h3>"},{"metadata":{"trusted":true},"cell_type":"code","source":"inj_players=injury_rec['PlayKey'].tolist()\nplayer_trackdata.query('PlayKey in @inj_players')['s'].plot.hist(bins=20)\nplt.title('Distribution of speed of injured players')\nplt.xlabel('speed')\nplt.ylabel('frequency')\nplt.axvline(player_trackdata.query('PlayKey in @inj_players')['s'].mean(),label='mean',color='red')\nplt.axvline(player_trackdata.query('PlayKey in @inj_players')['s'].std(),label='std',color='blue')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h3>Plot the distribution of Orientation of injured players</h3>"},{"metadata":{"trusted":true},"cell_type":"code","source":"player_trackdata.query('PlayKey in @inj_players')['o'].plot.hist(bins=30)\nplt.title('Distribution of Orientation of injured players')\nplt.xlabel('Orientation')\nplt.ylabel('frequency')\nplt.axvline(player_trackdata.query('PlayKey in @inj_players')['o'].mean(),label='mean',color='red')\nplt.axvline(player_trackdata.query('PlayKey in @inj_players')['o'].std(),label='std',color='blue')\nplt.legend(loc='best')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h3>Plot the distribution of Direction of injured players</h3>"},{"metadata":{"trusted":true},"cell_type":"code","source":"player_trackdata.query('PlayKey in @inj_players')['dir'].plot.hist(bins=30)\nplt.title('Distribution of Direction of injured players')\nplt.xlabel('Direction')\nplt.ylabel('frequency')\nplt.axvline(player_trackdata.query('PlayKey in @inj_players')['dir'].mean(),label='mean',color='red')\nplt.axvline(player_trackdata.query('PlayKey in @inj_players')['dir'].std(),label='std',color='blue')\nplt.legend(loc='best')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<h1>More Investigations</h1>"},{"metadata":{"trusted":true},"cell_type":"code","source":"inj_synth=injury_rec[injury_rec['Surface']=='Synthetic']['PlayKey'].tolist()\ninj_natural=injury_rec[injury_rec['Surface']=='Natural']['PlayKey'].tolist()\nplayer_trackdata.query('PlayKey in @inj_synth')['s'].plot.hist(bins=20,alpha=0.4,label='Synthetic')\nplayer_trackdata.query('PlayKey in @inj_natural')['s'].plot.hist(bins=20,color='red',alpha=0.4,label='natural')\nplt.title('injured players in synthetic VS Natural')\nplt.xlabel('Speed')\nplt.ylabel('frequency')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('mean of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['s'].mean())\nprint('mean of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['s'].mean())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('std of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['s'].std())\nprint('std of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['s'].std())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('skewness of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['s'].skew())\nprint('skewness of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['s'].skew())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('kurtosis of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['s'].kurtosis())\nprint('kurtosis of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['s'].kurtosis())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"player_trackdata.query('PlayKey in @inj_synth')['o'].plot.hist(bins=20,alpha=0.4,label='Synthetic')\nplayer_trackdata.query('PlayKey in @inj_natural')['o'].plot.hist(bins=20,color='red',alpha=0.4,label='natural')\nplt.title('injured players in synthetic VS Natural')\nplt.xlabel('Orientation')\nplt.ylabel('frequency')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('mean of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['o'].mean())\nprint('mean of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['o'].mean())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('std of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['o'].std())\nprint('std of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['o'].std())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('skewness of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['o'].skew())\nprint('skewness of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['o'].skew())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('kurtosis of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['o'].kurtosis())\nprint('kurtosis of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['o'].kurtosis())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"player_trackdata.query('PlayKey in @inj_synth')['dir'].plot.hist(bins=20,alpha=0.4,label='Synthetic')\nplayer_trackdata.query('PlayKey in @inj_natural')['dir'].plot.hist(bins=20,color='red',alpha=0.4,label='natural')\nplt.title('injured players in synthetic VS Natural')\nplt.xlabel('Direction')\nplt.ylabel('frequency')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('mean of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['dir'].mean())\nprint('mean of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['dir'].mean())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('std of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['dir'].std())\nprint('std of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['dir'].std())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('skewness of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['dir'].skew())\nprint('skewness of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['dir'].skew())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('kurtosis of injured players in synthetic field: ',player_trackdata.query('PlayKey in @inj_synth')['dir'].kurtosis())\nprint('kurtosis of injured players in natural field: ',player_trackdata.query('PlayKey in @inj_natural')['dir'].kurtosis())","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}