{"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","execution":{"iopub.status.busy":"2021-11-14T14:19:44.633561Z","iopub.execute_input":"2021-11-14T14:19:44.634574Z","iopub.status.idle":"2021-11-14T14:19:44.642259Z","shell.execute_reply.started":"2021-11-14T14:19:44.634519Z","shell.execute_reply":"2021-11-14T14:19:44.641423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading in the data from the csv files.","metadata":{}},{"cell_type":"code","source":"df_gamedata=pd.read_csv('../input/nfl-big-data-bowl-2022/games.csv')\ndf_gamedata.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:19:47.733640Z","iopub.execute_input":"2021-11-14T14:19:47.734235Z","iopub.status.idle":"2021-11-14T14:19:47.769154Z","shell.execute_reply.started":"2021-11-14T14:19:47.734195Z","shell.execute_reply":"2021-11-14T14:19:47.768571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_plays=pd.read_csv('../input/nfl-big-data-bowl-2022/plays.csv')\ndf_plays.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:20:12.628385Z","iopub.execute_input":"2021-11-14T14:20:12.628772Z","iopub.status.idle":"2021-11-14T14:20:12.803150Z","shell.execute_reply.started":"2021-11-14T14:20:12.628731Z","shell.execute_reply":"2021-11-14T14:20:12.802247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_players=pd.read_csv('../input/nfl-big-data-bowl-2022/players.csv')\ndf_players.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:20:14.867660Z","iopub.execute_input":"2021-11-14T14:20:14.868263Z","iopub.status.idle":"2021-11-14T14:20:14.894375Z","shell.execute_reply.started":"2021-11-14T14:20:14.868216Z","shell.execute_reply":"2021-11-14T14:20:14.893771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_scouting=pd.read_csv('../input/nfl-big-data-bowl-2022/PFFScoutingData.csv')\ndf_scouting.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:20:17.136554Z","iopub.execute_input":"2021-11-14T14:20:17.137668Z","iopub.status.idle":"2021-11-14T14:20:17.250699Z","shell.execute_reply.started":"2021-11-14T14:20:17.137623Z","shell.execute_reply":"2021-11-14T14:20:17.249647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading in the tracking data for the year 2018.","metadata":{}},{"cell_type":"markdown","source":"### In this notebook,in general, I have worked upon data from the year 2018. However, the same could be done for the years 2019 and 2020.","metadata":{}},{"cell_type":"code","source":"df_t2018=pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2018.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:20:20.032447Z","iopub.execute_input":"2021-11-14T14:20:20.033550Z","iopub.status.idle":"2021-11-14T14:20:53.430371Z","shell.execute_reply.started":"2021-11-14T14:20:20.033492Z","shell.execute_reply":"2021-11-14T14:20:53.429369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t2018.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:20:53.432271Z","iopub.execute_input":"2021-11-14T14:20:53.432948Z","iopub.status.idle":"2021-11-14T14:20:53.454502Z","shell.execute_reply.started":"2021-11-14T14:20:53.432889Z","shell.execute_reply":"2021-11-14T14:20:53.453672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_players.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:20:53.456188Z","iopub.execute_input":"2021-11-14T14:20:53.456503Z","iopub.status.idle":"2021-11-14T14:20:53.474016Z","shell.execute_reply.started":"2021-11-14T14:20:53.456461Z","shell.execute_reply":"2021-11-14T14:20:53.472969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Some info regarding the data types and the number of null-values we have in the game data file.","metadata":{}},{"cell_type":"code","source":"df_gamedata.info()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:21:00.493708Z","iopub.execute_input":"2021-11-14T14:21:00.494045Z","iopub.status.idle":"2021-11-14T14:21:00.517995Z","shell.execute_reply.started":"2021-11-14T14:21:00.494009Z","shell.execute_reply":"2021-11-14T14:21:00.517033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Play data regarding the specific game ID of 2018090600","metadata":{}},{"cell_type":"code","source":"df_plays[df_plays['gameId']==2018090600].head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:24:07.385655Z","iopub.execute_input":"2021-11-14T14:24:07.385971Z","iopub.status.idle":"2021-11-14T14:24:07.412958Z","shell.execute_reply.started":"2021-11-14T14:24:07.385940Z","shell.execute_reply":"2021-11-14T14:24:07.412398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_plays[df_plays['gameId']==2018090600][:1]['playDescription']","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:24:09.213801Z","iopub.execute_input":"2021-11-14T14:24:09.214633Z","iopub.status.idle":"2021-11-14T14:24:09.223357Z","shell.execute_reply.started":"2021-11-14T14:24:09.214575Z","shell.execute_reply":"2021-11-14T14:24:09.222752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Displaying the 1st 5 rows of the tracking data of the year 2018","metadata":{}},{"cell_type":"code","source":"df_t2018.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:24:11.241510Z","iopub.execute_input":"2021-11-14T14:24:11.242626Z","iopub.status.idle":"2021-11-14T14:24:11.263625Z","shell.execute_reply.started":"2021-11-14T14:24:11.242543Z","shell.execute_reply":"2021-11-14T14:24:11.262665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t2018[df_t2018['displayName']=='Justin Tucker']","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:24:12.883439Z","iopub.execute_input":"2021-11-14T14:24:12.884446Z","iopub.status.idle":"2021-11-14T14:24:13.655500Z","shell.execute_reply.started":"2021-11-14T14:24:12.884381Z","shell.execute_reply":"2021-11-14T14:24:13.654653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Video output for ingame player movement","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nfrom skimage import io\nf='https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/AmFBfield.svg/1200px-AmFBfield.svg.png'\na = io.imread(f)\n# determining the height ratio\n# resize image and save\nfrom PIL import Image\nfrom matplotlib import cm\nimport matplotlib.pyplot as plt\nim = Image.fromarray(a)\nim = im.resize((120,53))\nplt.imshow(im)\nim.save('sompic.png') # Saving the resized background image","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:24:52.525524Z","iopub.execute_input":"2021-11-14T14:24:52.526214Z","iopub.status.idle":"2021-11-14T14:24:53.503221Z","shell.execute_reply.started":"2021-11-14T14:24:52.526162Z","shell.execute_reply":"2021-11-14T14:24:53.502422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t=df_t2018.loc[(df_t2018['time'].str.contains('2018-12-30',case=False))]","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:31:23.008090Z","iopub.execute_input":"2021-11-14T14:31:23.010035Z","iopub.status.idle":"2021-11-14T14:31:28.359010Z","shell.execute_reply.started":"2021-11-14T14:31:23.009930Z","shell.execute_reply":"2021-11-14T14:31:28.357920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T14:45:08.398620Z","iopub.execute_input":"2021-11-14T14:45:08.398976Z","iopub.status.idle":"2021-11-14T14:45:08.425880Z","shell.execute_reply.started":"2021-11-14T14:45:08.398937Z","shell.execute_reply":"2021-11-14T14:45:08.424902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# A sample Image for a particular datetime value and a particular match id ingame","metadata":{}},{"cell_type":"code","source":"import time\nfrom datetime import datetime, date, time, timedelta\nt['time'][len(t)-1]\ntime_string = \"2018-12-30 21:25:32.200\"\nformat_string = \"%Y-%m-%d %H:%M:%S.%f\"\n\nfor i in range(100):\n    date_object = datetime.strptime(time_string, format_string)\n    date_object = date_object + timedelta(milliseconds=100)\n    time_string=str(date_object)\n    if '.' in time_string:\n        time_string=time_string[:-3]\n    else:\n        time_string=time_string+'.000'\n    final_string=time_string.split(' ')\n    final_string=final_string[0]+'T'+final_string[1]\n    print(final_string)\n    break\nprint(len(t['time'].unique()))\nz=t.loc[(t['time'].str.contains('2018-12-30T21:25:32.200')) & (t['gameId']==2018123000)]\nz.index = range(len(z))\nplt.rcParams[\"figure.figsize\"] = [7.50, 3.50]\nplt.rcParams[\"figure.autolayout\"] = True\nf='sompic.png'\na = io.imread(f)\n#plt.imshow(a)\nfig=plt.figure()\nax = fig.add_subplot(111)\na = io.imread(f)\nprint(len(z))\nfor i in range(len(z)):\n    ax.imshow(a)\n    if z[i:i+1]['team'][i]=='home': \n        ax.plot(z[i:i+1]['x'][i],z[i:i+1]['y'][i],'ro',color='blue')\n    elif z[i:i+1]['team'][i]=='away':\n        ax.plot(z[i:i+1]['x'][i],z[i:i+1]['y'][i],'ro',color='red')\n    else:\n        ax.plot(z[i:i+1]['x'][i],z[i:i+1]['y'][i],'ro',color='yellow')\nax.set_xlim(0, 120)\nax.set_ylim(0, 53.3)\nax.axis('on')\nleg = ax.legend(loc='upper center')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T15:40:04.134994Z","iopub.execute_input":"2021-11-14T15:40:04.135322Z","iopub.status.idle":"2021-11-14T15:40:04.944781Z","shell.execute_reply.started":"2021-11-14T15:40:04.135285Z","shell.execute_reply":"2021-11-14T15:40:04.943767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Real in-game video analysis","metadata":{}},{"cell_type":"markdown","source":"## Here, I have provided video analysis of in-game footage of the players and the movement of the ball throughout the game. The ball is denoted by the yellow dot and the home team is denoted by the blue dots and the away team by red dots.\nYou can check the entire video out by downloading it after running the code from the /kaggle/working directory. Else, you could just (copy+paste) the following youtube link to view the video: https://youtu.be/boLcsod1toE","metadata":{}},{"cell_type":"code","source":"import time\nfrom datetime import datetime, date, time, timedelta","metadata":{"execution":{"iopub.status.busy":"2021-11-14T15:44:04.292744Z","iopub.execute_input":"2021-11-14T15:44:04.293097Z","iopub.status.idle":"2021-11-14T15:44:04.298294Z","shell.execute_reply.started":"2021-11-14T15:44:04.293062Z","shell.execute_reply":"2021-11-14T15:44:04.297372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"time_string = \"2018-12-30\" #given this date \ngame_id= 2018123000 #given this gameID","metadata":{"execution":{"iopub.status.busy":"2021-11-14T15:44:57.643968Z","iopub.execute_input":"2021-11-14T15:44:57.644287Z","iopub.status.idle":"2021-11-14T15:44:57.649140Z","shell.execute_reply.started":"2021-11-14T15:44:57.644255Z","shell.execute_reply":"2021-11-14T15:44:57.648266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t=df_t2018.loc[(df_t2018['time'].str.contains('2018-12-30')) & (df_t2018['gameId']==2018123000)]","metadata":{"execution":{"iopub.status.busy":"2021-11-14T15:53:41.092105Z","iopub.execute_input":"2021-11-14T15:53:41.092462Z","iopub.status.idle":"2021-11-14T15:53:45.492726Z","shell.execute_reply.started":"2021-11-14T15:53:41.092419Z","shell.execute_reply":"2021-11-14T15:53:45.491775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"times=t['time'].unique()\nprint(len(times))\nprint(times)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T15:54:10.399976Z","iopub.execute_input":"2021-11-14T15:54:10.400324Z","iopub.status.idle":"2021-11-14T15:54:10.412032Z","shell.execute_reply.started":"2021-11-14T15:54:10.400280Z","shell.execute_reply":"2021-11-14T15:54:10.411058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.pyplot import plot, draw, show\nimport cv2\nplt.rcParams[\"figure.figsize\"] = [7.50, 3.50]\nplt.rcParams[\"figure.autolayout\"] = True\nf='sompic.png'\nimg=[]\nct=0\nfor i in times:\n    fig=plt.figure()\n    ax = fig.add_subplot(111)\n    a = io.imread(f)\n    time_string = i\n    time_string=time_string.split('T')\n    time_string=time_string[0]+' '+time_string[1]\n    format_string = \"%Y-%m-%d %H:%M:%S.%f\"\n    z=t.loc[(t['time'].str.contains(i)) & (t['gameId']==game_id)]\n    z.index = range(len(z))\n    for i in range(len(z)):\n        ax.imshow(a)\n        if z[i:i+1]['team'][i]=='home':\n            ax.plot(z[i:i+1]['x'][i],z[i:i+1]['y'][i],'ro',color='blue')\n        elif z[i:i+1]['team'][i]=='away':\n            ax.plot(z[i:i+1]['x'][i],z[i:i+1]['y'][i],'ro',color='red')\n        else:\n            ax.plot(z[i:i+1]['x'][i],z[i:i+1]['y'][i],'ro',color='yellow')\n    ax.set_xlim(0, 120)\n    ax.set_ylim(0, 53.3)\n    ax.axis('on')\n    leg = ax.legend(loc='upper center')\n    plt.savefig('Img'+str(i)+'.png')\n    img.append(cv2.imread('Img'+str(i)+'.png'))\n    os.remove('Img'+str(i)+'.png')\n    plt.close(fig)\n    ct+=1\nheight,width,layers=img[0].shape\nfourcc = cv2.VideoWriter_fourcc(*'mp4v') \nvideo=cv2.VideoWriter('video_match.mp4',fourcc,1,(width,height))\nfor j in range(0,ct):\n    video.write(img[j]) ","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:04:26.387299Z","iopub.execute_input":"2021-11-14T17:04:26.387648Z","iopub.status.idle":"2021-11-14T17:21:29.518835Z","shell.execute_reply.started":"2021-11-14T17:04:26.387614Z","shell.execute_reply":"2021-11-14T17:21:29.517030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.chdir(r'/kaggle/working')\nfrom IPython.display import FileLink\nFileLink('video_match.mp4')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:25:06.845610Z","iopub.execute_input":"2021-11-14T17:25:06.846273Z","iopub.status.idle":"2021-11-14T17:25:06.854205Z","shell.execute_reply.started":"2021-11-14T17:25:06.846234Z","shell.execute_reply":"2021-11-14T17:25:06.853339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Video output for individual player movement during a specific game on a certain day","metadata":{}},{"cell_type":"markdown","source":"#### Here, I used a background I found online and resized it in order to help us better understand where the player might've been on the football pitch. At each instant in every graph, there are 2 arrows pointed in the angle of player motion and player orientation respectively. In every graph, the speed and accuracy along with the time is mentioned.","metadata":{}},{"cell_type":"markdown","source":" The arrows essentially represent the tangent to a point on the circle whose length varies. Each plot is saved and read in as an image in an array which is later converted into a video where each frame is a graph at a particular instant of the required match and player on the specified day.","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nfrom skimage import io\nf='https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/AmFBfield.svg/1200px-AmFBfield.svg.png'\na = io.imread(f)\n# determining the height ratio\n# resize image and save\nfrom PIL import Image\nfrom matplotlib import cm\nimport matplotlib.pyplot as plt\nim = Image.fromarray(a)\nim = im.resize((120,53))\nplt.imshow(im)\nim.save('sompic.png') # Saving the resized background image","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:46:45.594862Z","iopub.execute_input":"2021-11-14T17:46:45.596819Z","iopub.status.idle":"2021-11-14T17:46:46.118518Z","shell.execute_reply.started":"2021-11-14T17:46:45.596740Z","shell.execute_reply":"2021-11-14T17:46:46.117602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Just some sample data I have taken in this case. This could be implemented on any player in any year for any particular scouting agent that is looking for some player data and want to easily analyze the player movement on the pitch as per his/her needs.","metadata":{}},{"cell_type":"code","source":"t=df_t2018.loc[(df_t2018['time'].str.contains('2018-12-30',case=False)) & (df_t2018['displayName']=='Justin Tucker')]","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:46:49.822730Z","iopub.execute_input":"2021-11-14T17:46:49.823042Z","iopub.status.idle":"2021-11-14T17:46:55.717678Z","shell.execute_reply.started":"2021-11-14T17:46:49.823007Z","shell.execute_reply":"2021-11-14T17:46:55.716969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t.index = range(851)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:46:59.175499Z","iopub.execute_input":"2021-11-14T17:46:59.175963Z","iopub.status.idle":"2021-11-14T17:46:59.180607Z","shell.execute_reply.started":"2021-11-14T17:46:59.175931Z","shell.execute_reply":"2021-11-14T17:46:59.179865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t[0:1]['time'][0]","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:47:08.932467Z","iopub.execute_input":"2021-11-14T17:47:08.932822Z","iopub.status.idle":"2021-11-14T17:47:08.940642Z","shell.execute_reply.started":"2021-11-14T17:47:08.932773Z","shell.execute_reply":"2021-11-14T17:47:08.939822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating a function to create a graph of the player at every instant in the game and combine it in 1 video for easy analysis","metadata":{}},{"cell_type":"code","source":"from matplotlib.pyplot import plot, draw, show\nimport cv2\nimg=[]\ndef picperfect(df):\n    plt.rcParams[\"figure.figsize\"] = [7.50, 3.50]\n    plt.rcParams[\"figure.autolayout\"] = True\n    f='sompic.png'\n    a = io.imread(f)\n    plt.imshow(a)\n    for i in range(len(df)):\n        fig=plt.figure()\n        ax = fig.add_subplot(111)\n        a = io.imread(f)\n        ax.imshow(a)\n        heading=df[i:i+1]['displayName'][i]+'s='+str(df[i:i+1]['s'][i])+' a='+str(df[i:i+1]['a'][i])+' Time='+ str(df[i:i+1]['time'][i])\n        ax.plot(df[i:i+1]['x'][i],df[i:i+1]['y'][i],'ro',label=heading)\n        ax.arrow(df[i:i+1]['x'][i],df[i:i+1]['y'][i],2,2*np.tan(np.pi*df[i:i+1]['o'][i]/180),head_width=1, head_length=2, color='blue')\n        if df[i:i+1]['playDirection'][i]=='right':\n            ax.arrow(10,10,2,2*np.tan(np.pi*0/180),head_width=1, head_length=2, color='yellow', label='Play_Direction='+'right')\n        else:\n            ax.arrow(10,10,2,2*np.tan(np.pi*0/180),head_width=1, head_length=2, color='yellow', label='Play_Direction='+'left')\n        ax.arrow(df[i:i+1]['x'][i],df[i:i+1]['y'][i],2,2*np.tan(np.pi*df[i:i+1]['dir'][i]/180),head_width=1, head_length=2, color='red')\n        ax.arrow(df[i:i+1]['x'][i],df[i:i+1]['y'][i],2,2*np.tan(np.pi*df[i:i+1]['o'][i]/180),head_width=0.05, head_length=0.1)\n        ax.set_xlim(0, 120)\n        ax.set_ylim(0, 53.3)\n        ax.axis('on')\n        leg = ax.legend(loc='upper center')\n        #plt.show()\n        plt.savefig('Img'+str(i)+'.png')\n        img.append(cv2.imread('Img'+str(i)+'.png'))\n        os.remove('Img'+str(i)+'.png')\n        plt.close(fig)\n    height,width,layers=img[0].shape\n    fourcc = cv2.VideoWriter_fourcc(*'mp4v') \n    video=cv2.VideoWriter('video.mp4',fourcc,1,(width,height))\n    for j in range(0,len(df)):\n        video.write(img[j])  # Saving all the images in a video of the '.mp4' format","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:47:14.318032Z","iopub.execute_input":"2021-11-14T17:47:14.318352Z","iopub.status.idle":"2021-11-14T17:47:14.350267Z","shell.execute_reply.started":"2021-11-14T17:47:14.318316Z","shell.execute_reply":"2021-11-14T17:47:14.349204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Using the picperfect() function to create a video of the player's movement","metadata":{}},{"cell_type":"code","source":"picperfect(t)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:47:25.155950Z","iopub.execute_input":"2021-11-14T17:47:25.156236Z","iopub.status.idle":"2021-11-14T17:50:47.781406Z","shell.execute_reply.started":"2021-11-14T17:47:25.156203Z","shell.execute_reply":"2021-11-14T17:50:47.780532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(t[105:106]['displayName'][105])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:50:47.783254Z","iopub.execute_input":"2021-11-14T17:50:47.783488Z","iopub.status.idle":"2021-11-14T17:50:47.788477Z","shell.execute_reply.started":"2021-11-14T17:50:47.783461Z","shell.execute_reply":"2021-11-14T17:50:47.787880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## The video generated could be accessed through the link in the below cell. However, if that doesn't work, you could download it from the /kaggle/working directory after running the code. \nOr,you can just view it right here: https://youtu.be/x86Dtk0n3x0","metadata":{}},{"cell_type":"code","source":"#!pip install google.colab\n#from google.colab import files\n#files.download('../input/nfl-big-data-bowl-2022/games.csv')\nimport os\nos.chdir(r'/kaggle/working')\nfrom IPython.display import FileLink\nFileLink('video.mp4')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:53:10.438211Z","iopub.execute_input":"2021-11-14T17:53:10.438753Z","iopub.status.idle":"2021-11-14T17:53:10.446782Z","shell.execute_reply.started":"2021-11-14T17:53:10.438715Z","shell.execute_reply":"2021-11-14T17:53:10.445870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Some EDA on some useful/insightful data","metadata":{}},{"cell_type":"markdown","source":"## The Height distribution","metadata":{}},{"cell_type":"code","source":"df_players.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:53:13.125642Z","iopub.execute_input":"2021-11-14T17:53:13.126060Z","iopub.status.idle":"2021-11-14T17:53:13.139506Z","shell.execute_reply.started":"2021-11-14T17:53:13.126030Z","shell.execute_reply":"2021-11-14T17:53:13.138494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check = df_players['height'].str.split('-', expand=True)\ncheck.columns = ['first', 'second']\ncheck.loc[(check['second'].notnull()), 'first'] = check[check['second'].notnull()]['first'].astype(np.int16) * 12 + check[check['second'].notnull()]['second'].astype(np.int16)\ndf_players['height'] = check['first']\ndf_players['height'] = df_players['height'].astype(np.float32)\ndf_players['height'] /= 12\ndf_players.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:53:16.135834Z","iopub.execute_input":"2021-11-14T17:53:16.136510Z","iopub.status.idle":"2021-11-14T17:53:16.165625Z","shell.execute_reply.started":"2021-11-14T17:53:16.136468Z","shell.execute_reply":"2021-11-14T17:53:16.164752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nplt.figure(figsize=(5, 4))\n\nax = sns.distplot(df_players['height'], bins=10)\nax.set_title('Height Distribution among NFL players');","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:53:18.860158Z","iopub.execute_input":"2021-11-14T17:53:18.860419Z","iopub.status.idle":"2021-11-14T17:53:19.843561Z","shell.execute_reply.started":"2021-11-14T17:53:18.860391Z","shell.execute_reply":"2021-11-14T17:53:19.842665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Weight distribution","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(5, 4))\n\nax = sns.distplot(df_players['weight'], bins=10)\nax.set_title('Weight Distribution among NFL players');","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:53:23.276918Z","iopub.execute_input":"2021-11-14T17:53:23.277712Z","iopub.status.idle":"2021-11-14T17:53:23.515072Z","shell.execute_reply.started":"2021-11-14T17:53:23.277663Z","shell.execute_reply":"2021-11-14T17:53:23.513982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Player birth year distribution","metadata":{}},{"cell_type":"markdown","source":"### This column has some null values. Hence, we shall get rid of that.","metadata":{}},{"cell_type":"code","source":"df_players.info()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:53:54.021438Z","iopub.execute_input":"2021-11-14T17:53:54.022374Z","iopub.status.idle":"2021-11-14T17:53:54.036101Z","shell.execute_reply.started":"2021-11-14T17:53:54.022327Z","shell.execute_reply":"2021-11-14T17:53:54.035173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_players.dropna(subset=['birthDate'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:53:56.323949Z","iopub.execute_input":"2021-11-14T17:53:56.324717Z","iopub.status.idle":"2021-11-14T17:53:56.331911Z","shell.execute_reply.started":"2021-11-14T17:53:56.324667Z","shell.execute_reply":"2021-11-14T17:53:56.331064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_players['birthDate']","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:53:58.248966Z","iopub.execute_input":"2021-11-14T17:53:58.249450Z","iopub.status.idle":"2021-11-14T17:53:58.257300Z","shell.execute_reply.started":"2021-11-14T17:53:58.249403Z","shell.execute_reply":"2021-11-14T17:53:58.256570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in df_players.iterrows():\n    if len(row['birthDate'].split('/'))>2: \n        df_players.loc[index, 'birthYear'] = row['birthDate'].split('/')[2]\n    elif len(row['birthDate'].split('-'))>2: \n        df_players.loc[index, 'birthYear'] = row['birthDate'].split('-')[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:54:01.009413Z","iopub.execute_input":"2021-11-14T17:54:01.009722Z","iopub.status.idle":"2021-11-14T17:54:01.850406Z","shell.execute_reply.started":"2021-11-14T17:54:01.009687Z","shell.execute_reply":"2021-11-14T17:54:01.849489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5, 4))\n\nax = sns.distplot(df_players['birthYear'], bins=10)\nax.set_title('Birth year Distribution of NFL Players');","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:54:04.918033Z","iopub.execute_input":"2021-11-14T17:54:04.918339Z","iopub.status.idle":"2021-11-14T17:54:05.175236Z","shell.execute_reply.started":"2021-11-14T17:54:04.918302Z","shell.execute_reply":"2021-11-14T17:54:05.174407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Play Result Distribution","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(5, 4))\nax = sns.distplot(df_plays['playResult'], bins=10);\nax.set_title('playResult Distribution'); ","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:54:07.900360Z","iopub.execute_input":"2021-11-14T17:54:07.900997Z","iopub.status.idle":"2021-11-14T17:54:08.288950Z","shell.execute_reply.started":"2021-11-14T17:54:07.900950Z","shell.execute_reply":"2021-11-14T17:54:08.288031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## preSnap Home Score Distribution","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(5, 4))\nax = sns.distplot(df_plays['preSnapHomeScore'], bins=10);\nax.set_title('preSnap_Home_Score Chart in the NFL'); ","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:54:10.571127Z","iopub.execute_input":"2021-11-14T17:54:10.571408Z","iopub.status.idle":"2021-11-14T17:54:11.036733Z","shell.execute_reply.started":"2021-11-14T17:54:10.571381Z","shell.execute_reply":"2021-11-14T17:54:11.035863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## preSnap Visitor Score Distribution","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(5, 4))\nax = sns.distplot(df_plays['preSnapVisitorScore'], bins=10);\nax.set_title('preSnap_Visitor_Score Graph in the NFL'); ","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:54:13.499292Z","iopub.execute_input":"2021-11-14T17:54:13.499886Z","iopub.status.idle":"2021-11-14T17:54:13.898534Z","shell.execute_reply.started":"2021-11-14T17:54:13.499847Z","shell.execute_reply":"2021-11-14T17:54:13.897824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Some visualizations about Scouting data","metadata":{}},{"cell_type":"code","source":"df_scouting.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:54:15.815338Z","iopub.execute_input":"2021-11-14T17:54:15.816326Z","iopub.status.idle":"2021-11-14T17:54:15.839177Z","shell.execute_reply.started":"2021-11-14T17:54:15.816274Z","shell.execute_reply":"2021-11-14T17:54:15.838547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_scouting.info()","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:54:17.515505Z","iopub.execute_input":"2021-11-14T17:54:17.515829Z","iopub.status.idle":"2021-11-14T17:54:17.555569Z","shell.execute_reply.started":"2021-11-14T17:54:17.515792Z","shell.execute_reply":"2021-11-14T17:54:17.554559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### The number of kicktypes and how frequently each one is implemented in the game","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\ndf_scouting['kickType'].value_counts().plot(ax=ax, kind='bar')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:54:20.158163Z","iopub.execute_input":"2021-11-14T17:54:20.158732Z","iopub.status.idle":"2021-11-14T17:54:20.376742Z","shell.execute_reply.started":"2021-11-14T17:54:20.158693Z","shell.execute_reply":"2021-11-14T17:54:20.375793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## I thought of implementing the pairplots here for data as they end up giving us very insightful observations into the dataset as a whole","metadata":{}},{"cell_type":"markdown","source":"### A pairplot of the scouting data with the hue as 'kicktype'","metadata":{}},{"cell_type":"code","source":"sns.pairplot(df_scouting, hue='kickType')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:54:23.892657Z","iopub.execute_input":"2021-11-14T17:54:23.893061Z","iopub.status.idle":"2021-11-14T17:54:45.848904Z","shell.execute_reply.started":"2021-11-14T17:54:23.893031Z","shell.execute_reply":"2021-11-14T17:54:45.848100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Pairplot for the gamedata file with the home team abbreviation as the hue","metadata":{}},{"cell_type":"code","source":"sns.pairplot(df_gamedata, hue='homeTeamAbbr')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:56:05.708741Z","iopub.execute_input":"2021-11-14T17:56:05.709607Z","iopub.status.idle":"2021-11-14T17:56:13.864696Z","shell.execute_reply.started":"2021-11-14T17:56:05.709547Z","shell.execute_reply":"2021-11-14T17:56:13.864044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### A pairplot for the gameplay information file with the hue as the quarter of the game","metadata":{}},{"cell_type":"code","source":"sns.pairplot(df_plays,hue='quarter')","metadata":{"execution":{"iopub.status.busy":"2021-11-14T17:56:18.671553Z","iopub.execute_input":"2021-11-14T17:56:18.672261Z","iopub.status.idle":"2021-11-14T18:00:01.245271Z","shell.execute_reply.started":"2021-11-14T17:56:18.672216Z","shell.execute_reply":"2021-11-14T18:00:01.244255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Violin Plots and Box Plots","metadata":{}},{"cell_type":"markdown","source":"## Here are some plots for comparing various parameters available in the datasets provided, such as kickLength, kickReturnYardage, hangTime, kickType, yardsToGo, etc.","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nsns.set_theme(style=\"whitegrid\")\nax = sns.violinplot(x=df_plays['quarter'])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:41:45.000038Z","iopub.execute_input":"2021-11-14T18:41:45.000710Z","iopub.status.idle":"2021-11-14T18:41:45.370601Z","shell.execute_reply.started":"2021-11-14T18:41:45.000659Z","shell.execute_reply":"2021-11-14T18:41:45.369664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_theme(style=\"whitegrid\")\nax = sns.violinplot(x=df_plays['yardsToGo'],y=df_plays['specialTeamsPlayType'])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:46:56.711764Z","iopub.execute_input":"2021-11-14T18:46:56.712232Z","iopub.status.idle":"2021-11-14T18:46:57.186546Z","shell.execute_reply.started":"2021-11-14T18:46:56.712199Z","shell.execute_reply":"2021-11-14T18:46:57.185453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_theme(style=\"whitegrid\")\nax = sns.violinplot(x=df_plays['kickLength'],y=df_plays['kickReturnYardage'])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:47:58.219441Z","iopub.execute_input":"2021-11-14T18:47:58.219828Z","iopub.status.idle":"2021-11-14T18:48:02.700402Z","shell.execute_reply.started":"2021-11-14T18:47:58.219787Z","shell.execute_reply":"2021-11-14T18:48:02.699455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_theme(style=\"whitegrid\")\nax = sns.violinplot(x=df_scouting['hangTime'],y=df_scouting['kickContactType'])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:56:36.299018Z","iopub.execute_input":"2021-11-14T18:56:36.299299Z","iopub.status.idle":"2021-11-14T18:56:37.049766Z","shell.execute_reply.started":"2021-11-14T18:56:36.299270Z","shell.execute_reply":"2021-11-14T18:56:37.048862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.set_theme(style=\"whitegrid\")\nax = sns.boxplot(x=df_scouting[\"hangTime\"], y=df_scouting[\"kickType\"])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:53:50.003218Z","iopub.execute_input":"2021-11-14T18:53:50.003535Z","iopub.status.idle":"2021-11-14T18:53:50.486113Z","shell.execute_reply.started":"2021-11-14T18:53:50.003502Z","shell.execute_reply":"2021-11-14T18:53:50.485078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_theme(style=\"whitegrid\")\nax = sns.boxplot(x=df_players[\"height\"], y=df_players[\"Position\"])","metadata":{"execution":{"iopub.status.busy":"2021-11-14T18:59:10.281050Z","iopub.execute_input":"2021-11-14T18:59:10.281653Z","iopub.status.idle":"2021-11-14T18:59:11.085927Z","shell.execute_reply.started":"2021-11-14T18:59:10.281601Z","shell.execute_reply":"2021-11-14T18:59:11.085058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Finally, this is a work in progess and I am working on it to make it better/ get any more meaningful outputs from the data provided. Thanks for viewing my work and please feel free to upvote it and let me know about what more could be worked upon.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}