{"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_minor":4,"nbformat":4,"cells":[{"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)\nimport seaborn as sns\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 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-06T14:43:02.801652Z","iopub.execute_input":"2022-01-06T14:43:02.802286Z","iopub.status.idle":"2022-01-06T14:43:02.810594Z","shell.execute_reply.started":"2022-01-06T14:43:02.802247Z","shell.execute_reply":"2022-01-06T14:43:02.809890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Introduction**\n\nIn this notebook the variables affecting field goal success rate will be discussed, graphed and illustrated using the data provided by the NFL 2022 Databowl. Some key words will be defined here. \n\nAmerican football is played on a rectangular 360 feet length by 160 feet width game field. The goal of american football is to score as many points and to stop the opposing team from scoring points. There are essentialy 2 ways for scoring in american football. A field goal or a touch down. A touch down which is worth 6 points and enables the scoring team to gain extra points which is called conversion by either scoring another touchdown or a field goal from the 15 yard line. If a field goal is missed no points are given and the other team then starts on offence. Therefore it is integral that the success rate for an attempted field is as high as possible to increase the chance of winning. ","metadata":{}},{"cell_type":"markdown","source":"# Analysis of field goal data\n\n","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"plays_data = pd.read_csv('../input/nfl-big-data-bowl-2022/plays.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:43:02.812208Z","iopub.execute_input":"2022-01-06T14:43:02.812447Z","iopub.status.idle":"2022-01-06T14:43:02.933707Z","shell.execute_reply.started":"2022-01-06T14:43:02.812419Z","shell.execute_reply":"2022-01-06T14:43:02.932644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Selecting all the data that have the tag of Field goal in the special teams play type data column. This is to make sure there aren't any mixups between the different types of special teams.","metadata":{}},{"cell_type":"code","source":"kickoff_data = plays_data.loc[plays_data['specialTeamsPlayType'] == 'Field Goal']\nkickoff_data.count()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:43:02.935469Z","iopub.execute_input":"2022-01-06T14:43:02.935702Z","iopub.status.idle":"2022-01-06T14:43:02.955455Z","shell.execute_reply.started":"2022-01-06T14:43:02.935675Z","shell.execute_reply":"2022-01-06T14:43:02.954564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The results above show that for kickLength there are 2606 counts of data and for yardsToGo there are 2657 points of data. Therefore 51 data points are included in the set for yardsToGo data column which aren't for the kickLength.","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"kickoff_data.specialTeamsResult.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:43:02.956874Z","iopub.execute_input":"2022-01-06T14:43:02.957676Z","iopub.status.idle":"2022-01-06T14:43:02.970591Z","shell.execute_reply.started":"2022-01-06T14:43:02.957625Z","shell.execute_reply":"2022-01-06T14:43:02.969652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kickoff_data.kickLength.loc[kickoff_data['specialTeamsResult'] == 'Blocked Kick Attempt'].count() + kickoff_data.kickLength.loc[kickoff_data['specialTeamsResult'] == 'Non-Special Teams Result '].count()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:43:02.973617Z","iopub.execute_input":"2022-01-06T14:43:02.973955Z","iopub.status.idle":"2022-01-06T14:43:02.983033Z","shell.execute_reply.started":"2022-01-06T14:43:02.973912Z","shell.execute_reply":"2022-01-06T14:43:02.982092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For kick Length Blocked Kcik Attempt and Non-special teams result are not included in the kick length data which accounts for the different quantities in data for yards to go and for kick length. This can be seen in the calculation above where all of the kicklength data rows containing either blocked Kick Attempts or Non-Special team results are in total 0. Therefore either for each Blocked Kick attempts the kick Length should be made 0 and the Non-special teams should be included as well or both should be excluded from the data set. Here this data will be excluded.","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig1, ax1 = plt.subplots()\nlabels = ['Kick Attempt Good','Kick Attempt No Good','Other','','','']\nl = ax1.pie(kickoff_data['specialTeamsResult'].value_counts(), labels=labels, autopct='%1.1f%%', startangle=45)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:43:02.984579Z","iopub.execute_input":"2022-01-06T14:43:02.984896Z","iopub.status.idle":"2022-01-06T14:43:03.121249Z","shell.execute_reply.started":"2022-01-06T14:43:02.984854Z","shell.execute_reply":"2022-01-06T14:43:03.120405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The chart above is a Pie chart showing the distrubution of field goal attempts. This shows generally that most field goal attempts are successfull. But 16.5% of the field goals are not. Therefore visualizing the the kick attempts may give insights into why there are unsuccessful kick attempts.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(24,16))\nplt.subplot(1, 2, 1)\nplt.ylabel(\"Field goal attempt count for each yardsToGo value\")\nplt.xlabel('The attempted field goal for each yards to go in yards')\nplt.title(\"Bar plot showing the number of Field Goal attempts for each value in the yardsToGo column\")\n#kickoff_data.loc[kickoff_data['specialTeamsResult'] == 'Kick Attempt Good'].value_counts(kickoff_data['kickLength']).sort_index(axis=0).plot(kind='bar')\nkickoff_data.value_counts(kickoff_data['yardsToGo']).sort_index(axis=0).plot(kind='bar')\n\n\nplt.subplot(1, 2, 2)\nplt.ylabel(\"Field goal attempt count for each kickLength value\")\nplt.xlabel('The attempted field goal for each kick length in yards')\nplt.title(\"Bar plot showing the number of Field Goal attempts for each value in the kickLength column\")\n#plt.figure(figsize=(12,8))\nkickoff_data.value_counts(kickoff_data['kickLength']).sort_index(axis=0).plot(kind='bar', color='green')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:43:03.123040Z","iopub.execute_input":"2022-01-06T14:43:03.123569Z","iopub.status.idle":"2022-01-06T14:43:04.108244Z","shell.execute_reply.started":"2022-01-06T14:43:03.123521Z","shell.execute_reply":"2022-01-06T14:43:04.107408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The total number of yards to go and the total number of kick length are the same for the exception that kick length does not take blocked kicks into account which the yards to go does. \n\nThe two graphs above shows the count of all field goal attempts (except for kickLength blocked shots). As can be seen above the two graphs are showing two different distributions of between the yards to go count bar graph and the kick length count bar graph. This means that either there is faulty data or that the kickers are different distances away from the yard line. The data from the two graphs should in theory line up perfectly but since kickers choose varying distances from the each other since in theory the kick length count bar graph should be the same as the yards to go count bar graph for the exeption that the kick length count bar  graph should start 17 yards after the yards to go count bar graph since the kick will be taken 7 yards behind the yard line and there are 10 yards from the goal line to the field goal. Which in total are 17 yards difference between yards to go and the kick length.\n\nThe total number of yards to go and the total number of kick length are the same for the exception that kick length does not take blocked kicks into account which the yards to go does.\nTherefore the blocked kicks distribution needs to be visualized and compared the graphs above","metadata":{}},{"cell_type":"code","source":"kickoff_data.loc[kickoff_data['specialTeamsResult'] == 'Blocked Kick Attempt'].value_counts(kickoff_data['yardsToGo']).sort_index(axis=0).plot(kind='bar')\nplt.ylabel(\"Count of blocked field goals\")\nplt.xlabel('The attempted field goal for each yards to go in yards')\nplt.title(\"Bar plot showing the count of the blocked field goal attempts from the yardsToGo column\")","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:43:04.109791Z","iopub.execute_input":"2022-01-06T14:43:04.110267Z","iopub.status.idle":"2022-01-06T14:43:04.377454Z","shell.execute_reply.started":"2022-01-06T14:43:04.110224Z","shell.execute_reply":"2022-01-06T14:43:04.376749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The blocked field goal attempt are distributed within the means of expectation. The highest number of attempted shots which is 10 will likely also have the highest number of blocked shots which the graph shows above. But what is interesting that even though there is a very limited number of blocked shots that there for higher yardsToGo that are relativily increases in the chance of being blocked. This could be because as the further the kicker is away from the field goal the closer the kicker will be to the scrimmage line. This could cause then that the kicker is more likely to be blocked than if not. ","metadata":{}},{"cell_type":"markdown","source":"Above displays a bar graph of the count of blocked field goal attempts. Since most shots are taken ","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(24,16))\nplt.subplot(1, 2, 1)\n#plt.figure(figsize=(12,8))\nkickoffratio = kickoff_data.loc[kickoff_data['specialTeamsResult'] == 'Kick Attempt Good'].loc[:,['yardsToGo','specialTeamsResult']].value_counts(kickoff_data['yardsToGo']).sort_index(axis=0) / kickoff_data.loc[:,['yardsToGo','specialTeamsResult']].value_counts(kickoff_data['yardsToGo']).sort_index(axis=0)\nplt.ylabel(\"Field goal attempt average percentage success rate for each yardsToGo value\")\nplt.xlabel('The attempted field goal for each yards to go in yards')\nplt.title(\"Bar plot showing the average percentage success rate of Field Goal attempts for each value in the yardsToGo column\")\nkickoffratio.plot(kind='bar', color = 'gold')\n\nplt.subplot(1, 2, 2)\n#plt.figure(figsize=(12,8))\nkickoffratio = kickoff_data.loc[kickoff_data['specialTeamsResult'] == 'Kick Attempt Good'].loc[:,['kickLength','specialTeamsResult']].value_counts(kickoff_data['kickLength']).sort_index(axis=0) / kickoff_data.loc[:,['kickLength','specialTeamsResult']].value_counts(kickoff_data['kickLength']).sort_index(axis=0)\nplt.ylabel(\"Field goal attempt average percentage success ratefor each kickLength value\")\nplt.xlabel('The attempted field goal for each kick length in yards')\nplt.title(\"Bar plot showing the average percentage success rate of Field Goal attempts for each value in the kickLength column\")\nkickoffratio.plot(kind='bar', color='mediumorchid')","metadata":{"execution":{"iopub.status.busy":"2022-01-06T14:43:04.378753Z","iopub.execute_input":"2022-01-06T14:43:04.379171Z","iopub.status.idle":"2022-01-06T14:43:05.366084Z","shell.execute_reply.started":"2022-01-06T14:43:04.379125Z","shell.execute_reply":"2022-01-06T14:43:05.365103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Above are two bar plots showing the average percentage success rate for each attempted field goal for every yards to Go length and the average percentage success rate for each attempted field goal for every kick length. Surprisingly is that the average kick length success rate in total decreases as the kick length increases but does so in periods. For example the average success rate of a kick length of 30 is higher than that of 27. \nThis may suggest that the further the kicker attempts a field goal from the line of scrimmage, the higher the average chances of landing a successful field goal are. Which would be supported by the Bar plot showing the count of the blocked field goal attempts from the yardsToGo column. ","metadata":{}},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"markdown","source":"Sadly I ran out of time before I could fully finalize the notebook to clearly show supporting for my conclusion but further investigation into if the kicker stands further away from the line of scrimmage increases the chances of landing a successful field goal. This can be tested by comparing the mean distance from the kicker to the defensive linemen by using the tracking data for 2018-20 and link them using the tracking ID. Furthermore weather data could be incorporated especially for the longer shots how that affects successful field goals, especially wind direction, strength, and precipitation strength and type. ","metadata":{}},{"cell_type":"markdown","source":"# Conclusion\n","metadata":{}},{"cell_type":"markdown","source":"In conlcusion there is definitely some form of evidence that the further the kicker stands from the line of scrimmage the higher the chance of an attempted field goal is. This information can be used to intentionaly set the kicker a bit further away from the scrimmage line. There could be a couple of variables why this happens some of which will be discussed here. There might be more psychological pressure if the kicker is standing closer to the defensive linemen which may decrease the kickers shooting accuracy. The kicker may also be distracted more by the defensive linemen trying to break trough causing a lower sucessful field goal chance.\n\nThank you for reading my notebook and if you have any comments or suggestions (which there are bound to be many) please just comment and let me know.","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}}]}