{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "054f0cca3061be223d76116cb8544b598eb5a297"
   },
   "source": [
    "# Customer Segmentation and Analysis \n",
    "### Steps to solve the problem :\n",
    "1. Importing Libraries.\n",
    "2. Exploration of data.\n",
    "3. Data Visualization.\n",
    "4. Clustering using K-Means.\n",
    "5. Selection of Clusters.\n",
    "6. Ploting the Cluster Boundry and Clusters.\n",
    "7. 3D Plot of Clusters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "fd0fc89fe26333add074845d0629b52ae828584c"
   },
   "source": [
    "## Importing Libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_kg_hide-input": false,
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
    "execution": {
     "iopub.execute_input": "2021-07-06T12:23:02.749481Z",
     "iopub.status.busy": "2021-07-06T12:23:02.749172Z",
     "iopub.status.idle": "2021-07-06T12:23:05.985353Z",
     "shell.execute_reply": "2021-07-06T12:23:05.984660Z",
     "shell.execute_reply.started": "2021-07-06T12:23:02.749430Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "        <script type=\"text/javascript\">\n",
       "        window.PlotlyConfig = {MathJaxConfig: 'local'};\n",
       "        if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
       "        if (typeof require !== 'undefined') {\n",
       "        require.undef(\"plotly\");\n",
       "        requirejs.config({\n",
       "            paths: {\n",
       "                'plotly': ['https://cdn.plot.ly/plotly-latest.min']\n",
       "            }\n",
       "        });\n",
       "        require(['plotly'], function(Plotly) {\n",
       "            window._Plotly = Plotly;\n",
       "        });\n",
       "        }\n",
       "        </script>\n",
       "        "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "import matplotlib.pyplot as plt \n",
    "import seaborn as sns \n",
    "import plotly as py\n",
    "import plotly.graph_objs as go\n",
    "from sklearn.cluster import KMeans\n",
    "import warnings\n",
    "import os\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "py.offline.init_notebook_mode(connected = True)\n",
    "#print(os.listdir(\"../input\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "5b838f778af8cb2740c6d54e5bb6608223ab54f4"
   },
   "source": [
    "## Data Exploration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
    "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a",
    "execution": {
     "iopub.execute_input": "2021-07-06T12:23:14.060751Z",
     "iopub.status.busy": "2021-07-06T12:23:14.060257Z",
     "iopub.status.idle": "2021-07-06T12:23:14.106025Z",
     "shell.execute_reply": "2021-07-06T12:23:14.105464Z",
     "shell.execute_reply.started": "2021-07-06T12:23:14.060689Z"
    },
    "scrolled": true
   },
   "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>CustomerID</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Age</th>\n",
       "      <th>Annual Income (k$)</th>\n",
       "      <th>Spending Score (1-100)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Male</td>\n",
       "      <td>19</td>\n",
       "      <td>15</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Male</td>\n",
       "      <td>21</td>\n",
       "      <td>15</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Female</td>\n",
       "      <td>20</td>\n",
       "      <td>16</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Female</td>\n",
       "      <td>23</td>\n",
       "      <td>16</td>\n",
       "      <td>77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Female</td>\n",
       "      <td>31</td>\n",
       "      <td>17</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   CustomerID          ...           Spending Score (1-100)\n",
       "0           1          ...                               39\n",
       "1           2          ...                               81\n",
       "2           3          ...                                6\n",
       "3           4          ...                               77\n",
       "4           5          ...                               40\n",
       "\n",
       "[5 rows x 5 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(r'../input/Mall_Customers.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_uuid": "067b51b55e3daed31cfed664a1eacde23b932ec1"
   },
   "outputs": [],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_uuid": "0e352ff53fe883942afd06b2e830ff7fa7cfdabd"
   },
   "outputs": [],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "_uuid": "57e39b6ed035b3e1b9e3dda9efd273fe552f48a7",
    "execution": {
     "iopub.execute_input": "2021-07-06T12:23:21.096483Z",
     "iopub.status.busy": "2021-07-06T12:23:21.096105Z",
     "iopub.status.idle": "2021-07-06T12:23:21.103396Z",
     "shell.execute_reply": "2021-07-06T12:23:21.102286Z",
     "shell.execute_reply.started": "2021-07-06T12:23:21.096425Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CustomerID                 int64\n",
       "Gender                    object\n",
       "Age                        int64\n",
       "Annual Income (k$)         int64\n",
       "Spending Score (1-100)     int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "_uuid": "e57f14ca0de9ce6d08a1dd1fb6470707db3eb85a",
    "execution": {
     "iopub.execute_input": "2021-07-06T12:23:24.933566Z",
     "iopub.status.busy": "2021-07-06T12:23:24.933283Z",
     "iopub.status.idle": "2021-07-06T12:23:24.941337Z",
     "shell.execute_reply": "2021-07-06T12:23:24.940201Z",
     "shell.execute_reply.started": "2021-07-06T12:23:24.933519Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CustomerID                0\n",
       "Gender                    0\n",
       "Age                       0\n",
       "Annual Income (k$)        0\n",
       "Spending Score (1-100)    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "2459a03e13d3bae3d946715b17c151f0615b3dcf"
   },
   "source": [
    "## Data Visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "25f06dcc79fbf5f5b83d3dc7a337865aacb7657e",
    "execution": {
     "iopub.execute_input": "2021-07-06T12:23:28.234461Z",
     "iopub.status.busy": "2021-07-06T12:23:28.234162Z",
     "iopub.status.idle": "2021-07-06T12:23:28.239069Z",
     "shell.execute_reply": "2021-07-06T12:23:28.238195Z",
     "shell.execute_reply.started": "2021-07-06T12:23:28.234417Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.style.use('fivethirtyeight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "0ee494fa3103f3aead92791fd958ac8e97b7beed"
   },
   "source": [
    "### Histograms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "7e72632388acada5fd66e2323f0544af06cac54d",
    "execution": {
     "iopub.execute_input": "2021-07-06T12:23:35.100298Z",
     "iopub.status.busy": "2021-07-06T12:23:35.099978Z",
     "iopub.status.idle": "2021-07-06T12:23:35.808795Z",
     "shell.execute_reply": "2021-07-06T12:23:35.807991Z",
     "shell.execute_reply.started": "2021-07-06T12:23:35.100251Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 86268 characters]\n",
      "text/plain": [
       "<Figure size 1080x432 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(1 , figsize = (15 , 6))\n",
    "n = 0 \n",
    "for x in ['Age' , 'Annual Income (k$)' , 'Spending Score (1-100)']:\n",
    "    n += 1\n",
    "    plt.subplot(1 , 3 , n)\n",
    "    plt.subplots_adjust(hspace =0.5 , wspace = 0.5)\n",
    "    sns.distplot(df[x] , bins = 20)\n",
    "    plt.title('Distplot of {}'.format(x))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "f3b491e34170ac1ee8e670bf1292fb8e451d5053"
   },
   "source": [
    "### Count Plot of Gender"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "befa7a57e4e37f96c8644b24e6982f354e964864"
   },
   "outputs": [],
   "source": [
    "plt.figure(1 , figsize = (15 , 5))\n",
    "sns.countplot(y = 'Gender' , data = df)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "fcc79189184015bc626be9264fbd2836b2ebad8c"
   },
   "source": [
    "### Ploting the Relation between Age , Annual Income and Spending Score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "89e99c64c4e69e1141090185da444c8991e5ce8f"
   },
   "outputs": [],
   "source": [
    "plt.figure(1 , figsize = (15 , 7))\n",
    "n = 0 \n",
    "for x in ['Age' , 'Annual Income (k$)' , 'Spending Score (1-100)']:\n",
    "    for y in ['Age' , 'Annual Income (k$)' , 'Spending Score (1-100)']:\n",
    "        n += 1\n",
    "        plt.subplot(3 , 3 , n)\n",
    "        plt.subplots_adjust(hspace = 0.5 , wspace = 0.5)\n",
    "        sns.regplot(x = x , y = y , data = df)\n",
    "        plt.ylabel(y.split()[0]+' '+y.split()[1] if len(y.split()) > 1 else y )\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "18679353d562a05276b4ece94738f06b6b1e7d5b"
   },
   "outputs": [],
   "source": [
    "plt.figure(1 , figsize = (15 , 6))\n",
    "for gender in ['Male' , 'Female']:\n",
    "    plt.scatter(x = 'Age' , y = 'Annual Income (k$)' , data = df[df['Gender'] == gender] ,\n",
    "                s = 200 , alpha = 0.5 , label = gender)\n",
    "plt.xlabel('Age'), plt.ylabel('Annual Income (k$)') \n",
    "plt.title('Age vs Annual Income w.r.t Gender')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "0cc3399fa8aebfa774c7ba298ee998e323c6c86b"
   },
   "outputs": [],
   "source": [
    "plt.figure(1 , figsize = (15 , 6))\n",
    "for gender in ['Male' , 'Female']:\n",
    "    plt.scatter(x = 'Annual Income (k$)',y = 'Spending Score (1-100)' ,\n",
    "                data = df[df['Gender'] == gender] ,s = 200 , alpha = 0.5 , label = gender)\n",
    "plt.xlabel('Annual Income (k$)'), plt.ylabel('Spending Score (1-100)') \n",
    "plt.title('Annual Income vs Spending Score w.r.t Gender')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "9f55e102bd9a6ec847ec61fa67479f21cafa7fde"
   },
   "source": [
    "### Distribution of values in Age , Annual Income and Spending Score according to Gender"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "c0b99c70642918f21368920c3d3fb42207539eae"
   },
   "outputs": [],
   "source": [
    "plt.figure(1 , figsize = (15 , 7))\n",
    "n = 0 \n",
    "for cols in ['Age' , 'Annual Income (k$)' , 'Spending Score (1-100)']:\n",
    "    n += 1 \n",
    "    plt.subplot(1 , 3 , n)\n",
    "    plt.subplots_adjust(hspace = 0.5 , wspace = 0.5)\n",
    "    sns.violinplot(x = cols , y = 'Gender' , data = df , palette = 'vlag')\n",
    "    sns.swarmplot(x = cols , y = 'Gender' , data = df)\n",
    "    plt.ylabel('Gender' if n == 1 else '')\n",
    "    plt.title('Boxplots & Swarmplots' if n == 2 else '')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "ab264c94de58ad05158ad3bfd95a9fd35f4106de"
   },
   "source": [
    "## Clustering using K- means\n",
    "### 1.Segmentation using Age and Spending Score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_uuid": "4f97c1afeb25bf42b8535f92cfece17b83ab3f26"
   },
   "outputs": [],
   "source": [
    "'''Age and spending Score'''\n",
    "X1 = df[['Age' , 'Spending Score (1-100)']].iloc[: , :].values\n",
    "inertia = []\n",
    "for n in range(1 , 11):\n",
    "    algorithm = (KMeans(n_clusters = n ,init='k-means++', n_init = 10 ,max_iter=300, \n",
    "                        tol=0.0001,  random_state= 111  , algorithm='elkan') )\n",
    "    algorithm.fit(X1)\n",
    "    inertia.append(algorithm.inertia_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "d0f7c7370e95de02ad7deb7cd52aba9a6466b940"
   },
   "source": [
    "### Selecting N Clusters based in Inertia (Squared Distance between Centroids and data points, should be less)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "b13b40170b4cfb2c2261e1a229e7af366988f120"
   },
   "outputs": [],
   "source": [
    "plt.figure(1 , figsize = (15 ,6))\n",
    "plt.plot(np.arange(1 , 11) , inertia , 'o')\n",
    "plt.plot(np.arange(1 , 11) , inertia , '-' , alpha = 0.5)\n",
    "plt.xlabel('Number of Clusters') , plt.ylabel('Inertia')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_uuid": "e4f5806a8c9a5581292b4f16df6977d053f833b7"
   },
   "outputs": [],
   "source": [
    "algorithm = (KMeans(n_clusters = 4 ,init='k-means++', n_init = 10 ,max_iter=300, \n",
    "                        tol=0.0001,  random_state= 111  , algorithm='elkan') )\n",
    "algorithm.fit(X1)\n",
    "labels1 = algorithm.labels_\n",
    "centroids1 = algorithm.cluster_centers_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "5fc2c8fd38f10117e53e7007f25f6b9340847543"
   },
   "outputs": [],
   "source": [
    "h = 0.02\n",
    "x_min, x_max = X1[:, 0].min() - 1, X1[:, 0].max() + 1\n",
    "y_min, y_max = X1[:, 1].min() - 1, X1[:, 1].max() + 1\n",
    "xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))\n",
    "Z = algorithm.predict(np.c_[xx.ravel(), yy.ravel()]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "b1c20956ada339bc4deff7eb11c8cdd5dcf919b6"
   },
   "outputs": [],
   "source": [
    "plt.figure(1 , figsize = (15 , 7) )\n",
    "plt.clf()\n",
    "Z = Z.reshape(xx.shape)\n",
    "plt.imshow(Z , interpolation='nearest', \n",
    "           extent=(xx.min(), xx.max(), yy.min(), yy.max()),\n",
    "           cmap = plt.cm.Pastel2, aspect = 'auto', origin='lower')\n",
    "\n",
    "plt.scatter( x = 'Age' ,y = 'Spending Score (1-100)' , data = df , c = labels1 , \n",
    "            s = 200 )\n",
    "plt.scatter(x = centroids1[: , 0] , y =  centroids1[: , 1] , s = 300 , c = 'red' , alpha = 0.5)\n",
    "plt.ylabel('Spending Score (1-100)') , plt.xlabel('Age')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "74f9b70c7d0a5cd5e5efd75a884afed8f5ba6b55"
   },
   "source": [
    "### 2. Segmentation using  Annual Income and Spending Score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_uuid": "50d645b4b8de36eecfa983b319ade64277a44ef4"
   },
   "outputs": [],
   "source": [
    "'''Annual Income and spending Score'''\n",
    "X2 = df[['Annual Income (k$)' , 'Spending Score (1-100)']].iloc[: , :].values\n",
    "inertia = []\n",
    "for n in range(1 , 11):\n",
    "    algorithm = (KMeans(n_clusters = n ,init='k-means++', n_init = 10 ,max_iter=300, \n",
    "                        tol=0.0001,  random_state= 111  , algorithm='elkan') )\n",
    "    algorithm.fit(X2)\n",
    "    inertia.append(algorithm.inertia_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "706408f25f2bd360f416873bcf3c413f965dd6bf",
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "plt.figure(1 , figsize = (15 ,6))\n",
    "plt.plot(np.arange(1 , 11) , inertia , 'o')\n",
    "plt.plot(np.arange(1 , 11) , inertia , '-' , alpha = 0.5)\n",
    "plt.xlabel('Number of Clusters') , plt.ylabel('Inertia')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_uuid": "6f29f2ac514b0dd53cd6e2056ec40633377fce53"
   },
   "outputs": [],
   "source": [
    "algorithm = (KMeans(n_clusters = 5 ,init='k-means++', n_init = 10 ,max_iter=300, \n",
    "                        tol=0.0001,  random_state= 111  , algorithm='elkan') )\n",
    "algorithm.fit(X2)\n",
    "labels2 = algorithm.labels_\n",
    "centroids2 = algorithm.cluster_centers_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "985030ed6b53ea56ecb02458a3e585b076a7702d"
   },
   "outputs": [],
   "source": [
    "h = 0.02\n",
    "x_min, x_max = X2[:, 0].min() - 1, X2[:, 0].max() + 1\n",
    "y_min, y_max = X2[:, 1].min() - 1, X2[:, 1].max() + 1\n",
    "xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))\n",
    "Z2 = algorithm.predict(np.c_[xx.ravel(), yy.ravel()]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "331b3edad2d7a98eca26b2ddd5247f6c81b63584"
   },
   "outputs": [],
   "source": [
    "plt.figure(1 , figsize = (15 , 7) )\n",
    "plt.clf()\n",
    "Z2 = Z2.reshape(xx.shape)\n",
    "plt.imshow(Z2 , interpolation='nearest', \n",
    "           extent=(xx.min(), xx.max(), yy.min(), yy.max()),\n",
    "           cmap = plt.cm.Pastel2, aspect = 'auto', origin='lower')\n",
    "\n",
    "plt.scatter( x = 'Annual Income (k$)' ,y = 'Spending Score (1-100)' , data = df , c = labels2 , \n",
    "            s = 200 )\n",
    "plt.scatter(x = centroids2[: , 0] , y =  centroids2[: , 1] , s = 300 , c = 'red' , alpha = 0.5)\n",
    "plt.ylabel('Spending Score (1-100)') , plt.xlabel('Annual Income (k$)')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "0c5fb52572554d3c9c9600bee4f3e8afdbaf186f"
   },
   "source": [
    "### 3.Segmentation using Age , Annual Income and Spending Score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_uuid": "5796114de2c01278b7d5e5807abedaf62b97f348"
   },
   "outputs": [],
   "source": [
    "X3 = df[['Age' , 'Annual Income (k$)' ,'Spending Score (1-100)']].iloc[: , :].values\n",
    "inertia = []\n",
    "for n in range(1 , 11):\n",
    "    algorithm = (KMeans(n_clusters = n ,init='k-means++', n_init = 10 ,max_iter=300, \n",
    "                        tol=0.0001,  random_state= 111  , algorithm='elkan') )\n",
    "    algorithm.fit(X3)\n",
    "    inertia.append(algorithm.inertia_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "43149b8677b07db3e3666b3901b536c6f98e8cdd"
   },
   "outputs": [],
   "source": [
    "plt.figure(1 , figsize = (15 ,6))\n",
    "plt.plot(np.arange(1 , 11) , inertia , 'o')\n",
    "plt.plot(np.arange(1 , 11) , inertia , '-' , alpha = 0.5)\n",
    "plt.xlabel('Number of Clusters') , plt.ylabel('Inertia')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": false,
    "_uuid": "530b2a22467c0b040d515889f5351d9a47ca760a"
   },
   "outputs": [],
   "source": [
    "algorithm = (KMeans(n_clusters = 6 ,init='k-means++', n_init = 10 ,max_iter=300, \n",
    "                        tol=0.0001,  random_state= 111  , algorithm='elkan') )\n",
    "algorithm.fit(X3)\n",
    "labels3 = algorithm.labels_\n",
    "centroids3 = algorithm.cluster_centers_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_kg_hide-input": true,
    "_uuid": "426b9db9440eec0c170b214bf3d2611d9daf4954"
   },
   "outputs": [],
   "source": [
    "df['label3'] =  labels3\n",
    "trace1 = go.Scatter3d(\n",
    "    x= df['Age'],\n",
    "    y= df['Spending Score (1-100)'],\n",
    "    z= df['Annual Income (k$)'],\n",
    "    mode='markers',\n",
    "     marker=dict(\n",
    "        color = df['label3'], \n",
    "        size= 20,\n",
    "        line=dict(\n",
    "            color= df['label3'],\n",
    "            width= 12\n",
    "        ),\n",
    "        opacity=0.8\n",
    "     )\n",
    ")\n",
    "data = [trace1]\n",
    "layout = go.Layout(\n",
    "#     margin=dict(\n",
    "#         l=0,\n",
    "#         r=0,\n",
    "#         b=0,\n",
    "#         t=0\n",
    "#     )\n",
    "    title= 'Clusters',\n",
    "    scene = dict(\n",
    "            xaxis = dict(title  = 'Age'),\n",
    "            yaxis = dict(title  = 'Spending Score'),\n",
    "            zaxis = dict(title  = 'Annual Income')\n",
    "        )\n",
    ")\n",
    "fig = go.Figure(data=data, layout=layout)\n",
    "py.offline.iplot(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "_uuid": "52ea58d41c7a58d870856abeed9173de9ac293e7"
   },
   "source": [
    "# If you liked my Work Please Upvote , Thank you."
   ]
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
