{"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":"Load data files\n---","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom IPython.display import display_html, HTML\n\nlbs = []\nlbs.append(pd.read_csv(\"/kaggle/input/asl-leaderboards/asl-fingerspelling-publicleaderboard-2023-08-25T13_03_35.csv\"))\nlbs.append(pd.read_csv(\"/kaggle/input/asl-leaderboards/asl-fingerspelling-privateleaderboard-2023-08-25T13_03_36.csv\"))\n\ndef print_header(text, size=1):\n    display_html(HTML(\"<h{}>{}</h{}>\".format(size, text, size)))","metadata":{"execution":{"iopub.status.busy":"2023-08-25T13:05:16.803113Z","iopub.execute_input":"2023-08-25T13:05:16.803517Z","iopub.status.idle":"2023-08-25T13:05:16.878217Z","shell.execute_reply.started":"2023-08-25T13:05:16.803483Z","shell.execute_reply":"2023-08-25T13:05:16.877104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Build table where each row contains old and new positions and scores\n---","metadata":{}},{"cell_type":"code","source":"# Concatenate lbs and remove duplicates to get TeamIDs and TeamNames\nteams = pd.concat(lbs[::-1]).drop_duplicates(subset=\"TeamId\")[['TeamId', 'TeamName']] # Go through lbs in reverse order to obtain latest team names\n\nteamNames = []\nchangeTable = []\n\nfor index, row in teams.iterrows():\n    \n    teamId = row['TeamId']\n    teamName = row['TeamName']\n\n    pos = []\n    score = []\n    \n    for i in range(len(lbs)):\n        elem = lbs[i].loc[lbs[i]['TeamId'] == teamId]\n        \n        if len(elem) > 0:\n            pos.append(elem.index[0] + 1)\n            score.append(elem['Score'].iloc[0])\n        else:\n            pos.append(np.nan)\n            score.append(np.nan)\n\n    teamNames.append(teamName)\n    changeTable.append([teamId] + pos + score)\n    \nchangeTable = np.array(changeTable)\nteamNames = np.array(teamNames)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T13:05:16.880099Z","iopub.execute_input":"2023-08-25T13:05:16.880432Z","iopub.status.idle":"2023-08-25T13:05:18.241696Z","shell.execute_reply.started":"2023-08-25T13:05:16.880404Z","shell.execute_reply":"2023-08-25T13:05:18.240751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, k in [(0, 1)]:\n\n    print_header(\"Change from update {} to update {}:\".format(i + 1, k + 1))\n    \n    order = np.argsort(changeTable[:, 1 + i])\n    \n    sortedTeamNames = teamNames[order]\n    sortedChangeTable = changeTable[order]\n\n    print_header(\"Median overall rank/score change:\", 4)\n    print(np.median(np.abs(changeTable[:, 1 + i] - changeTable[:, 1 + k])), \n          round(np.median(np.abs(changeTable[:, 1 + len(lbs) + k] - changeTable[:, 1 + len(lbs) + i])), 4))\n\n    for n in [1000, 500, 200, 100, 50, 20, 10]:\n        print_header(\"Median top {} rank/score change:\".format(n), 4)\n        print(np.median(np.abs(sortedChangeTable[:n, 1 + i] - sortedChangeTable[:n, 1 + k])), \n              round(np.median(np.abs(sortedChangeTable[:n, 1 + len(lbs) + k] - sortedChangeTable[:n, 1 + len(lbs) + i])), 4))\n    \n    print_header(\"Maximum overall rank improvement:\", 4)\n    argtop = np.argmax(sortedChangeTable[:, 1 + i] - sortedChangeTable[:, 1 + k])\n\n    print(\"Team:\", sortedTeamNames[argtop])\n    print(\"Rank:\", int(sortedChangeTable[argtop, 1 + i]), \"->\", int(sortedChangeTable[argtop, 1 + k]))\n    print(\"Score:\", sortedChangeTable[argtop, 1 + len(lbs) + i], \"->\", sortedChangeTable[argtop, 1 + len(lbs) + k])  \n    \n    for n in [1000, 500, 200, 100, 50, 20, 10]:\n        print_header(\"Maximum top {} rank improvement:\".format(n), 4)\n        argtop = np.argmax(sortedChangeTable[:n, 1 + i] - sortedChangeTable[:n, 1 + k])\n\n        print(\"Team:\", sortedTeamNames[argtop])\n        print(\"Rank:\", int(sortedChangeTable[argtop, 1 + i]), \"->\", int(sortedChangeTable[argtop, 1 + k]))\n        print(\"Score:\", sortedChangeTable[argtop, 1 + len(lbs) + i], \"->\", sortedChangeTable[argtop, 1 + len(lbs) + k])  \n    \n    print_header(\"Shakeup rank change histogram\", 4)\n    plt.hist(sortedChangeTable[:, 1 + i] - sortedChangeTable[:, 1 + k], 200)\n    plt.show()\n    \n    print_header(\"Shakeup score change histogram\", 4)\n    plt.hist(sortedChangeTable[:, 1 + len(lbs) + i] - sortedChangeTable[:, 1 + len(lbs) + k], 200)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-25T13:05:18.243Z","iopub.execute_input":"2023-08-25T13:05:18.243632Z","iopub.status.idle":"2023-08-25T13:05:19.664297Z","shell.execute_reply.started":"2023-08-25T13:05:18.243599Z","shell.execute_reply":"2023-08-25T13:05:19.662948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot the shakeup\n---","metadata":{}},{"cell_type":"code","source":"from plotly.offline import init_notebook_mode, iplot, plot\nimport plotly.graph_objs as go","metadata":{"execution":{"iopub.status.busy":"2023-08-25T13:05:19.667094Z","iopub.execute_input":"2023-08-25T13:05:19.667822Z","iopub.status.idle":"2023-08-25T13:05:19.708179Z","shell.execute_reply.started":"2023-08-25T13:05:19.667781Z","shell.execute_reply":"2023-08-25T13:05:19.706961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, k in [(0, 1)]:\n    \n    print_header(\"Change from update {} to update {}:\".format(i + 1, k + 1))\n    \n    # Scatter old vs new rank\n    trace = go.Scatter(x = changeTable[:, 1 + i],\n                       y = changeTable[:, 1 + k],\n                       mode = \"markers\",\n                       name = \"Rank\",\n                       marker = dict(color = 'rgba(128, 128, 255, 0.8)'),\n                       text = np.array(teamNames))\n\n    layout = dict(title = 'Rank Shakeup',\n                  xaxis= dict(title= 'Old Rank',ticklen= 5,zeroline= False),\n                  yaxis= dict(title= 'New Rank',ticklen= 5,zeroline= False))\n\n    fig = dict(data = [trace], layout = layout)\n    iplot(fig)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T13:05:19.709722Z","iopub.execute_input":"2023-08-25T13:05:19.710336Z","iopub.status.idle":"2023-08-25T13:05:21.641658Z","shell.execute_reply.started":"2023-08-25T13:05:19.710294Z","shell.execute_reply":"2023-08-25T13:05:21.640549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, k in [(0, 1)]:\n    \n    print_header(\"Change from update {} to update {}:\".format(i + 1, k + 1))\n    \n    # Scatter old vs new rank\n    trace = go.Scatter(x = changeTable[:, 1 + len(lbs) + i],\n                       y = changeTable[:, 1 + len(lbs) + k],\n                       mode = \"markers\",\n                       name = \"Score\",\n                       marker = dict(color = 'rgba(128, 128, 255, 0.8)'),\n                       text= np.array(teamNames))\n\n    layout = dict(title = 'Score Shakeup',\n                  xaxis= dict(title= 'Old Score',ticklen= 5,zeroline= False),\n                  yaxis= dict(title= 'New Score',ticklen= 5,zeroline= False))\n\n    fig = dict(data = [trace], layout = layout)\n    iplot(fig)","metadata":{"execution":{"iopub.status.busy":"2023-08-25T13:05:21.643253Z","iopub.execute_input":"2023-08-25T13:05:21.643963Z","iopub.status.idle":"2023-08-25T13:05:21.689824Z","shell.execute_reply.started":"2023-08-25T13:05:21.643927Z","shell.execute_reply":"2023-08-25T13:05:21.688893Z"},"trusted":true},"execution_count":null,"outputs":[]}]}