{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70367,"databundleVersionId":9188054,"sourceType":"competition"},{"sourceId":9779229,"sourceType":"datasetVersion","datasetId":5990879}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Load data files","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/ariel-leaderboards/ariel-data-challenge-2024-publicleaderboard.csv\"))\nlbs.append(pd.read_csv(\"/kaggle/input/ariel-leaderboards/ariel-data-challenge-2024-privateleaderboard.csv\"))\n\ndef print_header(text, size=1):\n    display_html(HTML(\"<h{}>{}</h{}>\".format(size, text, size)))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-11-01T10:01:41.573422Z","iopub.execute_input":"2024-11-01T10:01:41.574125Z","iopub.status.idle":"2024-11-01T10:01:41.596289Z","shell.execute_reply.started":"2024-11-01T10:01:41.574078Z","shell.execute_reply":"2024-11-01T10:01:41.594965Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Build table where each row contains old and new positions and scores","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":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T10:01:41.599083Z","iopub.execute_input":"2024-11-01T10:01:41.599626Z","iopub.status.idle":"2024-11-01T10:01:42.745248Z","shell.execute_reply.started":"2024-11-01T10:01:41.599562Z","shell.execute_reply":"2024-11-01T10:01:42.743977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i, k in [(0, 1)]:\n\n    print_header(\"Change from public LB to private LB:\")\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":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T10:01:42.74677Z","iopub.execute_input":"2024-11-01T10:01:42.747171Z","iopub.status.idle":"2024-11-01T10:01:43.70772Z","shell.execute_reply.started":"2024-11-01T10:01:42.747131Z","shell.execute_reply":"2024-11-01T10:01:43.706485Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Plot the shakeup","metadata":{}},{"cell_type":"code","source":"from plotly.offline import init_notebook_mode, iplot, plot\nimport plotly.graph_objs as go","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T10:01:43.709285Z","iopub.execute_input":"2024-11-01T10:01:43.709676Z","iopub.status.idle":"2024-11-01T10:01:43.715134Z","shell.execute_reply.started":"2024-11-01T10:01:43.709635Z","shell.execute_reply":"2024-11-01T10:01:43.713772Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i, k in [(0, 1)]:\n    \n    print_header(\"Change from public LB to private LB:\")\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":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T10:01:43.718591Z","iopub.execute_input":"2024-11-01T10:01:43.719704Z","iopub.status.idle":"2024-11-01T10:01:43.773168Z","shell.execute_reply.started":"2024-11-01T10:01:43.719657Z","shell.execute_reply":"2024-11-01T10:01:43.771723Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i, k in [(0, 1)]:\n    \n    print_header(\"Change from public LB to private LB:\")\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":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-01T10:01:43.774777Z","iopub.execute_input":"2024-11-01T10:01:43.77528Z","iopub.status.idle":"2024-11-01T10:01:43.823965Z","shell.execute_reply.started":"2024-11-01T10:01:43.775225Z","shell.execute_reply":"2024-11-01T10:01:43.822694Z"}},"outputs":[],"execution_count":null}]}