{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport numpy as np\nimport networkx as nx","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:13:38.13567Z","iopub.execute_input":"2022-01-07T12:13:38.13652Z","iopub.status.idle":"2022-01-07T12:13:38.64752Z","shell.execute_reply.started":"2022-01-07T12:13:38.136388Z","shell.execute_reply":"2022-01-07T12:13:38.646601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datasetFilePath = '../input/nfl-big-data-bowl-2022/'\ngamesFilePath = datasetFilePath + 'games.csv'\nPFFScoutingFilePath = datasetFilePath + 'PFFScoutingData.csv'\nplaysFilePath = datasetFilePath + 'plays.csv'\nplayersFilePath = datasetFilePath + 'players.csv'\ntracking2018FilePath = datasetFilePath + 'tracking2018.csv'\ntracking2019FilePath = datasetFilePath + 'tracking2019.csv'\ntracking2020FilePath = datasetFilePath + 'tracking2020.csv'\nPFFScoutingFilePath = datasetFilePath + 'PFFScoutingData.csv'\n","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:13:38.64954Z","iopub.execute_input":"2022-01-07T12:13:38.649787Z","iopub.status.idle":"2022-01-07T12:13:38.655077Z","shell.execute_reply.started":"2022-01-07T12:13:38.649755Z","shell.execute_reply":"2022-01-07T12:13:38.653987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gamesDF = pd.read_csv(gamesFilePath);\nPFFScoutingDF = pd.read_csv(PFFScoutingFilePath);\nplaysDF = pd.read_csv(playsFilePath)\nplayersDF = pd.read_csv(playersFilePath)\ntracking2018DF = pd.read_csv(tracking2018FilePath)\ntracking2019DF = pd.read_csv(tracking2019FilePath)\ntracking2020DF = pd.read_csv(tracking2020FilePath)\nPFFScoutingDF = pd.read_csv(PFFScoutingFilePath)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:13:38.656436Z","iopub.execute_input":"2022-01-07T12:13:38.656676Z","iopub.status.idle":"2022-01-07T12:15:16.468934Z","shell.execute_reply.started":"2022-01-07T12:13:38.656646Z","shell.execute_reply":"2022-01-07T12:15:16.467864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frames = [tracking2018DF, tracking2019DF, tracking2020DF]\nmergedTrackingDF = pd.concat(frames, keys=[\"2018\", \"2019\", \"2020\"])","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:15:16.470386Z","iopub.execute_input":"2022-01-07T12:15:16.470625Z","iopub.status.idle":"2022-01-07T12:15:30.019397Z","shell.execute_reply.started":"2022-01-07T12:15:16.470588Z","shell.execute_reply":"2022-01-07T12:15:30.018549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_crosstab = pd.crosstab(index=playsDF[\"specialTeamsResult\"], \n                            columns=playsDF[\"specialTeamsPlayType\"])","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:15:30.021166Z","iopub.execute_input":"2022-01-07T12:15:30.021738Z","iopub.status.idle":"2022-01-07T12:15:30.067229Z","shell.execute_reply.started":"2022-01-07T12:15:30.021693Z","shell.execute_reply":"2022-01-07T12:15:30.066478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_crosstab","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:15:30.068585Z","iopub.execute_input":"2022-01-07T12:15:30.069072Z","iopub.status.idle":"2022-01-07T12:15:30.08658Z","shell.execute_reply.started":"2022-01-07T12:15:30.069029Z","shell.execute_reply":"2022-01-07T12:15:30.085728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(5,5)) \nsns.heatmap(my_crosstab, annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:15:30.087899Z","iopub.execute_input":"2022-01-07T12:15:30.0882Z","iopub.status.idle":"2022-01-07T12:15:30.686749Z","shell.execute_reply.started":"2022-01-07T12:15:30.088162Z","shell.execute_reply":"2022-01-07T12:15:30.685914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_punts(filter):\n    df = playsDF.loc[(playsDF['specialTeamsPlayType'] == 'Punt') & (playsDF['specialTeamsResult'].isin(filter))]\n    df = df[['playId', 'gameId','specialTeamsResult', 'playResult', 'possessionTeam', 'kickerId']]\n    df['operationTime'] = 0\n    df['hangTime'] = 0\n    df['kickDirectionActual'] = 0\n    df['kickDirectionIntended'] = 0\n    df['ActualVsIntended'] = 0\n    df['kickType'] = 0\n    \n    for index,  x in df.iterrows():\n        df.loc[[index], ['operationTime']] = PFFScoutingDF['operationTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['hangTime']] = PFFScoutingDF['hangTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionActual']] = PFFScoutingDF['kickDirectionActual'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionIntended']] = PFFScoutingDF['kickDirectionIntended'].loc[x['playId'] == PFFScoutingDF['playId']]\n        actual = df.loc[index, 'kickDirectionActual']\n        intended = df.loc[index, 'kickDirectionIntended']\n        df.loc[index, 'ActualVsIntended'] = (actual == intended)\n        df.loc[[index], ['kickType']] = PFFScoutingDF['kickType'].loc[x['playId'] == PFFScoutingDF['playId']]\n        \n    return df","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:15:30.688195Z","iopub.execute_input":"2022-01-07T12:15:30.688575Z","iopub.status.idle":"2022-01-07T12:15:30.698689Z","shell.execute_reply.started":"2022-01-07T12:15:30.688531Z","shell.execute_reply":"2022-01-07T12:15:30.697797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"good_filter = ['Fair Catch', 'Touchback']\nbad_filter = ['Return', 'Out of Bounds']\n\ngood_punt_df = process_punts(good_filter)\nbad_punt_df = process_punts(bad_filter)","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:15:30.699779Z","iopub.execute_input":"2022-01-07T12:15:30.700023Z","iopub.status.idle":"2022-01-07T12:16:13.024913Z","shell.execute_reply.started":"2022-01-07T12:15:30.699985Z","shell.execute_reply":"2022-01-07T12:16:13.023969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nax = bad_punt_df.plot(kind='scatter', x='hangTime', y='playResult',\n                                           color='Red', label='bad')\ngood_punt_df.plot(kind='scatter', x='hangTime', y='playResult',\n                                          color='Green', label='good', ax=ax)\n\nplt.title(\"Good punt vs bad punt - Hangtime and distance\")","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:13.02614Z","iopub.execute_input":"2022-01-07T12:16:13.027105Z","iopub.status.idle":"2022-01-07T12:16:13.356472Z","shell.execute_reply.started":"2022-01-07T12:16:13.027066Z","shell.execute_reply":"2022-01-07T12:16:13.355363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"good_punt_df.loc[(good_punt_df['ActualVsIntended'] == False)]","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:13.357808Z","iopub.execute_input":"2022-01-07T12:16:13.358717Z","iopub.status.idle":"2022-01-07T12:16:13.396049Z","shell.execute_reply.started":"2022-01-07T12:16:13.358669Z","shell.execute_reply":"2022-01-07T12:16:13.395037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot3D_kickType(df):\n    x1 = df.loc[(df['kickType'] == 'N'), 'hangTime']\n    y1 = df.loc[(df['kickType'] == 'N'), 'operationTime']\n    z1 = df.loc[(df['kickType'] == 'N'), 'playResult']\n\n    x2 = df.loc[(df['kickType'] == 'R'), 'hangTime']\n    y2 = df.loc[(df['kickType'] == 'R'), 'operationTime']\n    z2 = df.loc[(df['kickType'] == 'R'), 'playResult']\n\n    x3 = df.loc[(df['kickType'] == 'A'), 'hangTime']\n    y3 = df.loc[(df['kickType'] == 'A'), 'operationTime']\n    z3 = df.loc[(df['kickType'] == 'A'), 'playResult']\n\n\n    fig = plt.figure(figsize=(8, 8))\n    ax = fig.add_subplot(111, projection='3d')\n    ax.scatter(x1, y1, z1,\n               linewidths=1, alpha=.7,\n               edgecolor='k',\n               s = 200,\n               label = \"Normal\"\n               )\n\n    ax.scatter(x2, y2, z2,\n               linewidths=1, alpha=.7,\n               edgecolor='k',\n               s = 200,\n               label = \"Rugby\"\n               )\n    ax.scatter(x3, y3, z3,\n               linewidths=1, alpha=.7,\n               edgecolor='k',\n               s = 200,\n               label = \"Aussie\"\n               )\n    ax.legend()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:13.397174Z","iopub.execute_input":"2022-01-07T12:16:13.397386Z","iopub.status.idle":"2022-01-07T12:16:13.40701Z","shell.execute_reply.started":"2022-01-07T12:16:13.39736Z","shell.execute_reply":"2022-01-07T12:16:13.405863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot3D_kickType(bad_punt_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:13.408754Z","iopub.execute_input":"2022-01-07T12:16:13.409124Z","iopub.status.idle":"2022-01-07T12:16:13.835069Z","shell.execute_reply.started":"2022-01-07T12:16:13.409076Z","shell.execute_reply":"2022-01-07T12:16:13.833998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_ActualVsIntended(df, x_axis, y_axis, title):\n    plt.figure(figsize=(10,10))\n    ax = df.loc[(df['ActualVsIntended'] == True)].plot(kind='scatter', x=x_axis, y=y_axis,\n                                               color='Green', label='TRUE')\n    ax1 = df.loc[(df['ActualVsIntended'] == False)].plot(kind='scatter', x=x_axis, y=y_axis,\n                                              color='Red', label='FALSE', ax=ax)\n\n    plt.title(title)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:13.839538Z","iopub.execute_input":"2022-01-07T12:16:13.840128Z","iopub.status.idle":"2022-01-07T12:16:13.847638Z","shell.execute_reply.started":"2022-01-07T12:16:13.840075Z","shell.execute_reply":"2022-01-07T12:16:13.846977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_ActualVsIntended(good_punt_df, 'hangTime', 'playResult', 'Good Punt')\nplot_ActualVsIntended(bad_punt_df, 'hangTime', 'playResult', 'Bad Punt')","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:13.848774Z","iopub.execute_input":"2022-01-07T12:16:13.849575Z","iopub.status.idle":"2022-01-07T12:16:14.426416Z","shell.execute_reply.started":"2022-01-07T12:16:13.849507Z","shell.execute_reply":"2022-01-07T12:16:14.425456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_kickDirectionActual(df, x_axis, y_axis, title):\n    plt.figure(figsize=(10,10))\n    ax = df.loc[(df['kickDirectionActual'] == 'L')].plot(kind='scatter', x=x_axis, y=y_axis,\n                                               color='Red', label='LEFT')\n    ax1 = df.loc[(df['kickDirectionActual'] == 'R')].plot(kind='scatter',  x=x_axis, y=y_axis,\n                                              color='Green', label='RIGHT', ax=ax)\n    ax2 = df.loc[(df['kickDirectionActual'] == 'C')].plot(kind='scatter',  x=x_axis, y=y_axis,\n                                              color='BLUE', label='CENTER', ax=ax1)\n    ax.legend()\n    plt.title(title)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:14.427957Z","iopub.execute_input":"2022-01-07T12:16:14.428303Z","iopub.status.idle":"2022-01-07T12:16:14.436978Z","shell.execute_reply.started":"2022-01-07T12:16:14.428237Z","shell.execute_reply":"2022-01-07T12:16:14.435909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_kickDirectionActual(good_punt_df, 'hangTime', 'playResult', 'Good Punt')\nplot_kickDirectionActual(bad_punt_df, 'hangTime', 'playResult', 'Bad Punt')","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:14.438129Z","iopub.execute_input":"2022-01-07T12:16:14.438726Z","iopub.status.idle":"2022-01-07T12:16:15.206171Z","shell.execute_reply.started":"2022-01-07T12:16:14.438686Z","shell.execute_reply":"2022-01-07T12:16:15.20526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#%matplotlib notebook\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport numpy as np\n\nx = good_punt_df['hangTime']\ny = good_punt_df['operationTime']\nz = good_punt_df['playResult']\n\nx1 = bad_punt_df['hangTime']\ny1 = bad_punt_df['operationTime']\nz1 = bad_punt_df['playResult']\n\nfig = plt.figure(figsize=(8, 8))\nax = fig.add_subplot(111, projection='3d')\nax.scatter(x, y, z,\n           linewidths=1, alpha=.7, label = \"Good Punt\",\n           edgecolor='k',\n           s = 200,\n           )\n\nax.scatter(x1, y1, z1,\n           linewidths=1, alpha=.7, label = \"Bad Punt\",\n           edgecolor='k',\n           s = 200,\n           )\nax.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:15.207566Z","iopub.execute_input":"2022-01-07T12:16:15.207836Z","iopub.status.idle":"2022-01-07T12:16:15.641383Z","shell.execute_reply.started":"2022-01-07T12:16:15.207796Z","shell.execute_reply":"2022-01-07T12:16:15.640693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot3D_kickDirectionActual(df):\n    x = df.loc[(df['kickDirectionActual'] == 'L'), 'hangTime']\n    y = df.loc[(df['kickDirectionActual'] == 'L'), 'operationTime']\n    z = df.loc[(df['kickDirectionActual'] == 'L'), 'playResult']\n\n    x1 = df.loc[(df['kickDirectionActual'] == 'C'), 'hangTime']\n    y1 = df.loc[(df['kickDirectionActual'] == 'C'), 'operationTime']\n    z1 = df.loc[(df['kickDirectionActual'] == 'C'), 'playResult']\n\n    x2 = df.loc[(df['kickDirectionActual'] == 'R'), 'hangTime']\n    y2 = df.loc[(df['kickDirectionActual'] == 'R'), 'operationTime']\n    z2 = df.loc[(df['kickDirectionActual'] == 'R'), 'playResult']\n\n\n    fig = plt.figure(figsize=(8, 8))\n    ax = fig.add_subplot(111, projection='3d')\n    ax.scatter(x, y, z,\n               linewidths=1, alpha=.7,\n               edgecolor='k',\n               s = 200,\n               )\n\n    ax.scatter(x1, y1, z1,\n               linewidths=1, alpha=.7,\n               edgecolor='k',\n               s = 200,\n               )\n    ax.scatter(x2, y2, z2,\n               linewidths=1, alpha=.7,\n               edgecolor='k',\n               s = 200,\n               )\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:15.642677Z","iopub.execute_input":"2022-01-07T12:16:15.642985Z","iopub.status.idle":"2022-01-07T12:16:15.652022Z","shell.execute_reply.started":"2022-01-07T12:16:15.642923Z","shell.execute_reply":"2022-01-07T12:16:15.65102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot3D_kickDirectionActual(bad_punt_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:15.653766Z","iopub.execute_input":"2022-01-07T12:16:15.654055Z","iopub.status.idle":"2022-01-07T12:16:15.985455Z","shell.execute_reply.started":"2022-01-07T12:16:15.654021Z","shell.execute_reply":"2022-01-07T12:16:15.984858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport numpy as np\n\nx = good_punt_df.loc[(good_punt_df['ActualVsIntended'] == True), 'hangTime']\ny = good_punt_df.loc[(good_punt_df['ActualVsIntended'] == True), 'operationTime']\nz = good_punt_df.loc[(good_punt_df['ActualVsIntended'] == True), 'playResult']\n\nx1 = bad_punt_df.loc[(bad_punt_df['ActualVsIntended'] == False), 'hangTime']\ny1 = bad_punt_df.loc[(bad_punt_df['ActualVsIntended'] == False), 'operationTime']\nz1 = bad_punt_df.loc[(bad_punt_df['ActualVsIntended'] == False), 'playResult']\n\nfig = plt.figure(figsize=(8, 8))\nax = fig.add_subplot(111, projection='3d')\nax.scatter(x, y, z,\n           linewidths=1, alpha=.7, label = \"Good Punt\",\n           edgecolor='k',\n           s = 200,\n           )\n\nax.scatter(x1, y1, z1, label = \"Bad Punt\",\n           linewidths=1, alpha=.7,\n           edgecolor='k',\n           s = 200,\n           )\nax.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:15.986517Z","iopub.execute_input":"2022-01-07T12:16:15.986833Z","iopub.status.idle":"2022-01-07T12:16:16.321266Z","shell.execute_reply.started":"2022-01-07T12:16:15.986804Z","shell.execute_reply":"2022-01-07T12:16:16.320341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Field Goal","metadata":{}},{"cell_type":"code","source":"def process_field_goals(filter):\n    df = playsDF.loc[(playsDF['specialTeamsPlayType'] == 'Field Goal') & (playsDF['specialTeamsResult'].isin(filter))]\n    df = df[['playId', 'specialTeamsResult', 'playResult']]\n    df['operationTime'] = 0\n    df['hangTime'] = 0\n    df['kickDirectionActual'] = 0\n    df['kickDirectionIntended'] = 0\n    df['ActualVsIntended'] = 0\n    df['kickType'] = 0\n    \n    for index,  x in df.iterrows():\n        df.loc[[index], ['operationTime']] = PFFScoutingDF['operationTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['hangTime']] = PFFScoutingDF['hangTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionActual']] = PFFScoutingDF['kickDirectionActual'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionIntended']] = PFFScoutingDF['kickDirectionIntended'].loc[x['playId'] == PFFScoutingDF['playId']]\n        actual = df.loc[index, 'kickDirectionActual']\n        intended = df.loc[index, 'kickDirectionIntended']\n        df.loc[index, 'ActualVsIntended'] = (actual == intended)\n        df.loc[[index], ['kickType']] = PFFScoutingDF['kickType'].loc[x['playId'] == PFFScoutingDF['playId']]\n        \n    return df","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:16.322807Z","iopub.execute_input":"2022-01-07T12:16:16.323462Z","iopub.status.idle":"2022-01-07T12:16:16.333316Z","shell.execute_reply.started":"2022-01-07T12:16:16.323413Z","shell.execute_reply":"2022-01-07T12:16:16.332675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"good_field_goal = ['Kick Attempt Good']\nbad_field_goal = ['Kick Attempt No Good']\n\ngood_field_goal_df = process_field_goals(good_field_goal)\ngood_field_goal_df.shape\n\nbad_field_goal_df = process_field_goals(bad_field_goal)\nbad_field_goal_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:16.334434Z","iopub.execute_input":"2022-01-07T12:16:16.334647Z","iopub.status.idle":"2022-01-07T12:16:38.561889Z","shell.execute_reply.started":"2022-01-07T12:16:16.334621Z","shell.execute_reply":"2022-01-07T12:16:38.56097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_punt_distance(filter):\n    df = playsDF.loc[(playsDF['specialTeamsPlayType'] == 'Punt') & (playsDF['specialTeamsResult'].isin(filter))]\n    df = df[['playId', 'specialTeamsResult', 'playResult']]\n    df['operationTime'] = 0\n    df['hangTime'] = 0\n    df['kickDirectionActual'] = 0\n    df['kickDirectionIntended'] = 0\n    df['ActualVsIntended'] = 0\n    df['kickType'] = 0\n    \n    for index,  x in df.iterrows():\n        df.loc[[index], ['operationTime']] = PFFScoutingDF['operationTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['hangTime']] = PFFScoutingDF['hangTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionActual']] = PFFScoutingDF['kickDirectionActual'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionIntended']] = PFFScoutingDF['kickDirectionIntended'].loc[x['playId'] == PFFScoutingDF['playId']]\n        actual = df.loc[index, 'kickDirectionActual']\n        intended = df.loc[index, 'kickDirectionIntended']\n        df.loc[index, 'ActualVsIntended'] = (actual == intended)\n        df.loc[[index], ['kickType']] = PFFScoutingDF['kickType'].loc[x['playId'] == PFFScoutingDF['playId']]\n    \n    small_punt = df.loc[(df['playResult'] <= 10)]\n    medium_punt = df.loc[(df['playResult'] > 10) & (df['playResult'] < 20)]\n    long_punt = df.loc[(df['playResult'] > 20)]\n    \n    return small_punt, medium_punt, long_punt","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:38.563163Z","iopub.execute_input":"2022-01-07T12:16:38.563385Z","iopub.status.idle":"2022-01-07T12:16:38.57388Z","shell.execute_reply.started":"2022-01-07T12:16:38.563358Z","shell.execute_reply":"2022-01-07T12:16:38.573005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"small_df, medium_df, long_df = process_punt_distance(good_filter) ","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:38.575021Z","iopub.execute_input":"2022-01-07T12:16:38.575241Z","iopub.status.idle":"2022-01-07T12:16:55.990924Z","shell.execute_reply.started":"2022-01-07T12:16:38.575214Z","shell.execute_reply":"2022-01-07T12:16:55.990005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_play_result(df, x_axis, y_axis, title):\n    plt.figure(figsize=(10,10))\n    ax = df.loc[(df['playResult'] <= 30)].plot(kind='scatter', x=x_axis, y=y_axis,\n                                               color='Red', label='Small')\n    ax1 = df.loc[(df['playResult'] > 30) & (df['playResult'] <= 60)].plot(kind='scatter',  x=x_axis, y=y_axis,\n                                              color='Green', label='Medium', ax=ax)\n    ax2 = df.loc[(df['playResult'] > 60)].plot(kind='scatter',  x=x_axis, y=y_axis,\n                                              color='BLUE', label='Long', ax=ax1)\n    ax.legend()\n    plt.title(title)","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:55.992122Z","iopub.execute_input":"2022-01-07T12:16:55.992363Z","iopub.status.idle":"2022-01-07T12:16:55.999061Z","shell.execute_reply.started":"2022-01-07T12:16:55.992332Z","shell.execute_reply":"2022-01-07T12:16:55.998095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_play_result(pd.concat([good_punt_df, bad_punt_df], ignore_index=True), 'hangTime', 'operationTime', 'All')\nplot_play_result(bad_punt_df, 'hangTime', 'operationTime', 'Bad Punt')\nplot_play_result(good_punt_df, 'hangTime', 'operationTime', 'Good Punt')","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:56.000196Z","iopub.execute_input":"2022-01-07T12:16:56.000637Z","iopub.status.idle":"2022-01-07T12:16:57.022037Z","shell.execute_reply.started":"2022-01-07T12:16:56.000593Z","shell.execute_reply":"2022-01-07T12:16:57.021058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Kickoff","metadata":{}},{"cell_type":"code","source":"def process_kickoff(filter):\n    df = playsDF.loc[(playsDF['specialTeamsPlayType'] == 'Kickoff') & (playsDF['specialTeamsResult'].isin(filter))]\n    df = df[['playId', 'specialTeamsResult', 'playResult']]\n    df['operationTime'] = 0\n    df['hangTime'] = 0\n    df['kickDirectionActual'] = 0\n    df['kickDirectionIntended'] = 0\n    df['ActualVsIntended'] = 0\n    df['kickType'] = 0\n    \n    for index,  x in df.iterrows():\n        df.loc[[index], ['operationTime']] = PFFScoutingDF['operationTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['hangTime']] = PFFScoutingDF['hangTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionActual']] = PFFScoutingDF['kickDirectionActual'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionIntended']] = PFFScoutingDF['kickDirectionIntended'].loc[x['playId'] == PFFScoutingDF['playId']]\n        actual = df.loc[index, 'kickDirectionActual']\n        intended = df.loc[index, 'kickDirectionIntended']\n        df.loc[index, 'ActualVsIntended'] = (actual == intended)\n        df.loc[[index], ['kickType']] = PFFScoutingDF['kickType'].loc[x['playId'] == PFFScoutingDF['playId']]\n        \n    return df","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:57.023258Z","iopub.execute_input":"2022-01-07T12:16:57.023483Z","iopub.status.idle":"2022-01-07T12:16:57.033523Z","shell.execute_reply.started":"2022-01-07T12:16:57.023454Z","shell.execute_reply":"2022-01-07T12:16:57.032517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"good_kickoff_filter = ['Touchback']\nbad_kickoff_filter = ['Return']\n\ngood_kickoff_df = process_kickoff(good_kickoff_filter)\nbad_kickoff_df = process_kickoff(bad_kickoff_filter)","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:16:57.035028Z","iopub.execute_input":"2022-01-07T12:16:57.035279Z","iopub.status.idle":"2022-01-07T12:18:04.612097Z","shell.execute_reply.started":"2022-01-07T12:16:57.035223Z","shell.execute_reply":"2022-01-07T12:18:04.611354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_good_and_bad(good_df, bad_df):\n    plt.figure(figsize=(10,10))\n    ax = bad_df.plot(kind='scatter', x='hangTime', y='playResult',\n                                               color='Red', label='bad')\n    good_df.plot(kind='scatter', x='hangTime', y='playResult',\n                                              color='Green', label='good', ax=ax)\n\n    plt.title(\"Good punt vs bad punt - Hangtime and distance\")\n\n    #plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:18:04.613094Z","iopub.execute_input":"2022-01-07T12:18:04.613755Z","iopub.status.idle":"2022-01-07T12:18:04.619195Z","shell.execute_reply.started":"2022-01-07T12:18:04.613713Z","shell.execute_reply":"2022-01-07T12:18:04.618309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_good_and_bad(good_kickoff_df, bad_kickoff_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:18:04.620554Z","iopub.execute_input":"2022-01-07T12:18:04.620877Z","iopub.status.idle":"2022-01-07T12:18:05.017087Z","shell.execute_reply.started":"2022-01-07T12:18:04.620834Z","shell.execute_reply":"2022-01-07T12:18:05.01614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"teams = playsDF['possessionTeam'].unique()\nprint(teams)\n\nNFLTeamsData = {}\n\nfor currentTeam in teams:\n    NFLTeamsData[currentTeam] = good_punt_df.loc[(good_punt_df['possessionTeam'] == currentTeam)]\n    NFLTeamsData[currentTeam]['kickerId'].astype({\"kickerId\" : 'int'})\n    NFLTeamsData[currentTeam]['kickerId'].astype({\"kickerId\" : 'str'})\n        \ndef get_team_data(team_name):\n    return NFLTeamsData[team_name]","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:18:05.018233Z","iopub.execute_input":"2022-01-07T12:18:05.018442Z","iopub.status.idle":"2022-01-07T12:18:05.060807Z","shell.execute_reply.started":"2022-01-07T12:18:05.018416Z","shell.execute_reply":"2022-01-07T12:18:05.059864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Player Interaction in Punt","metadata":{}},{"cell_type":"code","source":"def process_team_punts( filter, team):\n    df = playsDF.loc[(playsDF['specialTeamsPlayType'] == 'Punt') & (playsDF['specialTeamsResult'].isin(filter)) &\n                    (playsDF['possessionTeam'] == team)]\n    df = df[['playId', 'gameId','specialTeamsResult', 'playResult', 'possessionTeam', 'kickerId']]\n    df['operationTime'] = 0\n    df['hangTime'] = 0\n    df['kickDirectionActual'] = 0\n    df['kickDirectionIntended'] = 0\n    df['ActualVsIntended'] = 0\n    df['kickType'] = 0\n    \n    for index,  x in df.iterrows():\n        df.loc[[index], ['operationTime']] = PFFScoutingDF['operationTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['hangTime']] = PFFScoutingDF['hangTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionActual']] = PFFScoutingDF['kickDirectionActual'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionIntended']] = PFFScoutingDF['kickDirectionIntended'].loc[x['playId'] == PFFScoutingDF['playId']]\n        actual = df.loc[index, 'kickDirectionActual']\n        intended = df.loc[index, 'kickDirectionIntended']\n        df.loc[index, 'ActualVsIntended'] = (actual == intended)\n        df.loc[[index], ['kickType']] = PFFScoutingDF['kickType'].loc[x['playId'] == PFFScoutingDF['playId']]\n        \n    return df","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:18:05.062328Z","iopub.execute_input":"2022-01-07T12:18:05.062788Z","iopub.status.idle":"2022-01-07T12:18:05.07245Z","shell.execute_reply.started":"2022-01-07T12:18:05.06275Z","shell.execute_reply":"2022-01-07T12:18:05.071543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get_team_data('NE')\nteam = 'GB'\ngood_filter = ['Fair Catch', 'Touchback']\ngood_punt_df = process_team_punts( good_filter, team )\n","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:18:05.07374Z","iopub.execute_input":"2022-01-07T12:18:05.074117Z","iopub.status.idle":"2022-01-07T12:18:05.731444Z","shell.execute_reply.started":"2022-01-07T12:18:05.074076Z","shell.execute_reply":"2022-01-07T12:18:05.730575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playId_gameId_data = good_punt_df.groupby(['playId', 'gameId', 'kickerId']).size().reset_index().rename(columns={0:'count'})","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:18:05.734891Z","iopub.execute_input":"2022-01-07T12:18:05.735164Z","iopub.status.idle":"2022-01-07T12:18:05.746864Z","shell.execute_reply.started":"2022-01-07T12:18:05.735128Z","shell.execute_reply":"2022-01-07T12:18:05.746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playId_gameId_data","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:18:05.748328Z","iopub.execute_input":"2022-01-07T12:18:05.748747Z","iopub.status.idle":"2022-01-07T12:18:05.764914Z","shell.execute_reply.started":"2022-01-07T12:18:05.748701Z","shell.execute_reply":"2022-01-07T12:18:05.7643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mergedTrackingDF['position'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:18:05.76582Z","iopub.execute_input":"2022-01-07T12:18:05.766058Z","iopub.status.idle":"2022-01-07T12:18:07.860256Z","shell.execute_reply.started":"2022-01-07T12:18:05.766029Z","shell.execute_reply":"2022-01-07T12:18:07.859425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playId_gameId_data","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:18:07.861556Z","iopub.execute_input":"2022-01-07T12:18:07.861772Z","iopub.status.idle":"2022-01-07T12:18:07.87543Z","shell.execute_reply.started":"2022-01-07T12:18:07.861745Z","shell.execute_reply":"2022-01-07T12:18:07.874438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"player_connections = pd.DataFrame(columns = [\"player1\", \"player2\"])\npunt_positions = ['G', 'LS', 'K', 'P', 'OLB', 'TE', 'RB', 'WR', 'T', 'OT', 'OG', 'HB']\n\nfor i, current_pair in playId_gameId_data.iterrows():\n    co_players = mergedTrackingDF[(mergedTrackingDF['gameId'] == current_pair.gameId)\n                        &(mergedTrackingDF['playId'] == current_pair.playId)\n                       &(mergedTrackingDF['position'].isin(punt_positions))\n                        &(mergedTrackingDF['frameId'] == 1)\n                                 ]\n    #print(co_players.shape)\n    for j, x_co_player in co_players.iterrows():        \n        if( len (playersDF.loc[ playersDF['nflId'] == x_co_player.nflId].displayName) != 0):\n            player_name = playersDF.loc[ playersDF['nflId'] == x_co_player.nflId].displayName.values[0]\n            new_connection = {'player1': current_pair.playId, 'player2': player_name}\n            #print(new_connection)\n            player_connections = player_connections.append( new_connection, ignore_index = True)","metadata":{"execution":{"iopub.status.busy":"2022-01-07T12:18:07.876899Z","iopub.execute_input":"2022-01-07T12:18:07.877653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def showTeamConnections(player_connections_x):\n    G = nx.Graph()\n    \n    player_connections_x['weight'] = player_connections_x.groupby(['player1','player2'])['player2'].transform('size')\n    #player_connections_x['weight'] = np.log10(player_connections_x['weight'])\n    number_of_plays = len(player_connections_x['weight'])\n    player_connections_x['weight'] = (player_connections_x['weight'] / number_of_plays)*1000\n    \n    G = nx.from_pandas_edgelist(player_connections_x,source='player1',target='player2', edge_attr='weight')\n    \n    plt.figure(figsize=(20,20))\n    pos = nx.kamada_kawai_layout(G)\n\n    player1 = player_connections_x['player1'].unique()\n    player2 = player_connections_x['player2'].unique()\n               \n    G.add_nodes_from(player1, layer = 0)\n    G.add_nodes_from(player2, layer = 1)\n\n    nodes = G.nodes()\n\n    nodes_0 = set([n for n in nodes if G.nodes[n]['layer']==0])\n    nodes_1 = set([n for n in nodes if G.nodes[n]['layer']==1])\n\n    for n in G.nodes():\n        G.nodes[n]['color'] = 'orange' if n in nodes_0 else 'green'\n        #G.nodes[n]['label'] = playersDF.loc[playersDF['nflId'] == n, 'displayName'].iloc[0]\n        G.nodes[n]['label'] = n\n    colors = [node[1]['color'] for node in G.nodes(data=True)]\n    \n    node_colors = list(nx.get_node_attributes(G,'color').values())\n\n    deg_centrality = nx.degree_centrality(G)\n    centrality = np.fromiter(deg_centrality.values(), float)\n    \n    edge_width = list(nx.get_edge_attributes(G,'weight').values())\n\n    nx.draw_networkx(G, pos , node_size=centrality*10e3, with_labels = True, \n            labels = nx.get_node_attributes(G, 'label'), node_color = node_colors, width = edge_width,)\n    \n    plt.show()\n    return G\n    \nnewG = showTeamConnections(player_connections)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Player Interaction in Kickoff","metadata":{}},{"cell_type":"code","source":"def process_team_kickoffs( filter, team):\n    df = playsDF.loc[(playsDF['specialTeamsPlayType'] == 'Kickoff') & (playsDF['specialTeamsResult'].isin(filter)) &\n                    (playsDF['possessionTeam'] == team)]\n    df = df[['playId', 'gameId','specialTeamsResult', 'playResult', 'possessionTeam', 'kickerId']]\n    df['operationTime'] = 0\n    df['hangTime'] = 0\n    df['kickDirectionActual'] = 0\n    df['kickDirectionIntended'] = 0\n    df['ActualVsIntended'] = 0\n    df['kickType'] = 0\n    \n    for index,  x in df.iterrows():\n        df.loc[[index], ['operationTime']] = PFFScoutingDF['operationTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['hangTime']] = PFFScoutingDF['hangTime'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionActual']] = PFFScoutingDF['kickDirectionActual'].loc[x['playId'] == PFFScoutingDF['playId']]\n        df.loc[[index], ['kickDirectionIntended']] = PFFScoutingDF['kickDirectionIntended'].loc[x['playId'] == PFFScoutingDF['playId']]\n        actual = df.loc[index, 'kickDirectionActual']\n        intended = df.loc[index, 'kickDirectionIntended']\n        df.loc[index, 'ActualVsIntended'] = (actual == intended)\n        df.loc[[index], ['kickType']] = PFFScoutingDF['kickType'].loc[x['playId'] == PFFScoutingDF['playId']]\n        \n    return df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"team = 'GB'\ngood_filter = ['Muffed', 'Touchback']\ngood_punt_df = process_team_punts( good_filter, team )\nkickoff_positions = ['G', 'LS', 'K', 'P', 'OLB', 'TE', 'RB', 'WR', 'T', 'OT', 'OG', 'HB']\n\nplayId_gameId_data = good_punt_df.groupby(['playId', 'gameId', 'kickerId']).size().reset_index().rename(columns={0:'count'})\n\nplayer_connections = pd.DataFrame(columns = [\"player1\", \"player2\"])\n\nfor i, current_pair in playId_gameId_data.iterrows():\n    co_players = mergedTrackingDF[(mergedTrackingDF['gameId'] == current_pair.gameId)\n                        &(mergedTrackingDF['playId'] == current_pair.playId)\n                        &(mergedTrackingDF['position'].isin(kickoff_positions))\n                        &(mergedTrackingDF['frameId'] == 1)\n                                 ]\n    #print(co_players.shape)\n    for j, x_co_player in co_players.iterrows():\n        #new_connection = {'player1': current_pair.kickerId, 'player2': x_co_player.nflId}\n        player_name = playersDF.loc[ playersDF['nflId'] == x_co_player.nflId].displayName.values[0]\n        #new_connection = {'player1': current_pair.playId, 'player2': x_co_player.nflId}\n        new_connection = {'player1': current_pair.playId, 'player2': player_name}\n        #print(new_connection)\n        player_connections = player_connections.append( new_connection, ignore_index = True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def showTeamKickoffConnections(player_connections_x):\n    G = nx.Graph()\n    \n    player_connections_x['weight'] = player_connections_x.groupby(['player1','player2'])['player1'].transform('size')\n    #player_connections_x['weight'] = np.log10(player_connections_x['weight'])\n    number_of_plays = len(player_connections_x['weight'])\n    player_connections_x['weight'] = (player_connections_x['weight'] / number_of_plays)*100\n    \n    G = nx.from_pandas_edgelist(player_connections_x,source='player1',target='player2', edge_attr='weight')\n    \n    plt.figure(figsize=(20,20))\n    pos = nx.kamada_kawai_layout(G)\n\n    player1 = player_connections_x['player1'].unique()\n    player2 = player_connections_x['player2'].unique()\n               \n    G.add_nodes_from(player1, layer = 0)\n    G.add_nodes_from(player2, layer = 1)\n\n    nodes = G.nodes()\n\n    nodes_0 = set([n for n in nodes if G.nodes[n]['layer']==0])\n    nodes_1 = set([n for n in nodes if G.nodes[n]['layer']==1])\n\n    for n in G.nodes():\n        G.nodes[n]['color'] = 'orange' if n in nodes_0 else 'green'\n        #G.nodes[n]['label'] = playersDF.loc[playersDF['nflId'] == n, 'displayName'].iloc[0]\n        G.nodes[n]['label'] = n\n    colors = [node[1]['color'] for node in G.nodes(data=True)]\n    \n    node_colors = list(nx.get_node_attributes(G,'color').values())\n\n    deg_centrality = nx.degree_centrality(G)\n    centrality = np.fromiter(deg_centrality.values(), float)\n    \n    edge_width = list(nx.get_edge_attributes(G,'weight').values())\n\n    nx.draw_networkx(G, pos , node_size=centrality*10e3, with_labels = True, \n            labels = nx.get_node_attributes(G, 'label'), node_color = node_colors, width = edge_width,)\n    \n    plt.show()\n    return G\n    \nnewG = showTeamKickoffConnections(player_connections)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def edge_to_remove(graph):\n    G_dict = nx.edge_betweenness_centrality(graph)\n    edge = ()\n\n  # extract the edge with highest edge betweenness centrality score\n    for key, value in sorted(G_dict.items(), key=lambda item: item[1], reverse = True):\n        edge = key\n        break\n\n    return edge","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def girvan_newman(graph):\n    # find number of connected components\n    sg = nx.connected_components(graph)\n    sg_count = nx.number_connected_components(graph)\n\n    while(sg_count == 1):\n        graph.remove_edge(edge_to_remove(graph)[0], edge_to_remove(graph)[1])\n        sg = nx.connected_components(graph)\n        sg_count = nx.number_connected_components(graph)\n\n    return sg","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pip install cdlib","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c = girvan_newman(newG)\n#c = algorithms.louvain(newG, weight='weight', resolution=1., randomize=False)\n\n# find the nodes forming the communities\nnode_groups = []\n\nfor i in c:\n    node_groups.append(list(i))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#node_groups","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import community as community_louvain\nimport matplotlib.cm as cm\n\npartition = community_louvain.best_partition(newG)\n\npos = nx.spring_layout(newG)\n# color the nodes according to their partition\ncmap = cm.get_cmap('viridis', max(partition.values()) + 1)\nplt.figure(figsize=(20,20))\n#nx.draw_networkx_nodes(newG, pos, partition.keys(), node_size=40, with_labels=True,\n#                        cmap=cmap, node_color=list(partition.values()))\n\nnx.draw(newG, pos, node_size=1000, with_labels=True,\n                        cmap=cmap, node_color=list(partition.values()))\n\n#nx.draw_networkx_edges(newG, pos, alpha=0.5)\n#nx.draw(newG, with_labels=True)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#node_groups\n#player_connections_x.groupby(['player1','player2'])['player1'].transform('size')\ncomms = pd.DataFrame(partition.items(), columns = ['node', 'cluster'])\ncomms1 = comms.groupby(['cluster'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#comms.loc[comms['cluster'] == 1]\ncomms['cluster'].unique()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"color_map = []\nfor node in newG:\n    if node in node_groups[0]:\n        color_map.append('blue')\n    else: \n        color_map.append('green')  \nplt.figure(figsize=(20,20))\nnx.draw(newG, node_color=color_map, with_labels=True)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}