Skip to content

Instantly share code, notes, and snippets.

@bogdandm
Last active December 4, 2018 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogdandm/2360e89835592f0cad1a3970820e6b9a to your computer and use it in GitHub Desktop.
Save bogdandm/2360e89835592f0cad1a3970820e6b9a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Requirements\n",
"- matplotlib\n",
"- numpy\n",
"- pandas\n",
"- seaborn\n",
"- scipy\n",
"- requests\n",
"- (optional) tqdm"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pathlib\n",
"import sys\n",
"PATH = pathlib.Path().absolute().resolve()\n",
"\n",
"%matplotlib inline\n",
"import math\n",
"import json\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.ticker as tk\n",
"import matplotlib.colors as clr\n",
"import seaborn as sns\n",
" \n",
"sns.set(style=\"whitegrid\", font_scale=1.4)\n",
"COLORS = sns.color_palette()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from scipy.signal import savgol_filter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import requests\n",
"try:\n",
" from tqdm import tqdm\n",
"except ImportError:\n",
" # tqdm placeholder\n",
" class tqdm:\n",
" def __init__(self, it=None): \n",
" self.it=it\n",
" def __enter__(self): \n",
" return self\n",
" def __exit__(self, *args, **kwargs): \n",
" pass\n",
" def __iter__(self): \n",
" return iter(self.it)\n",
" def update(*args): \n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"leaderbord_url = lambda offset, limit=100: f\"https://api.2018.halite.io/v1/api/leaderboard?offset={offset}&limit={limit}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def ax_style(ax):\n",
" ax.grid(which='minor', axis='x', dashes=(5, 5), linewidth=.5)\n",
" ax.grid(which='minor', axis='y', dashes=(5, 5), linewidth=.5)\n",
" ax.legend()\n",
" ax.minorticks_on()\n",
" return ax"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"leaderboard_data = []\n",
"chunk = True\n",
"step = 100\n",
"i = 0\n",
"with tqdm() as progress:\n",
" while chunk:\n",
" url = leaderbord_url(step * i, step)\n",
" i += 1\n",
" chunk = requests.get(url).json()\n",
" leaderboard_data.extend(chunk)\n",
" progress.update(step)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"MU_CUT = 30\n",
"SIGMA_CUT = 2\n",
"df = pd.DataFrame(leaderboard_data)\n",
"df = df[df.mu >= MU_CUT]\n",
"df_filtered = df[df.sigma <= SIGMA_CUT]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"fig = plt.figure(figsize=(20, 10))\n",
"ax = fig.add_subplot(111)\n",
"df_filtered.plot(x=\"rank\", y=\"mu\", ax=ax, marker=',', linestyle=' ', label=r\"$\\mu$ raw\")\n",
"Y_smooth = savgol_filter(df.mu, 101, 3)\n",
"ax.plot(df[\"rank\"], Y_smooth, label=r\"$\\mu$\")\n",
"\n",
"ax.set_xlabel(\"Ladder position\")\n",
"ax.set_ylabel(r\"$\\mu$\")\n",
"ax.set_xlim(len(df), 0)\n",
"ax_style(ax)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = plt.figure(figsize=(20, 10))\n",
"ax1 = fig.add_subplot(121)\n",
"ax2 = fig.add_subplot(122)\n",
"for i, (df_, ax) in enumerate(((df, ax1), (df[df[\"rank\"] <= 100], ax2))):\n",
" values = df_.language.value_counts()[::-1]\n",
" values.plot(ax=ax, kind='barh', logx=not i)\n",
" ax_style(ax)\n",
" ax.set_xlim(not i, values.max() * 1.1)\n",
"ax1.set_title(\"All players\")\n",
"ax2.set_title(\"Top100 players\")\n",
"fig.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"fig = plt.figure(figsize=(20, 30))\n",
"ax1 = plt.subplot2grid((3, 1), (0, 0))\n",
"ax2 = plt.subplot2grid((3, 1), (1, 0))\n",
"rank_cut = 200\n",
"top_100 = {}\n",
"for lang in df.language.unique():\n",
" df_ = df[df.language == lang]\n",
" if len(df_) >= 30:\n",
" sns.distplot(df_.score, ax=ax1, hist=False, label=lang)\n",
" sns.distplot(df_[\"rank\"], ax=ax2, hist=False, bins=20, label=lang)\n",
" \n",
" values = df_[df_[\"rank\"] < rank_cut][\"rank\"]\n",
" if len(values) >= 3:\n",
" top_100[lang] = values\n",
" \n",
"sns.distplot(df[df.is_gpu_enabled == True][\"rank\"], ax=ax2, kde_kws={\"ls\": \"--\"}, hist=False, label=\"with GPU\")\n",
" \n",
"top_100 = sorted(top_100.items(), reverse=True,\n",
" key=lambda k_v: df[(df.language == k_v[0]) & (df[\"rank\"] <= rank_cut)].score.mean())\n",
"top_100_k, top_100_v = zip(*top_100)\n",
"\n",
"ax_style(ax1)\n",
"ax1.set_xlim(df.score.max(), MU_CUT)\n",
"ax1.set_xlabel(r\"Score = $\\mu - 3\\sigma$\")\n",
"ax1.set_ylabel(\"Distribution\")\n",
"ax1.set_title(\"Distrubution of score by prorg. lang.\")\n",
"\n",
"ax_style(ax2)\n",
"ax2.set_xlim(1, df[\"rank\"].max())\n",
"ax2.set_xlabel(r\"Rank\")\n",
"ax2.set_ylabel(\"Distribution\")\n",
"ax2.set_title(\"Distrubution of rank by prorg. lang.\")\n",
"\n",
"with sns.axes_style(style=\"darkgrid\"):\n",
" ax3 = plt.subplot2grid((3, 1), (2, 0))\n",
" ax3.hist(\n",
" top_100_v, \n",
" bins=np.arange(0, rank_cut + rank_cut // 10, 10), \n",
" stacked=True, label=top_100_k,\n",
" color=sns.color_palette(\"Spectral\", len(top_100_k)),\n",
" zorder=-1\n",
" )\n",
" \n",
" ax_style(ax3)\n",
" ax3.set_xlim(1, rank_cut + 40)\n",
" ax3.set_xlabel(f\"Rank (top{rank_cut})\")\n",
" ax3.set_ylabel(\"Distribution\")\n",
" ax3.set_title(f\"Distrubution of rank (top{rank_cut}) by prorg. lang.\")\n",
" \n",
"\n",
"fig.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
matplotlib
numpy
pandas
seaborn
scipy
requests
tqdm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment