{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import dautil as dl\n",
    "from sklearn.cluster import KMeans\n",
    "from sklearn.metrics import silhouette_score\n",
    "from sklearn.metrics import silhouette_samples\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "context = dl.nb.Context('evaluating_clusters')\n",
    "lr = dl.nb.LatexRenderer(chapter=10, start=8, context=context)\n",
    "lr.render(r' s(i) = \\frac{b(i) - a(i)}{\\max\\{a(i),b(i)\\}}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot_samples(ax, years, labels, i, avg):\n",
    "    silhouette_values = silhouette_samples(X, labels)\n",
    "    dl.plotting.plot_text(ax, years, silhouette_values,\n",
    "                          labels, add_scatter=True)\n",
    "    ax.set_title('KMeans k={0} Silhouette avg={1:.2f}'.format(i, avg))\n",
    "    ax.set_xlabel('Year')\n",
    "    ax.set_ylabel('Silhouette score')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = dl.data.Weather.load().resample('A').dropna()\n",
    "years = [d.year for d in df.index]\n",
    "X = df.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "dl.options.mimic_seaborn()\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "avgs = []\n",
    "rng = range(2, 9)\n",
    "\n",
    "for i in rng:\n",
    "    kmeans = KMeans(n_clusters=i, random_state=37)\n",
    "    labels = kmeans.fit_predict(X)\n",
    "    avg = silhouette_score(X, labels)\n",
    "    avgs.append(avg)\n",
    "\n",
    "    if i < 5:\n",
    "        if i > 2:\n",
    "            sp.next_ax()\n",
    "            \n",
    "        plot_samples(sp.ax, years, labels, i, avg)\n",
    "\n",
    "sp.next_ax().plot(rng, avgs)\n",
    "sp.label()\n",
    "HTML(sp.exit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
