{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Demo of OPTICS clustering algorithm\n\n.. currentmodule:: sklearn\n\nFinds core samples of high density and expands clusters from them.\nThis example uses data that is generated so that the clusters have\ndifferent densities.\n\nThe :class:`~cluster.OPTICS` is first used with its Xi cluster detection\nmethod, and then setting specific thresholds on the reachability, which\ncorresponds to :class:`~cluster.DBSCAN`. We can see that the different\nclusters of OPTICS's Xi method can be recovered with different choices of\nthresholds in DBSCAN.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nimport matplotlib.gridspec as gridspec\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn.cluster import OPTICS, cluster_optics_dbscan\n\n# Generate sample data\n\nnp.random.seed(0)\nn_points_per_cluster = 250\n\nC1 = [-5, -2] + 0.8 * np.random.randn(n_points_per_cluster, 2)\nC2 = [4, -1] + 0.1 * np.random.randn(n_points_per_cluster, 2)\nC3 = [1, -2] + 0.2 * np.random.randn(n_points_per_cluster, 2)\nC4 = [-2, 3] + 0.3 * np.random.randn(n_points_per_cluster, 2)\nC5 = [3, -2] + 1.6 * np.random.randn(n_points_per_cluster, 2)\nC6 = [5, 6] + 2 * np.random.randn(n_points_per_cluster, 2)\nX = np.vstack((C1, C2, C3, C4, C5, C6))\n\nclust = OPTICS(min_samples=50, xi=0.05, min_cluster_size=0.05)\n\n# Run the fit\nclust.fit(X)\n\nlabels_050 = cluster_optics_dbscan(\n    reachability=clust.reachability_,\n    core_distances=clust.core_distances_,\n    ordering=clust.ordering_,\n    eps=0.5,\n)\nlabels_200 = cluster_optics_dbscan(\n    reachability=clust.reachability_,\n    core_distances=clust.core_distances_,\n    ordering=clust.ordering_,\n    eps=2,\n)\n\nspace = np.arange(len(X))\nreachability = clust.reachability_[clust.ordering_]\nlabels = clust.labels_[clust.ordering_]\n\nplt.figure(figsize=(10, 7))\nG = gridspec.GridSpec(2, 3)\nax1 = plt.subplot(G[0, :])\nax2 = plt.subplot(G[1, 0])\nax3 = plt.subplot(G[1, 1])\nax4 = plt.subplot(G[1, 2])\n\n# Reachability plot\ncolors = [\"g.\", \"r.\", \"b.\", \"y.\", \"c.\"]\nfor klass, color in enumerate(colors):\n    Xk = space[labels == klass]\n    Rk = reachability[labels == klass]\n    ax1.plot(Xk, Rk, color, alpha=0.3)\nax1.plot(space[labels == -1], reachability[labels == -1], \"k.\", alpha=0.3)\nax1.plot(space, np.full_like(space, 2.0, dtype=float), \"k-\", alpha=0.5)\nax1.plot(space, np.full_like(space, 0.5, dtype=float), \"k-.\", alpha=0.5)\nax1.set_ylabel(\"Reachability (epsilon distance)\")\nax1.set_title(\"Reachability Plot\")\n\n# OPTICS\ncolors = [\"g.\", \"r.\", \"b.\", \"y.\", \"c.\"]\nfor klass, color in enumerate(colors):\n    Xk = X[clust.labels_ == klass]\n    ax2.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3)\nax2.plot(X[clust.labels_ == -1, 0], X[clust.labels_ == -1, 1], \"k+\", alpha=0.1)\nax2.set_title(\"Automatic Clustering\\nOPTICS\")\n\n# DBSCAN at 0.5\ncolors = [\"g.\", \"r.\", \"b.\", \"c.\"]\nfor klass, color in enumerate(colors):\n    Xk = X[labels_050 == klass]\n    ax3.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3)\nax3.plot(X[labels_050 == -1, 0], X[labels_050 == -1, 1], \"k+\", alpha=0.1)\nax3.set_title(\"Clustering at 0.5 epsilon cut\\nDBSCAN\")\n\n# DBSCAN at 2.\ncolors = [\"g.\", \"m.\", \"y.\", \"c.\"]\nfor klass, color in enumerate(colors):\n    Xk = X[labels_200 == klass]\n    ax4.plot(Xk[:, 0], Xk[:, 1], color, alpha=0.3)\nax4.plot(X[labels_200 == -1, 0], X[labels_200 == -1, 1], \"k+\", alpha=0.1)\nax4.set_title(\"Clustering at 2.0 epsilon cut\\nDBSCAN\")\n\nplt.tight_layout()\nplt.show()"
      ]
    }
  ],
  "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.11.14"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}