{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Step 1 :- DATAPREPROCESSING "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Importing necessary libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "from sklearn.preprocessing import LabelEncoder , OneHotEncoder \n",
    "from sklearn.impute import SimpleImputer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from comet_ml import Experiment\n",
    "# experiment = Experiment(api_key='Dz2W3DAahv0OvSAERUfhA5b7I')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Importing of data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "first 5 rows in data (df)\n",
      "   Country   Age   Salary Purchased\n",
      "0   France  44.0  72000.0        No\n",
      "1    Spain  27.0  48000.0       Yes\n",
      "2  Germany  30.0  54000.0        No\n",
      "3    Spain  38.0  61000.0        No\n",
      "4  Germany  40.0      NaN       Yes\n",
      "\n",
      "Statics of the data\n",
      "             Age        Salary\n",
      "count   9.000000      9.000000\n",
      "mean   38.777778  63777.777778\n",
      "std     7.693793  12265.579662\n",
      "min    27.000000  48000.000000\n",
      "25%    35.000000  54000.000000\n",
      "50%    38.000000  61000.000000\n",
      "75%    44.000000  72000.000000\n",
      "max    50.000000  83000.000000\n",
      "\n",
      "Describing the data\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 10 entries, 0 to 9\n",
      "Data columns (total 4 columns):\n",
      " #   Column     Non-Null Count  Dtype  \n",
      "---  ------     --------------  -----  \n",
      " 0   Country    10 non-null     object \n",
      " 1   Age        9 non-null      float64\n",
      " 2   Salary     9 non-null      float64\n",
      " 3   Purchased  10 non-null     object \n",
      "dtypes: float64(2), object(2)\n",
      "memory usage: 448.0+ bytes\n",
      "None\n",
      "\n",
      "Presenting the num values in data\n",
      "Country      0\n",
      "Age          1\n",
      "Salary       1\n",
      "Purchased    0\n",
      "dtype: int64\n",
      "\n",
      "Telling us is there is null value in the data or not\n",
      "True\n",
      "\n",
      "Giving the sum of null values\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv('Data.csv')\n",
    "print('first 5 rows in data (df)')\n",
    "print(df.head() , end='\\n\\n')\n",
    "print('Statics of the data')\n",
    "print(df.describe() , end='\\n\\n')\n",
    "print('Describing the data')\n",
    "print(df.info() ,end='\\n\\n')\n",
    "print('Presenting the num values in data')\n",
    "print(df.isnull().sum() ,end='\\n\\n')\n",
    "print('Telling us is there is null value in the data or not')\n",
    "print(df.isnull().values.any() , end='\\n\\n')\n",
    "print('Giving the sum of null values')\n",
    "print(df.isnull().sum().sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# creating a list \n",
    "X1 = df.iloc[: , [0,1,2]].values\n",
    "y1 = df.iloc[: , 3].values\n",
    "\n",
    "# sends the data in variable\n",
    "X = df.drop('Purchased' , axis = 1)\n",
    "y = df.Purchased"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['France' 44.0 72000.0]\n",
      " ['Spain' 27.0 48000.0]\n",
      " ['Germany' 30.0 54000.0]\n",
      " ['Spain' 38.0 61000.0]\n",
      " ['Germany' 40.0 nan]\n",
      " ['France' 35.0 58000.0]\n",
      " ['Spain' nan 52000.0]\n",
      " ['France' 48.0 79000.0]\n",
      " ['Germany' 50.0 83000.0]\n",
      " ['France' 37.0 67000.0]]\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "   Country   Age   Salary\n",
      "0   France  44.0  72000.0\n",
      "1    Spain  27.0  48000.0\n",
      "2  Germany  30.0  54000.0\n",
      "3    Spain  38.0  61000.0\n",
      "4  Germany  40.0      NaN\n"
     ]
    }
   ],
   "source": [
    "print(X1)\n",
    "print('\\n\\n\\n')\n",
    "print(X.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MISSING VALUES"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "imputer = SimpleImputer(missing_values=np.nan , strategy='mean' )\n",
    "imputer = imputer.fit(X1[:, 1:3])\n",
    "X1[:,1:3] = imputer.transform(X1[:,1:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Age          38.777778\n",
       "Salary    63777.777778\n",
       "dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.select_dtypes(exclude='object').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# impute = SimpleImputer(missing_values=np.nan , strategy='mean')\n",
    "# impute = impute.fit(X.select_dtypes(exclude='object'))\n",
    "# X[:1:3] = impute.transform(X.select_dtypes(exclude='object'))\n",
    "X.Age.fillna(value=np.mean(X.Age) , inplace=True)\n",
    "X.Salary.fillna(value=np.mean(X.Salary) , inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   Country        Age        Salary\n",
      "0   France  44.000000  72000.000000\n",
      "1    Spain  27.000000  48000.000000\n",
      "2  Germany  30.000000  54000.000000\n",
      "3    Spain  38.000000  61000.000000\n",
      "4  Germany  40.000000  63777.777778\n",
      "5   France  35.000000  58000.000000\n",
      "6    Spain  38.777778  52000.000000\n",
      "7   France  48.000000  79000.000000\n",
      "8  Germany  50.000000  83000.000000\n",
      "9   France  37.000000  67000.000000\n",
      "\n",
      "\n",
      "\n",
      "[['France' 44.0 72000.0]\n",
      " ['Spain' 27.0 48000.0]\n",
      " ['Germany' 30.0 54000.0]\n",
      " ['Spain' 38.0 61000.0]\n",
      " ['Germany' 40.0 63777.77777777778]\n",
      " ['France' 35.0 58000.0]\n",
      " ['Spain' 38.77777777777778 52000.0]\n",
      " ['France' 48.0 79000.0]\n",
      " ['Germany' 50.0 83000.0]\n",
      " ['France' 37.0 67000.0]]\n"
     ]
    }
   ],
   "source": [
    "print(X)\n",
    "print('\\n\\n')\n",
    "print(X1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Encoding Categorical data\n",
    "#### Encoding independent variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "Lebcoder1 = LabelEncoder()\n",
    "X1[:,0] = Lebcoder1.fit_transform(X1[:,0])\n",
    "lebcoder = LabelEncoder()\n",
    "X.Country = lebcoder.fit_transform(X.Country)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "OHencoder1 = OneHotEncoder()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "X1 = OHencoder1.fit_transform(X1).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "OHencoder = OneHotEncoder()\n",
    "X = OHencoder.fit_transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "labelencoder_y = LabelEncoder()\n",
    "y = labelencoder_y.fit_transform(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "lablencoder_y1 = LabelEncoder()\n",
    "y1 = lablencoder_y1.fit_transform(y1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
