{"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":"markdown","source":"# \"Play Type Recommendation System & Result Prediction\"","metadata":{}},{"cell_type":"markdown","source":"> American Football is a very fast pace sports, during the match coaches might have to make certain strategic judgements within a small time period.\n\n> We are trying to build a recommendation system to facilitate coaches to make faster and more sound judgements! By entering some parameters of the current circumstance (e.g. quarter, scores, opponent team), the system will recommend the best formation of play (play type) in descending order, with success rate and even suggested strategy (including kick type and direction). The expected result will also be predicted, such as kick length of a punt.\nEven if coaches have this huge load of historical data that they can study, but such amount cannot be processed by a human's mind, especially not during a match. But with a tool like this, strategic decisions can be generated in a blink and with the support of the big data as well.\n\n> Besides strategies of own team, as American Football is a team sports, synergy plays a very important factor on strategy planning. But in this case, we are trying to predict the key and support players of the opponent team that own team need to be aware when doing certain types of play.","metadata":{}},{"cell_type":"markdown","source":"## Import libs","metadata":{"id":"9_DIcGcuIckc"}},{"cell_type":"code","source":"# !pip install --upgrade pandas","metadata":{"id":"ZzPJJNhrr20A","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T22:14:32.973348Z","iopub.execute_input":"2022-01-06T22:14:32.974780Z","iopub.status.idle":"2022-01-06T22:14:33.001980Z","shell.execute_reply.started":"2022-01-06T22:14:32.974593Z","shell.execute_reply":"2022-01-06T22:14:33.000623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom datetime import datetime, timedelta, date\nimport re","metadata":{"id":"9ZOmtYr3I3Cr","execution":{"iopub.status.busy":"2022-01-06T22:14:33.004992Z","iopub.execute_input":"2022-01-06T22:14:33.005913Z","iopub.status.idle":"2022-01-06T22:14:34.342918Z","shell.execute_reply.started":"2022-01-06T22:14:33.005851Z","shell.execute_reply":"2022-01-06T22:14:34.341946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.discriminant_analysis import LinearDiscriminantAnalysis","metadata":{"id":"7nZHc7C9Mw5m","execution":{"iopub.status.busy":"2022-01-06T22:14:34.344233Z","iopub.execute_input":"2022-01-06T22:14:34.344485Z","iopub.status.idle":"2022-01-06T22:14:34.672301Z","shell.execute_reply.started":"2022-01-06T22:14:34.344455Z","shell.execute_reply":"2022-01-06T22:14:34.671390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 1 - Importing files ","metadata":{"id":"-SlHIrPKH-OR"}},{"cell_type":"code","source":"# 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":{"execution":{"iopub.status.busy":"2022-01-06T22:14:34.673486Z","iopub.execute_input":"2022-01-06T22:14:34.673722Z","iopub.status.idle":"2022-01-06T22:14:34.682504Z","shell.execute_reply.started":"2022-01-06T22:14:34.673693Z","shell.execute_reply":"2022-01-06T22:14:34.681372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd \"/kaggle/input\"","metadata":{"id":"9nCGECGKky7i","outputId":"802fedf9-b73f-42f7-fb1a-9f217128363b","execution":{"iopub.status.busy":"2022-01-06T22:14:34.684640Z","iopub.execute_input":"2022-01-06T22:14:34.685531Z","iopub.status.idle":"2022-01-06T22:14:34.699822Z","shell.execute_reply.started":"2022-01-06T22:14:34.685488Z","shell.execute_reply":"2022-01-06T22:14:34.698498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = 'nfl-big-data-bowl-2022/'","metadata":{"id":"knicBNy8Jaxk","execution":{"iopub.status.busy":"2022-01-06T22:14:34.701122Z","iopub.execute_input":"2022-01-06T22:14:34.701617Z","iopub.status.idle":"2022-01-06T22:14:34.721260Z","shell.execute_reply.started":"2022-01-06T22:14:34.701526Z","shell.execute_reply":"2022-01-06T22:14:34.720135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays = pd.read_csv (data_path + 'plays.csv')\nplayers = pd.read_csv (data_path + 'players.csv')\nPFFScouting = pd.read_csv (data_path + 'PFFScoutingData.csv')\ngames = pd.read_csv (data_path + 'games.csv')","metadata":{"id":"rYBfVJBJk1R9","execution":{"iopub.status.busy":"2022-01-06T22:14:34.722456Z","iopub.execute_input":"2022-01-06T22:14:34.722699Z","iopub.status.idle":"2022-01-06T22:14:34.995376Z","shell.execute_reply.started":"2022-01-06T22:14:34.722667Z","shell.execute_reply":"2022-01-06T22:14:34.994424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players_ori = players.copy()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:14:34.996817Z","iopub.execute_input":"2022-01-06T22:14:34.997057Z","iopub.status.idle":"2022-01-06T22:14:35.001229Z","shell.execute_reply.started":"2022-01-06T22:14:34.997028Z","shell.execute_reply":"2022-01-06T22:14:35.000584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018 = pd.read_csv(data_path + 'tracking2018.csv')\ntracking2019 = pd.read_csv(data_path + 'tracking2019.csv')\ntracking2020 = pd.read_csv(data_path + 'tracking2020.csv')","metadata":{"id":"UY7nt79PnL8Z","execution":{"iopub.status.busy":"2022-01-06T22:14:35.002664Z","iopub.execute_input":"2022-01-06T22:14:35.003378Z","iopub.status.idle":"2022-01-06T22:16:30.672901Z","shell.execute_reply.started":"2022-01-06T22:14:35.003341Z","shell.execute_reply":"2022-01-06T22:16:30.671488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playgames = pd.merge(plays,games,on='gameId',how='left')\nplaygamesScout = pd.merge(playgames,PFFScouting,on=['gameId','playId'],how='left')\nrawDf = pd.merge(playgamesScout,players, left_on=['kickerId'],right_on=['nflId'],how='left')","metadata":{"id":"M2FU0FGAWJhs","execution":{"iopub.status.busy":"2022-01-06T22:16:30.674783Z","iopub.execute_input":"2022-01-06T22:16:30.675167Z","iopub.status.idle":"2022-01-06T22:16:30.769703Z","shell.execute_reply.started":"2022-01-06T22:16:30.675109Z","shell.execute_reply":"2022-01-06T22:16:30.768787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 2 - Preprocessing\n","metadata":{"id":"n_tNHijxJ5Rg"}},{"cell_type":"markdown","source":"## Compress size for saving memory space","metadata":{"id":"LbHOZtANZCKJ"}},{"cell_type":"code","source":"def downcast(df, verbose=True):\n    start_mem = df.memory_usage().sum() / 1024**2\n    for col in df.columns:\n        dtype_name = df[col].dtype.name\n        if dtype_name == 'object':\n            pass\n        elif dtype_name == 'bool':\n            df[col] = df[col].astype('int8')\n        elif dtype_name.startswith('int') or (df[col].round() == df[col]).all():\n            df[col] = pd.to_numeric(df[col], downcast='integer')\n        else:\n            df[col] = pd.to_numeric(df[col], downcast='float')\n    end_mem = df.memory_usage().sum() / 1024**2\n    if verbose:\n        print('{:.1f}% Compressed'.format(100 * (start_mem - end_mem) / start_mem))\n    \n    return df","metadata":{"id":"PPZ90_d_uynR","execution":{"iopub.status.busy":"2022-01-06T22:16:30.771031Z","iopub.execute_input":"2022-01-06T22:16:30.771275Z","iopub.status.idle":"2022-01-06T22:16:30.779339Z","shell.execute_reply.started":"2022-01-06T22:16:30.771247Z","shell.execute_reply":"2022-01-06T22:16:30.778234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resumetable(df):\n    print(f'Shape : {df.shape}')\n    summary = pd.DataFrame(df.dtypes, columns=['Data Type'])\n    summary = summary.reset_index()\n    summary = summary.rename(columns={'index': 'Feature'})\n    summary['Num of null'] = df.isnull().sum().values\n    summary['Num of unique'] = df.nunique().values\n    summary['First value'] = df.loc[0].values\n    summary['Second value'] = df.loc[1].values\n    summary['Third value'] = df.loc[2].values\n    return summary","metadata":{"id":"xvtmeeYSY0bV","execution":{"iopub.status.busy":"2022-01-06T22:16:30.780664Z","iopub.execute_input":"2022-01-06T22:16:30.780883Z","iopub.status.idle":"2022-01-06T22:16:30.798888Z","shell.execute_reply.started":"2022-01-06T22:16:30.780858Z","shell.execute_reply":"2022-01-06T22:16:30.797892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"games = downcast(games)\nplayers = downcast(players)\nplays = downcast(plays)\nPFFScouting = downcast(PFFScouting)","metadata":{"id":"ko5PVTcEY3vm","outputId":"3de31f6e-9acf-4efc-d087-d7ee28a9243d","execution":{"iopub.status.busy":"2022-01-06T22:16:30.800424Z","iopub.execute_input":"2022-01-06T22:16:30.800685Z","iopub.status.idle":"2022-01-06T22:16:30.857537Z","shell.execute_reply.started":"2022-01-06T22:16:30.800657Z","shell.execute_reply":"2022-01-06T22:16:30.856616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check data\nresumetable(players)\nresumetable(games)\nresumetable(plays)\nresumetable(PFFScouting)","metadata":{"id":"sJPyB86VY9_T","outputId":"e09d7545-b0e3-425e-deb6-1fb77483f69c","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T22:16:30.860524Z","iopub.execute_input":"2022-01-06T22:16:30.860764Z","iopub.status.idle":"2022-01-06T22:16:30.993898Z","shell.execute_reply.started":"2022-01-06T22:16:30.860736Z","shell.execute_reply":"2022-01-06T22:16:30.992504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Clean and combine data","metadata":{"id":"sXmxe-g5Al3f"}},{"cell_type":"markdown","source":"### Before combining","metadata":{"id":"vaH0suaFTWNQ"}},{"cell_type":"code","source":"def cleanPlayers(players):\n    #convert height & weight\n\n    # Get the Height data from DataFrame & Split the heights by hyphen (\"-\")\n    players_heights = players[\"height\"].apply(lambda x: x.split(\"-\"))  \n\n    # Convert Heights to Centimeters and add them to DataFrame\n    players[\"height\"] = players_heights.apply(lambda x: int(x[0]) * 12 + int(x[1]) if len(x) == 2 else int(x[0])) * 2.54\n\n    # Convert Weights to Kilograms and them to DataFrame\n    players[\"weight\"] = round(players.weight * 0.453592, 2)\n\n    #fill in NAN Value on age and college name (only players in special team)\n    players.loc[players['displayName'] =='Hunter Niswander', ['birthDate']] = '1994-11-26'\n    players.loc[players['displayName'] =='Taylor Russolino', ['birthDate']] = '1989-05-23'\n    players.loc[players['displayName'] =='Brandon Wright', ['collegeName']] = 'North Carolina State'\n    players.loc[players['displayName'] =='Hunter Niswander', ['collegeName']] = 'Northwestern'\n    players.loc[players['displayName'] =='Taylor Russolino', ['collegeName']] = 'Mississippi'\n\n    players['birthDate'] = pd.to_datetime(players['birthDate'])\n\n    return players\n","metadata":{"id":"JO6vTkWnwh8Y","execution":{"iopub.status.busy":"2022-01-06T22:16:30.995610Z","iopub.execute_input":"2022-01-06T22:16:30.995901Z","iopub.status.idle":"2022-01-06T22:16:31.005064Z","shell.execute_reply.started":"2022-01-06T22:16:30.995870Z","shell.execute_reply":"2022-01-06T22:16:31.004034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cleanBeforeCombine(players, plays, games):\n    players = cleanPlayers(players)\n\n    games['gameDate'] = pd.to_datetime(games['gameDate'],infer_datetime_format=True)\n    plays= plays.loc[(plays['specialTeamsPlayType'] == 'Field Goal') | (plays['specialTeamsPlayType'] == 'Punt' ) | (plays['specialTeamsResult'] == 'Non-Special Teams Result' )]\n\n    return players, plays, games","metadata":{"id":"q1p00298LlLt","execution":{"iopub.status.busy":"2022-01-06T22:16:31.007345Z","iopub.execute_input":"2022-01-06T22:16:31.007713Z","iopub.status.idle":"2022-01-06T22:16:31.028432Z","shell.execute_reply.started":"2022-01-06T22:16:31.007666Z","shell.execute_reply":"2022-01-06T22:16:31.027452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players, plays, games = cleanBeforeCombine(players, plays, games)","metadata":{"id":"3XeY86sJj00S","execution":{"iopub.status.busy":"2022-01-06T22:16:31.029355Z","iopub.execute_input":"2022-01-06T22:16:31.029589Z","iopub.status.idle":"2022-01-06T22:16:31.083534Z","shell.execute_reply.started":"2022-01-06T22:16:31.029558Z","shell.execute_reply":"2022-01-06T22:16:31.082382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Combine data ","metadata":{"id":"P0vXozWSnSYE"}},{"cell_type":"code","source":"playgames = pd.merge(plays,games,on='gameId',how='left')\nplaygamesScout = pd.merge(playgames,PFFScouting,on=['gameId','playId'],how='left')\nalldata = pd.merge(playgamesScout,players, left_on=['kickerId'],right_on=['nflId'],how='left')","metadata":{"id":"1I37upu4hVcA","execution":{"iopub.status.busy":"2022-01-06T22:16:31.084787Z","iopub.execute_input":"2022-01-06T22:16:31.085444Z","iopub.status.idle":"2022-01-06T22:16:31.148436Z","shell.execute_reply.started":"2022-01-06T22:16:31.085398Z","shell.execute_reply":"2022-01-06T22:16:31.146915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Clean data","metadata":{"id":"Wk9CNfUuCNH-"}},{"cell_type":"code","source":"typeCols = ['quarter', 'down', 'yardsToGo', 'possessionTeam',\n            'specialTeamsPlayType', 'specialTeamsResult', 'yardlineSide',\n            'yardlineNumber', 'gameClock', 'preSnapHomeScore',\n            'preSnapVisitorScore', 'absoluteYardlineNumber', 'homeTeamAbbr', 'visitorTeamAbbr']","metadata":{"id":"0JQod61DoRbc","execution":{"iopub.status.busy":"2022-01-06T22:16:31.150553Z","iopub.execute_input":"2022-01-06T22:16:31.150916Z","iopub.status.idle":"2022-01-06T22:16:31.156643Z","shell.execute_reply.started":"2022-01-06T22:16:31.150872Z","shell.execute_reply":"2022-01-06T22:16:31.155762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"puntCols = ['quarter', 'down', 'yardsToGo', 'possessionTeam', \n            'yardlineSide', 'yardlineNumber', 'gameClock', 'preSnapHomeScore',\n            'preSnapVisitorScore', 'kickLength', 'absoluteYardlineNumber',\n            'homeTeamAbbr', 'visitorTeamAbbr', 'kickType', 'direction',\n            'nflId', 'height', 'weight', 'Position', 'age']","metadata":{"id":"kyFDEl6gEmIA","execution":{"iopub.status.busy":"2022-01-06T22:16:31.158073Z","iopub.execute_input":"2022-01-06T22:16:31.159138Z","iopub.status.idle":"2022-01-06T22:16:31.185542Z","shell.execute_reply.started":"2022-01-06T22:16:31.159074Z","shell.execute_reply":"2022-01-06T22:16:31.184503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fgCols = ['quarter', 'down', 'yardsToGo', 'possessionTeam', 'specialTeamsResult',\n        'yardlineSide', 'yardlineNumber', 'gameClock', 'preSnapHomeScore',\n        'preSnapVisitorScore', 'absoluteYardlineNumber', 'homeTeamAbbr', 'visitorTeamAbbr',\n        'nflId', 'height', 'weight', 'Position', 'age']","metadata":{"id":"6CrqvG4fEzWI","execution":{"iopub.status.busy":"2022-01-06T22:16:31.187392Z","iopub.execute_input":"2022-01-06T22:16:31.187748Z","iopub.status.idle":"2022-01-06T22:16:31.202724Z","shell.execute_reply.started":"2022-01-06T22:16:31.187712Z","shell.execute_reply":"2022-01-06T22:16:31.201460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"allCols = list(set().union(puntCols, fgCols, typeCols))","metadata":{"id":"5In6RqwtGGfB","execution":{"iopub.status.busy":"2022-01-06T22:16:31.204732Z","iopub.execute_input":"2022-01-06T22:16:31.205307Z","iopub.status.idle":"2022-01-06T22:16:31.222142Z","shell.execute_reply.started":"2022-01-06T22:16:31.205266Z","shell.execute_reply":"2022-01-06T22:16:31.220912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pos_map = {'K':1,\"P\":0 }\nkick_map = {'N':1,\"A\":2,\"R\":3,0:0}\ndir_map = {'C':1,\"L\":2,\"R\":3,0:0 }","metadata":{"id":"mJKbbzCBoOcS","execution":{"iopub.status.busy":"2022-01-06T22:16:31.223459Z","iopub.execute_input":"2022-01-06T22:16:31.224316Z","iopub.status.idle":"2022-01-06T22:16:31.238735Z","shell.execute_reply.started":"2022-01-06T22:16:31.224234Z","shell.execute_reply":"2022-01-06T22:16:31.236880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cleanAll(alldata):\n\n    # count the players age in the play\n    alldata[\"age\"]=alldata[\"gameDate\"].dt.year - alldata[\"birthDate\"].dt.year\n    alldata = alldata.drop(columns=[\"birthDate\", \"gameDate\"])\n\n    alldata['gameClock'] = pd.to_timedelta(alldata['gameClock'])\n    alldata['gameClock'] = alldata['gameClock'].dt.total_seconds()\n\n    #  convert team abbr to number\n    team_idx = alldata['homeTeamAbbr'].value_counts().sort_index(key=lambda x : x.str.lower())\n    team_map = {}\n    i = 0\n    for t in team_idx.index:\n        team_map[t] = i\n        i += 1\n\n    alldata['homeTeamAbbr'] = alldata['homeTeamAbbr'].map(team_map)\n    alldata['visitorTeamAbbr'] = alldata['visitorTeamAbbr'].map(team_map)\n    alldata['possessionTeam'] = alldata['possessionTeam'].map(team_map)\n    alldata['yardlineSide'] = alldata['yardlineSide'].map(team_map)\n\n    alldata['kickLength'] = alldata['kickLength'].fillna(0)\n\n    # conver categorical data to number\n    alldata['Position']= alldata['Position'].map(pos_map)\n    alldata['kickType'] = alldata['kickType'].map(kick_map)\n    alldata['direction'] = alldata['kickDirectionActual'].map(dir_map)\n\n    alldata = alldata[allCols]\n    return alldata, team_map\n","metadata":{"id":"yCfED2ouC9z2","execution":{"iopub.status.busy":"2022-01-06T22:16:31.240432Z","iopub.execute_input":"2022-01-06T22:16:31.241508Z","iopub.status.idle":"2022-01-06T22:16:31.257058Z","shell.execute_reply.started":"2022-01-06T22:16:31.241456Z","shell.execute_reply":"2022-01-06T22:16:31.255588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alldata, team_map = cleanAll(alldata)","metadata":{"id":"kv7jxtkWrdHC","execution":{"iopub.status.busy":"2022-01-06T22:16:31.259628Z","iopub.execute_input":"2022-01-06T22:16:31.260093Z","iopub.status.idle":"2022-01-06T22:16:31.332251Z","shell.execute_reply.started":"2022-01-06T22:16:31.260044Z","shell.execute_reply":"2022-01-06T22:16:31.331442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alldata.head()","metadata":{"id":"XieB0ZxxICiH","outputId":"178f0eab-9b45-4450-f020-6f8bf3c16d4f","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T22:16:31.333403Z","iopub.execute_input":"2022-01-06T22:16:31.333759Z","iopub.status.idle":"2022-01-06T22:16:31.360792Z","shell.execute_reply.started":"2022-01-06T22:16:31.333729Z","shell.execute_reply":"2022-01-06T22:16:31.359564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 3 - Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"alldata.isna().sum()","metadata":{"id":"DwoF2XVUJVwh","outputId":"84c39a85-2123-4520-e186-e61a3cc1ade9","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T22:16:31.362594Z","iopub.execute_input":"2022-01-06T22:16:31.362924Z","iopub.status.idle":"2022-01-06T22:16:31.375717Z","shell.execute_reply.started":"2022-01-06T22:16:31.362889Z","shell.execute_reply":"2022-01-06T22:16:31.374508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"punt = alldata[alldata['specialTeamsPlayType']=='Punt']\nfg = alldata[alldata['specialTeamsPlayType']=='Field Goal']\ncountpunt=punt['specialTeamsResult'].value_counts().reset_index()\ncountfg=fg['specialTeamsResult'].value_counts().reset_index()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T22:16:31.377978Z","iopub.execute_input":"2022-01-06T22:16:31.378452Z","iopub.status.idle":"2022-01-06T22:16:31.393000Z","shell.execute_reply.started":"2022-01-06T22:16:31.378409Z","shell.execute_reply":"2022-01-06T22:16:31.391885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nfrom matplotlib import gridspec, ticker\ncolor_counts = (punt['specialTeamsResult'].value_counts().reset_index())\ncolor_counts.columns = ['specialTeamsResult', 'count']\n\norder = color_counts['specialTeamsResult']\npalette = color_counts['specialTeamsResult'].replace('other', None) # \"other\" is not a color name\n\nfig = plt.figure(figsize=(15, 8))\ngs = gridspec.GridSpec(1, 3, figure=fig)\n\n# Left plot\nax = fig.add_subplot(gs[0])\nsns.barplot(data = color_counts, x = 'count', y = 'specialTeamsResult',\n            #palette = palette, \n            ax = ax)\nax.set(title = 'Punt Result')\nax.set_xlim((0, 5600))\nfor p in ax.patches:\n    ax.annotate(f\"{int(p.get_width())}\", xy = (p.get_width(), p.get_y() + 0.5),\n                horizontalalignment = 'left')\n    clr = p.get_facecolor()\n    if clr == (1, 1, 1, 1):\n        # If facecolor is white\n        p.set_edgecolor('magenta')\nax.set_ylabel('')\n\n\n# Right plot\nax = fig.add_subplot(gs[1:3])\nsns.boxenplot(data = punt, x = 'kickLength', y = 'specialTeamsResult',\n              order = order, \n              #palette = palette, \n              ax = ax)\nax.set(title = 'Punt Results - Kick Lengths', xscale = 'log')\nax.yaxis.tick_right()\nax.set_ylabel('')\n\nplt.suptitle(\"How Punt Results Compared when it comes to kick lengths\", fontsize = 15)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:16:31.394846Z","iopub.execute_input":"2022-01-06T22:16:31.395289Z","iopub.status.idle":"2022-01-06T22:16:32.467098Z","shell.execute_reply.started":"2022-01-06T22:16:31.395245Z","shell.execute_reply":"2022-01-06T22:16:32.466007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import gridspec, ticker\ncolor_counts = (fg['specialTeamsResult'].value_counts().reset_index())\ncolor_counts.columns = ['specialTeamsResult', 'count']\n\norder = color_counts['specialTeamsResult']\npalette = color_counts['specialTeamsResult'].replace('other', None) # \"other\" is not a color name\n\nfig = plt.figure(figsize=(15, 8))\ngs = gridspec.GridSpec(1, 3, figure=fig)\n\n# Left plot\nax = fig.add_subplot(gs[0])\nsns.barplot(data = color_counts, x = 'count', y = 'specialTeamsResult',\n            #palette = palette, \n            ax = ax)\nax.set(title = 'Field Goal Result')\nax.set_xlim((0, 5600))\nfor p in ax.patches:\n    ax.annotate(f\"{int(p.get_width())}\", xy = (p.get_width(), p.get_y() + 0.5),\n                horizontalalignment = 'left')\n    clr = p.get_facecolor()\n    if clr == (1, 1, 1, 1):\n        # If facecolor is white\n        p.set_edgecolor('magenta')\nax.set_ylabel('')\n\n\n# Right plot\nax = fig.add_subplot(gs[1:3])\nsns.boxenplot(data = fg, x = 'kickLength', y = 'specialTeamsResult',\n              order = order, \n              #palette = palette, \n              ax = ax)\nax.set(title = 'Field Goal Results - Kick Lengths', xscale = 'log')\nax.yaxis.tick_right()\nax.set_ylabel('')\n\nplt.suptitle(\"How Field Goal Results Compared when it comes to kick lengths\", fontsize = 15)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:16:32.468622Z","iopub.execute_input":"2022-01-06T22:16:32.468959Z","iopub.status.idle":"2022-01-06T22:16:33.100179Z","shell.execute_reply.started":"2022-01-06T22:16:32.468915Z","shell.execute_reply":"2022-01-06T22:16:33.099202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 4 - Building whole model","metadata":{"id":"w2Vzf1NBIN8A"}},{"cell_type":"markdown","source":"## Some shared functions","metadata":{"id":"bXww92alI8N2"}},{"cell_type":"code","source":"def fillna(df):\n    for c in df.columns:\n        col = df[c]\n        if col.isna().sum()>0:\n            df[c] = df[c].fillna(method='ffill')\n    return df","metadata":{"id":"pIvjvfOhI_Do","execution":{"iopub.status.busy":"2022-01-06T22:16:33.101775Z","iopub.execute_input":"2022-01-06T22:16:33.102319Z","iopub.status.idle":"2022-01-06T22:16:33.107995Z","shell.execute_reply.started":"2022-01-06T22:16:33.102272Z","shell.execute_reply":"2022-01-06T22:16:33.106879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Play Type Classifier","metadata":{"id":"4TrLHkCSIUD2"}},{"cell_type":"markdown","source":"### Clean train data","metadata":{"id":"hZwUR7QfIap9"}},{"cell_type":"code","source":"def cleanTypeData(alldata):\n\n    classplaytype = alldata.drop(alldata[alldata.specialTeamsPlayType=='Extra Point'].index)\n    classplaytype = fillna(classplaytype)\n\n    classplaytype['specialTeamsPlayType'].mask(classplaytype['specialTeamsResult'] == 'Non-Special Teams Result', 'Non-Special Teams', inplace=True)\n\n    classplaytype =classplaytype[typeCols]\n    return classplaytype\n    ","metadata":{"id":"s2A7Q5_c7lFU","execution":{"iopub.status.busy":"2022-01-06T22:16:33.109986Z","iopub.execute_input":"2022-01-06T22:16:33.110773Z","iopub.status.idle":"2022-01-06T22:16:33.124364Z","shell.execute_reply.started":"2022-01-06T22:16:33.110704Z","shell.execute_reply":"2022-01-06T22:16:33.123307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classplaytype = cleanTypeData(alldata)","metadata":{"id":"xdZVycKI6OHj","execution":{"iopub.status.busy":"2022-01-06T22:16:33.126340Z","iopub.execute_input":"2022-01-06T22:16:33.126866Z","iopub.status.idle":"2022-01-06T22:16:33.159505Z","shell.execute_reply.started":"2022-01-06T22:16:33.126816Z","shell.execute_reply":"2022-01-06T22:16:33.158419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train model","metadata":{"id":"ptD_HBrlMX7t"}},{"cell_type":"code","source":"typeX = classplaytype.drop(columns=[\"specialTeamsPlayType\",\"specialTeamsResult\"])\ntypeInputCol = typeX.columns\ntypeX = typeX.to_numpy()\ntypeY = classplaytype['specialTeamsPlayType']\nprint(typeX.shape, typeY.shape)","metadata":{"id":"I9mqik7d70nx","outputId":"819f88e4-0ce7-4845-e8e0-7e46ed68f7b7","execution":{"iopub.status.busy":"2022-01-06T22:16:33.161831Z","iopub.execute_input":"2022-01-06T22:16:33.162067Z","iopub.status.idle":"2022-01-06T22:16:33.171694Z","shell.execute_reply.started":"2022-01-06T22:16:33.162039Z","shell.execute_reply":"2022-01-06T22:16:33.170843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classplaytype['specialTeamsPlayType']","metadata":{"id":"MktYKDr44tOd","outputId":"2789ed4f-78f0-49a5-8c32-0dd35b8b72fc","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T22:16:33.173386Z","iopub.execute_input":"2022-01-06T22:16:33.173822Z","iopub.status.idle":"2022-01-06T22:16:33.191804Z","shell.execute_reply.started":"2022-01-06T22:16:33.173776Z","shell.execute_reply":"2022-01-06T22:16:33.191132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"typeX_train, typeX_test, typeY_train, typeY_test = train_test_split(typeX, typeY, random_state=20, train_size=0.8)","metadata":{"id":"DCVt5EET9TTu","execution":{"iopub.status.busy":"2022-01-06T22:16:33.193212Z","iopub.execute_input":"2022-01-06T22:16:33.193675Z","iopub.status.idle":"2022-01-06T22:16:33.209781Z","shell.execute_reply.started":"2022-01-06T22:16:33.193636Z","shell.execute_reply":"2022-01-06T22:16:33.208779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"typeModel = GaussianNB()\ntypeModel.fit(typeX_train, typeY_train)\nprint('Accuracy of type classifier on training set: {:.2f}'\n     .format(typeModel.score(typeX_train, typeY_train)))\nprint('Accuracy of type classifier on test set: {:.2f}'\n     .format(typeModel.score(typeX_test, typeY_test)))","metadata":{"id":"BuFXUDSQBFe5","outputId":"9837b59e-c489-4040-9bb9-2d70a8d9e301","execution":{"iopub.status.busy":"2022-01-06T22:16:33.210866Z","iopub.execute_input":"2022-01-06T22:16:33.211618Z","iopub.status.idle":"2022-01-06T22:16:33.244949Z","shell.execute_reply.started":"2022-01-06T22:16:33.211578Z","shell.execute_reply":"2022-01-06T22:16:33.244278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"typeModel.predict([typeX_test[2]])","metadata":{"id":"8ohdXKunzoDl","outputId":"293f54b8-bb76-4daa-c052-f736845cb0dd","execution":{"iopub.status.busy":"2022-01-06T22:16:33.246054Z","iopub.execute_input":"2022-01-06T22:16:33.246746Z","iopub.status.idle":"2022-01-06T22:16:33.253660Z","shell.execute_reply.started":"2022-01-06T22:16:33.246708Z","shell.execute_reply":"2022-01-06T22:16:33.252642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Punt Model","metadata":{"id":"edFhie6PO2Yv"}},{"cell_type":"markdown","source":"### Prepare data","metadata":{"id":"a3kVCmaNPtwO"}},{"cell_type":"code","source":"def cleanPuntData(alldata):\n\n    punt = alldata.loc[(alldata['specialTeamsPlayType'] == 'Punt') & (alldata['specialTeamsResult']!='Non-Special Teams Result')]\n    \n    punt = punt.reset_index()\n    punt = fillna(punt)\n    punt = punt[puntCols]\n\n    return punt","metadata":{"id":"rsyoxIxnO4BC","execution":{"iopub.status.busy":"2022-01-06T22:16:33.255118Z","iopub.execute_input":"2022-01-06T22:16:33.255948Z","iopub.status.idle":"2022-01-06T22:16:33.267998Z","shell.execute_reply.started":"2022-01-06T22:16:33.255883Z","shell.execute_reply":"2022-01-06T22:16:33.267094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"punt = cleanPuntData(alldata)\npunt.info()","metadata":{"id":"sB49xjEpPU04","outputId":"2600e6fb-0025-4987-b30c-47aae30c60a9","execution":{"iopub.status.busy":"2022-01-06T22:16:33.269348Z","iopub.execute_input":"2022-01-06T22:16:33.270460Z","iopub.status.idle":"2022-01-06T22:16:33.304754Z","shell.execute_reply.started":"2022-01-06T22:16:33.270405Z","shell.execute_reply":"2022-01-06T22:16:33.304066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"create y data","metadata":{"id":"1_FCFdAvPOFM"}},{"cell_type":"code","source":"def create_puntY(df):\n    categorized = []\n    for i, r in df.iterrows():\n\n        kickLength = r.kickLength\n        if 30>=kickLength:\n            # punt failed\n            categorized.append(0)\n        elif (kickLength>30) & (45>=kickLength):\n            categorized.append(1)\n        elif (kickLength>45) & (60>=kickLength):\n            categorized.append(2)\n        else: #kickLength > 60\n            categorized.append(3)\n    return categorized","metadata":{"id":"awS6BiPZPNZe","execution":{"iopub.status.busy":"2022-01-06T22:16:33.308552Z","iopub.execute_input":"2022-01-06T22:16:33.308950Z","iopub.status.idle":"2022-01-06T22:16:33.314200Z","shell.execute_reply.started":"2022-01-06T22:16:33.308909Z","shell.execute_reply":"2022-01-06T22:16:33.313539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"puntY = create_puntY(punt)\npuntX = punt.drop(columns=['kickLength'])\npuntInputCols = puntX.columns\npuntX = puntX.to_numpy()","metadata":{"id":"oFUpaGR1PiFZ","execution":{"iopub.status.busy":"2022-01-06T22:16:33.315312Z","iopub.execute_input":"2022-01-06T22:16:33.315866Z","iopub.status.idle":"2022-01-06T22:16:33.604632Z","shell.execute_reply.started":"2022-01-06T22:16:33.315827Z","shell.execute_reply":"2022-01-06T22:16:33.603630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"puntX_train, puntX_test, puntY_train, puntY_test = train_test_split(puntX, puntY, random_state=234, train_size=0.9)\n","metadata":{"id":"2X5Aio-hPl2d","execution":{"iopub.status.busy":"2022-01-06T22:16:33.605965Z","iopub.execute_input":"2022-01-06T22:16:33.606232Z","iopub.status.idle":"2022-01-06T22:16:33.614654Z","shell.execute_reply.started":"2022-01-06T22:16:33.606200Z","shell.execute_reply":"2022-01-06T22:16:33.613631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train Model","metadata":{"id":"FfbznTWwQPb5"}},{"cell_type":"code","source":"puntModel = LinearDiscriminantAnalysis()\npuntModel.fit(puntX, puntY)\nprint('Accuracy of LDA classifier on training set: {:.2f}'\n     .format(puntModel.score(puntX, puntY)))\nprint('Accuracy of LDA classifier on test set: {:.2f}'\n     .format(puntModel.score(puntX_test, puntY_test)))","metadata":{"id":"c_rDruNzQPHo","outputId":"1bb30d56-544e-4028-9d0d-96a76e13bab8","execution":{"iopub.status.busy":"2022-01-06T22:16:33.615758Z","iopub.execute_input":"2022-01-06T22:16:33.616784Z","iopub.status.idle":"2022-01-06T22:16:33.670957Z","shell.execute_reply.started":"2022-01-06T22:16:33.616715Z","shell.execute_reply":"2022-01-06T22:16:33.670106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"puntModel.predict_proba([puntX_test[0]])","metadata":{"id":"GTo1fyAWwr_3","outputId":"6e70578f-1b26-4580-8a06-14b26668a49e","execution":{"iopub.status.busy":"2022-01-06T22:16:33.672541Z","iopub.execute_input":"2022-01-06T22:16:33.673026Z","iopub.status.idle":"2022-01-06T22:16:33.680845Z","shell.execute_reply.started":"2022-01-06T22:16:33.672969Z","shell.execute_reply":"2022-01-06T22:16:33.679943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Field Goal Model","metadata":{"id":"aoNkIMEmTaAC"}},{"cell_type":"markdown","source":"### Prepare data","metadata":{"id":"5szAF7lYTcDb"}},{"cell_type":"code","source":"def cleanFgData(alldata):\n\n    FG = alldata.loc[(alldata['specialTeamsPlayType'] == 'Field Goal') & (alldata['specialTeamsResult']!='Non-Special Teams Result')]\n\n    FG = FG.reset_index()\n    FG = fillna(FG)\n    FG = FG[fgCols]\n    return FG","metadata":{"id":"qegbqyWIQd4o","execution":{"iopub.status.busy":"2022-01-06T22:16:33.682692Z","iopub.execute_input":"2022-01-06T22:16:33.683253Z","iopub.status.idle":"2022-01-06T22:16:33.696423Z","shell.execute_reply.started":"2022-01-06T22:16:33.683191Z","shell.execute_reply":"2022-01-06T22:16:33.695217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FG = cleanFgData(alldata)","metadata":{"id":"MA6RnILFQnjy","execution":{"iopub.status.busy":"2022-01-06T22:16:33.697970Z","iopub.execute_input":"2022-01-06T22:16:33.698491Z","iopub.status.idle":"2022-01-06T22:16:33.731729Z","shell.execute_reply.started":"2022-01-06T22:16:33.698448Z","shell.execute_reply":"2022-01-06T22:16:33.730707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FG.info()","metadata":{"id":"U2DImohcUv6s","outputId":"cc66d460-194b-454f-cf12-0ad869fa35f0","execution":{"iopub.status.busy":"2022-01-06T22:16:33.733247Z","iopub.execute_input":"2022-01-06T22:16:33.733725Z","iopub.status.idle":"2022-01-06T22:16:33.754378Z","shell.execute_reply.started":"2022-01-06T22:16:33.733684Z","shell.execute_reply":"2022-01-06T22:16:33.753529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fgY = FG['specialTeamsResult'].map(\n    {'Kick Attempt Good':True, \n     \"Kick Attempt No Good\":False, \n     \"Blocked Kick Attempt\":False, \n     \"Out of Bounds\":False, \n     \"Downed\":False\n     })\nfgX = FG.drop(columns=['specialTeamsResult'])\nfgInputCols = fgX.columns\nfgX = fgX.to_numpy()","metadata":{"id":"JYcdg2oUUzi4","execution":{"iopub.status.busy":"2022-01-06T22:16:33.755824Z","iopub.execute_input":"2022-01-06T22:16:33.756303Z","iopub.status.idle":"2022-01-06T22:16:33.767423Z","shell.execute_reply.started":"2022-01-06T22:16:33.756262Z","shell.execute_reply":"2022-01-06T22:16:33.766411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(fgInputCols)","metadata":{"id":"bdMn4iYAEI_h","outputId":"8f88e484-6fad-40f6-a375-811092e4f5e2","execution":{"iopub.status.busy":"2022-01-06T22:16:33.771721Z","iopub.execute_input":"2022-01-06T22:16:33.772369Z","iopub.status.idle":"2022-01-06T22:16:33.781094Z","shell.execute_reply.started":"2022-01-06T22:16:33.772313Z","shell.execute_reply":"2022-01-06T22:16:33.780210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fgX_train, fgX_test, fgY_train, fgY_test = train_test_split(fgX, fgY, random_state=2, train_size=0.8)\n","metadata":{"id":"nLV6Con7U_Eu","execution":{"iopub.status.busy":"2022-01-06T22:16:33.782817Z","iopub.execute_input":"2022-01-06T22:16:33.783311Z","iopub.status.idle":"2022-01-06T22:16:33.792429Z","shell.execute_reply.started":"2022-01-06T22:16:33.783266Z","shell.execute_reply":"2022-01-06T22:16:33.791726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train model","metadata":{"id":"SH4VXLqIVJyd"}},{"cell_type":"code","source":"fgModel = LinearDiscriminantAnalysis()\nfgModel.fit(fgX_train,fgY_train)\nprint('Accuracy of LDA classifier on training set: {:.2f}'\n     .format(fgModel.score(fgX_train, fgY_train)))\nprint('Accuracy of LDA classifier on test set: {:.2f}'\n     .format(fgModel.score(fgX_test, fgY_test)))","metadata":{"id":"t-rvzmlDVJSD","outputId":"f0f2c1d5-378f-4bcd-cfeb-6795c7e1869b","execution":{"iopub.status.busy":"2022-01-06T22:16:33.793612Z","iopub.execute_input":"2022-01-06T22:16:33.794045Z","iopub.status.idle":"2022-01-06T22:16:33.815461Z","shell.execute_reply.started":"2022-01-06T22:16:33.794014Z","shell.execute_reply":"2022-01-06T22:16:33.814476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fgModel.predict_proba([fgX_test[0]])","metadata":{"id":"hgHIydWSVfEW","outputId":"8163fa4f-3db5-41d6-cac9-9cc5b4c039ad","execution":{"iopub.status.busy":"2022-01-06T22:16:33.817526Z","iopub.execute_input":"2022-01-06T22:16:33.818308Z","iopub.status.idle":"2022-01-06T22:16:33.828738Z","shell.execute_reply.started":"2022-01-06T22:16:33.818253Z","shell.execute_reply":"2022-01-06T22:16:33.827537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Synergy Matrix\n### (Effective combination of Key + Support player)","metadata":{"id":"kYJbMtG7p5eG"}},{"cell_type":"code","source":"merged_df = pd.merge(plays, PFFScouting,  how='left', left_on=['gameId','playId'], right_on = ['gameId','playId'])\n\nmerged_df = pd.merge(merged_df, games,  how='left', left_on=['gameId'], right_on = ['gameId'])","metadata":{"id":"FReT0gm4p5eG","execution":{"iopub.status.busy":"2022-01-06T22:20:23.442535Z","iopub.execute_input":"2022-01-06T22:20:23.442840Z","iopub.status.idle":"2022-01-06T22:20:23.479204Z","shell.execute_reply.started":"2022-01-06T22:20:23.442809Z","shell.execute_reply":"2022-01-06T22:20:23.478317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def idReplace(df):\n    print(f\"Possible {df.playResult}:\")\n    print(\"- Key player: \", players_ori[players_ori.nflId == df['key']].displayName.item())\n    \n    supportNames = []\n    for i in df['support']:\n        supportNames.append(str(players_ori[players_ori.nflId == i].displayName.item()))\n    print(\"- Support player: \", supportNames, \"\\n\")","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:20:23.756284Z","iopub.execute_input":"2022-01-06T22:20:23.756857Z","iopub.status.idle":"2022-01-06T22:20:23.762677Z","shell.execute_reply.started":"2022-01-06T22:20:23.756821Z","shell.execute_reply":"2022-01-06T22:20:23.761684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## (a) Blocked Punt - kickBlockerId & puntRushers","metadata":{"id":"9adHCBGfp5eH"}},{"cell_type":"code","source":"new_df = merged_df[(merged_df.specialTeamsResult == \"Blocked Punt\")]\n\nnew_df = new_df.reset_index(drop=True)","metadata":{"id":"_l7_YniXp5eH","execution":{"iopub.status.busy":"2022-01-06T22:20:25.032625Z","iopub.execute_input":"2022-01-06T22:20:25.032883Z","iopub.status.idle":"2022-01-06T22:20:25.043936Z","shell.execute_reply.started":"2022-01-06T22:20:25.032857Z","shell.execute_reply":"2022-01-06T22:20:25.043307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check for missing values\nnew_df.puntRushers.isna().sum()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T22:20:25.325334Z","iopub.execute_input":"2022-01-06T22:20:25.325810Z","iopub.status.idle":"2022-01-06T22:20:25.331792Z","shell.execute_reply.started":"2022-01-06T22:20:25.325767Z","shell.execute_reply":"2022-01-06T22:20:25.330874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"team = []\nteamSide = []\nkeyId = []\nsupportId = []\n\n\ndef ksm_blockedpunt(new_df):\n    ## Team\n    if new_df.possessionTeam == new_df.homeTeamAbbr:\n        team.append(new_df.visitorTeamAbbr)\n    else:\n        team.append(new_df.homeTeamAbbr)\n    \n    ## Key\n    keyId.append(int(new_df.kickBlockerId))\n    \n    ## Support\n    supportId_temp = [] ## To store one row after each loop, then append to \"supportId\"\n    \n    JerseyArr = re.split(r'; |\\s+', new_df.puntRushers)\n    \n    if JerseyArr[0] == new_df.homeTeamAbbr:\n        teamSide.append('home')\n    else:\n        teamSide.append('away')\n    \n    ## Retrieve the tracking data based on the year\n    if str(new_df.gameId).startswith('2018'):\n        trackingData = globals()['tracking' + '2018']\n    elif str(new_df.gameId).startswith('2019'):\n        trackingData = globals()['tracking' + '2019']\n    else:\n        trackingData = globals()['tracking' + '2020']\n          \n    ## Retrieve nflId of \n    for j in range(0, len(JerseyArr), 2):\n        if JerseyArr[j] == new_df.homeTeamAbbr:\n            supportId_temp.append( int(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'home') & (trackingData.jerseyNumber == int(JerseyArr[j+1]))].iloc[0].nflId) )\n        else:\n            supportId_temp.append( int(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'away') & (trackingData.jerseyNumber == int(JerseyArr[j+1]))].iloc[0].nflId) )\n \n    ## Append \"supportId_temp\" to \"supportId\"\n    supportId.append(supportId_temp)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:20:25.625126Z","iopub.execute_input":"2022-01-06T22:20:25.625416Z","iopub.status.idle":"2022-01-06T22:20:25.635441Z","shell.execute_reply.started":"2022-01-06T22:20:25.625387Z","shell.execute_reply":"2022-01-06T22:20:25.634574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.apply(lambda x: ksm_blockedpunt(x), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:20:25.917275Z","iopub.execute_input":"2022-01-06T22:20:25.918061Z","iopub.status.idle":"2022-01-06T22:22:37.227341Z","shell.execute_reply.started":"2022-01-06T22:20:25.918013Z","shell.execute_reply":"2022-01-06T22:22:37.226449Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_BlockedPunt_players = pd.DataFrame({\"playResult\": 'punt block', \"team\": [team], \"side\": [teamSide], \"key\": [keyId], \"support\": [supportId]})\n\ndf_BlockedPunt_players = df_BlockedPunt_players.explode(['team','side','key','support']).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:22:37.229487Z","iopub.execute_input":"2022-01-06T22:22:37.229789Z","iopub.status.idle":"2022-01-06T22:22:37.242575Z","shell.execute_reply.started":"2022-01-06T22:22:37.229751Z","shell.execute_reply":"2022-01-06T22:22:37.241999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_BlockedPunt_players","metadata":{"scrolled":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T22:22:37.243464Z","iopub.execute_input":"2022-01-06T22:22:37.244026Z","iopub.status.idle":"2022-01-06T22:22:37.282135Z","shell.execute_reply.started":"2022-01-06T22:22:37.243994Z","shell.execute_reply":"2022-01-06T22:22:37.281217Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## (b) Blocked Kick attempt (Field Goal/Extra Point) - kickBlockerId & specialTeamsSafeties","metadata":{}},{"cell_type":"code","source":"new_df = merged_df[(merged_df.specialTeamsResult == \"Blocked Kick Attempt\")]\n\nnew_df = new_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:22:37.283687Z","iopub.execute_input":"2022-01-06T22:22:37.283964Z","iopub.status.idle":"2022-01-06T22:22:37.290402Z","shell.execute_reply.started":"2022-01-06T22:22:37.283935Z","shell.execute_reply":"2022-01-06T22:22:37.289799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check for missing values\nnew_df.specialTeamsSafeties.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:22:37.291396Z","iopub.execute_input":"2022-01-06T22:22:37.291868Z","iopub.status.idle":"2022-01-06T22:22:37.304901Z","shell.execute_reply.started":"2022-01-06T22:22:37.291830Z","shell.execute_reply":"2022-01-06T22:22:37.304021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Removing records with missing values\nnew_df = new_df.dropna(subset=['specialTeamsSafeties'])","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:22:37.305914Z","iopub.execute_input":"2022-01-06T22:22:37.306274Z","iopub.status.idle":"2022-01-06T22:22:37.321032Z","shell.execute_reply.started":"2022-01-06T22:22:37.306243Z","shell.execute_reply":"2022-01-06T22:22:37.320214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"team = []\nteamSide = []\nkeyId = []\nsupportId = []\n\n\ndef ksm_blockedkick(new_df):\n    ## Team\n    if new_df.possessionTeam == new_df.homeTeamAbbr:\n        team.append(new_df.visitorTeamAbbr)\n    else:\n        team.append(new_df.homeTeamAbbr)\n        \n    ## Key\n    keyId.append(int(new_df.kickBlockerId))\n    \n    ## Support\n    supportId_temp = [] ## To store one row after each loop, then append to \"supportId\"\n    \n    if new_df.possessionTeam == new_df.homeTeamAbbr:\n        teamSide.append('home')\n    else:\n        teamSide.append('away')\n            \n    if not pd.isna(new_df.specialTeamsSafeties):\n        JerseyArr = re.split(r'; |\\s+', new_df.specialTeamsSafeties)\n        \n\n        ## Retrieve the tracking data based on the year\n        if str(new_df.gameId).startswith('2018'):\n            trackingData = globals()['tracking' + '2018']\n        elif str(new_df.gameId).startswith('2019'):\n            trackingData = globals()['tracking' + '2019']\n        else:\n            trackingData = globals()['tracking' + '2020']\n          \n        ## Retrieve nflId of \n        for j in range(0, len(JerseyArr), 2):\n            if JerseyArr[j] == new_df.homeTeamAbbr:\n                supportId_temp.append( int(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'home') & (trackingData.jerseyNumber == int(JerseyArr[j+1]))].iloc[0].nflId) )\n            else:\n                supportId_temp.append( int(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'away') & (trackingData.jerseyNumber == int(JerseyArr[j+1]))].iloc[0].nflId) )\n    \n    ## Append \"supportId_temp\" to \"supportId\"\n    supportId.append(supportId_temp)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-01-06T22:22:37.322108Z","iopub.execute_input":"2022-01-06T22:22:37.322870Z","iopub.status.idle":"2022-01-06T22:22:37.334233Z","shell.execute_reply.started":"2022-01-06T22:22:37.322835Z","shell.execute_reply":"2022-01-06T22:22:37.333334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.apply(lambda x: ksm_blockedkick(x), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:22:37.336360Z","iopub.execute_input":"2022-01-06T22:22:37.336785Z","iopub.status.idle":"2022-01-06T22:23:32.121552Z","shell.execute_reply.started":"2022-01-06T22:22:37.336742Z","shell.execute_reply":"2022-01-06T22:23:32.120682Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_BlockedKickAttempt_players = pd.DataFrame({\"playResult\": 'kick block attempt', \"team\": [team], \"side\": [teamSide], \"key\": [keyId], \"support\": [supportId]})\n\ndf_BlockedKickAttempt_players = df_BlockedKickAttempt_players.explode(['team','side','key','support']).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T22:23:32.122824Z","iopub.execute_input":"2022-01-06T22:23:32.123102Z","iopub.status.idle":"2022-01-06T22:23:32.134817Z","shell.execute_reply.started":"2022-01-06T22:23:32.123063Z","shell.execute_reply":"2022-01-06T22:23:32.133832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_BlockedKickAttempt_players = df_BlockedKickAttempt_players[~df_BlockedKickAttempt_players.support.str.len().eq(0)].reset_index(drop=True)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-01-06T22:23:32.137245Z","iopub.execute_input":"2022-01-06T22:23:32.137480Z","iopub.status.idle":"2022-01-06T22:23:32.151994Z","shell.execute_reply.started":"2022-01-06T22:23:32.137453Z","shell.execute_reply":"2022-01-06T22:23:32.151184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## (c) Tackle on return - tacklers & assistTacklers","metadata":{}},{"cell_type":"code","source":"new_df = merged_df[(merged_df.specialTeamsResult == \"Return\")]\n\nnew_df = new_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T19:21:25.500205Z","iopub.execute_input":"2022-01-06T19:21:25.500469Z","iopub.status.idle":"2022-01-06T19:21:25.509438Z","shell.execute_reply.started":"2022-01-06T19:21:25.500444Z","shell.execute_reply":"2022-01-06T19:21:25.508832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking for missing values\nnew_df.assistTackler.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-01-06T19:21:25.761323Z","iopub.execute_input":"2022-01-06T19:21:25.761563Z","iopub.status.idle":"2022-01-06T19:21:25.767184Z","shell.execute_reply.started":"2022-01-06T19:21:25.761536Z","shell.execute_reply":"2022-01-06T19:21:25.766777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Removing records with missing values\nnew_df = new_df.dropna(subset=['assistTackler']).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T19:21:26.808376Z","iopub.execute_input":"2022-01-06T19:21:26.808915Z","iopub.status.idle":"2022-01-06T19:21:26.81534Z","shell.execute_reply.started":"2022-01-06T19:21:26.808889Z","shell.execute_reply":"2022-01-06T19:21:26.814617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\nteam = []\nteamSide = []\n\nkeyArr = []\n\nkeyId = []\nsupportId = []\n\n\ndef ksm_returntackled(new_df):    \n    ## Team\n    if new_df.possessionTeam == new_df.homeTeamAbbr:\n        team.append(new_df.visitorTeamAbbr)\n    else:\n        team.append(new_df.homeTeamAbbr)\n    \n    \n    \n    if new_df.possessionTeam == new_df.homeTeamAbbr:\n        teamSide.append('home')\n    else:\n        teamSide.append('away')\n        \n\n    ## Retrieve the tracking data based on the year\n    if str(new_df.gameId).startswith('2018'):\n        trackingData = globals()['tracking' + '2018']\n    elif str(new_df.gameId).startswith('2019'):\n        trackingData = globals()['tracking' + '2019']\n    else:\n        trackingData = globals()['tracking' + '2020']\n        \n    \n    ## Key\n    keyArr = re.split(r'; |\\s+', new_df.tackler)\n    if keyArr[0] == new_df.homeTeamAbbr:\n        keyId.append( int(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'home') & (trackingData.jerseyNumber == int(keyArr[1]))].iloc[0].nflId) )\n    else:\n        keyId.append( int(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'away') & (trackingData.jerseyNumber == int(keyArr[1]))].iloc[0].nflId) )\n\n    \n    \n    ## Support\n    supportId_temp = [] ## To store one row after each loop, then append to \"supportId\"\n    \n    if not pd.isna(new_df.assistTackler):\n        JerseyArr = re.split(r'; |\\s+', new_df.assistTackler)\n\n        ## Retrieve nflId of \n        for j in range(0, len(JerseyArr), 2):\n            if JerseyArr[j] == new_df.homeTeamAbbr:\n                supportId_temp.append( int(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'home') & (trackingData.jerseyNumber == int(JerseyArr[j+1]))].iloc[0].nflId) )\n            else:\n                supportId_temp.append( int(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'away') & (trackingData.jerseyNumber == int(JerseyArr[j+1]))].iloc[0].nflId) )\n \n    ## Append \"supportId_temp\" to \"supportId\"\n    supportId.append(supportId_temp)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T19:25:25.001949Z","iopub.execute_input":"2022-01-06T19:25:25.002189Z","iopub.status.idle":"2022-01-06T19:25:25.016455Z","shell.execute_reply.started":"2022-01-06T19:25:25.002165Z","shell.execute_reply":"2022-01-06T19:25:25.015441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.apply(lambda x: ksm_returntackled(x), axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T19:25:28.332036Z","iopub.execute_input":"2022-01-06T19:25:28.332321Z","iopub.status.idle":"2022-01-06T19:37:27.845282Z","shell.execute_reply.started":"2022-01-06T19:25:28.332292Z","shell.execute_reply":"2022-01-06T19:37:27.843839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ReturnTackled_players = pd.DataFrame({\"playType\": 'tackle on return', \"team\": [team], \"side\": [teamSide], \"key\": [keyId], \"support\": [supportId]})\n\ndf_ReturnTackled_players = df_ReturnTackled_players.explode(['team','side','key','support']).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T19:37:27.846256Z","iopub.status.idle":"2022-01-06T19:37:27.846641Z","shell.execute_reply.started":"2022-01-06T19:37:27.84643Z","shell.execute_reply":"2022-01-06T19:37:27.84645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ReturnTackled_players","metadata":{"_kg_hide-output":true,"_kg_hide-input":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## (d) Return (returnerId & vises)","metadata":{}},{"cell_type":"code","source":"new_df = merged_df[(merged_df.specialTeamsPlayType == \"Punt\") & (merged_df.specialTeamsResult == \"Return\")]\n\nnew_df = new_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T19:25:20.050557Z","iopub.status.idle":"2022-01-06T19:25:20.050832Z","shell.execute_reply.started":"2022-01-06T19:25:20.05069Z","shell.execute_reply":"2022-01-06T19:25:20.05071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking missing values\nprint(new_df.returnerId.isna().sum())\nprint(new_df.vises.isna().sum())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df = new_df.dropna(subset=['returnerId','vises']).reset_index(drop=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\nteam = []\nteamSide = []\n\nkeyArr = []\n\nkeyId = []\nsupportId = []\n\n\ndef ksm_return(new_df):\n    ## Team\n    if new_df.possessionTeam == new_df.homeTeamAbbr:\n        team.append(new_df.visitorTeamAbbr)\n    else:\n        team.append(new_df.homeTeamAbbr)\n    \n    \n    ## Key\n    keyId.append(new_df.returnerId)\n    \n    \n    if new_df.possessionTeam == new_df.homeTeamAbbr:\n        teamSide.append('home')\n    else:\n        teamSide.append('away')\n        \n\n    ## Retrieve the tracking data based on the year\n    if str(new_df.gameId).startswith('2018'):\n        trackingData = globals()['tracking' + '2018']\n    elif str(new_df.gameId).startswith('2019'):\n        trackingData = globals()['tracking' + '2019']\n    else:\n        trackingData = globals()['tracking' + '2020']\n        \n    \n\n    ## Support\n    supportId_temp = [] ## To store one row after each loop, then append to \"supportId\"\n    \n    JerseyArr = re.split(r'; |\\s+', new_df.vises)\n\n    \n    ## Retrieve nflId of SUPPORT\n    for j in range(0, len(JerseyArr), 2):\n        if JerseyArr[j] == new_df.homeTeamAbbr:\n            if len(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'home') & (trackingData.jerseyNumber == int(JerseyArr[j+1]))]) > 0:\n                supportId_temp.append( int(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'home') & (trackingData.jerseyNumber == int(JerseyArr[j+1]))].iloc[0].nflId) )\n        else:\n            if len(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'away') & (trackingData.jerseyNumber == int(JerseyArr[j+1]))]) > 0:\n                supportId_temp.append( int(trackingData.loc[(trackingData.gameId == new_df.gameId) & (trackingData.team == 'away') & (trackingData.jerseyNumber == int(JerseyArr[j+1]))].iloc[0].nflId) )\n        \n    ## Append \"supportId_temp\" to \"supportId\"\n    supportId.append(supportId_temp)","metadata":{"execution":{"iopub.status.busy":"2022-01-06T19:37:27.848166Z","iopub.status.idle":"2022-01-06T19:37:27.848505Z","shell.execute_reply.started":"2022-01-06T19:37:27.848326Z","shell.execute_reply":"2022-01-06T19:37:27.848345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.apply(lambda x: ksm_return(x), axis=1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_Return_players = pd.DataFrame({\"playResult\": 'punt return', \"team\": [team], \"side\": [teamSide], \"key\": [keyId], \"support\": [supportId]})\n\ndf_Return_players = df_Return_players.explode(['team','side','key','support']).reset_index(drop=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_Return_players","metadata":{"_kg_hide-output":true,"_kg_hide-input":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Step 5 - Recommendation System / Prediction","metadata":{"id":"j5ZJewihVwHf"}},{"cell_type":"code","source":"alldata.columns","metadata":{"id":"SinrsvxWY75w","outputId":"279fdd0e-0847-4bce-dc6d-8500f6405a4b","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-01-06T22:23:32.153143Z","iopub.execute_input":"2022-01-06T22:23:32.153766Z","iopub.status.idle":"2022-01-06T22:23:32.166430Z","shell.execute_reply.started":"2022-01-06T22:23:32.153722Z","shell.execute_reply":"2022-01-06T22:23:32.165713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputCols = ['gameClock','down', 'quarter', 'yardsToGo',\n             'yardlineSide','yardlineNumber', 'absoluteYardlineNumber',\n             'preSnapVisitorScore', 'preSnapHomeScore',\n             'possessionTeam', 'homeTeamAbbr', 'visitorTeamAbbr', 'nflId', 'gameDate']\nstrategyCols = ['direction', 'kickType']\ngoalCols = ['kickLength','specialTeamsResult','specialTeamsPlayType']\nprint(len(inputCols) + len(strategyCols) + len(goalCols))","metadata":{"id":"7KLPRpz7X51p","outputId":"19502df2-6d47-49c6-a7a7-62161f8628b7","execution":{"iopub.status.busy":"2022-01-06T22:23:32.167541Z","iopub.execute_input":"2022-01-06T22:23:32.167918Z","iopub.status.idle":"2022-01-06T22:23:32.180766Z","shell.execute_reply.started":"2022-01-06T22:23:32.167890Z","shell.execute_reply":"2022-01-06T22:23:32.179878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cleanInput(inputs, team_map):\n\n    # count the players age in the play\n    nflid = inputs['nflId']\n    player = players_ori.loc[players.nflId == nflid].to_dict('r')[0]\n\n     # Get the Height data from DataFrame & Split the heights by hyphen (\"-\")\n    player_height = player[\"height\"]\n    player_height = player_height.split(\"-\")\n\n    # Convert Heights to Centimeters and add them to DataFrame\n    if len(player_height) == 2:\n        player[\"height\"] = int(player_height[0]) * 12 + int(player_height[1]) \n    else: \n        player[\"height\"] = int(player_height[0]) * 2.54\n\n    # Convert Weights to Kilograms and them to DataFrame\n    player[\"weight\"] = round(player['weight'] * 0.453592, 2)\n\n    inputs['Position'] = player['Position']\n    inputs['height'] = player['height']\n    inputs['weight'] = player['weight']\n\n    inputs['birthDate'] = datetime.strptime(player['birthDate'], \"%Y-%m-%d\")\n    inputs[\"gameDate\"] = datetime.strptime(inputs[\"gameDate\"], \"%m/%d/%Y\")\n\n    inputs[\"age\"]=inputs[\"gameDate\"].year - inputs[\"birthDate\"].year\n    inputs = inputs.drop(labels=[\"birthDate\", \"gameDate\"])\n\n    inputs['gameClock'] = pd.to_timedelta(inputs['gameClock'])\n    inputs['gameClock'] = inputs['gameClock'].total_seconds()\n    #  convert team abbr to number\n    inputs['homeTeamAbbr'] = team_map[inputs['homeTeamAbbr']]\n    inputs['visitorTeamAbbr'] = team_map[inputs['visitorTeamAbbr']]\n    inputs['possessionTeam'] = team_map[inputs['possessionTeam']]\n    inputs['yardlineSide'] = team_map[inputs['yardlineSide']]\n\n    # conver categorical data to number\n    inputs['Position']= pos_map[inputs['Position']]\n    inputs = inputs.astype('float64')\n\n    return inputs\n","metadata":{"id":"BWL3yjXMcZMk","execution":{"iopub.status.busy":"2022-01-06T22:23:32.181923Z","iopub.execute_input":"2022-01-06T22:23:32.182335Z","iopub.status.idle":"2022-01-06T22:23:32.194222Z","shell.execute_reply.started":"2022-01-06T22:23:32.182294Z","shell.execute_reply":"2022-01-06T22:23:32.193433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getPuntRes(inputs):\n    bestK=0\n    bestD =0\n    bestRes = 0\n    bestProb = 0\n    for k in range(1,4):\n        tmp = []\n        for d in range(1,4):\n            puntInputs = inputs\n\n            puntInputs['kickType'] = k\n            puntInputs['direction'] = d\n            puntInputs = puntInputs[puntInputCols].astype('float64')\n            puntInputs = puntInputs.to_numpy()\n            curRes = puntModel.predict([puntInputs])[0]\n            curProb = puntModel.predict_proba([puntInputs])[0, curRes]\n            \n            if (curRes>bestRes) &(curProb>bestProb) :\n                bestK=k\n                bestD =d\n                bestRes = curRes\n                bestProb = curProb\n\n    return bestK, bestD, bestRes, bestProb","metadata":{"id":"XL9QVGlSPTOI","execution":{"iopub.status.busy":"2022-01-06T22:23:32.195331Z","iopub.execute_input":"2022-01-06T22:23:32.195899Z","iopub.status.idle":"2022-01-06T22:23:32.212460Z","shell.execute_reply.started":"2022-01-06T22:23:32.195868Z","shell.execute_reply":"2022-01-06T22:23:32.211448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"re_kick_map ={\n    1: 'Normal - standard punt style',\n    3: 'Rugby style punt',\n    2: 'Nose down or Aussie-style punts' }","metadata":{"id":"Y9dFN1uEUQa7","execution":{"iopub.status.busy":"2022-01-06T22:23:32.214978Z","iopub.execute_input":"2022-01-06T22:23:32.215556Z","iopub.status.idle":"2022-01-06T22:23:32.228220Z","shell.execute_reply.started":"2022-01-06T22:23:32.215520Z","shell.execute_reply":"2022-01-06T22:23:32.227551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"re_dir_map ={\n    2: 'Left', 3: 'Right', 1: 'Center'}","metadata":{"id":"GKAQeFcAU6nA","execution":{"iopub.status.busy":"2022-01-06T22:23:32.230204Z","iopub.execute_input":"2022-01-06T22:23:32.230532Z","iopub.status.idle":"2022-01-06T22:23:32.242621Z","shell.execute_reply.started":"2022-01-06T22:23:32.230502Z","shell.execute_reply":"2022-01-06T22:23:32.241709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"punt_res_map = {\n    0:'less than 30',\n    1:\"between 30 and 45\",\n    2:\"between 45 and 60\",\n    3: 'over 60'\n}","metadata":{"id":"FoB-r0WQV3Ov","execution":{"iopub.status.busy":"2022-01-06T22:23:32.243766Z","iopub.execute_input":"2022-01-06T22:23:32.244225Z","iopub.status.idle":"2022-01-06T22:23:32.255673Z","shell.execute_reply.started":"2022-01-06T22:23:32.244190Z","shell.execute_reply":"2022-01-06T22:23:32.254736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type_map ={0:'FG',1:'nonSP',2:'Punt'}","metadata":{"id":"BOPjRMtEXzu-","execution":{"iopub.status.busy":"2022-01-06T22:23:32.257050Z","iopub.execute_input":"2022-01-06T22:23:32.257279Z","iopub.status.idle":"2022-01-06T22:23:32.269333Z","shell.execute_reply.started":"2022-01-06T22:23:32.257254Z","shell.execute_reply":"2022-01-06T22:23:32.268417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(inputs):\n    if inputs.possessionTeam == inputs.homeTeamAbbr:\n        opponentTeam = inputs.visitorTeamAbbr\n    else:\n        opponentTeam = inputs.homeTeamAbbr\n    \n    Type=[]\n    inputs = cleanInput(inputs, team_map)\n    # predict play type\n    typeInputs = inputs[typeInputCol].to_numpy()\n    typeRes = typeModel.predict_proba([typeInputs])\n    Type.append(np.argwhere(typeRes == np.max(typeRes))[0,1])\n    Type.append(np.argwhere(typeRes == np.unique(typeRes)[-2])[0,1])\n#     print(typeRes)\n#     print(Type)\n\n    \n    for idx, t in enumerate(Type):\n        print(f'No. {idx+1} recommended play type: {type_map.get(t)}')\n        \n        # FG\n        if t == 0:\n            fgInputs = inputs[fgInputCols].to_numpy()\n            fgSuccessRate = fgModel.predict_proba([fgInputs])[0,1]\n            print(f'    Success rate of Field Goal is {round(fgSuccessRate*100,2)}%\\n')\n            \n            if len(df_BlockedKickAttempt_players[df_BlockedKickAttempt_players.team == opponentTeam]) > 0:\n                df_BlockedKickAttempt_players[df_BlockedKickAttempt_players.team == opponentTeam].apply(lambda x: idReplace(x), axis=1)\n                \n            print(\"--------------------------------------\\n\")    \n        \n        elif t == 1:\n            print('Non-special team result')\n        \n        # Punt\n        else:\n            k, d, res, prob = getPuntRes(inputs)\n            resKick = re_kick_map.get(k)\n            resDir = re_dir_map.get(d)\n            puntRes = punt_res_map.get(res)\n            puntProb = round(prob*100)\n            print(f'    Suggested strategy for Punt: \\n    Direction: {resDir}, KicktType: {resKick}')\n            print(f'    Prediction: {puntProb}% of change kick to the distance {puntRes}\\n')\n            \n            if len(df_BlockedPunt_players[df_BlockedPunt_players.team == opponentTeam]) > 0:\n               df_BlockedPunt_players[df_BlockedPunt_players.team == opponentTeam].apply(lambda x: idReplace(x), axis=1)\n            \n            print(\"--------------------------------------\\n\") \n        \n\n","metadata":{"id":"LlfRawKxWS2G","execution":{"iopub.status.busy":"2022-01-06T22:23:32.270714Z","iopub.execute_input":"2022-01-06T22:23:32.270921Z","iopub.status.idle":"2022-01-06T22:23:32.283958Z","shell.execute_reply.started":"2022-01-06T22:23:32.270896Z","shell.execute_reply":"2022-01-06T22:23:32.283205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try_inputs = rawDf.loc[120]\ntry_inputs = try_inputs[inputCols]\npredict(try_inputs)","metadata":{"id":"6qcdRi75tR6j","outputId":"8410b642-735b-43e8-aa0f-bf4c21ac2e50","execution":{"iopub.status.busy":"2022-01-06T22:23:32.285380Z","iopub.execute_input":"2022-01-06T22:23:32.285685Z","iopub.status.idle":"2022-01-06T22:23:32.340216Z","shell.execute_reply.started":"2022-01-06T22:23:32.285646Z","shell.execute_reply":"2022-01-06T22:23:32.339335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}