Dbscan

H
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DBSCAN\n",
    "This implementation and notebook is inspired from the original DBSCAN algorithm and article as given in \n",
    "[DBSCAN Wikipedia](https://en.wikipedia.org/wiki/DBSCAN).\n",
    "\n",
    "Stands for __Density-based spatial clustering of applications with noise__ . \n",
    "\n",
    "DBSCAN is clustering algorithm that tries to captures the intuition that if two points belong to the same cluster they should be close to one another. It does so by finding regions that are densely packed together, i.e, the points that have many close neighbours.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### When to use ?\n",
    "\n",
    "1. You need a robust clustering algorithm.\n",
    "2. You don't know how many clusters there are in the dataset\n",
    "3. You find it difficult to guess the number of clusters there are just by eyeballing the dataset.\n",
    "4. The clusters are of arbitrary shapes.\n",
    "5. You want to detect outliers/noise."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Why DBSCAN ? \n",
    "\n",
    "This algorithm is way better than other clustering algorithms such as [k-means](https://en.wikipedia.org/wiki/K-means_clustering) whose only job is to find circular blobs. It is smart enough to figure out the number of clusters in the dataset on its own, unlike k-means where you need to specify 'k'.  It can also find clusters of arbitrary shapes, not just circular blobs.  Its too robust to be affected by outliers (the noise points) and isn't fooled by them, unlike k-means where the entire centroid get pulled thanks to pesky outliers. Plus, you can fine-tune its parameters depending on what you are clustering.\n",
    "\n",
    "#### Have a look at these [neat animations](https://www.naftaliharris.com/blog/visualizing-dbscan-clustering/) of DBSCAN to see for yourself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## First lets grab a dataset\n",
    "We will take the moons dataset which is pretty good at showing the power of DBSCAN. \n",
    "\n",
    "Lets generate 200 random points in the shape of two moons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import make_moons\n",
    "\n",
    "x, label = make_moons(n_samples=200, noise=0.1, random_state=19)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize the dataset using matplotlib\n",
    "You will observe that the points are in the shape of two crescent moons. \n",
    "\n",
    "The challenge here is to cluster the two moons. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x11a00e588>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD6CAYAAACs/ECRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO2df5AlV3XfP2dnZ4QWsLX7VsZroZmViAIWToxhIoNMUcIQkFUpLSkrFeHRejGixlrFKVwpUqxqEyelZMv8qApgEyFvFJmFmUgCYjuKC0WWQIQ/jAQjol+grLRapEUqGVa7WLAlh5VWN390P03Pm/75+tft199PVdfrH7e7b9/Xfc+955x7rjnnEEII0V82tJ0BIYQQ7SJBIIQQPUeCQAgheo4EgRBC9BwJAiGE6DkSBEII0XMqEQRmdqOZ/dDMHko4vmBmD5jZg2b212b2y5Fjj4f77zOzlSryI4QQIj9WxTgCM3sbcAL4nHPul2KOXwg87Jz7kZn9BvDvnXO/Gh57HJh3zj2T935bt25127dvL51vIYToE/fee+8zzrkzR/dvrOLizrmvm9n2lON/Hdm8G3h1mftt376dlRV1HoQQoghm9kTc/jZsBFcCt0W2HfBXZnavmS22kB8hhOg1lfQI8mJmbycQBG+N7H6rc+4pM/s54A4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JACCF6jgSBEEL0HAkCIYToOf8fbbT41ArTNJwAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(x[:,0], x[:,1],'ro')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Abstract of the Algorithm\n",
    "The DBSCAN algorithm can be abstracted into the following steps:\n",
    "\n",
    "- Find the points in the $ε$ (eps) neighborhood of every point, and identify the core points with more than <b>min_pts</b> neighbors.\n",
    "- Find the connected components of core points on the neighbor graph, ignoring all non-core points.\n",
    "- Assign each non-core point to a nearby cluster if the cluster is an $ε$ (eps) neighbor, otherwise assign it to noise.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Preparing the points\n",
    "Initially we label all the points in the dataset as __undefined__ .\n",
    "\n",
    "__points__ is our database of all points in the dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "points = { (point[0],point[1]):{'label':'undefined'} for point in x }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Helper functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def euclidean_distance(q, p):\n",
    "    \"\"\"\n",
    "        Calculates the Euclidean distance\n",
    "        between points P and Q\n",
    "    \"\"\"\n",
    "    a = pow((q[0] - p[0]), 2)\n",
    "    b = pow((q[1] - p[1]), 2)\n",
    "    return pow((a + b), 0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_neighbors(db, q, eps):\n",
    "    \"\"\"\n",
    "        Finds all points in the DB that\n",
    "        are within a distance of eps from Q\n",
    "    \"\"\"\n",
    "    return [p for p in db if euclidean_distance(q, p) <= eps]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_cluster(db, clusters):\n",
    "    \"\"\"\n",
    "        Extracts all the points in the DB and puts them together\n",
    "        as seperate clusters and finally plots them\n",
    "    \"\"\"\n",
    "    temp = []\n",
    "    noise = []\n",
    "    for i in clusters:\n",
    "        stack = []\n",
    "        for k, v in db.items():\n",
    "            if v[\"label\"] == i:\n",
    "                stack.append(k)\n",
    "            elif v[\"label\"] == \"noise\":\n",
    "                noise.append(k)\n",
    "        temp.append(stack)\n",
    "\n",
    "    color = iter(plt.cm.rainbow(np.linspace(0, 1, len(clusters))))\n",
    "    for i in range(0, len(temp)):\n",
    "        c = next(color)\n",
    "        x = [l[0] for l in temp[i]]\n",
    "        y = [l[1] for l in temp[i]]\n",
    "        plt.plot(x, y, \"ro\", c=c)\n",
    "\n",
    "    x = [l[0] for l in noise]\n",
    "    y = [l[1] for l in noise]\n",
    "    plt.plot(x, y, \"ro\", c=\"0\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Implementation of DBSCAN\n",
    "\n",
    "Initialize an empty list, clusters = $[ ]$ and cluster identifier, c = 0\n",
    "\n",
    "1. For each point  p in our database/dict  db :\n",
    "\n",
    "    1.1 Check if p is already labelled. If it's already labelled (means it already been associated to a cluster), continue to the next point,i.e, go to step 1\n",
    "    \n",
    "    1.2. Find the list of neighbors of p , i.e, points that are within a distance of eps from p\n",
    "    \n",
    "    1.3. If p does not have atleast min_pts neighbours, we label it as noise and go back to step 1\n",
    "    \n",
    "    1.4. Initialize the cluster, by incrementing c by 1\n",
    "    \n",
    "    1.5. Append the cluster identifier c to clusters\n",
    "    \n",
    "    1.6. Label p with the cluster identifier c\n",
    "    \n",
    "    1.7 Remove p from the list of neighbors (p will be detected as its own neighbor because it is within eps of itself)\n",
    "    \n",
    "    1.8. Initialize the seed_set as a copy of neighbors\n",
    "   \n",
    "    1.9. While the seed_set is not empty:\n",
    "        1.9.1. Removing the 1st point from seed_set and initialise it as q\n",
    "        1.9.2. If it's label is noise, label it with c\n",
    "        1.9.3. If it's not unlabelled, go back to step 1.9\n",
    "        1.9.4. Label q with c\n",
    "        1.9.5. Find the neighbours of q \n",
    "        1.9.6. If there are atleast min_pts neighbors, append them to the seed_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def dbscan(db,eps,min_pts):\n",
    "    '''\n",
    "        Implementation of the DBSCAN algorithm\n",
    "    '''\n",
    "    clusters = []\n",
    "    c = 0\n",
    "    for p in db:\n",
    "        if db[p][\"label\"] != \"undefined\":\n",
    "            continue\n",
    "        neighbors = find_neighbors(db, p, eps)\n",
    "        if len(neighbors) < min_pts:\n",
    "            db[p][\"label\"] = \"noise\"\n",
    "            continue\n",
    "        c += 1\n",
    "        clusters.append(c)\n",
    "        db[p][\"label\"] = c\n",
    "        neighbors.remove(p)\n",
    "        seed_set = neighbors.copy()\n",
    "        while seed_set != []:\n",
    "            q = seed_set.pop(0)\n",
    "            if db[q][\"label\"] == \"noise\":\n",
    "                db[q][\"label\"] = c\n",
    "            if db[q][\"label\"] != \"undefined\":\n",
    "                continue\n",
    "            db[q][\"label\"] = c\n",
    "            neighbors_n = find_neighbors(db, q, eps)\n",
    "            if len(neighbors_n) >= min_pts:\n",
    "                seed_set = seed_set + neighbors_n\n",
    "    return db, clusters\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lets run it!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD6CAYAAACs/ECRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO2de7Ac1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rKAzXn7ofM7JXAl81sn7t/q+q2DpwvAJ909++b2b8jmNUohqY+/oHgvj5mZpcBnwcuaLlNL2JmpwP/E/gdd/+XttsDEgQAuPubSx7iEEHk9Ihzw7JaSWu3mf1zJIXH2cB3Eo5xKHx/3My+SjBKaUMQ5LmGozoHzWwlcAZwpJnmJZLZbg8CL0fcTGC/6QOt3NdliXau7v5FM9tpZuvdvfUcRGa2ikAILLr77TFVWrnmUg1Vw73ABWZ2vpmtJjBktuqBw6k8UJCQB8rMzhyl/jaz9cDPAQ831sKl5LmG0d/0TuDLHlrYWiSz3WM63ssJdMN9YA/wG6Eny88Az0TUjZ3FzH5wZDsys4sI+rm2BwyEbfpz4BF3/28J1dq55m1b0rv+At5BoKf7PvDPwJ1h+Q8BX4zUu4zAC+BbBCqltts9S7Bq3GPAl4B1Yfkm4Obw8xsJ8jw9EL6/r+U2L7uGBGnJLw8/vwT4DLAf+HvglW1f55zt/q/AQ+F1/grw4223OWzXJ4GngOfDe/x9wFXAVeF2Az4a/q59JHjNdbDdH4hc73uAN7bd5rBdPw848CBwf/i6rAvXXJHFQggxcKQaEkKIgSNBIIQQA0eCQAghBo4EgRBCDBwJAiGEGDgSBEIIMXAkCIQQYuBIEAghxMD5/1SENh4utwCVAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "eps = 0.25\n",
    "min_pts = 12\n",
    "\n",
    "db,clusters = dbscan(points,eps,min_pts)\n",
    "\n",
    "plot_cluster(db,clusters)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I encourage you to try with different datasets and playing with the values of eps and min_pts.\n",
    "\n",
    "Also, try kmeans on this dataset and see how it compares to dbscan. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I hope by now you are convinced about about how cool dbscan is. But it has its pitfalls.\n",
    "### When NOT to use ?\n",
    "\n",
    "1. You have a high dimentional dataset. Euclidean distance will fail thanks to '[curse of dimentionality](https://en.wikipedia.org/wiki/Curse_of_dimensionality#Distance_functions)'.\n",
    "2. We have used a dict to store the points. So we can't do anything about the order in which the points will be processed. So it's not entirely deterministic.\n",
    "3. Won't work well if there are large differences in density. Finding the min_pts and $ε$ combination will be difficult.\n",
    "4. Choosing the $ε$ without understanding the data and its scale, might result is poor clustering performance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
About this Algorithm

DBSCAN

This implementation and notebook is inspired from the original DBSCAN algorithm and article as given in DBSCAN Wikipedia.

Stands for Density-based spatial clustering of applications with noise .

DBSCAN is clustering algorithm that tries to captures the intuition that if two points belong to the same cluster they should be close to one another. It does so by finding regions that are densely packed together, i.e, the points that have many close neighbours.

When to use ?

  1. You need a robust clustering algorithm.
  2. You don't know how many clusters there are in the dataset
  3. You find it difficult to guess the number of clusters there are just by eyeballing the dataset.
  4. The clusters are of arbitrary shapes.
  5. You want to detect outliers/noise.

Why DBSCAN ?

This algorithm is way better than other clustering algorithms such as k-means whose only job is to find circular blobs. It is smart enough to figure out the number of clusters in the dataset on its own, unlike k-means where you need to specify 'k'. It can also find clusters of arbitrary shapes, not just circular blobs. Its too robust to be affected by outliers (the noise points) and isn't fooled by them, unlike k-means where the entire centroid get pulled thanks to pesky outliers. Plus, you can fine-tune its parameters depending on what you are clustering.

Have a look at these neat animations of DBSCAN to see for yourself.

import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline

First lets grab a dataset

We will take the moons dataset which is pretty good at showing the power of DBSCAN.

Lets generate 200 random points in the shape of two moons

from sklearn.datasets import make_moons

x, label = make_moons(n_samples=200, noise=0.1, random_state=19)

Visualize the dataset using matplotlib

You will observe that the points are in the shape of two crescent moons.

The challenge here is to cluster the two moons.

plt.plot(x[:,0], x[:,1],'ro')
[&lt;matplotlib.lines.Line2D at 0x11a00e588&gt;]

Abstract of the Algorithm

The DBSCAN algorithm can be abstracted into the following steps:

  • Find the points in the $ε$ (eps) neighborhood of every point, and identify the core points with more than min_pts neighbors.
  • Find the connected components of core points on the neighbor graph, ignoring all non-core points.
  • Assign each non-core point to a nearby cluster if the cluster is an $ε$ (eps) neighbor, otherwise assign it to noise.

Preparing the points

Initially we label all the points in the dataset as undefined .

points is our database of all points in the dataset.

points = { (point[0],point[1]):{'label':'undefined'} for point in x }

Helper functions

def euclidean_distance(q, p):
    """
        Calculates the Euclidean distance
        between points P and Q
    """
    a = pow((q[0] - p[0]), 2)
    b = pow((q[1] - p[1]), 2)
    return pow((a + b), 0.5)
def find_neighbors(db, q, eps):
    """
        Finds all points in the DB that
        are within a distance of eps from Q
    """
    return [p for p in db if euclidean_distance(q, p) &lt;= eps]
def plot_cluster(db, clusters):
    """
        Extracts all the points in the DB and puts them together
        as seperate clusters and finally plots them
    """
    temp = []
    noise = []
    for i in clusters:
        stack = []
        for k, v in db.items():
            if v["label"] == i:
                stack.append(k)
            elif v["label"] == "noise":
                noise.append(k)
        temp.append(stack)

    color = iter(plt.cm.rainbow(np.linspace(0, 1, len(clusters))))
    for i in range(0, len(temp)):
        c = next(color)
        x = [l[0] for l in temp[i]]
        y = [l[1] for l in temp[i]]
        plt.plot(x, y, "ro", c=c)

    x = [l[0] for l in noise]
    y = [l[1] for l in noise]
    plt.plot(x, y, "ro", c="0")

Implementation of DBSCAN

Initialize an empty list, clusters = $[ ]$ and cluster identifier, c = 0

  1. For each point p in our database/dict db :

    1.1 Check if p is already labelled. If it's already labelled (means it already been associated to a cluster), continue to the next point,i.e, go to step 1

    1.2. Find the list of neighbors of p , i.e, points that are within a distance of eps from p

    1.3. If p does not have atleast min_pts neighbours, we label it as noise and go back to step 1

    1.4. Initialize the cluster, by incrementing c by 1

    1.5. Append the cluster identifier c to clusters

    1.6. Label p with the cluster identifier c

    1.7 Remove p from the list of neighbors (p will be detected as its own neighbor because it is within eps of itself)

    1.8. Initialize the seed_set as a copy of neighbors

    1.9. While the seed_set is not empty: 1.9.1. Removing the 1st point from seed_set and initialise it as q 1.9.2. If it's label is noise, label it with c 1.9.3. If it's not unlabelled, go back to step 1.9 1.9.4. Label q with c 1.9.5. Find the neighbours of q 1.9.6. If there are atleast min_pts neighbors, append them to the seed_set

def dbscan(db,eps,min_pts):
    '''
        Implementation of the DBSCAN algorithm
    '''
    clusters = []
    c = 0
    for p in db:
        if db[p]["label"] != "undefined":
            continue
        neighbors = find_neighbors(db, p, eps)
        if len(neighbors) &lt; min_pts:
            db[p]["label"] = "noise"
            continue
        c += 1
        clusters.append(c)
        db[p]["label"] = c
        neighbors.remove(p)
        seed_set = neighbors.copy()
        while seed_set != []:
            q = seed_set.pop(0)
            if db[q]["label"] == "noise":
                db[q]["label"] = c
            if db[q]["label"] != "undefined":
                continue
            db[q]["label"] = c
            neighbors_n = find_neighbors(db, q, eps)
            if len(neighbors_n) &gt;= min_pts:
                seed_set = seed_set + neighbors_n
    return db, clusters

Lets run it!

eps = 0.25
min_pts = 12

db,clusters = dbscan(points,eps,min_pts)

plot_cluster(db,clusters)

I encourage you to try with different datasets and playing with the values of eps and min_pts.

Also, try kmeans on this dataset and see how it compares to dbscan.

I hope by now you are convinced about about how cool dbscan is. But it has its pitfalls.

When NOT to use ?

  1. You have a high dimentional dataset. Euclidean distance will fail thanks to 'curse of dimentionality'.
  2. We have used a dict to store the points. So we can't do anything about the order in which the points will be processed. So it's not entirely deterministic.
  3. Won't work well if there are large differences in density. Finding the min_pts and $ε$ combination will be difficult.
  4. Choosing the $ε$ without understanding the data and its scale, might result is poor clustering performance.