{"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(\"../input/amex-leaderboards/amex-default-prediction-publicleaderboard.csv\"))\nlbs.append(pd.read_csv(\"../input/amex-leaderboards/amex-default-prediction-privateleaderboard.csv\"))\n\ndef print_header(text, size=1):\n    display_html(HTML(\"<h{}>{}</h{}>\".format(size, text, size)))","metadata":{"execution":{"iopub.status.busy":"2022-08-25T00:09:42.981455Z","iopub.execute_input":"2022-08-25T00:09:42.981857Z","iopub.status.idle":"2022-08-25T00:09:43.013568Z","shell.execute_reply.started":"2022-08-25T00:09:42.981823Z","shell.execute_reply":"2022-08-25T00:09:43.012346Z"},"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":"2022-08-25T00:09:43.016378Z","iopub.execute_input":"2022-08-25T00:09:43.016856Z","iopub.status.idle":"2022-08-25T00:09:48.070108Z","shell.execute_reply.started":"2022-08-25T00:09:43.016813Z","shell.execute_reply":"2022-08-25T00:09:48.069035Z"},"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], 100)\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], 100)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-25T00:09:48.071864Z","iopub.execute_input":"2022-08-25T00:09:48.072185Z","iopub.status.idle":"2022-08-25T00:09:48.879887Z","shell.execute_reply.started":"2022-08-25T00:09:48.072155Z","shell.execute_reply":"2022-08-25T00:09:48.878692Z"},"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":"2022-08-25T00:09:48.881261Z","iopub.execute_input":"2022-08-25T00:09:48.881626Z","iopub.status.idle":"2022-08-25T00:09:48.887366Z","shell.execute_reply.started":"2022-08-25T00:09:48.881593Z","shell.execute_reply":"2022-08-25T00:09:48.885841Z"},"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":"2022-08-25T00:09:48.889919Z","iopub.execute_input":"2022-08-25T00:09:48.890729Z","iopub.status.idle":"2022-08-25T00:09:48.934174Z","shell.execute_reply.started":"2022-08-25T00:09:48.890687Z","shell.execute_reply":"2022-08-25T00:09:48.932917Z"},"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":"2022-08-25T00:09:48.935811Z","iopub.execute_input":"2022-08-25T00:09:48.936253Z","iopub.status.idle":"2022-08-25T00:09:48.983441Z","shell.execute_reply.started":"2022-08-25T00:09:48.936209Z","shell.execute_reply":"2022-08-25T00:09:48.982461Z"},"trusted":true},"execution_count":null,"outputs":[]}]}