mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-01 00:41:09 +00:00
377 lines
40 KiB
Plaintext
377 lines
40 KiB
Plaintext
|
{
|
||
|
"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/ECRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgA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RSmwhuSBEEVqqELgEPOucPOuZPAzcCOlPTvBW6q4L7+U4dhNK/Kpqwqqg7jtqJzJqvmjh3LpzZTqGtRB3HSocgCXAbcENneCXw6Ie0c8DQwFdn3ArAC3A28J+U+i2G6ldnZ2TqFZjXUZRj1bTzCONdtajCajwPfio7fGG3pd83gLryCGlVDRQTBh4E/Htl3Vvh7LvA48Jqse3qrGopWPGV19Ek0qRrwsSLNi68VZlK+iuj+u/y/iFapUxC8Bbg9sn0NcE1C2v8DXJhyrc8Cl2Xd00tBEPeB11FhS8+eD5/LKcko7KPgEhNFkiCowkbwLeA8MzvHzGaAy4FbRxOZ2euAzcA3Ivs2m9lp4fpW4NeA71aQp+aJc42Mo6wuV3r2fPgcLjsuDLYmtREtUloQOOdeAH4PuB14GPiCc+47ZnatmV0aSXo5cHMolYb8IrBiZvcDdwEfcc51UxDkqWCqqLBVYeSjbaNqXkeBaLq9e4P3o+g8CV0arS38JK6b4PvipWooSRUxNZXt5lmXvrfPuuQ2VS15711FHqVSEgVA0UdrZpwPMuucMhW5Koj2BGFe+0QVdow81+hzg0CsQYKgCYp+cGkfcdmK3Gdj6aST17OrCg+wrGuoQSAiJAkChaFuk7TQ1LOz+cMVF722whfXS95Q00VCUo97ryruISYGhaH2jeXloLKOY3a2vNdL28bSPpPXs6sKD7Csa/jsPSW8QYK
|
||
|
"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+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO2de7Ac1X3nPz9dSVhabIyuiEMAXeGEPJwQv7Q4cVIpHGwHUxtkJ94KzjWRHbvuIsW7SaV2N6RUlcRkVYtx1RpSsSAqwhpbd40fix05MSEY2+s/EhxECpCBxcgEYalILCQvsRbWQuK3f3QP6ju3n9Pv6e+nampmTp/uPtPTfX7n/F7H3B0hhBDDZUXbDRBCCNEuEgRCCDFwJAiEEGLgSBAIIcTAkSAQQoiBI0EghBADpxJBYGa3mNl3zOwbCdvnzexBM9tnZn9rZq+ObHsiLL/fzPZW0R4hhBD5sSriCMzsF4BjwMfd/aditr8ReMTdv2tmbwP+yN3fEG57Atjk7k/nPd/69et948aNpdsthBBD4r777nva3c8aL19ZxcHd/WtmtjFl+99Gvt4DnFvmfBs3bmTvXk0ehBCiCGZ2IK68DRvB+4A7It8d+Bszu8/MFlpojxBCDJpKZgR5MbM3EQiCn48U/7y7HzKzHwDuMrP/7e5fi9l3AVgA2LBhQyPtFUKI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APpqcOcWRN9V0FSmpldZalEVJ5yoiyS3QZsBfSHfzrOshHnIHUdZ9tolzV9HGNn+n6B9KOlczSd4c77g12Wsny7OojNdLk2mvu0ib6wfn9eyqItI6zzG66D0luoVyDVXEJCqbrIe4jNdLV42
|
||
|
"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
|
||
|
}
|