{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import floatview\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.cluster import AgglomerativeClustering"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url=\"https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data\"\n",
    "features = ['sepal length', 'sepal width', 'petal length', 'petal width', 'target']\n",
    "tab = pd.read_csv(url, header=None, index_col=False, names=features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gmw = floatview.GlueManagerWidget(tab, modal=True, label=\"400Cars\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_clusters = 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cluster = AgglomerativeClustering(n_clusters=n_clusters, affinity='euclidean', linkage='ward') \n",
    "df = gmw.gluemanager.data.to_dataframe()\n",
    "diff = (df.columns.difference(['sepal length', 'sepal width', 'petal length', 'petal width']))\n",
    "cluster.fit_predict(df.drop(diff, axis=1)) \n",
    "for i in range(n_clusters):\n",
    "  state = np.nonzero(cluster.labels_== i)\n",
    "  gmw.gluemanager.updateSelection(state)\n",
    "  gmw.gluemanager.createSubsetFromSelection(label='cluster'+str(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gmw.gluemanager.newView(\"parallels\", ['sepal length', 'sepal width', 'petal length', 'petal width', 'target'], \"Parallel\");\n",
    "gmw.gluemanager.newView(\"scatter\", ['sepal length', 'sepal width'], \"Scatter\");\n",
    "gmw.gluemanager.newView(\"composed_polyfit_2d\", ['petal length', 'petal width'], title=\"Fitting\", only_subsets=True );"
   ]
  }
 ],
 "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.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
