{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "id": "5NzdG_lYBfEe"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "from sklearn.datasets import load_diabetes\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.linear_model import LinearRegression, Ridge, Lasso\n",
        "from sklearn.metrics import mean_squared_error, r2_score"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "data = load_diabetes()\n",
        "X = data.data\n",
        "y = data.target"
      ],
      "metadata": {
        "id": "_0KLCoJDBgO_"
      },
      "execution_count": 18,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "X_train, X_test, y_train, y_test = train_test_split(\n",
        "    X, y, test_size=0.2, random_state=42\n",
        ")"
      ],
      "metadata": {
        "id": "4V6zsU6wBgRb"
      },
      "execution_count": 19,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **building model without regularization**"
      ],
      "metadata": {
        "id": "-YMfFRLqSM_H"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "linear_model = LinearRegression()\n",
        "linear_model.fit(X_train, y_train)\n",
        "\n",
        "linear_pred = linear_model.predict(X_test)\n",
        "\n",
        "linear_mse = mean_squared_error(y_test, linear_pred)\n",
        "linear_r2 = r2_score(y_test, linear_pred)\n",
        "\n",
        "print(\"Linear Regression MSE:\", linear_mse)\n",
        "print(\"Linear Regression R2:\", linear_r2)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lol3dknmQUDb",
        "outputId": "523f8d57-45cc-4ce5-cdab-319f12c788b5"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Linear Regression MSE: 2900.193628493482\n",
            "Linear Regression R2: 0.4526027629719195\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **building model with L2 regularization**"
      ],
      "metadata": {
        "id": "yrLyVxwTVzp1"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "ridge_model = Ridge(alpha=1.0)\n",
        "ridge_model.fit(X_train, y_train)\n",
        "\n",
        "ridge_pred = ridge_model.predict(X_test)\n",
        "\n",
        "ridge_mse = mean_squared_error(y_test, ridge_pred)\n",
        "ridge_r2 = r2_score(y_test, ridge_pred)\n",
        "\n",
        "print(\"Ridge (L2) MSE:\", ridge_mse)\n",
        "print(\"Ridge (L2) R2 Score:\", ridge_r2)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "R15wLDsABgT2",
        "outputId": "d51262df-676f-4321-ef82-ed44cef3c039"
      },
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Ridge (L2) MSE: 3077.41593882723\n",
            "Ridge (L2) R2 Score: 0.41915292635986545\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **building model with L1 regularization**"
      ],
      "metadata": {
        "id": "uT6fa2rgV99A"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "lasso_model = Lasso(alpha=0.7)\n",
        "lasso_model.fit(X_train, y_train)\n",
        "\n",
        "lasso_pred = lasso_model.predict(X_test)\n",
        "\n",
        "lasso_mse = mean_squared_error(y_test, lasso_pred)\n",
        "lasso_r2 = r2_score(y_test, lasso_pred)\n",
        "\n",
        "print(\"Lasso (L1) MSE:\", lasso_mse)\n",
        "print(\"Lasso (L1) R2 Score:\", lasso_r2)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sYNw3aHtBgWJ",
        "outputId": "505ee204-8035-4a79-ea6b-b5509535f3ce"
      },
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Lasso (L1) MSE: 3097.856013107508\n",
            "Lasso (L1) R2 Score: 0.41529496319639037\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "uk58DglABlJK"
      },
      "execution_count": 22,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "DRYnODixBgYk"
      },
      "execution_count": 22,
      "outputs": []
    }
  ]
}