{"cells":[{"metadata":{},"cell_type":"markdown","source":"<h1 style='background:#FFFFFF; border:0; color:black'><center>How to Analyze\n    Leaderboard Easily?<center><h1>"},{"metadata":{},"cell_type":"markdown","source":"* In competitions, we frequently check leaderboard and confirm our rank.\n* Information about leaderboard becomes more important in competitions that counts towards tiers.\n\n* In this notebook, the way to get information about public leaderboard will be introduced.\n* This way is very easy, so you can apply it to other competitions.\n\n### If you find useful or interesting, please feel free to upvote!\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":true,"_kg_hide-input":true},"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Library"},{"metadata":{},"cell_type":"markdown","source":"* In this notebook, we get the information by reading a json format file, so we should import \"json\".\n* In addition to that, you should import some data visualization libraries if you want to visualize the leaderboard data."},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport sys\nimport numpy as np\nimport pandas as pd\nimport json\nimport plotly.express as px\nimport plotly.graph_objects as go","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Read JSON Files"},{"metadata":{},"cell_type":"markdown","source":"* You can easily read json files by using json.load() method.\n* Firstly, we read a json file which contains the information on public leaderboard in [Cassava Leaf Disease Classification competition](https://www.kaggle.com/c/cassava-leaf-disease-classification)."},{"metadata":{"trusted":true},"cell_type":"code","source":"!wget 'https://www.kaggle.com/c/cassava-leaf-disease-classification/leaderboard.json?includeBeforeUser=true&includeAfterUser=false' -O cassava_leaderboard.json","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* The Json file that contains the public leaderboard data has the path as described below. \"https://www.kaggle.com/c/competition_name/leaderboard.json?includeBeforeUser=true&includeAfterUser=false\"\n* In the \"competition_name\" part, put the name of a specific competition.\n* (e.g. Cassava Leaf Diasese Classification -> cassava-leaf-disease-classification)\n* (e.g. Tabular Playground Series - Jan 2021 -> tabular-playground-series-jan-2021)"},{"metadata":{},"cell_type":"markdown","source":"### Then, let's load the JSON file!"},{"metadata":{"trusted":true},"cell_type":"code","source":"with open(\"cassava_leaderboard.json\") as f:\n    cassava_jsn = json.load(f)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# What kind of information can we get from the data?"},{"metadata":{},"cell_type":"markdown","source":"* The json file we just read contains various information about public leaderboard.\n* As an example, we would like to look at only the data of 1st team."},{"metadata":{"trusted":true},"cell_type":"code","source":"cassava_jsn['submissions'][0]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* We can see easily that various information is lined up.\n* In addition to the rank and the score of each team, you can also confirm whether the team is in the medal zone and the number of submissions (entries), etc."},{"metadata":{},"cell_type":"markdown","source":"* Since up to 5 members can belong to one team, so we can get information such as member's profile url and tiers."},{"metadata":{},"cell_type":"markdown","source":"# Get only specific information"},{"metadata":{},"cell_type":"markdown","source":"* How to retrieve only specific information (e.g. the score of each team)?\n* It is very easy to do that!"},{"metadata":{"trusted":true},"cell_type":"code","source":"for user in cassava_jsn['beforeUser']+cassava_jsn['afterUser']:\n    if user['medal'] == \"gold\":\n        print(user['score'])\n    else:\n        break","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* In the code above, the scores of teams in the gold medal zone are output.\n* We can easily retrieve specific information by using a for loop."},{"metadata":{},"cell_type":"markdown","source":"# Application Example: Visualize the Leaderboard"},{"metadata":{},"cell_type":"markdown","source":"* Visualizing the information by using libraries makes it easier to understand the situation."},{"metadata":{"trusted":true},"cell_type":"code","source":"teams = []\nscores = []\nfor user in cassava_jsn['beforeUser']+cassava_jsn['afterUser']:\n    if user['medal'] == \"gold\":\n        user['score'] = float(user['score'])\n        scores.append(user['score'])\n        teams.append(user['teamName'])\n    else:\n        break\n\nscore_df = pd.DataFrame({\"Team\":teams,\"Public Score\":scores})\nfig = px.bar(score_df.iloc[::-1], x='Public Score', y='Team',\n              color='Public Score',height=700, title='Gold Medal Zone(Cassava Leaf Disease Classification)',text='Public Score')\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* The graph above shows the names and scores of teams in the gold medal zone.\n* The data contained in \"user['score']\" is treated as a character string, so if you want to compare scores using a graph, you have to use float()."},{"metadata":{},"cell_type":"markdown","source":"### We can easily do the same thing in other competitions!"},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"!wget 'https://www.kaggle.com/c/hubmap-kidney-segmentation/leaderboard.json?includeBeforeUser=true&includeAfterUser=false' -O hubmap_leaderboard.json","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"with open(\"hubmap_leaderboard.json\") as h:\n    hubmap_jsn = json.load(h)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"hubmap_teams = []\nhubmap_scores = []\nfor user in hubmap_jsn['beforeUser']+hubmap_jsn['afterUser']:\n    if user['medal'] == \"gold\":\n        user['score'] = float(user['score'])\n        hubmap_scores.append(user['score'])\n        hubmap_teams.append(user['teamName'])\n    else:\n        break\n\nscore_df = pd.DataFrame({\"Team\":hubmap_teams,\"Public Score\":hubmap_scores})\nfig = px.bar(score_df.iloc[::-1], x='Public Score', y='Team',\n              color='Public Score',height=700, title='Gold Medal Zone (HuBMAP - Hacking the Kidney)',text='Public Score')\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"!wget 'https://www.kaggle.com/c/rfcx-species-audio-detection/leaderboard.json?includeBeforeUser=true&includeAfterUser=false' -O rfcx_leaderboard.json","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"with open(\"rfcx_leaderboard.json\") as c:\n    rfcx_jsn = json.load(c)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"rfcx_teams = []\nrfcx_scores = []\nfor user in rfcx_jsn['beforeUser']+rfcx_jsn['afterUser']:\n    if user['medal'] == \"gold\":\n        user['score'] = float(user['score'])\n        rfcx_scores.append(user['score'])\n        rfcx_teams.append(user['teamName'])\n    else:\n        break\n\nscore_df = pd.DataFrame({\"Team\":rfcx_teams,\"Public Score\":rfcx_scores})\nfig = px.bar(score_df.iloc[::-1], x='Public Score', y='Team',\n              color='Public Score',height=700, title='Gold Medal Zone (Rainforest Connection Species Audio Detection)',text='Public Score')\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"!wget 'https://www.kaggle.com/c/jane-street-market-prediction/leaderboard.json?includeBeforeUser=true&includeAfterUser=false' -O jane_leaderboard.json","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"with open(\"jane_leaderboard.json\") as j:\n    jane_jsn = json.load(j)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"jane_teams = []\njane_scores = []\nfor user in jane_jsn['beforeUser']+jane_jsn['afterUser']:\n    if user['medal'] == \"gold\":\n        user['score'] = float(user['score'])\n        jane_scores.append(user['score'])\n        jane_teams.append(user['teamName'])\n    else:\n        break\n\nscore_df = pd.DataFrame({\"Team\":jane_teams,\"Public Score\":jane_scores})\nfig = px.bar(score_df.iloc[::-1], x='Public Score', y='Team',\n              color='Public Score',height=700, title='Gold Medal Zone (Jane Street Market Prediction)',text='Public Score')\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Can we get information on competitions that have already ended?"},{"metadata":{},"cell_type":"markdown","source":"* So far, we've got information on active competitions, but can we do the same thing on completed competitions?\n* The answer is **Yes**!\n* As an example, we get information on Mechanisms of Action (MoA) Prediction (ended December 1st 2020)."},{"metadata":{"trusted":true},"cell_type":"code","source":"!wget 'https://www.kaggle.com/c/lish-moa/leaderboard.json?includeBeforeUser=true&includeAfterUser=false' -O moa_leaderboard.json","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"with open(\"moa_leaderboard.json\") as m:\n    moa_jsn = json.load(m)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"moa_teams = []\nmoa_scores = []\nfor user in moa_jsn['beforeUser']+moa_jsn['afterUser']:\n    if user['medal'] == \"gold\":\n        user['score'] = float(user['score'])\n        moa_scores.append(user['score'])\n        moa_teams.append(user['teamName'])\n    else:\n        break\n\nscore_df = pd.DataFrame({\"Team\":moa_teams,\"Public Score\":moa_scores})\nfig = px.bar(score_df.iloc[::-1], x='Public Score', y='Team',\n              color='Public Score',height=700, title='Gold Medal Zone (Mechanisms of Action (MoA) Prediction)',text='Public Score')\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"* In this competition, private leaderboard was already announced, but only information on public can be obtained."},{"metadata":{},"cell_type":"markdown","source":"## Thank you for reading to the end!\n## If you like, feel free to upvote!"}],"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":4,"nbformat_minor":4}