{"cells":[{"metadata":{},"cell_type":"markdown","source":"# This kernel is forked from [this kernel](https://www.kaggle.com/robikscube/the-race-to-predict-molecular-properties). Be sure to upvote it!\n\nWith the competition coming to and end, it would be interesting to see how the community progressed throughout the competition.\nAs I didn't see a kernel like this for this competition, I decided to publish one myself. "},{"metadata":{},"cell_type":"markdown","source":"# Lets track the Public LB Standings\n## For the APTOS 2019 Blindness Detection \nLast updated **September 7, 2019** (end of competition)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-input":true},"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.graph_objs as go\nfrom plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\nfrom sklearn.linear_model import LinearRegression\nimport datetime\nimport colorlover as cl\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/aptos2019-public-lb/publicleaderboarddata/aptos2019-blindness-detection-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]\n\nFIFTEENTH_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['9']['div']['Spectral']\ncolors = cl.interp( mypal, 15 )\nannotations = []\ninit_notebook_mode(connected=True)\nTOP_TEAMS = df.max().loc[df.max() > FIFTEENTH_SCORE].index.values\ndf_filtered = df[TOP_TEAMS].ffill()\nteam_ordered = df_filtered.loc[df_filtered.index.max()] \\\n    .sort_values(ascending=True).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='APTOS 2019 Leaderboard Tracking',\n                              font=dict(family='Arial',\n                                        size=30,\n                                        color='rgb(37,37,37)'),\n                              showarrow=False))\n\nlayout = go.Layout(yaxis=dict(range=[0.6,TOP_SCORE]),\n                   hovermode='x',\n                   plot_bgcolor='white',\n                  annotations=annotations,\n                  )\nfig = go.Figure(data=data, layout=layout)\nfig.update_layout(\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.update_layout(legend_orientation=\"h\")\nfig.update_layout(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 all teams over time\nplt.rcParams[\"font.size\"] = \"12\"\nALL_TEAMS = df.columns.values\ndf[ALL_TEAMS[1:]].ffill().plot(figsize=(20, 10),\n                           color=color_pal[0],\n                           legend=False,\n                           alpha=0.05,\n                           ylim=(TOP_SCORE-0.5, 1),\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":"# Number of teams by Date"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"plt.rcParams[\"font.size\"] = \"12\"\nax =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":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"team_over_time = df.ffill() \\\n    .count(axis=1)\n\nlr = LinearRegression()\n_ = lr.fit(np.array(pd.to_numeric(team_over_time.index).tolist()).reshape(-1, 1),\n           team_over_time.values)\n\nteamcount_df = pd.DataFrame(team_over_time)\n\nteamcount_pred_df = pd.DataFrame(index=pd.date_range('06-03-2019','09-06-2019'))\nteamcount_pred_df['teamcount_predict'] = lr.predict(np.array(pd.to_numeric(teamcount_pred_df.index).tolist()).reshape(-1, 1))\n\nlr = LinearRegression()\n_ = lr.fit(np.array(pd.to_numeric(team_over_time[-1000:].index).tolist()).reshape(-1, 1),\n           team_over_time[-1000:].values)\n\nteamcount_pred_df['teamcount_predict_recent'] = lr.predict(np.array(pd.to_numeric(teamcount_pred_df.index).tolist()).reshape(-1, 1))\n\nplt.rcParams[\"font.size\"] = \"12\"\nax =df.ffill() \\\n    .count(axis=1) \\\n    .plot(figsize=(20, 8),\n          title='Forecasting the Final Number of Teams',\n         color=color_pal[5], lw=5,\n         xlim=('06-03-2019','09-07-2019'),\n         label='Acutal Team Count by Date')\nax.set_ylabel('Number of Teams')\nteamcount_pred_df['teamcount_predict'].plot(ax=ax, style='.-.', alpha=0.5, label='Regression Using All Data')\nteamcount_pred_df['teamcount_predict_recent'].plot(ax=ax, style='.-.', alpha=0.5, label='Regression Using last 1000 observations')\nplt.legend()\nplt.axvline('08-29-2019', color='orange', linestyle='-.')\nplt.text('08-29-2019', 1000,'Merger Deadline',rotation=-90)\nplt.axvline('09-07-2019', color='orange', linestyle='-.')\nplt.text('09-07-2019', 1000,'Final Deadline',rotation=-90)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Top LB Scores\n(Larger bar is better)"},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"plt.rcParams[\"font.size\"] = \"12\"\n# Create Top Teams List\nTOP_TEAMS = df.max().loc[df.max() > FIFTYTH_SCORE].index.values\ndf[TOP_TEAMS].max().sort_values(ascending=True).plot(kind='barh',\n                                       xlim=(TOP_SCORE-0.1,FIFTYTH_SCORE+0.1),\n                                       title='Top 50 Public LB Teams',\n                                       figsize=(12, 15),\n                                       color=color_pal[3])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Count of LB Submissions that improved score\nThis is the count of times the person submitted and got the fun \"Your score improved\" notification. This is not the total submission count."},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"plt.rcParams[\"font.size\"] = \"12\"\ndf[TOP_TEAMS].nunique().sort_values().plot(kind='barh',\n                                           figsize=(12, 15),\n                                           color=color_pal[1],\n                                           title='Count of Submissions improving LB score by Team')\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\"] = \"7\"\nn_days = (datetime.date.today() - datetime.date(2019, 6, 28)).days # Num days of the comp\nfig, axes = plt.subplots(n_days, 1, figsize=(15, 10), 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, 28) + datetime.timedelta(x)].index.max()\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)","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}