{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DBSCAN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.preprocessing import scale\n",
    "from sklearn.cluster import KMeans\n",
    "import seaborn as sns\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.045255</td>\n",
       "      <td>0.332214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.801944</td>\n",
       "      <td>-0.411547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.749356</td>\n",
       "      <td>0.775108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.975674</td>\n",
       "      <td>0.191768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.512188</td>\n",
       "      <td>0.929997</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          X         Y\n",
       "0  1.045255  0.332214\n",
       "1  0.801944 -0.411547\n",
       "2 -0.749356  0.775108\n",
       "3  0.975674  0.191768\n",
       "4 -0.512188  0.929997"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "mydata=pd.read_csv(\"moon_data.csv\").iloc[:,1:]\n",
    "mydata.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x2170ed17dd8>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 38948 characters]\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.lmplot('X','Y',data=mydata,fit_reg=False) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x217110c10f0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 40216 characters]\n",
      "text/plain": [
       "<Figure size 433.5x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.cluster import AgglomerativeClustering\n",
    "hclus=AgglomerativeClustering(n_clusters=2, affinity='euclidean',linkage='ward')\n",
    "labels_hclus=hclus.fit_predict(mydata)\n",
    "mydata['cluster_hclus']=labels_hclus\n",
    "sns.lmplot(fit_reg=False,x='X',y='Y',data=mydata,hue='cluster_hclus')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "kmeans=KMeans(n_clusters=2)\n",
    "kmeans.fit(mydata)\n",
    "mydata[\"cluster\"]=kmeans.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x26903b27a20>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 39312 characters]\n",
      "text/plain": [
       "<Figure size 402.375x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.lmplot('X','Y',data=mydata,hue='cluster',fit_reg=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import DBSCAN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "del mydata['cluster']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x26903bcb668>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 38684 characters]\n",
      "text/plain": [
       "<Figure size 406x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "db = DBSCAN(eps=0.1, min_samples=30, metric='euclidean').fit(mydata)\n",
    "mydata['cluster']=db.labels_\n",
    "sns.lmplot('X','Y',data=mydata,hue='cluster',fit_reg=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       " 1    999\n",
       " 0    995\n",
       "-1      6\n",
       "dtype: int64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series(db.labels_).value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# anamoly detection with dbscan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\MansiGupta\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:7: DataConversionWarning: Data with input dtype int64 were all converted to float64 by the scale function.\n",
      "  import sys\n"
     ]
    }
   ],
   "source": [
    "myfile=r'Wholesale customers data.csv'\n",
    "\n",
    "groc=pd.read_csv(myfile)\n",
    "\n",
    "groc=groc[[\"Milk\",\"Grocery\"]]\n",
    "\n",
    "groc_std=pd.DataFrame(scale(groc),columns=list(groc.columns))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x26903c0b278>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 15652 characters]\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.lmplot(x='Milk',y='Grocery',data=groc,fit_reg=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "For epsilon = 0.5 , percentage of outliers is:  9.77\n",
      "For epsilon = 0.5918367346938775 , percentage of outliers is:  7.95\n",
      "For epsilon = 0.6836734693877551 , percentage of outliers is:  6.36\n",
      "For epsilon = 0.7755102040816326 , percentage of outliers is:  5.68\n",
      "For epsilon = 0.8673469387755102 , percentage of outliers is:  4.55\n",
      "For epsilon = 0.9591836734693877 , percentage of outliers is:  4.32\n",
      "For epsilon = 1.0510204081632653 , percentage of outliers is:  3.41\n",
      "For epsilon = 1.1428571428571428 , percentage of outliers is:  3.41\n",
      "For epsilon = 1.2346938775510203 , percentage of outliers is:  2.05\n",
      "For epsilon = 1.3265306122448979 , percentage of outliers is:  2.05\n",
      "For epsilon = 1.4183673469387754 , percentage of outliers is:  2.05\n",
      "For epsilon = 1.5102040816326532 , percentage of outliers is:  1.82\n",
      "For epsilon = 1.6020408163265307 , percentage of outliers is:  1.82\n",
      "For epsilon = 1.6938775510204083 , percentage of outliers is:  1.36\n",
      "For epsilon = 1.7857142857142858 , percentage of outliers is:  1.36\n",
      "For epsilon = 1.8775510204081634 , percentage of outliers is:  1.36\n",
      "For epsilon = 1.969387755102041 , percentage of outliers is:  1.36\n",
      "For epsilon = 2.0612244897959187 , percentage of outliers is:  1.36\n",
      "For epsilon = 2.1530612244897958 , percentage of outliers is:  1.14\n",
      "For epsilon = 2.2448979591836737 , percentage of outliers is:  1.14\n",
      "For epsilon = 2.336734693877551 , percentage of outliers is:  1.14\n",
      "For epsilon = 2.428571428571429 , percentage of outliers is:  1.14\n",
      "For epsilon = 2.5204081632653064 , percentage of outliers is:  0.91\n",
      "For epsilon = 2.612244897959184 , percentage of outliers is:  0.91\n",
      "For epsilon = 2.7040816326530615 , percentage of outliers is:  0.91\n",
      "For epsilon = 2.795918367346939 , percentage of outliers is:  0.91\n",
      "For epsilon = 2.8877551020408165 , percentage of outliers is:  0.91\n",
      "For epsilon = 2.979591836734694 , percentage of outliers is:  0.91\n",
      "For epsilon = 3.0714285714285716 , percentage of outliers is:  0.91\n",
      "For epsilon = 3.163265306122449 , percentage of outliers is:  0.68\n",
      "For epsilon = 3.2551020408163267 , percentage of outliers is:  0.68\n",
      "For epsilon = 3.3469387755102042 , percentage of outliers is:  0.68\n",
      "For epsilon = 3.438775510204082 , percentage of outliers is:  0.68\n",
      "For epsilon = 3.5306122448979593 , percentage of outliers is:  0.68\n",
      "For epsilon = 3.622448979591837 , percentage of outliers is:  0.68\n",
      "For epsilon = 3.7142857142857144 , percentage of outliers is:  0.68\n",
      "For epsilon = 3.806122448979592 , percentage of outliers is:  0.68\n",
      "For epsilon = 3.8979591836734695 , percentage of outliers is:  0.45\n",
      "For epsilon = 3.989795918367347 , percentage of outliers is:  0.45\n",
      "For epsilon = 4.081632653061225 , percentage of outliers is:  0.45\n",
      "For epsilon = 4.173469387755102 , percentage of outliers is:  0.45\n",
      "For epsilon = 4.26530612244898 , percentage of outliers is:  0.23\n",
      "For epsilon = 4.357142857142858 , percentage of outliers is:  0.23\n",
      "For epsilon = 4.448979591836735 , percentage of outliers is:  0.23\n",
      "For epsilon = 4.540816326530613 , percentage of outliers is:  0.23\n",
      "For epsilon = 4.63265306122449 , percentage of outliers is:  0.0\n",
      "For epsilon = 4.724489795918368 , percentage of outliers is:  0.0\n",
      "For epsilon = 4.816326530612245 , percentage of outliers is:  0.0\n",
      "For epsilon = 4.908163265306123 , percentage of outliers is:  0.0\n",
      "For epsilon = 5.0 , percentage of outliers is:  0.0\n"
     ]
    }
   ],
   "source": [
    "r=np.linspace(0.5,5)\n",
    "for epsilon in r:\n",
    "    db = DBSCAN(eps=epsilon, min_samples=20, metric='euclidean').fit(groc_std)\n",
    "    labels = db.labels_\n",
    "#     n_clust=len(set(labels))-1\n",
    "    outlier=np.round((labels == -1).sum()/len(labels)*100,2)\n",
    "#     print('Estimated number of clusters: %d', n_clust)\n",
    "    print(\"For epsilon =\", epsilon ,\", percentage of outliers is: \",outlier)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "db = DBSCAN(eps=0.77, min_samples=20, metric='euclidean').fit(groc_std)\n",
    "groc['cluster']=db.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x26903ca8470>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 16588 characters]\n",
      "text/plain": [
       "<Figure size 406x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.lmplot(x='Milk',y='Grocery',data=groc,fit_reg=False,hue='cluster')"
   ]
  }
 ],
 "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
