{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.linear_model import LinearRegression, Ridge, Lasso\n",
    "from sklearn.model_selection import train_test_split, cross_val_score\n",
    "from statistics import mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(21613, 21) \n",
      "\n",
      "id                 int64\n",
      "date              object\n",
      "price            float64\n",
      "bedrooms           int64\n",
      "bathrooms        float64\n",
      "sqft_living        int64\n",
      "sqft_lot           int64\n",
      "floors           float64\n",
      "waterfront         int64\n",
      "view               int64\n",
      "condition          int64\n",
      "grade              int64\n",
      "sqft_above       float64\n",
      "sqft_basement      int64\n",
      "yr_built           int64\n",
      "yr_renovated       int64\n",
      "zipcode            int64\n",
      "lat              float64\n",
      "long             float64\n",
      "sqft_living15      int64\n",
      "sqft_lot15         int64\n",
      "dtype: object \n",
      "\n",
      "price            0\n",
      "bedrooms         0\n",
      "bathrooms        0\n",
      "sqft_living      0\n",
      "sqft_lot         0\n",
      "floors           0\n",
      "waterfront       0\n",
      "view             0\n",
      "condition        0\n",
      "grade            0\n",
      "sqft_above       2\n",
      "sqft_basement    0\n",
      "yr_built         0\n",
      "yr_renovated     0\n",
      "lat              0\n",
      "long             0\n",
      "sqft_living15    0\n",
      "sqft_lot15       0\n",
      "dtype: int64 \n",
      "\n",
      "price            0\n",
      "bedrooms         0\n",
      "bathrooms        0\n",
      "sqft_living      0\n",
      "sqft_lot         0\n",
      "floors           0\n",
      "waterfront       0\n",
      "view             0\n",
      "condition        0\n",
      "grade            0\n",
      "sqft_above       0\n",
      "sqft_basement    0\n",
      "yr_built         0\n",
      "yr_renovated     0\n",
      "lat              0\n",
      "long             0\n",
      "sqft_living15    0\n",
      "sqft_lot15       0\n",
      "dtype: int64 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Loading the data into a Pandas DataFrame\n",
    "data = pd.read_csv('kc_house_data.csv')\n",
    "\n",
    "print(data.shape, \"\\n\")\n",
    "\n",
    "print(data.dtypes, \"\\n\")\n",
    "\n",
    "# Dropping the numerically non-sensical variables\n",
    "dropColumns = ['id', 'date', 'zipcode']\n",
    "data = data.drop(dropColumns, axis = 1)\n",
    "\n",
    "print(data.isna().sum(), \"\\n\")\n",
    "\n",
    "data = data.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Separating the dependent and independent variables\n",
    "y = data['price']\n",
    "X = data.drop('price', axis = 1)\n",
    "\n",
    "# Dividing the data into training and testing set\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6993475629705098\n"
     ]
    }
   ],
   "source": [
    "# Bulding and fitting the Linear Regression model\n",
    "linearModel = LinearRegression()\n",
    "linearModel.fit(X_train, y_train)\n",
    "\n",
    "# Evaluating the Linear Regression model\n",
    "print(linearModel.score(X_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.25 : 69.09015837671018\n",
      "0.5 : 69.09033468066409\n",
      "0.75 : 69.09049125083848\n",
      "1.0 : 69.0906282899928\n",
      "1.25 : 69.09074599856257\n",
      "1.5 : 69.09084457469044\n",
      "1.75 : 69.09092421425662\n",
      "2.0 : 69.0909851109089\n"
     ]
    }
   ],
   "source": [
    "# List to maintain the different cross-validation scores\n",
    "cross_val_scores_ridge = []\n",
    "\n",
    "# List to maintain the different values of alpha\n",
    "alpha = []\n",
    "\n",
    "# Loop to compute the different values of cross-validation scores\n",
    "for i in range(1, 9):\n",
    "    ridgeModel = Ridge(alpha = i * 0.25)\n",
    "    ridgeModel.fit(X_train, y_train)\n",
    "    scores = cross_val_score(ridgeModel, X, y, cv = 10)\n",
    "    avg_cross_val_score = mean(scores)*100\n",
    "    cross_val_scores_ridge.append(avg_cross_val_score)\n",
    "    alpha.append(i * 0.25)\n",
    "\n",
    "# Loop to print the different values of cross-validation scores\n",
    "for i in range(0, len(alpha)):\n",
    "    print(str(alpha[i])+' : '+str(cross_val_scores_ridge[i]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.699325181696304\n"
     ]
    }
   ],
   "source": [
    "# Building and fitting the Ridge Regression model\n",
    "ridgeModelChosen = Ridge(alpha = 2)\n",
    "ridgeModelChosen.fit(X_train, y_train)\n",
    "\n",
    "# Evaluating the Ridge Regression model\n",
    "print(ridgeModelChosen.score(X_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.25 : 69.08996762461636\n",
      "0.5 : 69.08997309354582\n",
      "0.75 : 69.08997853389323\n",
      "1.0 : 69.08998394768227\n",
      "1.25 : 69.08998932707658\n",
      "1.5 : 69.08999467945566\n",
      "1.75 : 69.0900000188296\n",
      "2.0 : 69.09000531192429\n"
     ]
    }
   ],
   "source": [
    "# List to maintain the cross-validation scores\n",
    "cross_val_scores_lasso = []\n",
    "\n",
    "# List to maintain the different values of Lambda\n",
    "Lambda = []\n",
    "\n",
    "# Loop to compute the cross-validation scores\n",
    "for i in range(1, 9):\n",
    "    lassoModel = Lasso(alpha = i * 0.25, tol = 0.0925)\n",
    "    lassoModel.fit(X_train, y_train)\n",
    "    scores = cross_val_score(lassoModel, X, y, cv = 10)\n",
    "    avg_cross_val_score = mean(scores)*100\n",
    "    cross_val_scores_lasso.append(avg_cross_val_score)\n",
    "    Lambda.append(i * 0.25)\n",
    "\n",
    "# Loop to print the different values of cross-validation scores\n",
    "for i in range(0, len(alpha)):\n",
    "    print(str(alpha[i])+' : '+str(cross_val_scores_lasso[i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6993458530370762\n"
     ]
    }
   ],
   "source": [
    "# Building and fitting the Lasso Regression Model\n",
    "lassoModelChosen = Lasso(alpha = 2, tol = 0.0925)\n",
    "lassoModelChosen.fit(X_train, y_train)\n",
    "\n",
    "# Evaluating the Lasso Regression model\n",
    "print(lassoModelChosen.score(X_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Linear Regreesion : 0.6993475629705098\n",
      "Ridge Regreesion : 0.699325181696304\n",
      "Lasso Regression : 0.6993458530370762\n"
     ]
    }
   ],
   "source": [
    "# Building the two lists for visualization\n",
    "models = ['Linear Regression', 'Ridge Regression', 'Lasso Regression']\n",
    "scores = [linearModel.score(X_test, y_test), ridgeModelChosen.score(X_test, y_test), lassoModelChosen.score(X_test, y_test)]\n",
    "\n",
    "# Building the dictionary to compare the scores\n",
    "mapping = {}\n",
    "mapping['Linear Regreesion'] = linearModel.score(X_test, y_test)\n",
    "mapping['Ridge Regreesion'] = ridgeModelChosen.score(X_test, y_test)\n",
    "mapping['Lasso Regression'] = lassoModelChosen.score(X_test, y_test)\n",
    "\n",
    "# Printing the scores for different models\n",
    "for key, val in mapping.items():\n",
    "    print(str(key)+' : '+str(val))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 8948 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Plotting the scores\n",
    "plt.bar(models, scores)\n",
    "plt.xlabel('Regression Models')\n",
    "plt.ylabel('Score')\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
