{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "title = \"Iris K-Means Clustering\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "meta" ] }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from sklearn.cluster import KMeans\n", "from sklearn import datasets\n", "np.random.seed(5)\n", "\n", "import ipywidgets as widgets\n", "import plotly.graph_objects as go\n", "import colorlover as cl" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "sidebar" ] }, "source": [ "## Sidebar" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "iris = datasets.load_iris()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "options = [(fname, i) for i, fname in enumerate(iris.feature_names)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "inputs" ] }, "outputs": [], "source": [ "x_label = widgets.Label(value=\"X Variable:\")\n", "x_var = widgets.Dropdown(options=options, value=0)\n", "y_label = widgets.Label(value=\"Y Variable:\")\n", "y_var = widgets.Dropdown(options=options, value=1)\n", "cluster_label = widgets.Label(value=\"Cluster count:\")\n", "cluster_count = widgets.BoundedIntText(value=3, min=1, max=9, disabled=False)\n", "\n", "widgets.VBox([x_label, x_var, y_label, y_var, cluster_label, cluster_count])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Column" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### K-Means" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def fit_model():\n", " X = iris.data[:, [x_var.value, y_var.value]]\n", " y = iris.target\n", " estimator = KMeans(n_clusters=cluster_count.value)\n", " estimator.fit(X)\n", " return X, estimator" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scale = cl.scales[\"3\"][\"qual\"][\"Set1\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = go.FigureWidget()\n", "plot = go.Scatter(x=[], y=[], mode=\"markers\",\n", " marker=dict(color=[], colorscale=scale, size=10))\n", "fig.add_trace(plot)\n", "\n", "margin = go.layout.Margin(l=20, r=20, b=20, t=30)\n", "fig = fig.update_layout(margin=margin)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def on_value_change(change):\n", " X, estimator = fit_model()\n", " fig.data[0]['x'] = X[:, 0]\n", " fig.data[0]['y'] = X[:, 1]\n", " fig.data[0]['marker']['color'] = estimator.labels_\n", " scale = cl.scales[str(cluster_count.value)][\"qual\"][\"Set1\"] if cluster_count.value > 2 else None\n", " fig.data[0]['marker']['colorscale'] = scale\n", " fig.update_xaxes(title_text=x_var.label)\n", " fig.update_yaxes(title_text=y_var.label)\n", "\n", "x_var.observe(on_value_change, names='value')\n", "y_var.observe(on_value_change, names='value')\n", "cluster_count.observe(on_value_change, names='value')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "chart" ] }, "outputs": [], "source": [ "on_value_change(None)\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Tags", "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.8.0" } }, "nbformat": 4, "nbformat_minor": 4 }