{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Copy of Crime Data Analysis.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VGU6jOdp0Mjc"
      },
      "source": [
        "# Data Preprocessing on the training data"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pUe5pzwE0VLq"
      },
      "source": [
        "## Importing Libraries"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZLMJ8F870Yjl"
      },
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import pandas as pd"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SiWlHYLd0i7i"
      },
      "source": [
        "## Importing the dataset"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gEZlQGXc0m0R"
      },
      "source": [
        "train_dataset = pd.read_csv('Crime Data_2001_2012.csv')\n",
        "test_dataset = pd.read_csv('Crime Data_2013.csv')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Y464hboJ9QcR"
      },
      "source": [
        "##Removing Total value Rows from the dataset"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jIQ8m74r2wkB",
        "outputId": "0c5ad6d4-cf6f-4084-a587-196ddefb11e8"
      },
      "source": [
        "total_vals = (train_dataset == 'TOTAL').any(axis=1)\n",
        "for i in reversed(range(len(total_vals))):\n",
        "  if total_vals[i]:\n",
        "    train_dataset.drop(train_dataset.index[i],inplace=True)\n",
        "train_dataset.reset_index(drop=True,inplace=True)\n",
        "print(train_dataset)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "            STATE/UT       DISTRICT  ...  OTHER IPC CRIMES  TOTAL IPC CRIMES\n",
            "0     ANDHRA PRADESH       ADILABAD  ...              1518              4154\n",
            "1     ANDHRA PRADESH      ANANTAPUR  ...               754              4125\n",
            "2     ANDHRA PRADESH       CHITTOOR  ...              1262              5818\n",
            "3     ANDHRA PRADESH       CUDDAPAH  ...              1181              3140\n",
            "4     ANDHRA PRADESH  EAST GODAVARI  ...              2313              6507\n",
            "...              ...            ...  ...               ...               ...\n",
            "8604        DELHI UT            STF  ...                 1                 6\n",
            "8605        DELHI UT           WEST  ...              1144              5558\n",
            "8606     LAKSHADWEEP    LAKSHADWEEP  ...                32                60\n",
            "8607      PUDUCHERRY       KARAIKAL  ...               392               787\n",
            "8608      PUDUCHERRY     PUDUCHERRY  ...              1668              3494\n",
            "\n",
            "[8609 rows x 33 columns]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "SIdevup47XNG",
        "outputId": "4faea40e-1a4d-4939-bb4a-65e2783e5ef5"
      },
      "source": [
        "total_vals = (test_dataset == 'ZZ TOTAL').any(axis=1)\n",
        "for i in reversed(range(len(total_vals))):\n",
        "  if total_vals[i]:\n",
        "    test_dataset.drop(test_dataset.index[i],inplace=True)\n",
        "test_dataset.reset_index(drop=True,inplace=True)\n",
        "print(test_dataset)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "           STATE/UT     DISTRICT  ...  OTHER IPC CRIMES  TOTAL IPC CRIMES\n",
            "0    Andhra Pradesh     ADILABAD  ...              1390              6381\n",
            "1    Andhra Pradesh    ANANTAPUR  ...              1634              6913\n",
            "2    Andhra Pradesh     CHITTOOR  ...              2239              5610\n",
            "3    Andhra Pradesh     CUDDAPAH  ...              1741              7048\n",
            "4    Andhra Pradesh    CYBERABAD  ...              3139             19992\n",
            "..              ...          ...  ...               ...               ...\n",
            "783        Delhi UT          STF  ...                 4                15\n",
            "784        Delhi UT         WEST  ...              2393              8813\n",
            "785     Lakshadweep  LAKSHADWEEP  ...                18                40\n",
            "786      Puducherry     KARAIKAL  ...               329               643\n",
            "787      Puducherry   PUDUCHERRY  ...              1464              3113\n",
            "\n",
            "[788 rows x 33 columns]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "T4LeCI5S9b9B"
      },
      "source": [
        "##Extracting useful data such as districts, year and crime features. While removing total crimes per district column"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fpnogFqz17ot"
      },
      "source": [
        "x_train_vals = train_dataset.iloc[:,1:-1].values\n",
        "x_test_vals = test_dataset.iloc[:,1:-1].values"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sTIJuaBB089R",
        "outputId": "138eb1f4-e45a-406d-ff13-232c3d8efbee"
      },
      "source": [
        "print(x_train_vals)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[['ADILABAD' 2001 101 ... 0 181 1518]\n",
            " ['ANANTAPUR' 2001 151 ... 0 270 754]\n",
            " ['CHITTOOR' 2001 101 ... 0 404 1262]\n",
            " ...\n",
            " ['LAKSHADWEEP' 2012 0 ... 0 0 32]\n",
            " ['KARAIKAL' 2012 5 ... 0 44 392]\n",
            " ['PUDUCHERRY' 2012 24 ... 0 219 1668]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fIeFzddQTlMt",
        "outputId": "bd9442f9-0a91-474b-ec01-bfbf73e8c476"
      },
      "source": [
        "print(x_test_vals)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[['ADILABAD' 2013 96 ... 0 376 1390]\n",
            " ['ANANTAPUR' 2013 156 ... 0 573 1634]\n",
            " ['CHITTOOR' 2013 72 ... 0 546 2239]\n",
            " ...\n",
            " ['LAKSHADWEEP' 2013 0 ... 0 0 18]\n",
            " ['KARAIKAL' 2013 6 ... 0 32 329]\n",
            " ['PUDUCHERRY' 2013 25 ... 0 206 1464]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VSzvGibDigTn"
      },
      "source": [
        "##Keeping copy of this data for future alterations"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "70fF1f68qi0L"
      },
      "source": [
        "X_train = x_train_vals\n",
        "X_test = x_test_vals"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8Atup1JRimjj"
      },
      "source": [
        "#Applying Kmeans Algorithm "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "A8uZp1qqpURY",
        "outputId": "d1c957c0-8e6d-4b03-a2dc-5cf895fe5bac"
      },
      "source": [
        "print(X_train)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[['ADILABAD' 2001 1.15834478099478 ... -0.07243098488497635\n",
            "  0.406186562461889 0.21914665054412608]\n",
            " ['ANANTAPUR' 2001 2.240488258804757 ... -0.07243098488497635\n",
            "  0.9732153426377105 -0.2568454995502764]\n",
            " ['CHITTOOR' 2001 1.15834478099478 ... -0.07243098488497635\n",
            "  1.826944067846251 0.05965189868003309]\n",
            " ...\n",
            " ['LAKSHADWEEP' 2012 -1.0275850441813743 ... -0.07243098488497635\n",
            "  -0.7469843275586022 -0.7066705419169762]\n",
            " ['KARAIKAL' 2012 -0.9193706964003766 ... -0.07243098488497635\n",
            "  -0.46665549241549936 -0.48238104710809543]\n",
            " ['PUDUCHERRY' 2012 -0.5081561748325852 ... -0.07243098488497635\n",
            "  0.6482887382672959 0.3126006067144931]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vIraeXNfpxAe",
        "outputId": "bd249597-b004-47a6-a1b2-f5d270ace829"
      },
      "source": [
        "print(X_test)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[['ADILABAD' 2013 1.0501304332137822 ... -0.07243098488497635\n",
            "  1.6485529909370038 0.13939927461207957]\n",
            " ['ANANTAPUR' 2013 2.348702606585755 ... -0.07243098488497635\n",
            "  2.9036616391913506 0.2914177099825432]\n",
            " ['CHITTOOR' 2013 0.530701563864993 ... -0.07243098488497635\n",
            "  2.7316416721717194 0.6683486665363567]\n",
            " ...\n",
            " ['LAKSHADWEEP' 2013 -1.0275850441813743 ... -0.07243098488497635\n",
            "  -0.7469843275586022 -0.7153929111595437]\n",
            " ['KARAIKAL' 2013 -0.8977278268441771 ... -0.07243098488497635\n",
            "  -0.543108811090891 -0.5216317086996496]\n",
            " ['PUDUCHERRY' 2013 -0.4865133052763857 ... -0.07243098488497635\n",
            "  0.5654643097022883 0.18550322632279395]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "21aeDAvuitwm"
      },
      "source": [
        "###Applying Feature Scaling to the 29 features"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "W4pA6u0Lp86L"
      },
      "source": [
        "sc = StandardScaler()\n",
        "X_train[:,2:] = sc.fit_transform(X_train[:,2:])\n",
        "X_test[:,2:] = sc.transform(X_test[:,2:])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "e0iFDpVgwqxq",
        "outputId": "2a2183b7-97d8-4f74-f5e6-e2275789d54b"
      },
      "source": [
        "print(X_train)\n",
        "print(X_test)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[['ADILABAD' 2001 1.15834478099478 ... -0.07243098488497636\n",
            "  0.406186562461889 0.21914665054412608]\n",
            " ['ANANTAPUR' 2001 2.240488258804757 ... -0.07243098488497636\n",
            "  0.9732153426377105 -0.2568454995502764]\n",
            " ['CHITTOOR' 2001 1.15834478099478 ... -0.07243098488497636\n",
            "  1.826944067846251 0.05965189868003309]\n",
            " ...\n",
            " ['LAKSHADWEEP' 2012 -1.0275850441813743 ... -0.07243098488497636\n",
            "  -0.7469843275586022 -0.7066705419169762]\n",
            " ['KARAIKAL' 2012 -0.9193706964003767 ... -0.07243098488497636\n",
            "  -0.46665549241549936 -0.48238104710809543]\n",
            " ['PUDUCHERRY' 2012 -0.5081561748325854 ... -0.07243098488497636\n",
            "  0.6482887382672959 0.3126006067144931]]\n",
            "[['ADILABAD' 2013 1.0501304332137822 ... -0.07243098488497636\n",
            "  1.6485529909370038 0.13939927461207957]\n",
            " ['ANANTAPUR' 2013 2.348702606585755 ... -0.07243098488497636\n",
            "  2.9036616391913506 0.2914177099825432]\n",
            " ['CHITTOOR' 2013 0.5307015638649929 ... -0.07243098488497636\n",
            "  2.7316416721717194 0.6683486665363567]\n",
            " ...\n",
            " ['LAKSHADWEEP' 2013 -1.0275850441813743 ... -0.07243098488497636\n",
            "  -0.7469843275586022 -0.7153929111595437]\n",
            " ['KARAIKAL' 2013 -0.8977278268441772 ... -0.07243098488497636\n",
            "  -0.543108811090891 -0.5216317086996496]\n",
            " ['PUDUCHERRY' 2013 -0.48651330527638575 ... -0.07243098488497636\n",
            "  0.5654643097022883 0.18550322632279395]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3r_KHyAbi0FI"
      },
      "source": [
        "###Keeping the necessary fields, i.e. the features (29 types of crimes)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LBxIBGT4rq6S"
      },
      "source": [
        "train = np.delete(X_train,0,axis=1)\n",
        "test = np.delete(X_test,0,axis=1)\n",
        "train = np.delete(train,0,axis=1)\n",
        "test = np.delete(test,0,axis=1)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QTc8DbgJsK2x",
        "outputId": "c8630f37-b63b-453b-d2ff-c0c730bae97e"
      },
      "source": [
        "print(train)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[[1.15834478099478 0.3507242547900932 1.1408812243500706 ...\n",
            "  -0.07243098488497636 0.406186562461889 0.21914665054412608]\n",
            " [2.240488258804757 1.5828478068707175 -0.41727638022354047 ...\n",
            "  -0.07243098488497636 0.9732153426377105 -0.2568454995502764]\n",
            " [1.15834478099478 0.29385701392483365 -0.31989152993768977 ...\n",
            "  -0.07243098488497636 1.826944067846251 0.05965189868003309]\n",
            " ...\n",
            " [-1.0275850441813743 -0.7866205625150985 -0.5146612305093913 ...\n",
            "  -0.07243098488497636 -0.7469843275586022 -0.7066705419169762]\n",
            " [-0.9193706964003767 -0.6728860807845793 -0.31989152993768977 ...\n",
            "  -0.07243098488497636 -0.46665549241549936 -0.48238104710809543]\n",
            " [-0.5081561748325854 -0.3885498764582813 0.4591872723491157 ...\n",
            "  -0.07243098488497636 0.6482887382672959 0.3126006067144931]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "h8KAVz4XsMGC",
        "outputId": "b851ada4-5c1e-4883-9e86-12f45db9f79d"
      },
      "source": [
        "print(test)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[[1.0501304332137822 0.5781932182511316 0.7513418232066678 ...\n",
            "  -0.07243098488497636 1.6485529909370038 0.13939927461207957]\n",
            " [2.348702606585755 2.037785733792794 -0.22250667965183912 ...\n",
            "  -0.07243098488497636 2.9036616391913506 0.2914177099825432]\n",
            " [0.5307015638649929 0.3696800017451798 -0.31989152993768977 ...\n",
            "  -0.07243098488497636 2.7316416721717194 0.6683486665363567]\n",
            " ...\n",
            " [-1.0275850441813743 -0.7866205625150985 -0.5146612305093913 ...\n",
            "  -0.07243098488497636 -0.7469843275586022 -0.7153929111595437]\n",
            " [-0.8977278268441772 -0.7297533216498389 -0.5146612305093913 ...\n",
            "  -0.07243098488497636 -0.543108811090891 -0.5216317086996496]\n",
            " [-0.48651330527638575 -0.6349745868744062 -0.31989152993768977 ...\n",
            "  -0.07243098488497636 0.5654643097022883 0.18550322632279395]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0GY44Bxxi-uD"
      },
      "source": [
        "###Finding the number of cluster using the Elbow Method"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 313
        },
        "id": "Ku1SUcDprSEu",
        "outputId": "790efae8-7c41-4503-dc9a-b8191c010089"
      },
      "source": [
        "from sklearn.cluster import KMeans\n",
        "wcss = []\n",
        "for i in range(1,11):\n",
        "  Kmeans = KMeans(n_clusters = i, init='k-means++', random_state=42)\n",
        "  Kmeans.fit(train)\n",
        "  wcss.append(Kmeans.inertia_)\n",
        "plt.plot(range(1,11),wcss)\n",
        "plt.title('The Elbow Method')\n",
        "plt.xlabel('Number of Clusters')\n",
        "plt.ylabel('WCSS')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Text(0, 0.5, 'WCSS')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 42
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 17228 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YzqhEft-jHnr"
      },
      "source": [
        "###Finding the number of clusters using Silhouette Coefficient"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Y9NMuhvYtXD4",
        "outputId": "555621cf-e5ac-4015-b40f-d455435d8e1b"
      },
      "source": [
        "from sklearn.metrics import silhouette_score\n",
        "  \n",
        "for n_cluster in range(2, 11):\n",
        "    Kmeans1 = KMeans(n_clusters=n_cluster).fit(train)\n",
        "    label = Kmeans1.labels_\n",
        "    sil_coeff = silhouette_score(train, label, metric='euclidean')\n",
        "    print(\"For n_clusters={}, The Silhouette Coefficient is {}\".format(n_cluster, sil_coeff))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "For n_clusters=2, The Silhouette Coefficient is 0.8744789604109744\n",
            "For n_clusters=3, The Silhouette Coefficient is 0.44366754781514894\n",
            "For n_clusters=4, The Silhouette Coefficient is 0.3690598781147069\n",
            "For n_clusters=5, The Silhouette Coefficient is 0.3131045231709387\n",
            "For n_clusters=6, The Silhouette Coefficient is 0.31862331438749003\n",
            "For n_clusters=7, The Silhouette Coefficient is 0.30951065665330196\n",
            "For n_clusters=8, The Silhouette Coefficient is 0.18303696098695285\n",
            "For n_clusters=9, The Silhouette Coefficient is 0.1769354660713655\n",
            "For n_clusters=10, The Silhouette Coefficient is 0.18864688782716985\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kbUhKqI0jOvl"
      },
      "source": [
        "###Applying the Kmeans Algorithm for 5 clusters on train data"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9NrVl-VXtiCD"
      },
      "source": [
        "Kmeans = KMeans(n_clusters = 5, init='k-means++', random_state=42)\n",
        "Y_kmeans = Kmeans.fit_predict(train)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VLZcSl5ptpKy",
        "outputId": "caceea27-b693-424c-8d09-4ac93fbad0a7"
      },
      "source": [
        "print(Y_kmeans)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[0 0 0 ... 1 1 1]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Xogg2qxFjVAB"
      },
      "source": [
        "###Joining the Districts and Clusters together for reference"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0YFfq6AItzZF"
      },
      "source": [
        "a = pd.DataFrame(X_train)\n",
        "b = pd.DataFrame(Y_kmeans)\n",
        "trainset = pd.concat([a.iloc[:,0], b], ignore_index=True, axis = 1)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GtJCx31Ju0iN",
        "outputId": "518d7c73-6f88-40c4-842c-3f48a80596b7"
      },
      "source": [
        "print(trainset)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "                  0  1\n",
            "0          ADILABAD  0\n",
            "1         ANANTAPUR  0\n",
            "2          CHITTOOR  0\n",
            "3          CUDDAPAH  0\n",
            "4     EAST GODAVARI  0\n",
            "...             ... ..\n",
            "8604            STF  1\n",
            "8605           WEST  0\n",
            "8606    LAKSHADWEEP  1\n",
            "8607       KARAIKAL  1\n",
            "8608     PUDUCHERRY  1\n",
            "\n",
            "[8609 rows x 2 columns]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "B7EX_Lwjje1z"
      },
      "source": [
        "###Plotting the results for train data"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 296
        },
        "id": "yGiZFWIXxhNu",
        "outputId": "4899b320-f790-4fcd-ca48-0c1efe94dcc3"
      },
      "source": [
        "plt.bar(trainset[0], trainset[1])\n",
        "plt.title('District Vs Cluster')\n",
        "plt.xlabel('District')\n",
        "plt.ylabel('Cluster')\n",
        "plt.show()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 9672 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SlEaOu9PjoIw"
      },
      "source": [
        "###Applying Kmeans onto test data"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7ZLcI5eHypSZ"
      },
      "source": [
        "Kmeans_test = KMeans(n_clusters = 5, init='k-means', random_state=42)\n",
        "Y_kmeans_test = Kmeans.fit_predict(test)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yuQNPK21jwLl"
      },
      "source": [
        "###Joining the districts and clusters for test data"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2KYSmmu3yxAH"
      },
      "source": [
        "a = pd.DataFrame(X_test)\n",
        "b = pd.DataFrame(Y_kmeans_test)\n",
        "testset = pd.concat([a.iloc[:,0], b], ignore_index=True, axis = 1)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "MdgRgSSKy4oc",
        "outputId": "0a9505b6-b052-4e32-e019-c796749f27a1"
      },
      "source": [
        "print(testset)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "               0  1\n",
            "0       ADILABAD  3\n",
            "1      ANANTAPUR  3\n",
            "2       CHITTOOR  3\n",
            "3       CUDDAPAH  3\n",
            "4      CYBERABAD  2\n",
            "..           ... ..\n",
            "783          STF  0\n",
            "784         WEST  2\n",
            "785  LAKSHADWEEP  0\n",
            "786     KARAIKAL  0\n",
            "787   PUDUCHERRY  0\n",
            "\n",
            "[788 rows x 2 columns]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "NgydrQXAj3c8"
      },
      "source": [
        "###Visualizing results for test data"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 296
        },
        "id": "0iLaM4Qqy8cl",
        "outputId": "f35f2656-2078-4a4e-c91a-14f7c0939c02"
      },
      "source": [
        "plt.bar(testset[0], testset[1])\n",
        "plt.title('District Vs Cluster')\n",
        "plt.xlabel('District')\n",
        "plt.ylabel('Cluster')\n",
        "plt.show()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 9576 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yeRgzA8Ukbs1"
      },
      "source": [
        "##These results are not adequate. The initial intuition to compare the cluster centroids would not be very helpful. This is because the data  is not forming optimal clusters. We will now try to preprocess the data better."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ftC56Phj9--1"
      },
      "source": [
        "##Applying Feature Scaling to original data again"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "W-UCD7ezAJG2"
      },
      "source": [
        "from sklearn.preprocessing import StandardScaler\n",
        "sc = StandardScaler()\n",
        "x_train_vals[:,2:] = sc.fit_transform(x_train_vals[:,2:])\n",
        "x_test_vals[:,2:] = sc.transform(x_test_vals[:,2:])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 263
        },
        "id": "6QSiFCJ6s91F",
        "outputId": "09166bb9-f450-4fc1-b125-dc6e1aa9ef5a"
      },
      "source": [
        "display(x_train_vals)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "array([['ADILABAD', 2001, 1.15834478099478, ..., -0.07243098488497635,\n",
              "        0.406186562461889, 0.21914665054412608],\n",
              "       ['ANANTAPUR', 2001, 2.240488258804757, ..., -0.07243098488497635,\n",
              "        0.9732153426377105, -0.2568454995502764],\n",
              "       ['CHITTOOR', 2001, 1.15834478099478, ..., -0.07243098488497635,\n",
              "        1.826944067846251, 0.05965189868003309],\n",
              "       ...,\n",
              "       ['LAKSHADWEEP', 2012, -1.0275850441813743, ...,\n",
              "        -0.07243098488497635, -0.7469843275586022, -0.7066705419169762],\n",
              "       ['KARAIKAL', 2012, -0.9193706964003766, ..., -0.07243098488497635,\n",
              "        -0.46665549241549936, -0.48238104710809543],\n",
              "       ['PUDUCHERRY', 2012, -0.5081561748325852, ...,\n",
              "        -0.07243098488497635, 0.6482887382672959, 0.3126006067144931]],\n",
              "      dtype=object)"
            ]
          },
          "metadata": {
            "tags": []
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gMLbCDXktA_b",
        "outputId": "aaf9ab4a-9e05-46f4-c917-fedb5ea3389d"
      },
      "source": [
        "print(x_test_vals)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[['ADILABAD' 2013 1.0501304332137822 ... -0.07243098488497635\n",
            "  1.6485529909370038 0.13939927461207957]\n",
            " ['ANANTAPUR' 2013 2.348702606585755 ... -0.07243098488497635\n",
            "  2.9036616391913506 0.2914177099825432]\n",
            " ['CHITTOOR' 2013 0.530701563864993 ... -0.07243098488497635\n",
            "  2.7316416721717194 0.6683486665363567]\n",
            " ...\n",
            " ['LAKSHADWEEP' 2013 -1.0275850441813743 ... -0.07243098488497635\n",
            "  -0.7469843275586022 -0.7153929111595437]\n",
            " ['KARAIKAL' 2013 -0.8977278268441771 ... -0.07243098488497635\n",
            "  -0.543108811090891 -0.5216317086996496]\n",
            " ['PUDUCHERRY' 2013 -0.4865133052763857 ... -0.07243098488497635\n",
            "  0.5654643097022883 0.18550322632279395]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SntW9ceZknoG"
      },
      "source": [
        "#We now try to reduce the dimensions of the features. We apply Principal Component analysis to reduce the number of features to 2. These will be the 2 best projections of the 29 features on a principal dimension. This will also help us to visualize the results better."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Ye_EgtQAlTJL"
      },
      "source": [
        "#Applying Kmeans after PCA "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8XmE-_bOlXts"
      },
      "source": [
        "##Applying PCA"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rwW3DXIRtCnI"
      },
      "source": [
        "from sklearn.decomposition import PCA\n",
        "pca = PCA(n_components = 2)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4kgn7ftKlbm-"
      },
      "source": [
        "##Merging the data by dropping the original features, while concatenating the 2 new principal features."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9WyM3kJ0kWxu"
      },
      "source": [
        "pca_train_result = pca.fit_transform(x_train_vals[:,2:])\n",
        "x_train = pd.DataFrame(x_train_vals)\n",
        "pca_train = pd.DataFrame(pca_train_result)\n",
        "x_train.drop(x_train.iloc[:,2:], inplace = True, axis = 1) \n",
        "x_trainset = pd.concat([x_train, pca_train], ignore_index=True, axis = 1)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "i8kSD9tjuGNt",
        "outputId": "883e45a6-13f4-45ea-d527-a69b5b1723ea"
      },
      "source": [
        "print(x_trainset)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "                  0     1         2         3\n",
            "0          ADILABAD  2001  1.428720 -0.785335\n",
            "1         ANANTAPUR  2001  1.513024 -2.004361\n",
            "2          CHITTOOR  2001  2.193210 -2.650716\n",
            "3          CUDDAPAH  2001 -0.024711 -1.138970\n",
            "4     EAST GODAVARI  2001  2.279901 -2.273365\n",
            "...             ...   ...       ...       ...\n",
            "8604            STF  2012 -2.857952  0.496852\n",
            "8605           WEST  2012  5.198004  4.855982\n",
            "8606    LAKSHADWEEP  2012 -2.872274  0.489674\n",
            "8607       KARAIKAL  2012 -2.437590  0.327408\n",
            "8608     PUDUCHERRY  2012 -0.729543 -0.375557\n",
            "\n",
            "[8609 rows x 4 columns]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "B0tki0UKkV0O"
      },
      "source": [
        "pca_test_result = pca.transform(x_test_vals[:,2:])\n",
        "x_test = pd.DataFrame(x_test_vals)\n",
        "pca_test = pd.DataFrame(pca_test_result)\n",
        "x_test.drop(x_test.iloc[:,2:], inplace = True, axis = 1)\n",
        "x_testset = pd.concat([x_test, pca_test], ignore_index=True, axis = 1)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BjHY_0XpuclS",
        "outputId": "6b09beec-2a69-4f3b-90fb-4ed478581bb7"
      },
      "source": [
        "print(x_testset)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "               0     1          2         3\n",
            "0       ADILABAD  2013   3.422947 -2.747885\n",
            "1      ANANTAPUR  2013   3.688475 -2.866613\n",
            "2       CHITTOOR  2013   1.783107 -1.790103\n",
            "3       CUDDAPAH  2013   3.601937 -3.637887\n",
            "4      CYBERABAD  2013  16.820744 -7.324309\n",
            "..           ...   ...        ...       ...\n",
            "783          STF  2013  -2.785471  0.433639\n",
            "784         WEST  2013  10.421459  6.033382\n",
            "785  LAKSHADWEEP  2013  -2.859908  0.528613\n",
            "786     KARAIKAL  2013  -2.523605  0.365329\n",
            "787   PUDUCHERRY  2013  -0.900950 -0.175523\n",
            "\n",
            "[788 rows x 4 columns]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tHs8xlrqlwJy"
      },
      "source": [
        "##Extracting the features to apply Kmeans"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mboVIuVptNIk"
      },
      "source": [
        "k_train = x_trainset.iloc[:,2:].values\n",
        "k_test = x_testset.iloc[:,2:].values"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pn-RRs0Vu6JE",
        "outputId": "8cf48214-fd0c-4175-e03e-d1f24d9d8fdc"
      },
      "source": [
        "print(k_train)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[[ 1.42871999 -0.78533508]\n",
            " [ 1.51302397 -2.00436132]\n",
            " [ 2.19321035 -2.6507163 ]\n",
            " ...\n",
            " [-2.87227417  0.48967384]\n",
            " [-2.43758956  0.32740834]\n",
            " [-0.72954313 -0.37555689]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "8Gogl5hGu54U",
        "outputId": "22a35424-6d86-46f9-feac-37ca0ba396b3"
      },
      "source": [
        "print(k_test)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[[ 3.42294738 -2.74788518]\n",
            " [ 3.68847469 -2.86661272]\n",
            " [ 1.78310673 -1.79010326]\n",
            " ...\n",
            " [-2.85990758  0.52861282]\n",
            " [-2.52360548  0.36532886]\n",
            " [-0.90095024 -0.175523  ]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "n08X7DwDl1iz"
      },
      "source": [
        "##Applying the Kmeans"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "svXhKDZtmBJR"
      },
      "source": [
        "###Using Silhouette Coefficient to find number of optimal Clusters"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yjMXo1ysDA_Y",
        "outputId": "9b1475fa-c787-4700-a76a-becc335e8922"
      },
      "source": [
        "from sklearn.metrics import silhouette_score\n",
        "  \n",
        "for n_cluster in range(2, 11):\n",
        "    kmeans1 = KMeans(n_clusters=n_cluster).fit(k_train)\n",
        "    label = kmeans1.labels_\n",
        "    sil_coeff = silhouette_score(k_train, label, metric='euclidean')\n",
        "    print(\"For n_clusters={}, The Silhouette Coefficient is {}\".format(n_cluster, sil_coeff))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "For n_clusters=2, The Silhouette Coefficient is 0.8400897080419875\n",
            "For n_clusters=3, The Silhouette Coefficient is 0.5967175127312884\n",
            "For n_clusters=4, The Silhouette Coefficient is 0.53189118653319\n",
            "For n_clusters=5, The Silhouette Coefficient is 0.443216850901647\n",
            "For n_clusters=6, The Silhouette Coefficient is 0.43148804372418736\n",
            "For n_clusters=7, The Silhouette Coefficient is 0.45490812654495116\n",
            "For n_clusters=8, The Silhouette Coefficient is 0.4189107804954833\n",
            "For n_clusters=9, The Silhouette Coefficient is 0.42272323685262275\n",
            "For n_clusters=10, The Silhouette Coefficient is 0.43379173660819537\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "k71svbR7l8B5"
      },
      "source": [
        "###Using the Elbow method to find number of optimal clusters"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 313
        },
        "id": "RNy-EKRU8--w",
        "outputId": "df08e731-daee-43c5-b919-429c76ad7200"
      },
      "source": [
        "from sklearn.cluster import KMeans\n",
        "wcss = []\n",
        "for i in range(1,11):\n",
        "  kmeans = KMeans(n_clusters = i, init='k-means++', random_state=42)\n",
        "  kmeans.fit(k_train)\n",
        "  wcss.append(kmeans.inertia_)\n",
        "plt.plot(range(1,11),wcss)\n",
        "plt.title('The Elbow Method')\n",
        "plt.xlabel('Number of Clusters')\n",
        "plt.ylabel('WCSS')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Text(0, 0.5, 'WCSS')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 20
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 15856 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bQIybK3LmH3A"
      },
      "source": [
        "###Applying 2 Cluster Kmeans as suggested by Silhouette Coeff Method"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "17h1oob8mObn"
      },
      "source": [
        "kmeans3 = KMeans(n_clusters = 2, init='k-means++', random_state=42)\n",
        "y_kmeans1 = kmeans3.fit_predict(k_train)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "gXCuZeEdmR2m",
        "outputId": "351dd23d-613b-4e0e-ef9c-c4d4e370dd28"
      },
      "source": [
        "print(y_kmeans1)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[0 0 0 ... 0 0 0]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 295
        },
        "id": "jde-6fSymS3G",
        "outputId": "25eff6d8-db5f-4e12-9147-c9989a37e8de"
      },
      "source": [
        "plt.scatter(k_train[y_kmeans1 == 0, 0],k_train[y_kmeans1 == 0, 1], s = 10, c = 'red', label =  'Cluster 1')\n",
        "plt.scatter(k_train[y_kmeans1 == 1, 0],k_train[y_kmeans1 == 1, 1], s = 10, c = 'blue', label =  'Cluster 2')\n",
        "plt.scatter(kmeans3.cluster_centers_[:, 0], kmeans3.cluster_centers_[:, 1], s = 300, c = 'yellow', label = 'Centroids')\n",
        "plt.title('Clusters of Customers')\n",
        "plt.xlabel('Feature 1')\n",
        "plt.ylabel('Feature 2')\n",
        "plt.legend()\n",
        "plt.show()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 17260 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FnSbYifdmTnM"
      },
      "source": [
        "###This gives us some outliers. For this Outlier Detection, we increase the number of clusters to 4 so that the algorithm can identify the outliers as 1 cluster."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "j1NG8dpoDfVo"
      },
      "source": [
        "kmeans2 = KMeans(n_clusters = 4, init='k-means++', random_state=42)\n",
        "y_kmeans = kmeans2.fit_predict(k_train)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "n2mnee4RDzVp",
        "outputId": "cb7892c1-ce65-42af-e6a3-62676a107971"
      },
      "source": [
        "print(y_kmeans)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[0 0 0 ... 3 3 3]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 295
        },
        "id": "pYpzD2ZZEGMi",
        "outputId": "ace03d2f-aa4e-497c-91fe-2f72a0e1e92b"
      },
      "source": [
        "plt.scatter(k_train[y_kmeans == 0, 0],k_train[y_kmeans == 0, 1], s = 10, c = 'red', label =  'Cluster 1')\n",
        "plt.scatter(k_train[y_kmeans == 1, 0],k_train[y_kmeans == 1, 1], s = 10, c = 'blue', label =  'Cluster 2')\n",
        "plt.scatter(k_train[y_kmeans == 2, 0],k_train[y_kmeans == 2, 1], s = 10, c = 'green', label =  'Cluster 3')\n",
        "plt.scatter(k_train[y_kmeans == 3, 0],k_train[y_kmeans == 3, 1], s = 10, c = 'cyan', label =  'Cluster 4')\n",
        "plt.scatter(kmeans2.cluster_centers_[:, 0], kmeans2.cluster_centers_[:, 1], s = 300, c = 'yellow', label = 'Centroids')\n",
        "plt.title('Clusters of Customers')\n",
        "plt.xlabel('Feature 1')\n",
        "plt.ylabel('Feature 2')\n",
        "plt.legend()\n",
        "plt.show()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 18560 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xcDzCpGumiRj"
      },
      "source": [
        "###Now that the outliers are identified, we remove them by first adding the clusters to the original data. We do this so the respective districts and data gets removed"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "COmiXXW38UCv"
      },
      "source": [
        "y = pd.DataFrame(y_kmeans)\n",
        "x_result = pd.concat([x_trainset, y], ignore_index=True, axis = 1)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fJKq2ZJ_82hg",
        "outputId": "57fbc442-0830-4229-f59a-1bd3548cdfd3"
      },
      "source": [
        "print(x_result)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "                  0     1         2         3  4\n",
            "0          ADILABAD  2001  1.428720 -0.785335  0\n",
            "1         ANANTAPUR  2001  1.513024 -2.004361  0\n",
            "2          CHITTOOR  2001  2.193210 -2.650716  0\n",
            "3          CUDDAPAH  2001 -0.024711 -1.138970  3\n",
            "4     EAST GODAVARI  2001  2.279901 -2.273365  0\n",
            "...             ...   ...       ...       ... ..\n",
            "8604            STF  2012 -2.857952  0.496852  3\n",
            "8605           WEST  2012  5.198004  4.855982  0\n",
            "8606    LAKSHADWEEP  2012 -2.872274  0.489674  3\n",
            "8607       KARAIKAL  2012 -2.437590  0.327408  3\n",
            "8608     PUDUCHERRY  2012 -0.729543 -0.375557  3\n",
            "\n",
            "[8609 rows x 5 columns]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "k7pwTUm69A_T",
        "outputId": "0cde600a-0dbf-46c1-ac0f-00c9d67ac458"
      },
      "source": [
        "outliers = (x_result == 2).any(axis=1)\n",
        "for i in reversed(range(len(y_kmeans))):\n",
        "  if outliers[i]:\n",
        "    x_result.drop(x_result.index[i],inplace=True)\n",
        "x_result.reset_index(drop=True,inplace=True)\n",
        "print(x_result)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "                  0     1         2         3  4\n",
            "0          ADILABAD  2001  1.428720 -0.785335  0\n",
            "1         ANANTAPUR  2001  1.513024 -2.004361  0\n",
            "2          CHITTOOR  2001  2.193210 -2.650716  0\n",
            "3          CUDDAPAH  2001 -0.024711 -1.138970  3\n",
            "4     EAST GODAVARI  2001  2.279901 -2.273365  0\n",
            "...             ...   ...       ...       ... ..\n",
            "8592            STF  2012 -2.857952  0.496852  3\n",
            "8593           WEST  2012  5.198004  4.855982  0\n",
            "8594    LAKSHADWEEP  2012 -2.872274  0.489674  3\n",
            "8595       KARAIKAL  2012 -2.437590  0.327408  3\n",
            "8596     PUDUCHERRY  2012 -0.729543 -0.375557  3\n",
            "\n",
            "[8597 rows x 5 columns]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jb7LAKoBm2Lv"
      },
      "source": [
        "###We again extract the features to apply Kmeans"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LuH4FJXT9fv1",
        "outputId": "18d8f166-b5e9-469f-a204-ebcd34b99a5c"
      },
      "source": [
        "new_train = x_result.iloc[:,2:4]\n",
        "print(new_train)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "             2         3\n",
            "0     1.428720 -0.785335\n",
            "1     1.513024 -2.004361\n",
            "2     2.193210 -2.650716\n",
            "3    -0.024711 -1.138970\n",
            "4     2.279901 -2.273365\n",
            "...        ...       ...\n",
            "8592 -2.857952  0.496852\n",
            "8593  5.198004  4.855982\n",
            "8594 -2.872274  0.489674\n",
            "8595 -2.437590  0.327408\n",
            "8596 -0.729543 -0.375557\n",
            "\n",
            "[8597 rows x 2 columns]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "TwEQhurom9ah"
      },
      "source": [
        "###Again using the Elbow Method"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 313
        },
        "id": "XXaiRSpQ_zdz",
        "outputId": "82297d18-18f8-4c2b-9ef1-e1aa65fae6d9"
      },
      "source": [
        "wcss = []\n",
        "for i in range(1,11):\n",
        "  new_kmeans = KMeans(n_clusters = i, init='k-means++', random_state=42)\n",
        "  new_kmeans.fit(new_train)\n",
        "  wcss.append(new_kmeans.inertia_)\n",
        "plt.plot(range(1,11),wcss)\n",
        "plt.title('The Elbow Method')\n",
        "plt.xlabel('Number of Clusters')\n",
        "plt.ylabel('WCSS')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Text(0, 0.5, 'WCSS')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 32
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 16764 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YEKOk0XwnAw2"
      },
      "source": [
        "###Applying Kmeans with 4 Clusters"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uI-Sm8qVAJmv"
      },
      "source": [
        "new_kmeans1 = KMeans(n_clusters = 4, init='k-means++', random_state=42)\n",
        "new_kmeans_result = new_kmeans1.fit_predict(new_train)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 295
        },
        "id": "MRhgmD-XAabU",
        "outputId": "c7fd8aec-ac1c-4f79-8b1e-bb70e241f2be"
      },
      "source": [
        "new_train = new_train.values\n",
        "plt.scatter(new_train[new_kmeans_result == 0, 0],new_train[new_kmeans_result == 0, 1], s = 10, c = 'red', label =  'Cluster 1')\n",
        "plt.scatter(new_train[new_kmeans_result == 1, 0],new_train[new_kmeans_result == 1, 1], s = 10, c = 'blue', label =  'Cluster 2')\n",
        "plt.scatter(new_train[new_kmeans_result == 2, 0],new_train[new_kmeans_result == 2, 1], s = 10, c = 'green', label =  'Cluster 3')\n",
        "plt.scatter(new_train[new_kmeans_result == 3, 0],new_train[new_kmeans_result == 3, 1], s = 10, c = 'pink', label =  'Cluster 4')\n",
        "plt.scatter(new_kmeans1.cluster_centers_[:, 0], new_kmeans1.cluster_centers_[:, 1], s = 50, c = 'yellow', label = 'Centroids')\n",
        "plt.title('Clusters of Customers')\n",
        "plt.xlabel('Feature 1')\n",
        "plt.ylabel('Feature 2')\n",
        "plt.legend()\n",
        "plt.show()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 22304 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    }
  ]
}