{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"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/ubiquant-leaderboards/ubiquant-market-prediction-publicleaderboard.csv\"))\n#lbs.append(pd.read_csv(\"../input/ubiquant-leaderboards/ubiquant-market-prediction-publicleaderboard_update1.csv\"))\nlbs.append(pd.read_csv(\"../input/ubiquant-leaderboards/ubiquant-market-prediction-publicleaderboard_update2.csv\"))\nlbs.append(pd.read_csv(\"../input/ubiquant-leaderboards/ubiquant-market-prediction-publicleaderboard_update3.csv\"))\nlbs.append(pd.read_csv(\"../input/ubiquant-leaderboards/ubiquant-market-prediction-publicleaderboard_update4.csv\"))\nlbs.append(pd.read_csv(\"../input/ubiquant-leaderboards/ubiquant-market-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-07-19T22:27:49.084926Z","iopub.execute_input":"2022-07-19T22:27:49.085501Z","iopub.status.idle":"2022-07-19T22:27:49.143370Z","shell.execute_reply.started":"2022-07-19T22:27:49.085464Z","shell.execute_reply":"2022-07-19T22:27:49.142630Z"},"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-07-19T22:27:49.147601Z","iopub.execute_input":"2022-07-19T22:27:49.148243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lb_lens = []\n\nfor i, lb in enumerate(lbs):\n    print(\"Entries in the LB after update {}:\".format(i + 1), len(lb))\n    lb_lens.append(len(lb))\n\nplt.title(\"Failed submissions for each update\")\nplt.bar([\"Update {}\".format(i) for i in range(len(lbs))], len(changeTable) - np.array(lb_lens)) # Plot successful submissions with each update\nplt.show()\nlbs[-1].head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Total entries:\", len(changeTable))\nremove = np.isnan(np.sum(changeTable[:, 2:].astype(np.float32), axis=1))\nteamNames = teamNames[~remove]\nchangeTable = changeTable[~remove]\nprint(\"After removing failed submissions:\", len(changeTable))\nremove = np.min(changeTable[:, 1 + len(lbs):], axis = 1) == -1\nteamNames = teamNames[~remove]\nchangeTable = changeTable[~remove]\nprint(\"After removing -1 submission:\", len(changeTable))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, k in [(0, 1), (1, 2), (2, 3), (3, 4), (0, 4)]:\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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, k in [(0, 1), (1, 2), (2, 3), (3, 4), (0, 4)]:\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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, k in [(0, 1), (1, 2), (2, 3), (3, 4), (0, 4)]:\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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  Plot performance of top 10 teams over time\nplt.gcf().set_dpi(200)\nfor i in range(10):\n    \n    plt.plot(np.array([0, 2, 3, 4, 5]), changeTable[changeTable[:, 1] == i + 1, 1:6][0].astype(np.int32))\n    plt.yscale('log')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}