{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LOGISTIC REGRESSION\n",
    "\n",
    "### Iris flower Classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "## importing all the necessary libraries\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import math\n",
    "import random"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Binary Classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sigmoid(X):\n",
    "    \n",
    "    Y = 1 / (1 + np.exp(-X))\n",
    "    \n",
    "    return Y    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.03976526 0.2139404  0.38265868 0.28064247 0.58292526 0.40552943]\n",
      " [0.23193619 0.71946807 0.25837453 0.35892819 0.74299501 0.09888583]\n",
      " [0.68021095 0.87529062 0.93804178 0.80487551 0.90841527 0.81599868]\n",
      " [0.67218767 0.78680819 0.75940483 0.81848939 0.22046572 0.47369603]\n",
      " [0.92464499 0.66093219 0.31102984 0.7310941  0.09821365 0.23441834]]\n",
      "[[0.50994001 0.55328203 0.59451418 0.56970373 0.64174023 0.60001544]\n",
      " [0.5577255  0.67248987 0.56423667 0.58878095 0.67765043 0.52470133]\n",
      " [0.66378578 0.70584537 0.71870394 0.69101643 0.71267577 0.69338631]\n",
      " [0.66199284 0.68714558 0.6812245  0.69391559 0.55489427 0.61625818]\n",
      " [0.71598761 0.65946976 0.57713661 0.67504532 0.52453369 0.55833768]]\n"
     ]
    }
   ],
   "source": [
    "x_temp = np.random.rand(5,6)\n",
    "y_temp = sigmoid(x_temp)\n",
    "print(x_temp)\n",
    "print(y_temp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cost function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def cost_fun(y,h):\n",
    "    \n",
    "    cost = -1/y.size * np.sum( np.dot(y.T,np.log(h)) + np.dot(1-y.T, np.log(1-h)))\n",
    "    \n",
    "    return cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gradient(X):\n",
    "    \n",
    "    X = sigmoid(X)*(1-sigmoid(X))\n",
    "    \n",
    "    return X    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cost : 1.0721461211938885\n",
      "gradient : [[0.09753063]\n",
      " [0.0505594 ]\n",
      " [0.09780491]\n",
      " [0.07103742]\n",
      " [0.10575785]]\n"
     ]
    }
   ],
   "source": [
    "x_temp = np.random.rand(5,6)\n",
    "\n",
    "y_temp = [1.,0.,1.,1.,0.]\n",
    "y_temp = np.array(y_temp)\n",
    "y_temp = np.expand_dims(y_temp,axis = 1)\n",
    "\n",
    "w_temp = np.random.rand(x_temp.shape[1],1)\n",
    "\n",
    "y_hat = sigmoid(np.dot(x_temp,w_temp))\n",
    "\n",
    "cost = cost_fun(y_temp,y_hat)\n",
    "print(\"cost : {}\".format(cost))\n",
    "\n",
    "g = gradient(np.dot(x_temp,w_temp))\n",
    "print(f\"gradient : {g}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## loading a datasets of iris flower classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sepal_length</th>\n",
       "      <th>sepal_width</th>\n",
       "      <th>petal_length</th>\n",
       "      <th>petal_width</th>\n",
       "      <th>species</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5.4</td>\n",
       "      <td>3.9</td>\n",
       "      <td>1.7</td>\n",
       "      <td>0.4</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.4</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.3</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.4</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>4.4</td>\n",
       "      <td>2.9</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.1</td>\n",
       "      <td>Iris-setosa</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   sepal_length  sepal_width  petal_length  petal_width      species\n",
       "0           5.1          3.5           1.4          0.2  Iris-setosa\n",
       "1           4.9          3.0           1.4          0.2  Iris-setosa\n",
       "2           4.7          3.2           1.3          0.2  Iris-setosa\n",
       "3           4.6          3.1           1.5          0.2  Iris-setosa\n",
       "4           5.0          3.6           1.4          0.2  Iris-setosa\n",
       "5           5.4          3.9           1.7          0.4  Iris-setosa\n",
       "6           4.6          3.4           1.4          0.3  Iris-setosa\n",
       "7           5.0          3.4           1.5          0.2  Iris-setosa\n",
       "8           4.4          2.9           1.4          0.2  Iris-setosa\n",
       "9           4.9          3.1           1.5          0.1  Iris-setosa"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris_data = pd.read_csv(r\"IRIS.csv\")\n",
    "iris_data.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### storeing the data set in a array from tabular form"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(150, 5)\n",
      "[[5.1 3.5 1.4 0.2 'Iris-setosa']\n",
      " [4.9 3.0 1.4 0.2 'Iris-setosa']\n",
      " [4.7 3.2 1.3 0.2 'Iris-setosa']\n",
      " [4.6 3.1 1.5 0.2 'Iris-setosa']\n",
      " [5.0 3.6 1.4 0.2 'Iris-setosa']\n",
      " [5.4 3.9 1.7 0.4 'Iris-setosa']\n",
      " [4.6 3.4 1.4 0.3 'Iris-setosa']\n",
      " [5.0 3.4 1.5 0.2 'Iris-setosa']\n",
      " [4.4 2.9 1.4 0.2 'Iris-setosa']\n",
      " [4.9 3.1 1.5 0.1 'Iris-setosa']\n",
      " [5.4 3.7 1.5 0.2 'Iris-setosa']\n",
      " [4.8 3.4 1.6 0.2 'Iris-setosa']\n",
      " [4.8 3.0 1.4 0.1 'Iris-setosa']\n",
      " [4.3 3.0 1.1 0.1 'Iris-setosa']\n",
      " [5.8 4.0 1.2 0.2 'Iris-setosa']\n",
      " [5.7 4.4 1.5 0.4 'Iris-setosa']\n",
      " [5.4 3.9 1.3 0.4 'Iris-setosa']\n",
      " [5.1 3.5 1.4 0.3 'Iris-setosa']\n",
      " [5.7 3.8 1.7 0.3 'Iris-setosa']\n",
      " [5.1 3.8 1.5 0.3 'Iris-setosa']\n",
      " [5.4 3.4 1.7 0.2 'Iris-setosa']\n",
      " [5.1 3.7 1.5 0.4 'Iris-setosa']\n",
      " [4.6 3.6 1.0 0.2 'Iris-setosa']\n",
      " [5.1 3.3 1.7 0.5 'Iris-setosa']\n",
      " [4.8 3.4 1.9 0.2 'Iris-setosa']\n",
      " [5.0 3.0 1.6 0.2 'Iris-setosa']\n",
      " [5.0 3.4 1.6 0.4 'Iris-setosa']\n",
      " [5.2 3.5 1.5 0.2 'Iris-setosa']\n",
      " [5.2 3.4 1.4 0.2 'Iris-setosa']\n",
      " [4.7 3.2 1.6 0.2 'Iris-setosa']\n",
      " [4.8 3.1 1.6 0.2 'Iris-setosa']\n",
      " [5.4 3.4 1.5 0.4 'Iris-setosa']\n",
      " [5.2 4.1 1.5 0.1 'Iris-setosa']\n",
      " [5.5 4.2 1.4 0.2 'Iris-setosa']\n",
      " [4.9 3.1 1.5 0.1 'Iris-setosa']\n",
      " [5.0 3.2 1.2 0.2 'Iris-setosa']\n",
      " [5.5 3.5 1.3 0.2 'Iris-setosa']\n",
      " [4.9 3.1 1.5 0.1 'Iris-setosa']\n",
      " [4.4 3.0 1.3 0.2 'Iris-setosa']\n",
      " [5.1 3.4 1.5 0.2 'Iris-setosa']\n",
      " [5.0 3.5 1.3 0.3 'Iris-setosa']\n",
      " [4.5 2.3 1.3 0.3 'Iris-setosa']\n",
      " [4.4 3.2 1.3 0.2 'Iris-setosa']\n",
      " [5.0 3.5 1.6 0.6 'Iris-setosa']\n",
      " [5.1 3.8 1.9 0.4 'Iris-setosa']\n",
      " [4.8 3.0 1.4 0.3 'Iris-setosa']\n",
      " [5.1 3.8 1.6 0.2 'Iris-setosa']\n",
      " [4.6 3.2 1.4 0.2 'Iris-setosa']\n",
      " [5.3 3.7 1.5 0.2 'Iris-setosa']\n",
      " [5.0 3.3 1.4 0.2 'Iris-setosa']\n",
      " [7.0 3.2 4.7 1.4 'Iris-versicolor']\n",
      " [6.4 3.2 4.5 1.5 'Iris-versicolor']\n",
      " [6.9 3.1 4.9 1.5 'Iris-versicolor']\n",
      " [5.5 2.3 4.0 1.3 'Iris-versicolor']\n",
      " [6.5 2.8 4.6 1.5 'Iris-versicolor']\n",
      " [5.7 2.8 4.5 1.3 'Iris-versicolor']\n",
      " [6.3 3.3 4.7 1.6 'Iris-versicolor']\n",
      " [4.9 2.4 3.3 1.0 'Iris-versicolor']\n",
      " [6.6 2.9 4.6 1.3 'Iris-versicolor']\n",
      " [5.2 2.7 3.9 1.4 'Iris-versicolor']\n",
      " [5.0 2.0 3.5 1.0 'Iris-versicolor']\n",
      " [5.9 3.0 4.2 1.5 'Iris-versicolor']\n",
      " [6.0 2.2 4.0 1.0 'Iris-versicolor']\n",
      " [6.1 2.9 4.7 1.4 'Iris-versicolor']\n",
      " [5.6 2.9 3.6 1.3 'Iris-versicolor']\n",
      " [6.7 3.1 4.4 1.4 'Iris-versicolor']\n",
      " [5.6 3.0 4.5 1.5 'Iris-versicolor']\n",
      " [5.8 2.7 4.1 1.0 'Iris-versicolor']\n",
      " [6.2 2.2 4.5 1.5 'Iris-versicolor']\n",
      " [5.6 2.5 3.9 1.1 'Iris-versicolor']\n",
      " [5.9 3.2 4.8 1.8 'Iris-versicolor']\n",
      " [6.1 2.8 4.0 1.3 'Iris-versicolor']\n",
      " [6.3 2.5 4.9 1.5 'Iris-versicolor']\n",
      " [6.1 2.8 4.7 1.2 'Iris-versicolor']\n",
      " [6.4 2.9 4.3 1.3 'Iris-versicolor']\n",
      " [6.6 3.0 4.4 1.4 'Iris-versicolor']\n",
      " [6.8 2.8 4.8 1.4 'Iris-versicolor']\n",
      " [6.7 3.0 5.0 1.7 'Iris-versicolor']\n",
      " [6.0 2.9 4.5 1.5 'Iris-versicolor']\n",
      " [5.7 2.6 3.5 1.0 'Iris-versicolor']\n",
      " [5.5 2.4 3.8 1.1 'Iris-versicolor']\n",
      " [5.5 2.4 3.7 1.0 'Iris-versicolor']\n",
      " [5.8 2.7 3.9 1.2 'Iris-versicolor']\n",
      " [6.0 2.7 5.1 1.6 'Iris-versicolor']\n",
      " [5.4 3.0 4.5 1.5 'Iris-versicolor']\n",
      " [6.0 3.4 4.5 1.6 'Iris-versicolor']\n",
      " [6.7 3.1 4.7 1.5 'Iris-versicolor']\n",
      " [6.3 2.3 4.4 1.3 'Iris-versicolor']\n",
      " [5.6 3.0 4.1 1.3 'Iris-versicolor']\n",
      " [5.5 2.5 4.0 1.3 'Iris-versicolor']\n",
      " [5.5 2.6 4.4 1.2 'Iris-versicolor']\n",
      " [6.1 3.0 4.6 1.4 'Iris-versicolor']\n",
      " [5.8 2.6 4.0 1.2 'Iris-versicolor']\n",
      " [5.0 2.3 3.3 1.0 'Iris-versicolor']\n",
      " [5.6 2.7 4.2 1.3 'Iris-versicolor']\n",
      " [5.7 3.0 4.2 1.2 'Iris-versicolor']\n",
      " [5.7 2.9 4.2 1.3 'Iris-versicolor']\n",
      " [6.2 2.9 4.3 1.3 'Iris-versicolor']\n",
      " [5.1 2.5 3.0 1.1 'Iris-versicolor']\n",
      " [5.7 2.8 4.1 1.3 'Iris-versicolor']\n",
      " [6.3 3.3 6.0 2.5 'Iris-virginica']\n",
      " [5.8 2.7 5.1 1.9 'Iris-virginica']\n",
      " [7.1 3.0 5.9 2.1 'Iris-virginica']\n",
      " [6.3 2.9 5.6 1.8 'Iris-virginica']\n",
      " [6.5 3.0 5.8 2.2 'Iris-virginica']\n",
      " [7.6 3.0 6.6 2.1 'Iris-virginica']\n",
      " [4.9 2.5 4.5 1.7 'Iris-virginica']\n",
      " [7.3 2.9 6.3 1.8 'Iris-virginica']\n",
      " [6.7 2.5 5.8 1.8 'Iris-virginica']\n",
      " [7.2 3.6 6.1 2.5 'Iris-virginica']\n",
      " [6.5 3.2 5.1 2.0 'Iris-virginica']\n",
      " [6.4 2.7 5.3 1.9 'Iris-virginica']\n",
      " [6.8 3.0 5.5 2.1 'Iris-virginica']\n",
      " [5.7 2.5 5.0 2.0 'Iris-virginica']\n",
      " [5.8 2.8 5.1 2.4 'Iris-virginica']\n",
      " [6.4 3.2 5.3 2.3 'Iris-virginica']\n",
      " [6.5 3.0 5.5 1.8 'Iris-virginica']\n",
      " [7.7 3.8 6.7 2.2 'Iris-virginica']\n",
      " [7.7 2.6 6.9 2.3 'Iris-virginica']\n",
      " [6.0 2.2 5.0 1.5 'Iris-virginica']\n",
      " [6.9 3.2 5.7 2.3 'Iris-virginica']\n",
      " [5.6 2.8 4.9 2.0 'Iris-virginica']\n",
      " [7.7 2.8 6.7 2.0 'Iris-virginica']\n",
      " [6.3 2.7 4.9 1.8 'Iris-virginica']\n",
      " [6.7 3.3 5.7 2.1 'Iris-virginica']\n",
      " [7.2 3.2 6.0 1.8 'Iris-virginica']\n",
      " [6.2 2.8 4.8 1.8 'Iris-virginica']\n",
      " [6.1 3.0 4.9 1.8 'Iris-virginica']\n",
      " [6.4 2.8 5.6 2.1 'Iris-virginica']\n",
      " [7.2 3.0 5.8 1.6 'Iris-virginica']\n",
      " [7.4 2.8 6.1 1.9 'Iris-virginica']\n",
      " [7.9 3.8 6.4 2.0 'Iris-virginica']\n",
      " [6.4 2.8 5.6 2.2 'Iris-virginica']\n",
      " [6.3 2.8 5.1 1.5 'Iris-virginica']\n",
      " [6.1 2.6 5.6 1.4 'Iris-virginica']\n",
      " [7.7 3.0 6.1 2.3 'Iris-virginica']\n",
      " [6.3 3.4 5.6 2.4 'Iris-virginica']\n",
      " [6.4 3.1 5.5 1.8 'Iris-virginica']\n",
      " [6.0 3.0 4.8 1.8 'Iris-virginica']\n",
      " [6.9 3.1 5.4 2.1 'Iris-virginica']\n",
      " [6.7 3.1 5.6 2.4 'Iris-virginica']\n",
      " [6.9 3.1 5.1 2.3 'Iris-virginica']\n",
      " [5.8 2.7 5.1 1.9 'Iris-virginica']\n",
      " [6.8 3.2 5.9 2.3 'Iris-virginica']\n",
      " [6.7 3.3 5.7 2.5 'Iris-virginica']\n",
      " [6.7 3.0 5.2 2.3 'Iris-virginica']\n",
      " [6.3 2.5 5.0 1.9 'Iris-virginica']\n",
      " [6.5 3.0 5.2 2.0 'Iris-virginica']\n",
      " [6.2 3.4 5.4 2.3 'Iris-virginica']\n",
      " [5.9 3.0 5.1 1.8 'Iris-virginica']]\n"
     ]
    }
   ],
   "source": [
    "iris_data = np.array(iris_data)\n",
    "print(iris_data.shape)\n",
    "print(iris_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**y stores the data values of 4th column and x stores the other 4 parameters.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "150\n",
      "['Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'\n",
      " 'Iris-versicolor' 'Iris-versicolor' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'\n",
      " 'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica']\n"
     ]
    }
   ],
   "source": [
    "y = iris_data[:,4]\n",
    "print(len(y))\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "150\n",
      "[[5.1 3.5 1.4 0.2]\n",
      " [4.9 3.  1.4 0.2]\n",
      " [4.7 3.2 1.3 0.2]\n",
      " [4.6 3.1 1.5 0.2]\n",
      " [5.  3.6 1.4 0.2]\n",
      " [5.4 3.9 1.7 0.4]\n",
      " [4.6 3.4 1.4 0.3]\n",
      " [5.  3.4 1.5 0.2]\n",
      " [4.4 2.9 1.4 0.2]\n",
      " [4.9 3.1 1.5 0.1]\n",
      " [5.4 3.7 1.5 0.2]\n",
      " [4.8 3.4 1.6 0.2]\n",
      " [4.8 3.  1.4 0.1]\n",
      " [4.3 3.  1.1 0.1]\n",
      " [5.8 4.  1.2 0.2]\n",
      " [5.7 4.4 1.5 0.4]\n",
      " [5.4 3.9 1.3 0.4]\n",
      " [5.1 3.5 1.4 0.3]\n",
      " [5.7 3.8 1.7 0.3]\n",
      " [5.1 3.8 1.5 0.3]\n",
      " [5.4 3.4 1.7 0.2]\n",
      " [5.1 3.7 1.5 0.4]\n",
      " [4.6 3.6 1.  0.2]\n",
      " [5.1 3.3 1.7 0.5]\n",
      " [4.8 3.4 1.9 0.2]\n",
      " [5.  3.  1.6 0.2]\n",
      " [5.  3.4 1.6 0.4]\n",
      " [5.2 3.5 1.5 0.2]\n",
      " [5.2 3.4 1.4 0.2]\n",
      " [4.7 3.2 1.6 0.2]\n",
      " [4.8 3.1 1.6 0.2]\n",
      " [5.4 3.4 1.5 0.4]\n",
      " [5.2 4.1 1.5 0.1]\n",
      " [5.5 4.2 1.4 0.2]\n",
      " [4.9 3.1 1.5 0.1]\n",
      " [5.  3.2 1.2 0.2]\n",
      " [5.5 3.5 1.3 0.2]\n",
      " [4.9 3.1 1.5 0.1]\n",
      " [4.4 3.  1.3 0.2]\n",
      " [5.1 3.4 1.5 0.2]\n",
      " [5.  3.5 1.3 0.3]\n",
      " [4.5 2.3 1.3 0.3]\n",
      " [4.4 3.2 1.3 0.2]\n",
      " [5.  3.5 1.6 0.6]\n",
      " [5.1 3.8 1.9 0.4]\n",
      " [4.8 3.  1.4 0.3]\n",
      " [5.1 3.8 1.6 0.2]\n",
      " [4.6 3.2 1.4 0.2]\n",
      " [5.3 3.7 1.5 0.2]\n",
      " [5.  3.3 1.4 0.2]\n",
      " [7.  3.2 4.7 1.4]\n",
      " [6.4 3.2 4.5 1.5]\n",
      " [6.9 3.1 4.9 1.5]\n",
      " [5.5 2.3 4.  1.3]\n",
      " [6.5 2.8 4.6 1.5]\n",
      " [5.7 2.8 4.5 1.3]\n",
      " [6.3 3.3 4.7 1.6]\n",
      " [4.9 2.4 3.3 1. ]\n",
      " [6.6 2.9 4.6 1.3]\n",
      " [5.2 2.7 3.9 1.4]\n",
      " [5.  2.  3.5 1. ]\n",
      " [5.9 3.  4.2 1.5]\n",
      " [6.  2.2 4.  1. ]\n",
      " [6.1 2.9 4.7 1.4]\n",
      " [5.6 2.9 3.6 1.3]\n",
      " [6.7 3.1 4.4 1.4]\n",
      " [5.6 3.  4.5 1.5]\n",
      " [5.8 2.7 4.1 1. ]\n",
      " [6.2 2.2 4.5 1.5]\n",
      " [5.6 2.5 3.9 1.1]\n",
      " [5.9 3.2 4.8 1.8]\n",
      " [6.1 2.8 4.  1.3]\n",
      " [6.3 2.5 4.9 1.5]\n",
      " [6.1 2.8 4.7 1.2]\n",
      " [6.4 2.9 4.3 1.3]\n",
      " [6.6 3.  4.4 1.4]\n",
      " [6.8 2.8 4.8 1.4]\n",
      " [6.7 3.  5.  1.7]\n",
      " [6.  2.9 4.5 1.5]\n",
      " [5.7 2.6 3.5 1. ]\n",
      " [5.5 2.4 3.8 1.1]\n",
      " [5.5 2.4 3.7 1. ]\n",
      " [5.8 2.7 3.9 1.2]\n",
      " [6.  2.7 5.1 1.6]\n",
      " [5.4 3.  4.5 1.5]\n",
      " [6.  3.4 4.5 1.6]\n",
      " [6.7 3.1 4.7 1.5]\n",
      " [6.3 2.3 4.4 1.3]\n",
      " [5.6 3.  4.1 1.3]\n",
      " [5.5 2.5 4.  1.3]\n",
      " [5.5 2.6 4.4 1.2]\n",
      " [6.1 3.  4.6 1.4]\n",
      " [5.8 2.6 4.  1.2]\n",
      " [5.  2.3 3.3 1. ]\n",
      " [5.6 2.7 4.2 1.3]\n",
      " [5.7 3.  4.2 1.2]\n",
      " [5.7 2.9 4.2 1.3]\n",
      " [6.2 2.9 4.3 1.3]\n",
      " [5.1 2.5 3.  1.1]\n",
      " [5.7 2.8 4.1 1.3]\n",
      " [6.3 3.3 6.  2.5]\n",
      " [5.8 2.7 5.1 1.9]\n",
      " [7.1 3.  5.9 2.1]\n",
      " [6.3 2.9 5.6 1.8]\n",
      " [6.5 3.  5.8 2.2]\n",
      " [7.6 3.  6.6 2.1]\n",
      " [4.9 2.5 4.5 1.7]\n",
      " [7.3 2.9 6.3 1.8]\n",
      " [6.7 2.5 5.8 1.8]\n",
      " [7.2 3.6 6.1 2.5]\n",
      " [6.5 3.2 5.1 2. ]\n",
      " [6.4 2.7 5.3 1.9]\n",
      " [6.8 3.  5.5 2.1]\n",
      " [5.7 2.5 5.  2. ]\n",
      " [5.8 2.8 5.1 2.4]\n",
      " [6.4 3.2 5.3 2.3]\n",
      " [6.5 3.  5.5 1.8]\n",
      " [7.7 3.8 6.7 2.2]\n",
      " [7.7 2.6 6.9 2.3]\n",
      " [6.  2.2 5.  1.5]\n",
      " [6.9 3.2 5.7 2.3]\n",
      " [5.6 2.8 4.9 2. ]\n",
      " [7.7 2.8 6.7 2. ]\n",
      " [6.3 2.7 4.9 1.8]\n",
      " [6.7 3.3 5.7 2.1]\n",
      " [7.2 3.2 6.  1.8]\n",
      " [6.2 2.8 4.8 1.8]\n",
      " [6.1 3.  4.9 1.8]\n",
      " [6.4 2.8 5.6 2.1]\n",
      " [7.2 3.  5.8 1.6]\n",
      " [7.4 2.8 6.1 1.9]\n",
      " [7.9 3.8 6.4 2. ]\n",
      " [6.4 2.8 5.6 2.2]\n",
      " [6.3 2.8 5.1 1.5]\n",
      " [6.1 2.6 5.6 1.4]\n",
      " [7.7 3.  6.1 2.3]\n",
      " [6.3 3.4 5.6 2.4]\n",
      " [6.4 3.1 5.5 1.8]\n",
      " [6.  3.  4.8 1.8]\n",
      " [6.9 3.1 5.4 2.1]\n",
      " [6.7 3.1 5.6 2.4]\n",
      " [6.9 3.1 5.1 2.3]\n",
      " [5.8 2.7 5.1 1.9]\n",
      " [6.8 3.2 5.9 2.3]\n",
      " [6.7 3.3 5.7 2.5]\n",
      " [6.7 3.  5.2 2.3]\n",
      " [6.3 2.5 5.  1.9]\n",
      " [6.5 3.  5.2 2. ]\n",
      " [6.2 3.4 5.4 2.3]\n",
      " [5.9 3.  5.1 1.8]]\n"
     ]
    }
   ],
   "source": [
    "x=np.array(iris_data[:,:4],dtype=float) # Or x=np.array(iris_data[:,0:4],dtype=float)\n",
    "print(len(x))\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Feature Scaling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-7.43333333e-01  4.46000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [-9.43333333e-01 -5.40000000e-02 -2.35866667e+00 -9.98666667e-01]\n",
      " [-1.14333333e+00  1.46000000e-01 -2.45866667e+00 -9.98666667e-01]\n",
      " [-1.24333333e+00  4.60000000e-02 -2.25866667e+00 -9.98666667e-01]\n",
      " [-8.43333333e-01  5.46000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [-4.43333333e-01  8.46000000e-01 -2.05866667e+00 -7.98666667e-01]\n",
      " [-1.24333333e+00  3.46000000e-01 -2.35866667e+00 -8.98666667e-01]\n",
      " [-8.43333333e-01  3.46000000e-01 -2.25866667e+00 -9.98666667e-01]\n",
      " [-1.44333333e+00 -1.54000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [-9.43333333e-01  4.60000000e-02 -2.25866667e+00 -1.09866667e+00]\n",
      " [-4.43333333e-01  6.46000000e-01 -2.25866667e+00 -9.98666667e-01]\n",
      " [-1.04333333e+00  3.46000000e-01 -2.15866667e+00 -9.98666667e-01]\n",
      " [-1.04333333e+00 -5.40000000e-02 -2.35866667e+00 -1.09866667e+00]\n",
      " [-1.54333333e+00 -5.40000000e-02 -2.65866667e+00 -1.09866667e+00]\n",
      " [-4.33333333e-02  9.46000000e-01 -2.55866667e+00 -9.98666667e-01]\n",
      " [-1.43333333e-01  1.34600000e+00 -2.25866667e+00 -7.98666667e-01]\n",
      " [-4.43333333e-01  8.46000000e-01 -2.45866667e+00 -7.98666667e-01]\n",
      " [-7.43333333e-01  4.46000000e-01 -2.35866667e+00 -8.98666667e-01]\n",
      " [-1.43333333e-01  7.46000000e-01 -2.05866667e+00 -8.98666667e-01]\n",
      " [-7.43333333e-01  7.46000000e-01 -2.25866667e+00 -8.98666667e-01]\n",
      " [-4.43333333e-01  3.46000000e-01 -2.05866667e+00 -9.98666667e-01]\n",
      " [-7.43333333e-01  6.46000000e-01 -2.25866667e+00 -7.98666667e-01]\n",
      " [-1.24333333e+00  5.46000000e-01 -2.75866667e+00 -9.98666667e-01]\n",
      " [-7.43333333e-01  2.46000000e-01 -2.05866667e+00 -6.98666667e-01]\n",
      " [-1.04333333e+00  3.46000000e-01 -1.85866667e+00 -9.98666667e-01]\n",
      " [-8.43333333e-01 -5.40000000e-02 -2.15866667e+00 -9.98666667e-01]\n",
      " [-8.43333333e-01  3.46000000e-01 -2.15866667e+00 -7.98666667e-01]\n",
      " [-6.43333333e-01  4.46000000e-01 -2.25866667e+00 -9.98666667e-01]\n",
      " [-6.43333333e-01  3.46000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [-1.14333333e+00  1.46000000e-01 -2.15866667e+00 -9.98666667e-01]\n",
      " [-1.04333333e+00  4.60000000e-02 -2.15866667e+00 -9.98666667e-01]\n",
      " [-4.43333333e-01  3.46000000e-01 -2.25866667e+00 -7.98666667e-01]\n",
      " [-6.43333333e-01  1.04600000e+00 -2.25866667e+00 -1.09866667e+00]\n",
      " [-3.43333333e-01  1.14600000e+00 -2.35866667e+00 -9.98666667e-01]\n",
      " [-9.43333333e-01  4.60000000e-02 -2.25866667e+00 -1.09866667e+00]\n",
      " [-8.43333333e-01  1.46000000e-01 -2.55866667e+00 -9.98666667e-01]\n",
      " [-3.43333333e-01  4.46000000e-01 -2.45866667e+00 -9.98666667e-01]\n",
      " [-9.43333333e-01  4.60000000e-02 -2.25866667e+00 -1.09866667e+00]\n",
      " [-1.44333333e+00 -5.40000000e-02 -2.45866667e+00 -9.98666667e-01]\n",
      " [-7.43333333e-01  3.46000000e-01 -2.25866667e+00 -9.98666667e-01]\n",
      " [-8.43333333e-01  4.46000000e-01 -2.45866667e+00 -8.98666667e-01]\n",
      " [-1.34333333e+00 -7.54000000e-01 -2.45866667e+00 -8.98666667e-01]\n",
      " [-1.44333333e+00  1.46000000e-01 -2.45866667e+00 -9.98666667e-01]\n",
      " [-8.43333333e-01  4.46000000e-01 -2.15866667e+00 -5.98666667e-01]\n",
      " [-7.43333333e-01  7.46000000e-01 -1.85866667e+00 -7.98666667e-01]\n",
      " [-1.04333333e+00 -5.40000000e-02 -2.35866667e+00 -8.98666667e-01]\n",
      " [-7.43333333e-01  7.46000000e-01 -2.15866667e+00 -9.98666667e-01]\n",
      " [-1.24333333e+00  1.46000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [-5.43333333e-01  6.46000000e-01 -2.25866667e+00 -9.98666667e-01]\n",
      " [-8.43333333e-01  2.46000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [ 1.15666667e+00  1.46000000e-01  9.41333333e-01  2.01333333e-01]\n",
      " [ 5.56666667e-01  1.46000000e-01  7.41333333e-01  3.01333333e-01]\n",
      " [ 1.05666667e+00  4.60000000e-02  1.14133333e+00  3.01333333e-01]\n",
      " [-3.43333333e-01 -7.54000000e-01  2.41333333e-01  1.01333333e-01]\n",
      " [ 6.56666667e-01 -2.54000000e-01  8.41333333e-01  3.01333333e-01]\n",
      " [-1.43333333e-01 -2.54000000e-01  7.41333333e-01  1.01333333e-01]\n",
      " [ 4.56666667e-01  2.46000000e-01  9.41333333e-01  4.01333333e-01]\n",
      " [-9.43333333e-01 -6.54000000e-01 -4.58666667e-01 -1.98666667e-01]\n",
      " [ 7.56666667e-01 -1.54000000e-01  8.41333333e-01  1.01333333e-01]\n",
      " [-6.43333333e-01 -3.54000000e-01  1.41333333e-01  2.01333333e-01]\n",
      " [-8.43333333e-01 -1.05400000e+00 -2.58666667e-01 -1.98666667e-01]\n",
      " [ 5.66666667e-02 -5.40000000e-02  4.41333333e-01  3.01333333e-01]\n",
      " [ 1.56666667e-01 -8.54000000e-01  2.41333333e-01 -1.98666667e-01]\n",
      " [ 2.56666667e-01 -1.54000000e-01  9.41333333e-01  2.01333333e-01]\n",
      " [-2.43333333e-01 -1.54000000e-01 -1.58666667e-01  1.01333333e-01]\n",
      " [ 8.56666667e-01  4.60000000e-02  6.41333333e-01  2.01333333e-01]\n",
      " [-2.43333333e-01 -5.40000000e-02  7.41333333e-01  3.01333333e-01]\n",
      " [-4.33333333e-02 -3.54000000e-01  3.41333333e-01 -1.98666667e-01]\n",
      " [ 3.56666667e-01 -8.54000000e-01  7.41333333e-01  3.01333333e-01]\n",
      " [-2.43333333e-01 -5.54000000e-01  1.41333333e-01 -9.86666667e-02]\n",
      " [ 5.66666667e-02  1.46000000e-01  1.04133333e+00  6.01333333e-01]\n",
      " [ 2.56666667e-01 -2.54000000e-01  2.41333333e-01  1.01333333e-01]\n",
      " [ 4.56666667e-01 -5.54000000e-01  1.14133333e+00  3.01333333e-01]\n",
      " [ 2.56666667e-01 -2.54000000e-01  9.41333333e-01  1.33333333e-03]\n",
      " [ 5.56666667e-01 -1.54000000e-01  5.41333333e-01  1.01333333e-01]\n",
      " [ 7.56666667e-01 -5.40000000e-02  6.41333333e-01  2.01333333e-01]\n",
      " [ 9.56666667e-01 -2.54000000e-01  1.04133333e+00  2.01333333e-01]\n",
      " [ 8.56666667e-01 -5.40000000e-02  1.24133333e+00  5.01333333e-01]\n",
      " [ 1.56666667e-01 -1.54000000e-01  7.41333333e-01  3.01333333e-01]\n",
      " [-1.43333333e-01 -4.54000000e-01 -2.58666667e-01 -1.98666667e-01]\n",
      " [-3.43333333e-01 -6.54000000e-01  4.13333333e-02 -9.86666667e-02]\n",
      " [-3.43333333e-01 -6.54000000e-01 -5.86666667e-02 -1.98666667e-01]\n",
      " [-4.33333333e-02 -3.54000000e-01  1.41333333e-01  1.33333333e-03]\n",
      " [ 1.56666667e-01 -3.54000000e-01  1.34133333e+00  4.01333333e-01]\n",
      " [-4.43333333e-01 -5.40000000e-02  7.41333333e-01  3.01333333e-01]\n",
      " [ 1.56666667e-01  3.46000000e-01  7.41333333e-01  4.01333333e-01]\n",
      " [ 8.56666667e-01  4.60000000e-02  9.41333333e-01  3.01333333e-01]\n",
      " [ 4.56666667e-01 -7.54000000e-01  6.41333333e-01  1.01333333e-01]\n",
      " [-2.43333333e-01 -5.40000000e-02  3.41333333e-01  1.01333333e-01]\n",
      " [-3.43333333e-01 -5.54000000e-01  2.41333333e-01  1.01333333e-01]\n",
      " [-3.43333333e-01 -4.54000000e-01  6.41333333e-01  1.33333333e-03]\n",
      " [ 2.56666667e-01 -5.40000000e-02  8.41333333e-01  2.01333333e-01]\n",
      " [-4.33333333e-02 -4.54000000e-01  2.41333333e-01  1.33333333e-03]\n",
      " [-8.43333333e-01 -7.54000000e-01 -4.58666667e-01 -1.98666667e-01]\n",
      " [-2.43333333e-01 -3.54000000e-01  4.41333333e-01  1.01333333e-01]\n",
      " [-1.43333333e-01 -5.40000000e-02  4.41333333e-01  1.33333333e-03]\n",
      " [-1.43333333e-01 -1.54000000e-01  4.41333333e-01  1.01333333e-01]\n",
      " [ 3.56666667e-01 -1.54000000e-01  5.41333333e-01  1.01333333e-01]\n",
      " [-7.43333333e-01 -5.54000000e-01 -7.58666667e-01 -9.86666667e-02]\n",
      " [-1.43333333e-01 -2.54000000e-01  3.41333333e-01  1.01333333e-01]\n",
      " [ 4.56666667e-01  2.46000000e-01  2.24133333e+00  1.30133333e+00]\n",
      " [-4.33333333e-02 -3.54000000e-01  1.34133333e+00  7.01333333e-01]\n",
      " [ 1.25666667e+00 -5.40000000e-02  2.14133333e+00  9.01333333e-01]\n",
      " [ 4.56666667e-01 -1.54000000e-01  1.84133333e+00  6.01333333e-01]\n",
      " [ 6.56666667e-01 -5.40000000e-02  2.04133333e+00  1.00133333e+00]\n",
      " [ 1.75666667e+00 -5.40000000e-02  2.84133333e+00  9.01333333e-01]\n",
      " [-9.43333333e-01 -5.54000000e-01  7.41333333e-01  5.01333333e-01]\n",
      " [ 1.45666667e+00 -1.54000000e-01  2.54133333e+00  6.01333333e-01]\n",
      " [ 8.56666667e-01 -5.54000000e-01  2.04133333e+00  6.01333333e-01]\n",
      " [ 1.35666667e+00  5.46000000e-01  2.34133333e+00  1.30133333e+00]\n",
      " [ 6.56666667e-01  1.46000000e-01  1.34133333e+00  8.01333333e-01]\n",
      " [ 5.56666667e-01 -3.54000000e-01  1.54133333e+00  7.01333333e-01]\n",
      " [ 9.56666667e-01 -5.40000000e-02  1.74133333e+00  9.01333333e-01]\n",
      " [-1.43333333e-01 -5.54000000e-01  1.24133333e+00  8.01333333e-01]\n",
      " [-4.33333333e-02 -2.54000000e-01  1.34133333e+00  1.20133333e+00]\n",
      " [ 5.56666667e-01  1.46000000e-01  1.54133333e+00  1.10133333e+00]\n",
      " [ 6.56666667e-01 -5.40000000e-02  1.74133333e+00  6.01333333e-01]\n",
      " [ 1.85666667e+00  7.46000000e-01  2.94133333e+00  1.00133333e+00]\n",
      " [ 1.85666667e+00 -4.54000000e-01  3.14133333e+00  1.10133333e+00]\n",
      " [ 1.56666667e-01 -8.54000000e-01  1.24133333e+00  3.01333333e-01]\n",
      " [ 1.05666667e+00  1.46000000e-01  1.94133333e+00  1.10133333e+00]\n",
      " [-2.43333333e-01 -2.54000000e-01  1.14133333e+00  8.01333333e-01]\n",
      " [ 1.85666667e+00 -2.54000000e-01  2.94133333e+00  8.01333333e-01]\n",
      " [ 4.56666667e-01 -3.54000000e-01  1.14133333e+00  6.01333333e-01]\n",
      " [ 8.56666667e-01  2.46000000e-01  1.94133333e+00  9.01333333e-01]\n",
      " [ 1.35666667e+00  1.46000000e-01  2.24133333e+00  6.01333333e-01]\n",
      " [ 3.56666667e-01 -2.54000000e-01  1.04133333e+00  6.01333333e-01]\n",
      " [ 2.56666667e-01 -5.40000000e-02  1.14133333e+00  6.01333333e-01]\n",
      " [ 5.56666667e-01 -2.54000000e-01  1.84133333e+00  9.01333333e-01]\n",
      " [ 1.35666667e+00 -5.40000000e-02  2.04133333e+00  4.01333333e-01]\n",
      " [ 1.55666667e+00 -2.54000000e-01  2.34133333e+00  7.01333333e-01]\n",
      " [ 2.05666667e+00  7.46000000e-01  2.64133333e+00  8.01333333e-01]\n",
      " [ 5.56666667e-01 -2.54000000e-01  1.84133333e+00  1.00133333e+00]\n",
      " [ 4.56666667e-01 -2.54000000e-01  1.34133333e+00  3.01333333e-01]\n",
      " [ 2.56666667e-01 -4.54000000e-01  1.84133333e+00  2.01333333e-01]\n",
      " [ 1.85666667e+00 -5.40000000e-02  2.34133333e+00  1.10133333e+00]\n",
      " [ 4.56666667e-01  3.46000000e-01  1.84133333e+00  1.20133333e+00]\n",
      " [ 5.56666667e-01  4.60000000e-02  1.74133333e+00  6.01333333e-01]\n",
      " [ 1.56666667e-01 -5.40000000e-02  1.04133333e+00  6.01333333e-01]\n",
      " [ 1.05666667e+00  4.60000000e-02  1.64133333e+00  9.01333333e-01]\n",
      " [ 8.56666667e-01  4.60000000e-02  1.84133333e+00  1.20133333e+00]\n",
      " [ 1.05666667e+00  4.60000000e-02  1.34133333e+00  1.10133333e+00]\n",
      " [-4.33333333e-02 -3.54000000e-01  1.34133333e+00  7.01333333e-01]\n",
      " [ 9.56666667e-01  1.46000000e-01  2.14133333e+00  1.10133333e+00]\n",
      " [ 8.56666667e-01  2.46000000e-01  1.94133333e+00  1.30133333e+00]\n",
      " [ 8.56666667e-01 -5.40000000e-02  1.44133333e+00  1.10133333e+00]\n",
      " [ 4.56666667e-01 -5.54000000e-01  1.24133333e+00  7.01333333e-01]\n",
      " [ 6.56666667e-01 -5.40000000e-02  1.44133333e+00  8.01333333e-01]\n",
      " [ 3.56666667e-01  3.46000000e-01  1.64133333e+00  1.10133333e+00]\n",
      " [ 5.66666667e-02 -5.40000000e-02  1.34133333e+00  6.01333333e-01]]\n"
     ]
    }
   ],
   "source": [
    "temp_mean= np.mean(x,axis=0)      #the mean value of x\n",
    "x = x-temp_mean\n",
    "\n",
    "print(x) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Spliting the datasets into 80% train set and 20% test set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120\n",
      "[87, 57, 135, 1, 34, 16, 101, 83, 137, 130, 51, 115, 142, 91, 103, 118, 19, 141, 140, 49, 122, 15, 126, 123, 95, 62, 53, 26, 12, 3, 100, 108, 24, 138, 67, 46, 59, 92, 66, 124, 102, 31, 28, 111, 45, 80, 18, 77, 47, 117, 133, 63, 0, 139, 132, 14, 134, 131, 11, 39, 119, 43, 70, 35, 114, 64, 145, 6, 106, 76, 72, 44, 86, 74, 129, 144, 4, 54, 52, 127, 149, 136, 148, 96, 113, 93, 85, 50, 121, 112, 105, 8, 37, 110, 89, 42, 10, 71, 30, 99, 27, 32, 147, 55, 94, 20, 48, 104, 7, 5, 88, 41, 33, 107, 68, 25, 73, 65, 13, 79]\n"
     ]
    }
   ],
   "source": [
    "train = random.sample(range(0,150),120)\n",
    "print(len(train))\n",
    "print(train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n",
      "[2, 9, 17, 21, 22, 23, 29, 36, 38, 40, 56, 58, 60, 61, 69, 75, 78, 81, 82, 84, 90, 97, 98, 109, 116, 120, 125, 128, 143, 146]\n"
     ]
    }
   ],
   "source": [
    "validation = [i for i in range (150) if not (i in train)]\n",
    "print(len(validation))\n",
    "print(validation)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Here x_train is our input training data and y_train is our output training data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(120, 4)\n",
      "[[ 4.56666667e-01 -7.54000000e-01  6.41333333e-01  1.01333333e-01]\n",
      " [-9.43333333e-01 -6.54000000e-01 -4.58666667e-01 -1.98666667e-01]\n",
      " [ 1.85666667e+00 -5.40000000e-02  2.34133333e+00  1.10133333e+00]\n",
      " [-9.43333333e-01 -5.40000000e-02 -2.35866667e+00 -9.98666667e-01]\n",
      " [-9.43333333e-01  4.60000000e-02 -2.25866667e+00 -1.09866667e+00]\n",
      " [-4.43333333e-01  8.46000000e-01 -2.45866667e+00 -7.98666667e-01]\n",
      " [-4.33333333e-02 -3.54000000e-01  1.34133333e+00  7.01333333e-01]\n",
      " [ 1.56666667e-01 -3.54000000e-01  1.34133333e+00  4.01333333e-01]\n",
      " [ 5.56666667e-01  4.60000000e-02  1.74133333e+00  6.01333333e-01]\n",
      " [ 1.55666667e+00 -2.54000000e-01  2.34133333e+00  7.01333333e-01]\n",
      " [ 5.56666667e-01  1.46000000e-01  7.41333333e-01  3.01333333e-01]\n",
      " [ 5.56666667e-01  1.46000000e-01  1.54133333e+00  1.10133333e+00]\n",
      " [-4.33333333e-02 -3.54000000e-01  1.34133333e+00  7.01333333e-01]\n",
      " [ 2.56666667e-01 -5.40000000e-02  8.41333333e-01  2.01333333e-01]\n",
      " [ 4.56666667e-01 -1.54000000e-01  1.84133333e+00  6.01333333e-01]\n",
      " [ 1.85666667e+00 -4.54000000e-01  3.14133333e+00  1.10133333e+00]\n",
      " [-7.43333333e-01  7.46000000e-01 -2.25866667e+00 -8.98666667e-01]\n",
      " [ 1.05666667e+00  4.60000000e-02  1.34133333e+00  1.10133333e+00]\n",
      " [ 8.56666667e-01  4.60000000e-02  1.84133333e+00  1.20133333e+00]\n",
      " [-8.43333333e-01  2.46000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [ 1.85666667e+00 -2.54000000e-01  2.94133333e+00  8.01333333e-01]\n",
      " [-1.43333333e-01  1.34600000e+00 -2.25866667e+00 -7.98666667e-01]\n",
      " [ 3.56666667e-01 -2.54000000e-01  1.04133333e+00  6.01333333e-01]\n",
      " [ 4.56666667e-01 -3.54000000e-01  1.14133333e+00  6.01333333e-01]\n",
      " [-1.43333333e-01 -5.40000000e-02  4.41333333e-01  1.33333333e-03]\n",
      " [ 1.56666667e-01 -8.54000000e-01  2.41333333e-01 -1.98666667e-01]\n",
      " [-3.43333333e-01 -7.54000000e-01  2.41333333e-01  1.01333333e-01]\n",
      " [-8.43333333e-01  3.46000000e-01 -2.15866667e+00 -7.98666667e-01]\n",
      " [-1.04333333e+00 -5.40000000e-02 -2.35866667e+00 -1.09866667e+00]\n",
      " [-1.24333333e+00  4.60000000e-02 -2.25866667e+00 -9.98666667e-01]\n",
      " [ 4.56666667e-01  2.46000000e-01  2.24133333e+00  1.30133333e+00]\n",
      " [ 8.56666667e-01 -5.54000000e-01  2.04133333e+00  6.01333333e-01]\n",
      " [-1.04333333e+00  3.46000000e-01 -1.85866667e+00 -9.98666667e-01]\n",
      " [ 1.56666667e-01 -5.40000000e-02  1.04133333e+00  6.01333333e-01]\n",
      " [-4.33333333e-02 -3.54000000e-01  3.41333333e-01 -1.98666667e-01]\n",
      " [-7.43333333e-01  7.46000000e-01 -2.15866667e+00 -9.98666667e-01]\n",
      " [-6.43333333e-01 -3.54000000e-01  1.41333333e-01  2.01333333e-01]\n",
      " [-4.33333333e-02 -4.54000000e-01  2.41333333e-01  1.33333333e-03]\n",
      " [-2.43333333e-01 -5.40000000e-02  7.41333333e-01  3.01333333e-01]\n",
      " [ 8.56666667e-01  2.46000000e-01  1.94133333e+00  9.01333333e-01]\n",
      " [ 1.25666667e+00 -5.40000000e-02  2.14133333e+00  9.01333333e-01]\n",
      " [-4.43333333e-01  3.46000000e-01 -2.25866667e+00 -7.98666667e-01]\n",
      " [-6.43333333e-01  3.46000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [ 5.56666667e-01 -3.54000000e-01  1.54133333e+00  7.01333333e-01]\n",
      " [-1.04333333e+00 -5.40000000e-02 -2.35866667e+00 -8.98666667e-01]\n",
      " [-3.43333333e-01 -6.54000000e-01  4.13333333e-02 -9.86666667e-02]\n",
      " [-1.43333333e-01  7.46000000e-01 -2.05866667e+00 -8.98666667e-01]\n",
      " [ 8.56666667e-01 -5.40000000e-02  1.24133333e+00  5.01333333e-01]\n",
      " [-1.24333333e+00  1.46000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [ 1.85666667e+00  7.46000000e-01  2.94133333e+00  1.00133333e+00]\n",
      " [ 4.56666667e-01 -2.54000000e-01  1.34133333e+00  3.01333333e-01]\n",
      " [ 2.56666667e-01 -1.54000000e-01  9.41333333e-01  2.01333333e-01]\n",
      " [-7.43333333e-01  4.46000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [ 1.05666667e+00  4.60000000e-02  1.64133333e+00  9.01333333e-01]\n",
      " [ 5.56666667e-01 -2.54000000e-01  1.84133333e+00  1.00133333e+00]\n",
      " [-4.33333333e-02  9.46000000e-01 -2.55866667e+00 -9.98666667e-01]\n",
      " [ 2.56666667e-01 -4.54000000e-01  1.84133333e+00  2.01333333e-01]\n",
      " [ 2.05666667e+00  7.46000000e-01  2.64133333e+00  8.01333333e-01]\n",
      " [-1.04333333e+00  3.46000000e-01 -2.15866667e+00 -9.98666667e-01]\n",
      " [-7.43333333e-01  3.46000000e-01 -2.25866667e+00 -9.98666667e-01]\n",
      " [ 1.56666667e-01 -8.54000000e-01  1.24133333e+00  3.01333333e-01]\n",
      " [-8.43333333e-01  4.46000000e-01 -2.15866667e+00 -5.98666667e-01]\n",
      " [ 5.66666667e-02  1.46000000e-01  1.04133333e+00  6.01333333e-01]\n",
      " [-8.43333333e-01  1.46000000e-01 -2.55866667e+00 -9.98666667e-01]\n",
      " [-4.33333333e-02 -2.54000000e-01  1.34133333e+00  1.20133333e+00]\n",
      " [-2.43333333e-01 -1.54000000e-01 -1.58666667e-01  1.01333333e-01]\n",
      " [ 8.56666667e-01 -5.40000000e-02  1.44133333e+00  1.10133333e+00]\n",
      " [-1.24333333e+00  3.46000000e-01 -2.35866667e+00 -8.98666667e-01]\n",
      " [-9.43333333e-01 -5.54000000e-01  7.41333333e-01  5.01333333e-01]\n",
      " [ 9.56666667e-01 -2.54000000e-01  1.04133333e+00  2.01333333e-01]\n",
      " [ 4.56666667e-01 -5.54000000e-01  1.14133333e+00  3.01333333e-01]\n",
      " [-7.43333333e-01  7.46000000e-01 -1.85866667e+00 -7.98666667e-01]\n",
      " [ 8.56666667e-01  4.60000000e-02  9.41333333e-01  3.01333333e-01]\n",
      " [ 5.56666667e-01 -1.54000000e-01  5.41333333e-01  1.01333333e-01]\n",
      " [ 1.35666667e+00 -5.40000000e-02  2.04133333e+00  4.01333333e-01]\n",
      " [ 8.56666667e-01  2.46000000e-01  1.94133333e+00  1.30133333e+00]\n",
      " [-8.43333333e-01  5.46000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [ 6.56666667e-01 -2.54000000e-01  8.41333333e-01  3.01333333e-01]\n",
      " [ 1.05666667e+00  4.60000000e-02  1.14133333e+00  3.01333333e-01]\n",
      " [ 2.56666667e-01 -5.40000000e-02  1.14133333e+00  6.01333333e-01]\n",
      " [ 5.66666667e-02 -5.40000000e-02  1.34133333e+00  6.01333333e-01]\n",
      " [ 4.56666667e-01  3.46000000e-01  1.84133333e+00  1.20133333e+00]\n",
      " [ 3.56666667e-01  3.46000000e-01  1.64133333e+00  1.10133333e+00]\n",
      " [-1.43333333e-01 -1.54000000e-01  4.41333333e-01  1.01333333e-01]\n",
      " [-1.43333333e-01 -5.54000000e-01  1.24133333e+00  8.01333333e-01]\n",
      " [-8.43333333e-01 -7.54000000e-01 -4.58666667e-01 -1.98666667e-01]\n",
      " [ 1.56666667e-01  3.46000000e-01  7.41333333e-01  4.01333333e-01]\n",
      " [ 1.15666667e+00  1.46000000e-01  9.41333333e-01  2.01333333e-01]\n",
      " [-2.43333333e-01 -2.54000000e-01  1.14133333e+00  8.01333333e-01]\n",
      " [ 9.56666667e-01 -5.40000000e-02  1.74133333e+00  9.01333333e-01]\n",
      " [ 1.75666667e+00 -5.40000000e-02  2.84133333e+00  9.01333333e-01]\n",
      " [-1.44333333e+00 -1.54000000e-01 -2.35866667e+00 -9.98666667e-01]\n",
      " [-9.43333333e-01  4.60000000e-02 -2.25866667e+00 -1.09866667e+00]\n",
      " [ 6.56666667e-01  1.46000000e-01  1.34133333e+00  8.01333333e-01]\n",
      " [-3.43333333e-01 -5.54000000e-01  2.41333333e-01  1.01333333e-01]\n",
      " [-1.44333333e+00  1.46000000e-01 -2.45866667e+00 -9.98666667e-01]\n",
      " [-4.43333333e-01  6.46000000e-01 -2.25866667e+00 -9.98666667e-01]\n",
      " [ 2.56666667e-01 -2.54000000e-01  2.41333333e-01  1.01333333e-01]\n",
      " [-1.04333333e+00  4.60000000e-02 -2.15866667e+00 -9.98666667e-01]\n",
      " [-1.43333333e-01 -2.54000000e-01  3.41333333e-01  1.01333333e-01]\n",
      " [-6.43333333e-01  4.46000000e-01 -2.25866667e+00 -9.98666667e-01]\n",
      " [-6.43333333e-01  1.04600000e+00 -2.25866667e+00 -1.09866667e+00]\n",
      " [ 6.56666667e-01 -5.40000000e-02  1.44133333e+00  8.01333333e-01]\n",
      " [-1.43333333e-01 -2.54000000e-01  7.41333333e-01  1.01333333e-01]\n",
      " [-2.43333333e-01 -3.54000000e-01  4.41333333e-01  1.01333333e-01]\n",
      " [-4.43333333e-01  3.46000000e-01 -2.05866667e+00 -9.98666667e-01]\n",
      " [-5.43333333e-01  6.46000000e-01 -2.25866667e+00 -9.98666667e-01]\n",
      " [ 6.56666667e-01 -5.40000000e-02  2.04133333e+00  1.00133333e+00]\n",
      " [-8.43333333e-01  3.46000000e-01 -2.25866667e+00 -9.98666667e-01]\n",
      " [-4.43333333e-01  8.46000000e-01 -2.05866667e+00 -7.98666667e-01]\n",
      " [-2.43333333e-01 -5.40000000e-02  3.41333333e-01  1.01333333e-01]\n",
      " [-1.34333333e+00 -7.54000000e-01 -2.45866667e+00 -8.98666667e-01]\n",
      " [-3.43333333e-01  1.14600000e+00 -2.35866667e+00 -9.98666667e-01]\n",
      " [ 1.45666667e+00 -1.54000000e-01  2.54133333e+00  6.01333333e-01]\n",
      " [ 3.56666667e-01 -8.54000000e-01  7.41333333e-01  3.01333333e-01]\n",
      " [-8.43333333e-01 -5.40000000e-02 -2.15866667e+00 -9.98666667e-01]\n",
      " [ 2.56666667e-01 -2.54000000e-01  9.41333333e-01  1.33333333e-03]\n",
      " [ 8.56666667e-01  4.60000000e-02  6.41333333e-01  2.01333333e-01]\n",
      " [-1.54333333e+00 -5.40000000e-02 -2.65866667e+00 -1.09866667e+00]\n",
      " [-1.43333333e-01 -4.54000000e-01 -2.58666667e-01 -1.98666667e-01]]\n"
     ]
    }
   ],
   "source": [
    "x_train=(x[train,:4])\n",
    "print(x_train.shape)\n",
    "print (x_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Now we added a column of 1 in the x_train array "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(120, 5)\n"
     ]
    }
   ],
   "source": [
    "x_train = np.concatenate((x_train,np.ones((120,1),dtype=float)),axis=1)\n",
    "print(x_train.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Now we defined a array y_train of 120*3 whose each every element is 0 and it will define the output \n",
    "### If one data is of first class of 3 then it will make the first element of that row 1 , similarly for 2nd class second element of the row becomes 1 and for last class 3rd element of the row of y_train will become 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "y_train = np.zeros((120,3)) \n",
    "print(y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]]\n"
     ]
    }
   ],
   "source": [
    "for i,j in zip(train,range(120)):\n",
    "    if y[i]=='Iris-setosa':\n",
    "        y_train[j,0]=1                 # for iris_data in setosa make 1 in y_train\n",
    "    if y[i]=='Iris-versicolor':\n",
    "        y_train[j,1]=1                 # for iris_data in versicolor make 1 in y_train\n",
    "    if y[i]=='Iris-virginica':\n",
    "        y_train[j,2]=1                 # for iris_data in virginica make 1 in y_train\n",
    "print(y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Similarly we will proceed for the validation dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.14333333e+00  1.46000000e-01 -2.45866667e+00 -9.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [-9.43333333e-01  4.60000000e-02 -2.25866667e+00 -1.09866667e+00\n",
      "   1.00000000e+00]\n",
      " [-7.43333333e-01  4.46000000e-01 -2.35866667e+00 -8.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [-7.43333333e-01  6.46000000e-01 -2.25866667e+00 -7.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [-1.24333333e+00  5.46000000e-01 -2.75866667e+00 -9.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [-7.43333333e-01  2.46000000e-01 -2.05866667e+00 -6.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [-1.14333333e+00  1.46000000e-01 -2.15866667e+00 -9.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [-3.43333333e-01  4.46000000e-01 -2.45866667e+00 -9.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [-1.44333333e+00 -5.40000000e-02 -2.45866667e+00 -9.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [-8.43333333e-01  4.46000000e-01 -2.45866667e+00 -8.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [ 4.56666667e-01  2.46000000e-01  9.41333333e-01  4.01333333e-01\n",
      "   1.00000000e+00]\n",
      " [ 7.56666667e-01 -1.54000000e-01  8.41333333e-01  1.01333333e-01\n",
      "   1.00000000e+00]\n",
      " [-8.43333333e-01 -1.05400000e+00 -2.58666667e-01 -1.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [ 5.66666667e-02 -5.40000000e-02  4.41333333e-01  3.01333333e-01\n",
      "   1.00000000e+00]\n",
      " [-2.43333333e-01 -5.54000000e-01  1.41333333e-01 -9.86666667e-02\n",
      "   1.00000000e+00]\n",
      " [ 7.56666667e-01 -5.40000000e-02  6.41333333e-01  2.01333333e-01\n",
      "   1.00000000e+00]\n",
      " [ 1.56666667e-01 -1.54000000e-01  7.41333333e-01  3.01333333e-01\n",
      "   1.00000000e+00]\n",
      " [-3.43333333e-01 -6.54000000e-01 -5.86666667e-02 -1.98666667e-01\n",
      "   1.00000000e+00]\n",
      " [-4.33333333e-02 -3.54000000e-01  1.41333333e-01  1.33333333e-03\n",
      "   1.00000000e+00]\n",
      " [-4.43333333e-01 -5.40000000e-02  7.41333333e-01  3.01333333e-01\n",
      "   1.00000000e+00]\n",
      " [-3.43333333e-01 -4.54000000e-01  6.41333333e-01  1.33333333e-03\n",
      "   1.00000000e+00]\n",
      " [ 3.56666667e-01 -1.54000000e-01  5.41333333e-01  1.01333333e-01\n",
      "   1.00000000e+00]\n",
      " [-7.43333333e-01 -5.54000000e-01 -7.58666667e-01 -9.86666667e-02\n",
      "   1.00000000e+00]\n",
      " [ 1.35666667e+00  5.46000000e-01  2.34133333e+00  1.30133333e+00\n",
      "   1.00000000e+00]\n",
      " [ 6.56666667e-01 -5.40000000e-02  1.74133333e+00  6.01333333e-01\n",
      "   1.00000000e+00]\n",
      " [ 1.05666667e+00  1.46000000e-01  1.94133333e+00  1.10133333e+00\n",
      "   1.00000000e+00]\n",
      " [ 1.35666667e+00  1.46000000e-01  2.24133333e+00  6.01333333e-01\n",
      "   1.00000000e+00]\n",
      " [ 5.56666667e-01 -2.54000000e-01  1.84133333e+00  9.01333333e-01\n",
      "   1.00000000e+00]\n",
      " [ 9.56666667e-01  1.46000000e-01  2.14133333e+00  1.10133333e+00\n",
      "   1.00000000e+00]\n",
      " [ 4.56666667e-01 -5.54000000e-01  1.24133333e+00  7.01333333e-01\n",
      "   1.00000000e+00]]\n"
     ]
    }
   ],
   "source": [
    "x_test =(x[validation,:4])\n",
    "x_test = np.concatenate((x_test,np.ones((30,1))),axis=1)\n",
    "print(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "y_test = np.zeros((30,3))\n",
    "print(y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Simialrly we change the y_test array as per given data in the dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "for i,j in zip(validation,range(120)):\n",
    "    if y[i]=='Iris-setosa':\n",
    "        y_test[j,0]=1             # for iris_data in setosa make 1 in y_train\n",
    "    if y[i]=='Iris-versicolor':\n",
    "        y_test[j,1]=1             # for iris_data in vericolor make 1 in y_train\n",
    "    if y[i]=='Iris-virginica':\n",
    "        y_test[j,2]=1             # for iris_data in virginica make 1 in y_train\n",
    "print(y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gradient descent \n",
    "### Now we will define theta a 5*3 matrix which will give gradient for each parameter  for each class\n",
    "\n",
    "Weight matrix for logistice regression will have shape = (no_of_features + 1, no_of_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def initialize_parameters(x,classes):\n",
    "    \n",
    "    n = x.shape[1]\n",
    "    theta = np.random.rand(n, classes)*0.01\n",
    "    \n",
    "    return theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict_prob(x, theta):\n",
    "    \n",
    "    h = sigmoid(np.dot(x,theta))\n",
    "    \n",
    "    return h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "def accuracy(h, y):\n",
    "    \n",
    "    m = y.shape[0]\n",
    "    h = np.argmax(h,axis=1)                          # max value in each row of h array of sigmoid function\n",
    "    q = np.argmax(y,axis=1) == h                     # check how many output we got correct\n",
    "    print(np.sum(q*100/m))\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def model(x, y, classes = 3, learning_rate = 1, iterations = 1000):\n",
    "    \n",
    "    m = x.shape[0]\n",
    "    theta = initialize_parameters(x, classes)\n",
    "    \n",
    "    for i in range (iterations):\n",
    "        h = predict_prob(x, theta)                          # sigmoid function update\n",
    "        theta -= ((learning_rate)/m)*(np.dot((h-y).T , x).T)    # theta update\n",
    "    \n",
    "    accuracy(h,y)\n",
    "    \n",
    "    return theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "96.66666666666667\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[  0.36559371,  -0.12728254,  -2.44976641],\n",
       "       [  3.9090293 ,  -2.61883156,  -4.97912021],\n",
       "       [ -5.8049886 ,   1.12745913,   7.95669333],\n",
       "       [ -2.30917546,  -2.47779736,  14.04087279],\n",
       "       [ -6.16434421,  -1.10686071, -15.92921529]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta = model(x_train, y_train, learning_rate = 3, iterations = 5000)\n",
    "theta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Now we will test our test data set and see the accuracy "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99.99999999999997\n"
     ]
    }
   ],
   "source": [
    "h = predict_prob(x_test, theta)\n",
    "accuracy(h, y_test)"
   ]
  }
 ],
 "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
