{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 432x288 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%run ./cluster_utils.ipynb\n",
    "%run ./plot_metrics.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn_extra.cluster import KMedoids\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "import pandas as pd\n",
    "from sklearn import metrics\n",
    "\n",
    "import logging\n",
    "logging.getLogger().setLevel(logging.INFO)\n",
    "\n",
    "\n",
    "def run_kmedoids_algo(client_id, dataset, cols, modelling_config):\n",
    "\n",
    "    algo = 'KMedoids'\n",
    "    graph_locations = []\n",
    "    X = dataset[cols]\n",
    "    X2 = dataset[cols].copy()\n",
    "\n",
    "    \"\"\" Decide where do we want to do standardscaling \"\"\"\n",
    "    X = StandardScaler().fit_transform(X)\n",
    "\n",
    "    #n_clusters, elbow_graph = get_best_cluster_cnt(X, client_id, 'kmedoids')\n",
    "    n_clusters = 7\n",
    "    elbow_graph = \"empty_eblow_plot\"\n",
    "    graph_locations.append(elbow_graph)\n",
    "\n",
    "    # TODO! Try different distance metrics\n",
    "    hyperparams = {'n_clusters': n_clusters, 'init': 'k-medoids++', 'max_iter': 500, 'random_state': 0, 'metric': 'mahalanobis'}\n",
    "    kmediods = KMedoids(**hyperparams).fit(X)\n",
    "    labels = kmediods.labels_\n",
    "\n",
    "    dataset = dataset.join(pd.DataFrame(labels))\n",
    "    dataset = dataset.rename(columns={0: 'cluster'})\n",
    "\n",
    "    # TODO! can we do this more elegantly?\n",
    "    # Create a dataframe of our standard-scaled stuff\n",
    "    X = pd.DataFrame(X).join(pd.DataFrame(labels), lsuffix='left_')\n",
    "    X = X.rename(columns={'0': 'cluster'})\n",
    "\n",
    "    # X2 is already a dataframe\n",
    "    X2 = X2.join(pd.DataFrame(labels), lsuffix='left_')\n",
    "    X2 = X2.rename(columns={0: 'cluster'})\n",
    "\n",
    "    plot_2d = modelling_config.get('plot_2d')\n",
    "    plot_3d = modelling_config.get('plot_3d')\n",
    "    boxplot = modelling_config.get('boxplot')\n",
    "    if boxplot:\n",
    "        \"\"\" Plot boxplot \"\"\"\n",
    "        #graph_boxplot_location = plot_boxplot(X, client_id, algo, 1)\n",
    "        #graph_locations.append(graph_boxplot_location)\n",
    "        \"\"\" Plot boxplot \"\"\"\n",
    "        graph_boxplot_location = plot_boxplot(X2, client_id, algo, 2)\n",
    "        graph_locations.append(graph_boxplot_location)\n",
    "\n",
    "    if plot_2d:\n",
    "        \"\"\" Plot 2d scatter version 1\"\"\"\n",
    "        #graph_2d_v1_location = plot_2d_scatter(X, client_id, algo, 1)\n",
    "        #graph_locations.append(graph_2d_v1_location)\n",
    "        \"\"\" Plot 2d scatter version 2\"\"\"\n",
    "        graph_2d_v2_location = plot_2d_scatter(X2, client_id, algo, 2)\n",
    "        graph_locations.append(graph_2d_v2_location)\n",
    "\n",
    "    if plot_3d:\n",
    "        \"\"\" Plot 3d scatter version 1\"\"\"\n",
    "        #graph_3d_v1_location = plot_3d_scatter(X, client_id, algo, 1)\n",
    "        #graph_locations.append(graph_3d_v1_location)\n",
    "        \"\"\" Plot 3d scatter version 2\"\"\"\n",
    "        graph_3d_v2_location = plot_3d_scatter(X2, client_id, algo, 2)\n",
    "        graph_locations.append(graph_3d_v2_location)\n",
    "\n",
    "    eval_metrics = {'clusters': n_clusters,\n",
    "                    'intertia': kmediods.inertia_,\n",
    "                    'silhouette_coefficient': metrics.silhouette_score(X, labels)\n",
    "                    }\n",
    "    model = kmediods\n",
    "    output_cols = ['fan_id', 'cluster']\n",
    "    cluster_output = dataset[output_cols]\n",
    "\n",
    "    return model, eval_metrics, hyperparams, cluster_output, graph_locations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
