{"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)\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 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","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-10-30T13:27:59.343697Z","iopub.execute_input":"2021-10-30T13:27:59.344194Z","iopub.status.idle":"2021-10-30T13:27:59.373612Z","shell.execute_reply.started":"2021-10-30T13:27:59.344111Z","shell.execute_reply":"2021-10-30T13:27:59.372772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"table-of-contents\"></a>\n<h1 style='background:#B2FF33; border:0;'><center>Table of Contents</center></h1>\n\n## [1. Introduction](#1)\n### [1.1 Loading of Libraries](#1.1)\n### [1.2 Data Loading](#1.2)\n## [2. Data Exploration](#2)\n### [2.1 Automated EDA](#2.1)\n## [3. Features Analysis](#3)\n## [Work In Progress](#999)","metadata":{}},{"cell_type":"markdown","source":"###### [back to top](#table-of-contents)\n###### [¶](#1)\n<h1 style='background:#B2FF33; border:0;'><center>1. Introduction</center></h1>","metadata":{}},{"cell_type":"markdown","source":"| NFL Logo | NFL Team Locations |\n| :---: | :-----: |\n| ![NFL](https://upload.wikimedia.org/wikipedia/en/thumb/a/a2/National_Football_League_logo.svg/188px-National_Football_League_logo.svg.png) |   ![NFL Teams](https://upload.wikimedia.org/wikipedia/commons/thumb/e/e0/US_National_Football_League_Teams_Location-en.svg/500px-US_National_Football_League_Teams_Location-en.svg.png)   |\n\n\n\n\n\n###### Some Info from Wikipedia\n###### The 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 North American professional sports leagues, the highest professional level of American football in the world.\n\n###### The NFL's eighteen-week regular season runs from early September to early January, with each team playing seventeen games and having one bye week. Following the conclusion of the regular season, seven teams from each conference (four division winners and three wild card teams) advance to the playoffs, a single-elimination tournament culminating in the Super Bowl, which is usually held on the first Sunday in February and is played between the champions of the NFC and AFC. The league is headquartered in New York City.\n\n###### The NFL was formed in 1920 as the American Professional Football Association (APFA) before renaming itself the National Football League for the 1922 season. After initially determining champions through end-of-season standings, a playoff system was implemented in 1933 that culminated with the NFL Championship Game until 1966. Following an agreement to merge the NFL with the rival American Football League (AFL), the Super Bowl was first held in 1967 to determine a champion between the best teams from the two leagues and has remained as the final game of each NFL season since the merger was completed in 1970.\n\n###### 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 also among the biggest club sporting events in the world, with the individual games accounting for many of the most watched television programs in American history and all occupying the Nielsen's Top 5 tally of the all-time most watched U.S. television broadcasts by 2015. The NFL is the wealthiest professional sports league by revenue, and the sports league with the most valuable teams.","metadata":{}},{"cell_type":"markdown","source":"###### [back to top](#table-of-contents)\n###### [¶](#1.1)\n<h3 style='background:#B2FF33; border:0;'><center>1.1 Loading of libraries</center></h3>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n\nplt.style.use('fivethirtyeight')","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-10-30T13:27:59.531551Z","iopub.execute_input":"2021-10-30T13:27:59.531869Z","iopub.status.idle":"2021-10-30T13:28:00.592279Z","shell.execute_reply.started":"2021-10-30T13:27:59.531836Z","shell.execute_reply":"2021-10-30T13:28:00.591223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###### [back to top](#table-of-contents)\n###### [¶](#1.2)\n<h3 style='background:#B2FF33; border:0;'><center>1.2 Loading of data</center></h3>","metadata":{}},{"cell_type":"markdown","source":"### Game data Dateset has teams info in each game. The fields are\n* gameId: Game identifier, unique (numeric)\n* season: Season Year (numeric)\n* week: Game week (numeric)\n* gameDate: Game Date (time, mm/dd/yyyy)\n* gameTimeEastern: Start time of game (time, HH:MM:SS, Eastern Standard Time)\n* homeTeamAbbr: Home team three-letter code (text)\n* visitorTeamAbbr: Visiting team three-letter code (text)","metadata":{}},{"cell_type":"code","source":"nflgames = pd.read_csv('../input/nfl-big-data-bowl-2022/games.csv')\nnflgames.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:00.596353Z","iopub.execute_input":"2021-10-30T13:28:00.596731Z","iopub.status.idle":"2021-10-30T13:28:00.630523Z","shell.execute_reply.started":"2021-10-30T13:28:00.596699Z","shell.execute_reply":"2021-10-30T13:28:00.629437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nflscouting = pd.read_csv('../input/nfl-big-data-bowl-2022/PFFScoutingData.csv')\nnflscouting.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:00.631901Z","iopub.execute_input":"2021-10-30T13:28:00.632128Z","iopub.status.idle":"2021-10-30T13:28:00.748547Z","shell.execute_reply.started":"2021-10-30T13:28:00.632103Z","shell.execute_reply":"2021-10-30T13:28:00.747573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nflplayers = pd.read_csv('../input/nfl-big-data-bowl-2022/players.csv')\nnflplayers.head()\nnflplayers.dtypes","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:00.751153Z","iopub.execute_input":"2021-10-30T13:28:00.751485Z","iopub.status.idle":"2021-10-30T13:28:00.776286Z","shell.execute_reply.started":"2021-10-30T13:28:00.751445Z","shell.execute_reply":"2021-10-30T13:28:00.775687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nflplays = pd.read_csv('../input/nfl-big-data-bowl-2022/plays.csv')\nnflplays.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:00.777334Z","iopub.execute_input":"2021-10-30T13:28:00.777672Z","iopub.status.idle":"2021-10-30T13:28:00.947605Z","shell.execute_reply.started":"2021-10-30T13:28:00.777616Z","shell.execute_reply":"2021-10-30T13:28:00.946461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nfltracking2018 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2018.csv')\nnfltracking2018.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:00.948855Z","iopub.execute_input":"2021-10-30T13:28:00.949109Z","iopub.status.idle":"2021-10-30T13:28:48.831706Z","shell.execute_reply.started":"2021-10-30T13:28:00.949080Z","shell.execute_reply":"2021-10-30T13:28:48.830366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###### [back to top](#table-of-contents)\n###### [¶](#2)\n<h3 style='background:#B2FF33; border:0;'><center>2. Data Exploration</center></h3>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n\nplt.style.use('fivethirtyeight')\n\nfig, ax = plt.subplots(figsize=(12, 8))\nnfltracking2018.query('gameId == 2018091001').groupby('team') \\\n    .plot(x='x', y='y', ax=ax, style='.')\nplt.legend().remove()","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:48.833026Z","iopub.execute_input":"2021-10-30T13:28:48.833301Z","iopub.status.idle":"2021-10-30T13:28:49.437886Z","shell.execute_reply.started":"2021-10-30T13:28:48.833272Z","shell.execute_reply":"2021-10-30T13:28:49.436812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(12, 8))\nnfltracking2018.query('gameId == 2018091609 and position == \"WR\"').groupby('team') \\\n    .plot(x='x', y='y', ax=ax, style='.')\nplt.legend().remove();","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:49.439562Z","iopub.execute_input":"2021-10-30T13:28:49.439918Z","iopub.status.idle":"2021-10-30T13:28:50.325857Z","shell.execute_reply.started":"2021-10-30T13:28:49.439880Z","shell.execute_reply":"2021-10-30T13:28:50.324693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Player data: \n* nflId: Player identification number, unique across players (numeric)\n* height: Player height (text)\n* weight: Player weight (numeric)\n* birthDate: Date of birth (YYYY-MM-DD)\n* collegeName: Player college (text)\n* position: Player position (text)\n* displayName: Player name (text)","metadata":{}},{"cell_type":"code","source":"import datetime\nfrom datetime import date\n\ndef calculate_age(born):\n    born = datetime.strptime(born, '%Y-%m-%d').date()\n    today = date.today()\n    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))\n\n\n","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:50.327271Z","iopub.execute_input":"2021-10-30T13:28:50.327496Z","iopub.status.idle":"2021-10-30T13:28:50.334257Z","shell.execute_reply.started":"2021-10-30T13:28:50.327472Z","shell.execute_reply":"2021-10-30T13:28:50.333350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_position = {\"WR\" : \"Wide Receiver\",\n                   \"CB\" : \"Cornerback\",\n                   \"RB\" : \"Running Back\",\n                   \"TE\" : \"Tight End\",\n                   \"OLB\" : \"Outside Linebacker\",\n                   \"QB\" : \"Quarterback\",\n                   \"FS\" : \"Free Safety\",\n                   \"LB\" : \"Linebacker\",\n                   \"SS\" : \"Strong Safety\",\n                   \"ILB\" : \"Inside Linebacker\",\n                   \"DE\" : \"Defensive End\",\n                   \"DB\" : \"Defensive Back\",\n                   \"MLB\" : \"Middle Linebacker\",\n                   \"DT\" : \"Defensive Tackle\",\n                   \"FB\" : \"Fullback\",\n                   \"P\" : \"Punter\",\n                   \"LS\" : \"Long snapper\",\n                   \"S\" : \"Safety\",\n                   \"K\" : \"Kicker\",\n                   \"HB\" : \"Running back\",\n                   \"NT\" : \"Nose Tackle\"}","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:50.337705Z","iopub.execute_input":"2021-10-30T13:28:50.338503Z","iopub.status.idle":"2021-10-30T13:28:50.348688Z","shell.execute_reply.started":"2021-10-30T13:28:50.338472Z","shell.execute_reply":"2021-10-30T13:28:50.347607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nflplayers = pd.read_csv('../input/nfl-big-data-bowl-2022/players.csv')\nnflplayers.head()\n\ncheck = nflplayers['height'].str.split('-',expand=True)\n\ncheck.columns = [\n    'first', \n    'second'\n]\n\n\ncheck.loc[(check['second'].notnull()), 'first'] = check[check['second'].notnull()]['first'].astype(np.int16) * 12 + check[check['second'].notnull()]['second'].astype(np.int16)\nnflplayers['height'] = check['first']\nnflplayers['height'] = nflplayers['height'].astype(np.float32)\nnflplayers['height'] = nflplayers['height'] /12\nnflplayers['height'] = nflplayers['height'].round(2)\n\n#nflplayers['birthDate'] = pd.to_datetime(nflplayers['birthDate'].str.strip(), format='%d/%m/%Y')\n#print(nflplayers.birthDate.str.contains('(\\d{2})[/](\\d{2})[/](\\d{4})'))\n\nnflplayers['birthDate'] = pd.to_datetime(nflplayers.birthDate)\n\nnflplayers['birthDate'] = nflplayers['birthDate'].dt.strftime('%Y-%m-%d')\nnflplayers['birthDate'].fillna('1998-01-01')\n\ndef elasped_years(date):\n    reference_year = pd.to_datetime('today').year\n    reference_month = pd.to_datetime('today').month\n    year = date.str.slice(0, 4).astype(np.float)\n    month = date.str.slice(5, 7).astype(np.float)\n    duration = np.floor((12 * (reference_year - year) + (reference_month - month)) / 12)\n    return(duration)\n\nnflplayers[\"LongPosition\"]=nflplayers[\"Position\"].map(player_position)\n\nnflplayers['age'] =  elasped_years(nflplayers['birthDate'])\n\nnflplayers.sort_values('birthDate', ascending=True)\nprint(nflplayers.isnull().sum())\nprint(nflplayers.Position.unique())\nprint(nflplayers.LongPosition.unique())","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:50.350131Z","iopub.execute_input":"2021-10-30T13:28:50.350658Z","iopub.status.idle":"2021-10-30T13:28:50.442475Z","shell.execute_reply.started":"2021-10-30T13:28:50.350604Z","shell.execute_reply":"2021-10-30T13:28:50.441628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\nfrom plotly.subplots import make_subplots\nimport plotly.graph_objs as go\n\npd.set_option('display.max_columns', None)\n\nfig = make_subplots(rows=3, cols=1)\ntraces = [\n    go.Histogram(\n        x=nflplayers[col[0]], \n        nbinsx=col[1], \n        name=col[0]\n    ) for col in [('height', 20), ('weight', 50), ('age',100)]\n]\n\nfor i in range(len(traces)):\n    fig.append_trace(\n        traces[i], \n        (i % 3) + 1,\n        1\n    )\n\nfig.update_layout(\n    title_text='Height & weight distributions',\n    height=800,\n    width=800,\n)\nfig.update_layout(showlegend=False)\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:50.443833Z","iopub.execute_input":"2021-10-30T13:28:50.444057Z","iopub.status.idle":"2021-10-30T13:28:52.615615Z","shell.execute_reply.started":"2021-10-30T13:28:50.444032Z","shell.execute_reply":"2021-10-30T13:28:52.614663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check = nflplayers['collegeName'].value_counts().reset_index()\n\ncheck.columns = [\n    'college', \n    'players'\n]\n\ncheck = check.sort_values('players',ascending=False).head(60)\n\nfig = px.bar(\n    check, \n    y='college', \n    x=\"players\", \n    orientation='h', \n    title='Top 60 colleges by number of players',\n    height=1900,\n    width=600,\n    color='college',\n    color_discrete_sequence=px.colors.qualitative.G10,\n)\nfig.update_layout(showlegend=False) \nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:52.617048Z","iopub.execute_input":"2021-10-30T13:28:52.617349Z","iopub.status.idle":"2021-10-30T13:28:53.918122Z","shell.execute_reply.started":"2021-10-30T13:28:52.617313Z","shell.execute_reply":"2021-10-30T13:28:53.917032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check = nflplayers['LongPosition'].value_counts().reset_index()\n\ncheck.columns = [\n    'LongPosition', \n    'players'\n]\n\ncheck = check.sort_values('players',ascending=False)\n\nfig = px.bar(\n    check, \n    y='LongPosition', \n    x=\"players\", \n    orientation='h', \n    title='Top playing position',\n    height=900,\n    width=600,\n    color='LongPosition',\n    color_discrete_sequence=px.colors.qualitative.G10,\n)\nfig.update_layout(showlegend=False) \nfig.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:53.919773Z","iopub.execute_input":"2021-10-30T13:28:53.920106Z","iopub.status.idle":"2021-10-30T13:28:54.101720Z","shell.execute_reply.started":"2021-10-30T13:28:53.920066Z","shell.execute_reply":"2021-10-30T13:28:54.100695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.pyplot import figure\nfrom matplotlib import ticker\n\n\ntracking2018pivot = pd.pivot_table(nfltracking2018, values=['dis'], \n                     columns='position',index='gameId',aggfunc=np.sum)\nnumelements = tracking2018pivot.shape[0]\nprint(numelements, numelements/10, (numelements/10)+1)\n\nfor i in range(1,int(round((numelements/10),0)+1)):\n    numrows = i*10\n    subpivotrows=tracking2018pivot.iloc[(i*10) - 10 : i*10 + 10]\n    x=(i*10) - 10\n    y=(i*10) \n    fig = plt.figure()\n    fig.set_figheight(numrows*4)\n    fig.set_figwidth(24)\n    for spine in ['top', 'right']:\n        ax.spines[spine].set_visible(False)\n    \n    if (i == 1):\n        ax = subpivotrows.plot(kind='barh', stacked=True)\n        ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))\n        plt.legend(fontsize=\"xx-small\") # using a named size\n    else:\n        ax = subpivotrows.plot(kind='barh', stacked=True, legend=False)\n    plt.xticks(fontsize=6, rotation=90)\n    plt.yticks(fontsize=6, rotation=0)\n    #plt.set_xlabel('Distance Covered',fontdict={'fontsize':8})\n    titletxt = \"Game Details for for the games \" + str(x) + \" and \" + str(y) \n    plt.title(titletxt)\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-10-30T13:28:54.103155Z","iopub.execute_input":"2021-10-30T13:28:54.103388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install autoviz\n!pip install xlrd","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from autoviz.AutoViz_Class import AutoViz_Class\nAV = AutoViz_Class()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = \"/kaggle/input/nfl-big-data-bowl-2022/plays.csv\"\nsep = \",\"\ndft = AV.AutoViz(\n    filename,\n    sep=\",\",\n    depVar=\"\",\n    dfte=None,\n    header=0,\n    verbose=0,\n    lowess=False,\n    chart_format=\"svg\"\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = \"/kaggle/input/nfl-big-data-bowl-2022/games.csv\"\nsep = \",\"\ndft = AV.AutoViz(\n    filename,\n    sep=\",\",\n    depVar=\"\",\n    dfte=None,\n    header=0,\n    verbose=0,\n    lowess=False,\n    chart_format=\"svg\"\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = \"/kaggle/input/nfl-big-data-bowl-2022/PFFScoutingData.csv\"\nsep = \",\"\ndft = AV.AutoViz(\n    filename,\n    sep=\",\",\n    depVar=\"\",\n    dfte=None,\n    header=0,\n    verbose=0,\n    lowess=False,\n    chart_format=\"svg\"\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = \"/kaggle/input/nfl-big-data-bowl-2022/tracking2018.csv\"\nsep = \",\"\ndft = AV.AutoViz(\n    filename,\n    sep=\",\",\n    depVar=\"\",\n    dfte=None,\n    header=0,\n    verbose=0,\n    lowess=False,\n    chart_format=\"svg\",\n    max_rows_analyzed=150000\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = \"/kaggle/input/nfl-big-data-bowl-2022/tracking2019.csv\"\nsep = \",\"\ndft = AV.AutoViz(\n    filename,\n    sep=\",\",\n    depVar=\"\",\n    dfte=None,\n    header=0,\n    verbose=0,\n    lowess=False,\n    chart_format=\"svg\",\n    max_rows_analyzed=150000\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###### [back to top](#table-of-contents)\n###### [¶](#3)\n<h3 style='background:#B2FF33; border:0;'><center>3. Features Analysis</center></h3>","metadata":{}},{"cell_type":"markdown","source":"###### [back to top](#table-of-contents)\n###### [¶](#999)\n<h3 style='background:#B2FF33; border:0;'><center>Work in Progress - More to come</center></h3>","metadata":{}}]}