{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Support Vector Machine.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rhrV2tc5nh-h",
        "colab_type": "text"
      },
      "source": [
        "# Importing important libraries "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MLBYV4jBnMzK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "from sklearn.preprocessing import LabelEncoder\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.metrics import confusion_matrix , classification_report\n",
        "\n",
        "# Suppor vector machine (SVM) importing Algo or model\n",
        "from sklearn.svm import SVC"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZwRHP4YYorEC",
        "colab_type": "text"
      },
      "source": [
        "# Reading csv file ('Social_Network_Ads.csv')"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bb4MlvxKrgT5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "df = pd.read_csv(r'~/Social_Network_Ads.csv')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "81h52aVbrl9z",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        },
        "outputId": "3297f293-5b0e-4664-cf33-c93cb00f232b"
      },
      "source": [
        "df.head()\n"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "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>User ID</th>\n",
              "      <th>Gender</th>\n",
              "      <th>Age</th>\n",
              "      <th>EstimatedSalary</th>\n",
              "      <th>Purchased</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>15624510</td>\n",
              "      <td>Male</td>\n",
              "      <td>19</td>\n",
              "      <td>19000</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>15810944</td>\n",
              "      <td>Male</td>\n",
              "      <td>35</td>\n",
              "      <td>20000</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>15668575</td>\n",
              "      <td>Female</td>\n",
              "      <td>26</td>\n",
              "      <td>43000</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>15603246</td>\n",
              "      <td>Female</td>\n",
              "      <td>27</td>\n",
              "      <td>57000</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>15804002</td>\n",
              "      <td>Male</td>\n",
              "      <td>19</td>\n",
              "      <td>76000</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "    User ID  Gender  Age  EstimatedSalary  Purchased\n",
              "0  15624510    Male   19            19000          0\n",
              "1  15810944    Male   35            20000          0\n",
              "2  15668575  Female   26            43000          0\n",
              "3  15603246  Female   27            57000          0\n",
              "4  15804002    Male   19            76000          0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xsv94Ba-wz5T",
        "colab_type": "text"
      },
      "source": [
        "## checking for any null values in data"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "A-ChINcmxUMJ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        },
        "outputId": "df99e9f5-0ff3-4e74-e503-16021a25173a"
      },
      "source": [
        "df.isnull().sum()"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "User ID            0\n",
              "Gender             0\n",
              "Age                0\n",
              "EstimatedSalary    0\n",
              "Purchased          0\n",
              "dtype: int64"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wYHD1ovjxc-M",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "k1GwAYC91V_S",
        "colab_type": "text"
      },
      "source": [
        "## describing data"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VZPYbBC_1eow",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 425
        },
        "outputId": "261b8c63-cc29-4bfa-86bd-021f61eba9e5"
      },
      "source": [
        "print(df.describe())\n",
        "print('\\n')\n",
        "print(df.info())\n"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "            User ID         Age  EstimatedSalary   Purchased\n",
            "count  4.000000e+02  400.000000       400.000000  400.000000\n",
            "mean   1.569154e+07   37.655000     69742.500000    0.357500\n",
            "std    7.165832e+04   10.482877     34096.960282    0.479864\n",
            "min    1.556669e+07   18.000000     15000.000000    0.000000\n",
            "25%    1.562676e+07   29.750000     43000.000000    0.000000\n",
            "50%    1.569434e+07   37.000000     70000.000000    0.000000\n",
            "75%    1.575036e+07   46.000000     88000.000000    1.000000\n",
            "max    1.581524e+07   60.000000    150000.000000    1.000000\n",
            "\n",
            "\n",
            "<class 'pandas.core.frame.DataFrame'>\n",
            "RangeIndex: 400 entries, 0 to 399\n",
            "Data columns (total 5 columns):\n",
            " #   Column           Non-Null Count  Dtype \n",
            "---  ------           --------------  ----- \n",
            " 0   User ID          400 non-null    int64 \n",
            " 1   Gender           400 non-null    object\n",
            " 2   Age              400 non-null    int64 \n",
            " 3   EstimatedSalary  400 non-null    int64 \n",
            " 4   Purchased        400 non-null    int64 \n",
            "dtypes: int64(4), object(1)\n",
            "memory usage: 15.8+ KB\n",
            "None\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "314JgVuK1tHP",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "encoder = LabelEncoder()\n",
        "df.Gender = encoder.fit_transform(df.Gender)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wgD6rUNS2OVB",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 204
        },
        "outputId": "8a8a2590-8d84-47da-b9cd-9863d4b14aa7"
      },
      "source": [
        "df.sample(5)"
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "execute_result",
          "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>User ID</th>\n",
              "      <th>Gender</th>\n",
              "      <th>Age</th>\n",
              "      <th>EstimatedSalary</th>\n",
              "      <th>Purchased</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>322</th>\n",
              "      <td>15674331</td>\n",
              "      <td>1</td>\n",
              "      <td>41</td>\n",
              "      <td>52000</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>252</th>\n",
              "      <td>15795298</td>\n",
              "      <td>0</td>\n",
              "      <td>48</td>\n",
              "      <td>134000</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>263</th>\n",
              "      <td>15767681</td>\n",
              "      <td>0</td>\n",
              "      <td>35</td>\n",
              "      <td>72000</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>324</th>\n",
              "      <td>15575247</td>\n",
              "      <td>0</td>\n",
              "      <td>48</td>\n",
              "      <td>131000</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>309</th>\n",
              "      <td>15652400</td>\n",
              "      <td>0</td>\n",
              "      <td>38</td>\n",
              "      <td>50000</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "      User ID  Gender  Age  EstimatedSalary  Purchased\n",
              "322  15674331       1   41            52000          0\n",
              "252  15795298       0   48           134000          1\n",
              "263  15767681       0   35            72000          0\n",
              "324  15575247       0   48           131000          1\n",
              "309  15652400       0   38            50000          0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7cKKcoSV4nuW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "a70b7623-799f-46f3-8c4d-70d80772c49f"
      },
      "source": [
        "# count for zeros in dependent variable\n",
        "print('count for zeros :' , df.loc[df['Purchased']==0 ,'Purchased'].shape)\n",
        "\n",
        "# count for ones in dependent variable\n",
        "\n",
        "print('count for ones :' ,df.loc[df['Purchased'] ==1 , 'Purchased'].shape)"
      ],
      "execution_count": 34,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "count for zeros : (257,)\n",
            "count for ones : (143,)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_WYB96qU5MLr",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 296
        },
        "outputId": "997a39e3-bafa-48a2-918d-e0209f099380"
      },
      "source": [
        "sns.countplot(df.Purchased)"
      ],
      "execution_count": 35,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<matplotlib.axes._subplots.AxesSubplot at 0x7ff593e65e10>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 35
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 5352 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oMTAIWl65sp9",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 282
        },
        "outputId": "db442a7c-8446-4e25-d292-43e4e66ad771"
      },
      "source": [
        "df['Purchased'].hist(bins=20)"
      ],
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<matplotlib.axes._subplots.AxesSubplot at 0x7ff5934965c0>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 38
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 5692 characters]\n",
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ymFRDeqz5_Mp",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 775
        },
        "outputId": "4300c8bd-debd-432e-cc93-82d4b72eecc4"
      },
      "source": [
        "sns.pairplot(df , hue= 'Purchased' )"
      ],
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<seaborn.axisgrid.PairGrid at 0x7ff58fecb940>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 41
        },
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 214172 characters]\n",
            "text/plain": [
              "<Figure size 779.875x720 with 20 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ieKvlcLv8dCU",
        "colab_type": "text"
      },
      "source": [
        "### dependent and independent data"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Rnd52_G87K68",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "X = df.iloc[: , 1:4].values\n",
        "y = df.iloc[:,4].values"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "l9yuL3OU-wxt",
        "colab_type": "text"
      },
      "source": [
        "### spliting the training and test data"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ab59XUmN9WUK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "X_train , X_test , y_train , y_test = train_test_split(X , y, test_size=0.2 , random_state= 0)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tGpyb67O_IpU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "c77b6bcc-b66a-4d3c-8b01-da6d811f9258"
      },
      "source": [
        "print('Training data')\n",
        "print('Features :' , X_train.shape)\n",
        "print('Labels :',y_train.shape)\n",
        "\n",
        "print('\\nTest data')\n",
        "print('Features :',X_test.shape)\n",
        "print('Labels :',y_test.shape)"
      ],
      "execution_count": 47,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Training data\n",
            "Features : (320, 3)\n",
            "Labels : (320,)\n",
            "\n",
            "Test data\n",
            "Features : (80, 3)\n",
            "Labels : (80,)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "E6d88sMk_i8x",
        "colab_type": "text"
      },
      "source": [
        "### Training SVM model"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UNDrmiD2Abnt",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "1912989b-b751-4095-e550-136d0a6d49e8"
      },
      "source": [
        "model = SVC(C=100)\n",
        "model.fit(X_train ,y_train)"
      ],
      "execution_count": 49,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "SVC(C=100, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,\n",
              "    decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',\n",
              "    max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
              "    tol=0.001, verbose=False)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 49
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "f_DdZa_XAlxg",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "b5a90768-3829-4352-b69c-9504ab7f6c49"
      },
      "source": [
        "print('Scores')\n",
        "print('Training' , model.score(X_train , y_train))\n",
        "print('\\nTesting' , model.score(X_test , y_test))"
      ],
      "execution_count": 50,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Scores\n",
            "Training 0.775\n",
            "\n",
            "Testing 0.825\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7hicLZF6BJy_",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "ac5113fd-4845-4237-8f99-6bb21ae37872"
      },
      "source": [
        "yp = model.predict(X_test)\n",
        "yp"
      ],
      "execution_count": 51,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,\n",
              "       0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
              "       1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,\n",
              "       0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 51
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uXpAhfOfBq05",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "dc3fd907-8f6a-4fbc-cb85-36de4a74d17b"
      },
      "source": [
        "y_test"
      ],
      "execution_count": 52,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1,\n",
              "       0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,\n",
              "       1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1,\n",
              "       0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 52
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7YJjem4QBs9c",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 255
        },
        "outputId": "2ad1a602-3d35-47a2-fb5f-090c8ae88e9c"
      },
      "source": [
        "cm = confusion_matrix(y_test , yp)\n",
        "print('Confusion Matrix : ')\n",
        "print(cm)\n",
        "\n",
        "print('\\nClassification Report')\n",
        "print(classification_report(y_test , yp))"
      ],
      "execution_count": 54,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Confusion Matrix : \n",
            "[[56  2]\n",
            " [12 10]]\n",
            "\n",
            "Classification Report\n",
            "              precision    recall  f1-score   support\n",
            "\n",
            "           0       0.82      0.97      0.89        58\n",
            "           1       0.83      0.45      0.59        22\n",
            "\n",
            "    accuracy                           0.82        80\n",
            "   macro avg       0.83      0.71      0.74        80\n",
            "weighted avg       0.83      0.82      0.81        80\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Y0hoM4A4B85E",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 428
        },
        "outputId": "07af60cd-4990-4725-ff84-6ea03978514b"
      },
      "source": [
        "plt.figure(figsize=(10,7))\n",
        "sns.heatmap(cm, annot=True)\n",
        "plt.show()"
      ],
      "execution_count": 57,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "image/png": "[encoded data, 7392 characters]\n",
            "text/plain": [
              "<Figure size 720x504 with 2 Axes>"
            ]
          },
          "metadata": {
            "tags": [],
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "D24pwcWXCmyX",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}