{"cells":[{"metadata":{},"cell_type":"markdown","source":"# A look at the public LB standings over time.\n\n캐코 3회 대회 참가팀의 대회 기간 동안의 퍼블릭 리더보드 스코어 트렌드를 보여주는 커널입니다.\n\n대회가 끝나도 캐글은 퍼블릭 리더보드 스코어만 데이터로 제공하기 때문에 프라이빗 리더보드 스코어가 아님을 참고하세요.\n\n[Rob Mulla](https://www.kaggle.com/robikscube) 님이 만든 아래 커널을 바탕으로 만들었습니다.\n\nhttps://www.kaggle.com/robikscube/the-race-to-predict-molecular-properties\n\n**Last updated Aug 17, 2019**"},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"cell_type":"code","source":"!pip install -U plotly==3.10.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":false},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport matplotlib.pylab as plt\nimport plotly\nimport plotly.plotly as py\nimport plotly.graph_objs as go\nfrom plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\nimport cufflinks as cf\nimport datetime\nimport colorlover as cl\ncf.go_offline()\nplt.style.use('ggplot')\ncolor_pal = [x['color'] for x in plt.rcParams['axes.prop_cycle']]\n\n# Format the data\ndf = pd.read_csv('../input/2019-3rd-ml-month-with-kakr-leaderboard/2019-3rd-ml-month-with-kakr-publicleaderboard.csv')\ndf['SubmissionDate'] = pd.to_datetime(df['SubmissionDate'])\ndf = df.set_index(['TeamName','SubmissionDate'])['Score'].unstack(-1).T\ndf.columns = [name for name in df.columns]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true,"_kg_hide-output":false},"cell_type":"code","source":"FIFTEENTH_SCORE = df.max().sort_values(ascending=False)[15]\nFIFTYTH_SCORE = df.max().sort_values(ascending=False)[50]\nTOP_SCORE = df.max().sort_values(ascending=False)[0]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Public LB Scores of Top Teams over time"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# Interative Plotly\nmypal = cl.scales['8']['qual']['Dark2']\ncolors = cl.interp( mypal, 15 )\nannotations = []\ninit_notebook_mode(connected=True)\nTOP_TEAMS = df.min().loc[df.max() > FIFTEENTH_SCORE].index.values\ndf_filtered = df[TOP_TEAMS].ffill()\ndf_filtered = df_filtered.iloc[(df_filtered.index > '06-14-2019') & (df_filtered.index < '08-18-2019')]\nteam_ordered = df_filtered.loc[df_filtered.index.max()].sort_values(ascending=False).index.tolist()\n\ndata = []\ni = 0\nfor col in df_filtered[team_ordered].columns:\n    data.append(go.Scatter(\n                        x = df_filtered.index,\n                        y = df_filtered[col],\n                        name=col,\n                        line=dict(color=colors[i], width=2),)\n               )\n    i += 1\n\nannotations.append(dict(xref='paper', yref='paper', x=0.0, y=1.05,\n                              xanchor='left', yanchor='bottom',\n                              text='2019 3rd ML month with KaKR Leaderboard Tracking',\n                              font=dict(family='Arial',\n                                        size=30,\n                                        color='rgb(37,37,37)'),\n                              showarrow=False))\n\nlayout = go.Layout(\n            yaxis=dict(range=[0.82, TOP_SCORE+0.0001]),\n            hovermode='x',\n            plot_bgcolor='white',\n            annotations=annotations,\n)\nfig = go.Figure(data=data, layout=layout)\nfig.layout.update(\n    legend=go.layout.Legend(\n        traceorder=\"normal\",\n        font=dict(\n            family=\"sans-serif\",\n            size=12,\n            color=\"black\"\n        ),\n        bgcolor=\"LightSteelBlue\",\n        bordercolor=\"Black\",\n        borderwidth=2,\n    )\n)\n\nfig.layout.update(legend_orientation=\"h\")\nfig.layout.update(template=\"plotly_white\")\n#fig.update_yaxes(showgrid=True, gridwidth=0.5, gridcolor='LightGrey')\nfig.update_xaxes(showgrid=False)\n\niplot(fig)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Closer look at the top"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# Interative Plotly\nmypal = cl.scales['8']['qual']['Dark2']\ncolors = cl.interp( mypal, 15 )\nannotations = []\ninit_notebook_mode(connected=True)\nTOP_TEAMS = df.min().loc[df.max() > FIFTEENTH_SCORE].index.values\ndf_filtered = df[TOP_TEAMS].ffill()\ndf_filtered = df_filtered.iloc[(df_filtered.index > '07-10-2019') & (df_filtered.index < '08-18-2019')]\nteam_ordered = df_filtered.loc[df_filtered.index.max()].sort_values(ascending=False).index.tolist()\n\ndata = []\ni = 0\nfor col in df_filtered[team_ordered].columns:\n    data.append(go.Scatter(\n                        x = df_filtered.index,\n                        y = df_filtered[col],\n                        name=col,\n                        line=dict(color=colors[i], width=2),)\n               )\n    i += 1\n\nannotations.append(dict(xref='paper', yref='paper', x=0.0, y=1.05,\n                              xanchor='left', yanchor='bottom',\n                              text='2019 3rd ML month with KaKR Leaderboard Tracking',\n                              font=dict(family='Arial',\n                                        size=30,\n                                        color='rgb(37,37,37)'),\n                              showarrow=False))\n\nlayout = go.Layout(\n            yaxis=dict(range=[0.955, TOP_SCORE+0.0001]),\n            hovermode='x',\n            plot_bgcolor='white',\n            annotations=annotations,\n)\nfig = go.Figure(data=data, layout=layout)\nfig.layout.update(\n    legend=go.layout.Legend(\n        traceorder=\"normal\",\n        font=dict(\n            family=\"sans-serif\",\n            size=12,\n            color=\"black\"\n        ),\n        bgcolor=\"LightSteelBlue\",\n        bordercolor=\"Black\",\n        borderwidth=2,\n    )\n)\n\nfig.layout.update(legend_orientation=\"h\")\nfig.layout.update(template=\"plotly_white\")\n#fig.update_yaxes(showgrid=True, gridwidth=0.5, gridcolor='LightGrey')\nfig.update_xaxes(showgrid=False)\n\niplot(fig)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# All competitors LB Position over Time"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"# Scores of top teams over time\nALL_TEAMS = [x for x in df.columns.values if x != 'nan']\ndf[ALL_TEAMS].ffill().plot(figsize=(20, 10),\n                           color=color_pal[0],\n                           legend=False,\n                           alpha=0.05,\n                           ylim=(0.0001,TOP_SCORE+0.01),\n                           title='All Teams Public LB Scores over Time')\ndf.ffill().max(axis=1).plot(color=color_pal[1], label='1st Place Public LB', legend=True)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Top LB Scores"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"layout = cf.Layout(\n    height=1400,\n    width=800,\n    xaxis=dict(\n        range=[FIFTYTH_SCORE-0.0001,TOP_SCORE+0.0001]\n    ),\n    yaxis=go.layout.YAxis(\n        automargin=True,\n    )\n)\n\n# Create Top Teams List\nTOP_TEAMS = df.max().loc[df.max() > FIFTYTH_SCORE].index.values\n#TOP_TEAMS = df.max().index.values\n\ndf[TOP_TEAMS].max().sort_values().iplot(kind='barh',\n                                        layout=layout,\n                                        color='grey',\n                                        title='Top 50 Public LB Teams')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Count of LB Submissions that improved score\n\nThis is the count of times the person submitted and got the fun \"You're score improved\" notification. This is not the total submission count."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"layout = cf.Layout(\n    height=1400,\n    width=800,\n    yaxis=go.layout.YAxis(\n        automargin=True,\n    )\n)\n\n\ndf[TOP_TEAMS].nunique().sort_values().iplot(kind='barh',\n                                            layout=layout,\n                                            color='blue',\n                                            title='Count of Submissions improving LB score by Team')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Number of teams by date"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"ax = df.ffill() \\\n    .count(axis=1) \\\n    .plot(figsize=(20, 8),\n          title='Number of Teams in the Competition by Date',\n         color=color_pal[5], lw=5)\nax.set_ylabel('Number of Teams')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Distribution of Scores over time"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"plt.rcParams[\"font.size\"] = \"8\"\nn_days = (datetime.date(2019, 8, 17) - datetime.date(2019, 6, 14)).days # Num days of the comp\nfig, axes = plt.subplots(n_days, 1, figsize=(15, 12), sharex=True)\nplt.subplots_adjust(top=8, bottom=2)\nfor x in range(n_days):\n    date2 = df.loc[df.index.date == datetime.date(2019, 6, 14) + datetime.timedelta(x)].index.min()\n    num_teams = len(df.ffill().loc[date2].dropna())\n    max_cutoff = df.ffill().loc[date2] < 5\n    df.ffill().loc[date2].loc[max_cutoff].plot(kind='hist',\n                               bins=100,\n                               ax=axes[x],\n                               title='{} ({} Teams)'.format(date2.date().isoformat(),\n                                                            num_teams))\n    y_axis = axes[x].yaxis\n    y_axis.set_label_text('')\n    y_axis.label.set_visible(False)\n    \nplt.xlim(left=0, right=1);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}