Skip to content

Instantly share code, notes, and snippets.

@ProfAvery
Last active October 25, 2024 00:12
Show Gist options
  • Save ProfAvery/114e7d50a8c3a546b7413981370777e1 to your computer and use it in GitHub Desktop.
Save ProfAvery/114e7d50a8c3a546b7413981370777e1 to your computer and use it in GitHub Desktop.
CPSC 375 Examples - Linear Regression
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/ProfAvery/114e7d50a8c3a546b7413981370777e1/regression-examples.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "h6SOxm7V2meC"
},
"source": [
"# CPSC 375 Examples - Linear Regression"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TiGPqnCW2meQ",
"outputId": "0effc0de-dac4-40e2-a5db-e127e34f202a"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Collecting lets-plot\n",
" Downloading lets_plot-4.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (11 kB)\n",
"Collecting ucimlrepo\n",
" Downloading ucimlrepo-0.0.7-py3-none-any.whl.metadata (5.5 kB)\n",
"Collecting pypng (from lets-plot)\n",
" Downloading pypng-0.20220715.0-py3-none-any.whl.metadata (13 kB)\n",
"Collecting palettable (from lets-plot)\n",
" Downloading palettable-3.3.3-py2.py3-none-any.whl.metadata (3.3 kB)\n",
"Requirement already satisfied: pandas>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from ucimlrepo) (2.2.2)\n",
"Requirement already satisfied: certifi>=2020.12.5 in /usr/local/lib/python3.10/dist-packages (from ucimlrepo) (2024.8.30)\n",
"Requirement already satisfied: numpy>=1.22.4 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.0.0->ucimlrepo) (1.26.4)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.0.0->ucimlrepo) (2.8.2)\n",
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.0.0->ucimlrepo) (2024.2)\n",
"Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.0.0->ucimlrepo) (2024.2)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas>=1.0.0->ucimlrepo) (1.16.0)\n",
"Downloading lets_plot-4.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m15.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading ucimlrepo-0.0.7-py3-none-any.whl (8.0 kB)\n",
"Downloading palettable-3.3.3-py2.py3-none-any.whl (332 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m332.3/332.3 kB\u001b[0m \u001b[31m6.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading pypng-0.20220715.0-py3-none-any.whl (58 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.1/58.1 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hInstalling collected packages: pypng, palettable, lets-plot, ucimlrepo\n",
"Successfully installed lets-plot-4.5.0 palettable-3.3.3 pypng-0.20220715.0 ucimlrepo-0.0.7\n"
]
}
],
"source": [
"%pip install lets-plot ucimlrepo\n",
"\n",
"from lets_plot import *\n",
"from ucimlrepo import fetch_ucirepo\n",
"\n",
"LetsPlot.setup_html()\n",
"auto_mpg = fetch_ucirepo(id=9)"
]
},
{
"cell_type": "code",
"source": [
"autompg = auto_mpg.data.original\n",
"autompg.cylinders = autompg.cylinders.astype('str').astype('category')"
],
"metadata": {
"id": "TuMlatWJ39-i"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "EavLIivi2mek"
},
"source": [
"## Part 8 - Slide 54"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Weq0YCgy2mel"
},
"source": [
"Consider the `auto_mpg.data.original` dataset.\n",
"\n",
"> The goal is to model mpg given engine displacement (a continuous variable) and number of cylinders (pretend this is a categorical variable).\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "tHd2kdIK2mey",
"outputId": "19af6098-0895-44a7-bfc5-254d84434d4b"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" car_name cylinders displacement horsepower weight \\\n",
"0 chevrolet,chevelle,malibu 8 307.0 130.0 3504 \n",
"1 buick,skylark,320 8 350.0 165.0 3693 \n",
"2 plymouth,satellite 8 318.0 150.0 3436 \n",
"3 amc,rebel,sst 8 304.0 150.0 3433 \n",
"4 ford,torino 8 302.0 140.0 3449 \n",
"\n",
" acceleration model_year origin mpg \n",
"0 12.0 70 1 18.0 \n",
"1 11.5 70 1 15.0 \n",
"2 11.0 70 1 18.0 \n",
"3 12.0 70 1 16.0 \n",
"4 10.5 70 1 17.0 "
],
"text/html": [
"\n",
" <div id=\"df-1360c070-cdf7-487c-a550-3881fbd9b472\" class=\"colab-df-container\">\n",
" <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>car_name</th>\n",
" <th>cylinders</th>\n",
" <th>displacement</th>\n",
" <th>horsepower</th>\n",
" <th>weight</th>\n",
" <th>acceleration</th>\n",
" <th>model_year</th>\n",
" <th>origin</th>\n",
" <th>mpg</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>chevrolet,chevelle,malibu</td>\n",
" <td>8</td>\n",
" <td>307.0</td>\n",
" <td>130.0</td>\n",
" <td>3504</td>\n",
" <td>12.0</td>\n",
" <td>70</td>\n",
" <td>1</td>\n",
" <td>18.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>buick,skylark,320</td>\n",
" <td>8</td>\n",
" <td>350.0</td>\n",
" <td>165.0</td>\n",
" <td>3693</td>\n",
" <td>11.5</td>\n",
" <td>70</td>\n",
" <td>1</td>\n",
" <td>15.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>plymouth,satellite</td>\n",
" <td>8</td>\n",
" <td>318.0</td>\n",
" <td>150.0</td>\n",
" <td>3436</td>\n",
" <td>11.0</td>\n",
" <td>70</td>\n",
" <td>1</td>\n",
" <td>18.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>amc,rebel,sst</td>\n",
" <td>8</td>\n",
" <td>304.0</td>\n",
" <td>150.0</td>\n",
" <td>3433</td>\n",
" <td>12.0</td>\n",
" <td>70</td>\n",
" <td>1</td>\n",
" <td>16.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>ford,torino</td>\n",
" <td>8</td>\n",
" <td>302.0</td>\n",
" <td>140.0</td>\n",
" <td>3449</td>\n",
" <td>10.5</td>\n",
" <td>70</td>\n",
" <td>1</td>\n",
" <td>17.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <div class=\"colab-df-buttons\">\n",
"\n",
" <div class=\"colab-df-container\">\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-1360c070-cdf7-487c-a550-3881fbd9b472')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
"\n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
" <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
" </svg>\n",
" </button>\n",
"\n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" .colab-df-buttons div {\n",
" margin-bottom: 4px;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-1360c070-cdf7-487c-a550-3881fbd9b472 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-1360c070-cdf7-487c-a550-3881fbd9b472');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
"\n",
"\n",
"<div id=\"df-583db96c-7a83-436e-a1e7-be26ba1ff21f\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-583db96c-7a83-436e-a1e7-be26ba1ff21f')\"\n",
" title=\"Suggest charts\"\n",
" style=\"display:none;\">\n",
"\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <g>\n",
" <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
" </g>\n",
"</svg>\n",
" </button>\n",
"\n",
"<style>\n",
" .colab-df-quickchart {\n",
" --bg-color: #E8F0FE;\n",
" --fill-color: #1967D2;\n",
" --hover-bg-color: #E2EBFA;\n",
" --hover-fill-color: #174EA6;\n",
" --disabled-fill-color: #AAA;\n",
" --disabled-bg-color: #DDD;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-quickchart {\n",
" --bg-color: #3B4455;\n",
" --fill-color: #D2E3FC;\n",
" --hover-bg-color: #434B5C;\n",
" --hover-fill-color: #FFFFFF;\n",
" --disabled-bg-color: #3B4455;\n",
" --disabled-fill-color: #666;\n",
" }\n",
"\n",
" .colab-df-quickchart {\n",
" background-color: var(--bg-color);\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: var(--fill-color);\n",
" height: 32px;\n",
" padding: 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-quickchart:hover {\n",
" background-color: var(--hover-bg-color);\n",
" box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: var(--button-hover-fill-color);\n",
" }\n",
"\n",
" .colab-df-quickchart-complete:disabled,\n",
" .colab-df-quickchart-complete:disabled:hover {\n",
" background-color: var(--disabled-bg-color);\n",
" fill: var(--disabled-fill-color);\n",
" box-shadow: none;\n",
" }\n",
"\n",
" .colab-df-spinner {\n",
" border: 2px solid var(--fill-color);\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" animation:\n",
" spin 1s steps(1) infinite;\n",
" }\n",
"\n",
" @keyframes spin {\n",
" 0% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" border-left-color: var(--fill-color);\n",
" }\n",
" 20% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 30% {\n",
" border-color: transparent;\n",
" border-left-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 40% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-top-color: var(--fill-color);\n",
" }\n",
" 60% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" }\n",
" 80% {\n",
" border-color: transparent;\n",
" border-right-color: var(--fill-color);\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" 90% {\n",
" border-color: transparent;\n",
" border-bottom-color: var(--fill-color);\n",
" }\n",
" }\n",
"</style>\n",
"\n",
" <script>\n",
" async function quickchart(key) {\n",
" const quickchartButtonEl =\n",
" document.querySelector('#' + key + ' button');\n",
" quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n",
" quickchartButtonEl.classList.add('colab-df-spinner');\n",
" try {\n",
" const charts = await google.colab.kernel.invokeFunction(\n",
" 'suggestCharts', [key], {});\n",
" } catch (error) {\n",
" console.error('Error during call to suggestCharts:', error);\n",
" }\n",
" quickchartButtonEl.classList.remove('colab-df-spinner');\n",
" quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
" }\n",
" (() => {\n",
" let quickchartButtonEl =\n",
" document.querySelector('#df-583db96c-7a83-436e-a1e7-be26ba1ff21f button');\n",
" quickchartButtonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
" })();\n",
" </script>\n",
"</div>\n",
"\n",
" </div>\n",
" </div>\n"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "dataframe",
"variable_name": "autompg",
"summary": "{\n \"name\": \"autompg\",\n \"rows\": 398,\n \"fields\": [\n {\n \"column\": \"car_name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 305,\n \"samples\": [\n \"mazda,rx-4\",\n \"ford,f108\",\n \"buick,century,luxus,(sw)\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"cylinders\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 5,\n \"samples\": [\n \"4\",\n \"5\",\n \"6\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"displacement\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 104.26983817119581,\n \"min\": 68.0,\n \"max\": 455.0,\n \"num_unique_values\": 82,\n \"samples\": [\n 122.0,\n 307.0,\n 360.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"horsepower\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 38.49115993282855,\n \"min\": 46.0,\n \"max\": 230.0,\n \"num_unique_values\": 93,\n \"samples\": [\n 92.0,\n 100.0,\n 52.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"weight\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 846,\n \"min\": 1613,\n \"max\": 5140,\n \"num_unique_values\": 351,\n \"samples\": [\n 3730,\n 1995,\n 2215\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"acceleration\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 2.7576889298126757,\n \"min\": 8.0,\n \"max\": 24.8,\n \"num_unique_values\": 95,\n \"samples\": [\n 14.7,\n 18.0,\n 14.3\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"model_year\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 3,\n \"min\": 70,\n \"max\": 82,\n \"num_unique_values\": 13,\n \"samples\": [\n 81,\n 79,\n 70\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"origin\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 0,\n \"min\": 1,\n \"max\": 3,\n \"num_unique_values\": 3,\n \"samples\": [\n 1,\n 3,\n 2\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"mpg\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 7.815984312565782,\n \"min\": 9.0,\n \"max\": 46.6,\n \"num_unique_values\": 129,\n \"samples\": [\n 17.7,\n 30.5,\n 30.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}"
}
},
"metadata": {},
"execution_count": 3
}
],
"source": [
"autompg = auto_mpg.data.original\n",
"autompg.cylinders = autompg.cylinders.astype('str').astype('category')\n",
"autompg.head()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_F4tnEIn2mey"
},
"source": [
"* Create a linear model called `mod_displ_cyl` of mpg vs. displacement and cylinders.\n",
" - Give `sm.formula.ols` code, output of `mod_displ_cyl.summary()`\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kG3csppD2mez",
"outputId": "d1b4a3b0-c37f-48c2-cff1-6a3a61c44a9d"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" OLS Regression Results \n",
"==============================================================================\n",
"Dep. Variable: mpg R-squared: 0.685\n",
"Model: OLS Adj. R-squared: 0.681\n",
"Method: Least Squares F-statistic: 170.7\n",
"Date: Thu, 24 Oct 2024 Prob (F-statistic): 4.61e-96\n",
"Time: 23:34:56 Log-Likelihood: -1152.5\n",
"No. Observations: 398 AIC: 2317.\n",
"Df Residuals: 392 BIC: 2341.\n",
"Df Model: 5 \n",
"Covariance Type: nonrobust \n",
"==================================================================================\n",
" coef std err t P>|t| [0.025 0.975]\n",
"----------------------------------------------------------------------------------\n",
"Intercept 24.4345 2.263 10.799 0.000 19.986 28.883\n",
"cylinders[T.4] 10.7351 2.243 4.787 0.000 6.326 15.144\n",
"cylinders[T.5] 10.7011 3.407 3.141 0.002 4.002 17.400\n",
"cylinders[T.6] 7.2391 2.473 2.927 0.004 2.377 12.101\n",
"cylinders[T.8] 9.0138 2.936 3.070 0.002 3.242 14.785\n",
"displacement -0.0536 0.007 -7.737 0.000 -0.067 -0.040\n",
"==============================================================================\n",
"Omnibus: 54.193 Durbin-Watson: 0.907\n",
"Prob(Omnibus): 0.000 Jarque-Bera (JB): 98.199\n",
"Skew: 0.790 Prob(JB): 4.75e-22\n",
"Kurtosis: 4.851 Cond. No. 5.28e+03\n",
"==============================================================================\n",
"\n",
"Notes:\n",
"[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
"[2] The condition number is large, 5.28e+03. This might indicate that there are\n",
"strong multicollinearity or other numerical problems.\n"
]
}
],
"source": [
"import statsmodels.api as sm\n",
"\n",
"mod_displ_cyl = sm.formula.ols('mpg ~ displacement + cylinders', autompg)\n",
"\n",
"results = mod_displ_cyl.fit()\n",
"print(results.summary())"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jI9d3NqS2me0"
},
"source": [
" * Give the model equations relating mpg with displacement and cylinders.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1opSbzFy2me0"
},
"source": [
"$$\n",
" mpg = 24.4345\n",
" + 10.7351 * cylinders[T.4]\n",
" + 10.7011 * cylinders[T.5]\n",
" + 7.2391 * cylinders[T.6]\n",
" + 9.0138 * cylinders[T.8]\n",
" - 0.0536 * displacement\n",
"$$\n",
"\n",
"<center>or</center>\n",
"\n",
"$$\n",
" mpg =\n",
" \\begin{cases}\n",
" mpg = 24.4345 - 0.0536 \\times displacement\n",
" & \\text{if (cylinders == 3)} \\\\\n",
" mpg = 35.1696 - 0.0536 \\times displacement\n",
" & \\text{if (cylinders == 4)} \\\\\n",
" mpg = 35.1356 - 0.0536 \\times displacement\n",
" & \\text{if (cylinders == 5)} \\\\\n",
" mpg = 31.6736 - 0.0536 \\times displacement \n",
" & \\text{if (cylinders == 6)} \\\\\n",
" mpg = 33.4483 - 0.0536 \\times displacement\n",
" & \\text{if (cylinders == 8)} \\\\\n",
" \\end{cases}\n",
"$$\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rHm-MZLq2me0"
},
"source": [
"* Plot mpg vs. displacement and overlay the best fit model. (code, plot)\n",
" - Hint: plot the predictions; use `assign()` and `geom_line()` and use the color aesthetic for cylinders\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 417
},
"id": "rLutfamE2me0",
"outputId": "080df633-0911-41b4-b66c-b9cc8107a834"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<lets_plot.plot.core.PlotSpec at 0x7f16cf18ac80>"
],
"text/html": [
"<html lang=\"en\">\n",
" <head>\n",
" <meta charset=\"UTF-8\">\n",
" <script type=\"text/javascript\" data-lets-plot-script=\"library\" src=\"https://cdn.jsdelivr.net/gh/JetBrains/[email protected]/js-package/distr/lets-plot.min.js\"></script>\n",
" </head>\n",
" <body>\n",
" <div id=\"MzkQgu\"></div>\n",
" <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
" \n",
" (function() {\n",
" // ----------\n",
" \n",
" var containerDiv = document.getElementById(\"MzkQgu\");\n",
" var observer = new ResizeObserver(function(entries) {\n",
" for (let entry of entries) {\n",
" var width = containerDiv.clientWidth\n",
" if (entry.contentBoxSize && width > 0) {\n",
" \n",
" // Render plot\n",
" if (observer) {\n",
" observer.disconnect();\n",
" observer = null;\n",
" }\n",
"\n",
" var plotSpec={\n",
"\"data\":{\n",
"\"cylinders\":[\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"4\",\"6\",\"6\",\"6\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"6\",\"8\",\"8\",\"8\",\"8\",\"4\",\"4\",\"4\",\"4\",\"6\",\"6\",\"6\",\"6\",\"6\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"6\",\"4\",\"6\",\"6\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"3\",\"8\",\"8\",\"8\",\"8\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"6\",\"6\",\"6\",\"6\",\"6\",\"4\",\"8\",\"8\",\"8\",\"8\",\"6\",\"4\",\"4\",\"4\",\"3\",\"4\",\"6\",\"4\",\"8\",\"8\",\"4\",\"4\",\"4\",\"4\",\"8\",\"4\",\"6\",\"8\",\"6\",\"6\",\"6\",\"6\",\"4\",\"4\",\"4\",\"4\",\"6\",\"6\",\"6\",\"8\",\"8\",\"8\",\"8\",\"8\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"6\",\"6\",\"6\",\"6\",\"8\",\"8\",\"8\",\"8\",\"6\",\"6\",\"6\",\"6\",\"6\",\"8\",\"8\",\"4\",\"4\",\"6\",\"4\",\"4\",\"4\",\"4\",\"6\",\"4\",\"6\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"8\",\"8\",\"8\",\"8\",\"6\",\"6\",\"6\",\"6\",\"4\",\"4\",\"4\",\"4\",\"6\",\"6\",\"6\",\"6\",\"4\",\"4\",\"4\",\"4\",\"4\",\"8\",\"4\",\"6\",\"6\",\"8\",\"8\",\"8\",\"8\",\"4\",\"4\",\"4\",\"4\",\"4\",\"8\",\"8\",\"8\",\"8\",\"6\",\"6\",\"6\",\"6\",\"8\",\"8\",\"8\",\"8\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"6\",\"4\",\"3\",\"4\",\"4\",\"4\",\"4\",\"4\",\"8\",\"8\",\"8\",\"6\",\"6\",\"6\",\"4\",\"6\",\"6\",\"6\",\"6\",\"6\",\"6\",\"8\",\"6\",\"8\",\"8\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"5\",\"6\",\"4\",\"6\",\"4\",\"4\",\"6\",\"6\",\"4\",\"6\",\"6\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"8\",\"4\",\"4\",\"4\",\"4\",\"5\",\"8\",\"4\",\"8\",\"4\",\"4\",\"4\",\"4\",\"4\",\"6\",\"6\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"6\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"5\",\"4\",\"4\",\"4\",\"4\",\"4\",\"6\",\"3\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"6\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"6\",\"6\",\"6\",\"6\",\"8\",\"6\",\"6\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"6\",\"6\",\"4\",\"6\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\",\"4\"],\n",
"\"displacement\":[307.0,350.0,318.0,304.0,302.0,429.0,454.0,440.0,455.0,390.0,383.0,340.0,400.0,455.0,113.0,198.0,199.0,200.0,97.0,97.0,110.0,107.0,104.0,121.0,199.0,360.0,307.0,318.0,304.0,97.0,140.0,113.0,98.0,232.0,225.0,250.0,250.0,232.0,350.0,400.0,351.0,318.0,383.0,400.0,400.0,258.0,140.0,250.0,250.0,122.0,116.0,79.0,88.0,71.0,72.0,97.0,91.0,113.0,97.5,97.0,140.0,122.0,350.0,400.0,318.0,351.0,304.0,429.0,350.0,350.0,400.0,70.0,304.0,307.0,302.0,318.0,121.0,121.0,120.0,96.0,122.0,97.0,120.0,98.0,97.0,350.0,304.0,350.0,302.0,318.0,429.0,400.0,351.0,318.0,440.0,455.0,360.0,225.0,250.0,232.0,250.0,198.0,97.0,400.0,400.0,360.0,350.0,232.0,97.0,140.0,108.0,70.0,122.0,155.0,98.0,350.0,400.0,68.0,116.0,114.0,121.0,318.0,121.0,156.0,350.0,198.0,200.0,232.0,250.0,79.0,122.0,71.0,140.0,250.0,258.0,225.0,302.0,350.0,318.0,302.0,304.0,98.0,79.0,97.0,76.0,83.0,90.0,90.0,116.0,120.0,108.0,79.0,225.0,250.0,250.0,250.0,400.0,350.0,318.0,351.0,231.0,250.0,258.0,225.0,231.0,262.0,302.0,97.0,140.0,232.0,140.0,134.0,90.0,119.0,171.0,90.0,232.0,115.0,120.0,121.0,121.0,91.0,107.0,116.0,140.0,98.0,101.0,305.0,318.0,304.0,351.0,225.0,250.0,200.0,232.0,85.0,98.0,90.0,91.0,225.0,250.0,250.0,258.0,97.0,85.0,97.0,140.0,130.0,318.0,120.0,156.0,168.0,350.0,350.0,302.0,318.0,98.0,111.0,79.0,122.0,85.0,305.0,260.0,318.0,302.0,250.0,231.0,225.0,250.0,400.0,350.0,400.0,351.0,97.0,151.0,97.0,140.0,98.0,98.0,97.0,97.0,146.0,121.0,80.0,90.0,98.0,78.0,85.0,91.0,260.0,318.0,302.0,231.0,200.0,200.0,140.0,225.0,232.0,231.0,200.0,225.0,258.0,305.0,231.0,302.0,318.0,98.0,134.0,119.0,105.0,134.0,156.0,151.0,119.0,131.0,163.0,121.0,163.0,89.0,98.0,231.0,200.0,140.0,232.0,225.0,305.0,302.0,351.0,318.0,350.0,351.0,267.0,360.0,89.0,86.0,98.0,121.0,183.0,350.0,141.0,260.0,105.0,105.0,85.0,91.0,151.0,173.0,173.0,151.0,98.0,89.0,98.0,86.0,151.0,140.0,151.0,225.0,97.0,134.0,120.0,119.0,108.0,86.0,156.0,85.0,90.0,90.0,121.0,146.0,91.0,85.0,97.0,89.0,168.0,70.0,122.0,140.0,107.0,135.0,151.0,156.0,173.0,135.0,79.0,86.0,81.0,97.0,85.0,89.0,91.0,105.0,98.0,98.0,105.0,100.0,107.0,108.0,119.0,120.0,141.0,145.0,168.0,146.0,231.0,350.0,200.0,225.0,112.0,112.0,112.0,112.0,135.0,151.0,140.0,151.0,105.0,91.0,91.0,105.0,98.0,120.0,107.0,108.0,91.0,91.0,91.0,181.0,262.0,156.0,232.0,144.0,135.0,151.0,140.0,97.0,135.0,120.0,119.0],\n",
"\"mpg\":[18.0,15.0,18.0,16.0,17.0,15.0,14.0,14.0,14.0,15.0,15.0,14.0,15.0,14.0,24.0,22.0,18.0,21.0,27.0,26.0,25.0,24.0,25.0,26.0,21.0,10.0,10.0,11.0,9.0,27.0,28.0,25.0,25.0,19.0,16.0,17.0,19.0,18.0,14.0,14.0,14.0,14.0,12.0,13.0,13.0,18.0,22.0,19.0,18.0,23.0,28.0,30.0,30.0,31.0,35.0,27.0,26.0,24.0,25.0,23.0,20.0,21.0,13.0,14.0,15.0,14.0,17.0,11.0,13.0,12.0,13.0,19.0,15.0,13.0,13.0,14.0,18.0,22.0,21.0,26.0,22.0,28.0,23.0,28.0,27.0,13.0,14.0,13.0,14.0,15.0,12.0,13.0,13.0,14.0,13.0,12.0,13.0,18.0,16.0,18.0,18.0,23.0,26.0,11.0,12.0,13.0,12.0,18.0,20.0,21.0,22.0,18.0,19.0,21.0,26.0,15.0,16.0,29.0,24.0,20.0,19.0,15.0,24.0,20.0,11.0,20.0,21.0,19.0,15.0,31.0,26.0,32.0,25.0,16.0,16.0,18.0,16.0,13.0,14.0,14.0,14.0,29.0,26.0,26.0,31.0,32.0,28.0,24.0,26.0,24.0,26.0,31.0,19.0,18.0,15.0,15.0,16.0,15.0,16.0,14.0,17.0,16.0,15.0,18.0,21.0,20.0,13.0,29.0,23.0,20.0,23.0,24.0,25.0,24.0,18.0,29.0,19.0,23.0,23.0,22.0,25.0,33.0,28.0,25.0,25.0,26.0,27.0,17.5,16.0,15.5,14.5,22.0,22.0,24.0,22.5,29.0,24.5,29.0,33.0,20.0,18.0,18.5,17.5,29.5,32.0,28.0,26.5,20.0,13.0,19.0,19.0,16.5,16.5,13.0,13.0,13.0,31.5,30.0,36.0,25.5,33.5,17.5,17.0,15.5,15.0,17.5,20.5,19.0,18.5,16.0,15.5,15.5,16.0,29.0,24.5,26.0,25.5,30.5,33.5,30.0,30.5,22.0,21.5,21.5,43.1,36.1,32.8,39.4,36.1,19.9,19.4,20.2,19.2,20.5,20.2,25.1,20.5,19.4,20.6,20.8,18.6,18.1,19.2,17.7,18.1,17.5,30.0,27.5,27.2,30.9,21.1,23.2,23.8,23.9,20.3,17.0,21.6,16.2,31.5,29.5,21.5,19.8,22.3,20.2,20.6,17.0,17.6,16.5,18.2,16.9,15.5,19.2,18.5,31.9,34.1,35.7,27.4,25.4,23.0,27.2,23.9,34.2,34.5,31.8,37.3,28.4,28.8,26.8,33.5,41.5,38.1,32.1,37.2,28.0,26.4,24.3,19.1,34.3,29.8,31.3,37.0,32.2,46.6,27.9,40.8,44.3,43.4,36.4,30.0,44.6,40.9,33.8,29.8,32.7,23.7,35.0,23.6,32.4,27.2,26.6,25.8,23.5,30.0,39.1,39.0,35.1,32.3,37.0,37.7,34.1,34.7,34.4,29.9,33.0,34.5,33.7,32.4,32.9,31.6,28.1,30.7,25.4,24.2,22.4,26.6,20.2,17.6,28.0,27.0,34.0,31.0,29.0,27.0,24.0,23.0,36.0,37.0,31.0,38.0,36.0,36.0,36.0,34.0,38.0,32.0,38.0,25.0,38.0,26.0,22.0,32.0,36.0,27.0,27.0,44.0,32.0,28.0,31.0],\n",
"\"pred\":[16.99961645844325,14.69573362461242,16.41025108234699,17.160352470105863,17.267509811214275,10.463018650830193,9.123551886975061,9.873653274733936,9.069973216420856,12.552586802444203,12.92763749632364,15.23152033015447,12.016800096902148,9.069973216420856,29.115129111729228,21.06494179259142,21.011363122037217,20.957784451483008,29.97238784059651,29.97238784059651,29.275865123391842,29.43660113505446,29.597337146717074,28.686499747295585,21.011363122037217,14.159946919070364,16.99961645844325,16.41025108234699,17.160352470105863,29.97238784059651,27.668505006765685,29.115129111729228,29.91880917004231,19.24326699374844,19.618317687627876,18.278850923772744,18.278850923772744,19.24326699374844,14.69573362461242,12.016800096902148,14.642154954058213,16.41025108234699,12.92763749632364,12.016800096902148,12.016800096902148,17.8502215593391,27.668505006765685,18.278850923772744,18.278850923772744,28.63292107674138,28.95439310006661,30.93680391057221,30.45459587558436,31.365433275005852,31.311854604451646,29.97238784059651,30.293859863921746,29.115129111729228,29.94559850531941,29.97238784059651,27.668505006765685,28.63292107674138,14.69573362461242,12.016800096902148,16.41025108234699,14.642154954058213,17.160352470105863,10.463018650830193,14.69573362461242,14.69573362461242,12.016800096902148,20.683946676385325,17.160352470105863,16.99961645844325,17.267509811214275,16.41025108234699,28.686499747295585,28.686499747295585,28.74007841784979,30.025966511150717,28.63292107674138,29.97238784059651,28.74007841784979,29.91880917004231,29.97238784059651,14.69573362461242,17.160352470105863,14.69573362461242,17.267509811214275,16.41025108234699,10.463018650830193,12.016800096902148,14.642154954058213,16.41025108234699,9.873653274733936,9.069973216420856,14.159946919070364,19.618317687627876,18.278850923772744,19.24326699374844,18.278850923772744,21.06494179259142,29.97238784059651,12.016800096902148,12.016800096902148,14.159946919070364,14.69573362461242,19.24326699374844,29.97238784059651,27.668505006765685,29.383022464500254,20.683946676385325,28.63292107674138,23.368824626422253,29.91880917004231,14.69573362461242,12.016800096902148,31.526169286668466,28.95439310006661,29.061550441175022,28.686499747295585,16.41025108234699,28.686499747295585,23.315245955868043,14.69573362461242,21.06494179259142,20.957784451483008,19.24326699374844,18.278850923772744,30.93680391057221,28.63292107674138,31.365433275005852,27.668505006765685,18.278850923772744,17.8502215593391,19.618317687627876,17.267509811214275,14.69573362461242,16.41025108234699,17.267509811214275,17.160352470105863,29.91880917004231,30.93680391057221,29.97238784059651,31.097539922234823,30.722489228355386,30.34743853447595,30.34743853447595,28.95439310006661,28.74007841784979,29.383022464500254,30.93680391057221,19.618317687627876,18.278850923772744,18.278850923772744,18.278850923772744,12.016800096902148,14.69573362461242,16.41025108234699,14.642154954058213,19.296845664302644,18.278850923772744,17.8502215593391,19.618317687627876,19.296845664302644,19.410656633382487,17.267509811214275,29.97238784059651,27.668505006765685,19.24326699374844,27.668505006765685,27.989977030090913,30.34743853447595,28.793657088403997,22.511565897554966,30.34743853447595,19.24326699374844,29.007971770620816,28.74007841784979,28.686499747295585,28.686499747295585,30.293859863921746,29.43660113505446,28.95439310006661,27.668505006765685,29.91880917004231,29.75807315837969,17.106773799551657,16.41025108234699,17.160352470105863,14.642154954058213,19.618317687627876,18.278850923772744,20.957784451483008,19.24326699374844,30.615331887246978,29.91880917004231,30.34743853447595,30.293859863921746,19.618317687627876,18.278850923772744,18.278850923772744,17.8502215593391,29.97238784059651,30.615331887246978,29.97238784059651,27.668505006765685,28.204291712307736,16.41025108234699,28.74007841784979,23.315245955868043,22.67230190921758,14.69573362461242,14.69573362461242,17.267509811214275,16.41025108234699,29.91880917004231,29.222286452837636,30.93680391057221,28.63292107674138,30.615331887246978,17.106773799551657,19.5178139744909,16.41025108234699,17.267509811214275,18.278850923772744,19.296845664302644,19.618317687627876,18.278850923772744,12.016800096902148,14.69573362461242,12.016800096902148,14.642154954058213,29.97238784059651,27.079139630669424,29.97238784059651,27.668505006765685,29.91880917004231,29.91880917004231,29.97238784059651,29.97238784059651,23.8510326614101,28.686499747295585,20.14815997084327,30.34743853447595,29.91880917004231,30.990382581126415,30.615331887246978,30.293859863921746,19.5178139744909,16.41025108234699,17.267509811214275,19.296845664302644,20.957784451483008,20.957784451483008,27.668505006765685,19.618317687627876,19.24326699374844,19.296845664302644,20.957784451483008,19.618317687627876,17.8502215593391,17.106773799551657,19.296845664302644,17.267509811214275,16.41025108234699,29.91880917004231,27.989977030090913,28.793657088403997,29.54375847616287,27.989977030090913,26.811246277898398,27.079139630669424,28.793657088403997,28.11676805442524,22.940195261988606,28.686499747295585,22.940195261988606,30.401017205030154,29.91880917004231,19.296845664302644,20.957784451483008,27.668505006765685,19.24326699374844,19.618317687627876,17.106773799551657,17.267509811214275,14.642154954058213,16.41025108234699,14.69573362461242,14.642154954058213,19.14276328061146,14.159946919070364,30.401017205030154,30.56175321669277,29.91880917004231,28.686499747295585,25.33067718560656,14.69573362461242,27.614926336211475,19.5178139744909,29.54375847616287,29.54375847616287,30.615331887246978,30.293859863921746,27.079139630669424,22.404408556446555,22.404408556446555,27.079139630669424,29.91880917004231,30.401017205030154,29.91880917004231,30.56175321669277,27.079139630669424,27.668505006765685,27.079139630669424,19.618317687627876,29.97238784059651,27.989977030090913,28.74007841784979,28.793657088403997,29.383022464500254,30.56175321669277,26.811246277898398,30.615331887246978,30.34743853447595,30.34743853447595,28.652554759967295,27.34703298344045,30.293859863921746,30.615331887246978,29.97238784059651,30.401017205030154,22.67230190921758,20.683946676385325,28.63292107674138,27.668505006765685,29.43660113505446,27.93639835953671,27.079139630669424,26.811246277898398,22.404408556446555,27.93639835953671,30.93680391057221,30.56175321669277,30.829646569463797,29.97238784059651,30.615331887246978,30.401017205030154,30.293859863921746,29.54375847616287,29.91880917004231,29.91880917004231,29.54375847616287,29.811651828933897,29.43660113505446,29.383022464500254,28.793657088403997,28.74007841784979,27.614926336211475,23.904611331964304,22.67230190921758,23.8510326614101,19.296845664302644,14.69573362461242,20.957784451483008,19.618317687627876,29.168707782283434,29.168707782283434,29.168707782283434,29.168707782283434,27.93639835953671,27.079139630669424,27.668505006765685,27.079139630669424,29.54375847616287,30.293859863921746,30.293859863921746,29.54375847616287,29.91880917004231,28.74007841784979,29.43660113505446,29.383022464500254,30.293859863921746,30.293859863921746,30.293859863921746,21.97577919201291,17.63590687712228,26.811246277898398,19.24326699374844,27.45419032454886,27.93639835953671,27.079139630669424,27.668505006765685,29.97238784059651,27.93639835953671,28.74007841784979,28.793657088403997]\n",
"},\n",
"\"mapping\":{\n",
"},\n",
"\"data_meta\":{\n",
"\"series_annotations\":[{\n",
"\"type\":\"str\",\n",
"\"column\":\"car_name\"\n",
"},{\n",
"\"type\":\"str\",\n",
"\"column\":\"cylinders\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"displacement\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"horsepower\"\n",
"},{\n",
"\"type\":\"int\",\n",
"\"column\":\"weight\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"acceleration\"\n",
"},{\n",
"\"type\":\"int\",\n",
"\"column\":\"model_year\"\n",
"},{\n",
"\"type\":\"int\",\n",
"\"column\":\"origin\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"mpg\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"pred\"\n",
"}]\n",
"},\n",
"\"kind\":\"plot\",\n",
"\"scales\":[],\n",
"\"layers\":[{\n",
"\"geom\":\"point\",\n",
"\"mapping\":{\n",
"\"x\":\"displacement\",\n",
"\"y\":\"mpg\",\n",
"\"color\":\"cylinders\"\n",
"},\n",
"\"data_meta\":{\n",
"},\n",
"\"data\":{\n",
"}\n",
"},{\n",
"\"geom\":\"line\",\n",
"\"mapping\":{\n",
"\"x\":\"displacement\",\n",
"\"y\":\"pred\",\n",
"\"color\":\"cylinders\"\n",
"},\n",
"\"data_meta\":{\n",
"},\n",
"\"data\":{\n",
"}\n",
"}],\n",
"\"metainfo_list\":[],\n",
"\"spec_id\":\"4\"\n",
"};\n",
" \n",
" var toolbar = null;\n",
" var plotContainer = containerDiv; \n",
" \n",
" var options = {\n",
" sizing: {\n",
" width_mode: \"min\",\n",
" height_mode: \"scaled\",\n",
" width: width\n",
" }\n",
" };\n",
" var fig = LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer, options);\n",
" if (toolbar) {\n",
" toolbar.bind(fig);\n",
" }\n",
" \n",
" break;\n",
" }\n",
" }\n",
" });\n",
" \n",
" observer.observe(containerDiv);\n",
" \n",
" // ----------\n",
" })();\n",
" \n",
" </script>\n",
" </body>\n",
"</html>"
]
},
"metadata": {},
"execution_count": 10
}
],
"source": [
"(\n",
" ggplot(autompg.assign(pred=results.get_prediction().predicted_mean))\n",
" + geom_point(aes(x='displacement', y='mpg', color='cylinders'))\n",
" + geom_line(aes(x='displacement', y='pred', color='cylinders'))\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "e-B1v5c42me1"
},
"source": [
"# Part 9 - Slide 60"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-vd9N3Iw2me1"
},
"source": [
"Consider the `auto_mpg.data.original` dataset\n",
"* Build a linear model to predict mpg given:\n",
" \n",
" $$\\frac{1}{displacement^2}$$"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wNE8uqVt2me1",
"outputId": "29d67866-9436-4c54-e812-d96eda18ad61"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(16.502808546271417, 123227.9612325492)"
]
},
"metadata": {},
"execution_count": 15
}
],
"source": [
"from sklearn.linear_model import LinearRegression\n",
"\n",
"autompg = auto_mpg.data.original.copy()\n",
"autompg['d2_recip'] = 1 / (autompg.displacement**2)\n",
"\n",
"X = autompg[['d2_recip']]\n",
"y = autompg['mpg']\n",
"\n",
"lm = LinearRegression()\n",
"lm.fit(X, y)\n",
"\n",
"lm.intercept_, *lm.coef_"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8GFN9jgD2me1"
},
"source": [
"* What is the regression equation?"
]
},
{
"cell_type": "markdown",
"source": [
"$$\n",
" 16.5028 + \\frac{123227.9612}{displacement^2}\n",
"$$"
],
"metadata": {
"id": "57mJ0WxE9Gs_"
}
},
{
"cell_type": "markdown",
"metadata": {
"id": "8uFmMP9t2me2"
},
"source": [
"* Plot the model predictions"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 417
},
"id": "Vyl4rTFj2me2",
"outputId": "ece501eb-e6aa-44c4-b369-201a82fa2d46"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<lets_plot.plot.core.PlotSpec at 0x7f16cab79000>"
],
"text/html": [
"<html lang=\"en\">\n",
" <head>\n",
" <meta charset=\"UTF-8\">\n",
" <script type=\"text/javascript\" data-lets-plot-script=\"library\" src=\"https://cdn.jsdelivr.net/gh/JetBrains/[email protected]/js-package/distr/lets-plot.min.js\"></script>\n",
" </head>\n",
" <body>\n",
" <div id=\"SFNC7F\"></div>\n",
" <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
" \n",
" (function() {\n",
" // ----------\n",
" \n",
" var containerDiv = document.getElementById(\"SFNC7F\");\n",
" var observer = new ResizeObserver(function(entries) {\n",
" for (let entry of entries) {\n",
" var width = containerDiv.clientWidth\n",
" if (entry.contentBoxSize && width > 0) {\n",
" \n",
" // Render plot\n",
" if (observer) {\n",
" observer.disconnect();\n",
" observer = null;\n",
" }\n",
"\n",
" var plotSpec={\n",
"\"data\":{\n",
"\"displacement\":[307.0,350.0,318.0,304.0,302.0,429.0,454.0,440.0,455.0,390.0,383.0,340.0,400.0,455.0,113.0,198.0,199.0,200.0,97.0,97.0,110.0,107.0,104.0,121.0,199.0,360.0,307.0,318.0,304.0,97.0,140.0,113.0,98.0,232.0,225.0,250.0,250.0,232.0,350.0,400.0,351.0,318.0,383.0,400.0,400.0,258.0,140.0,250.0,250.0,122.0,116.0,79.0,88.0,71.0,72.0,97.0,91.0,113.0,97.5,97.0,140.0,122.0,350.0,400.0,318.0,351.0,304.0,429.0,350.0,350.0,400.0,70.0,304.0,307.0,302.0,318.0,121.0,121.0,120.0,96.0,122.0,97.0,120.0,98.0,97.0,350.0,304.0,350.0,302.0,318.0,429.0,400.0,351.0,318.0,440.0,455.0,360.0,225.0,250.0,232.0,250.0,198.0,97.0,400.0,400.0,360.0,350.0,232.0,97.0,140.0,108.0,70.0,122.0,155.0,98.0,350.0,400.0,68.0,116.0,114.0,121.0,318.0,121.0,156.0,350.0,198.0,200.0,232.0,250.0,79.0,122.0,71.0,140.0,250.0,258.0,225.0,302.0,350.0,318.0,302.0,304.0,98.0,79.0,97.0,76.0,83.0,90.0,90.0,116.0,120.0,108.0,79.0,225.0,250.0,250.0,250.0,400.0,350.0,318.0,351.0,231.0,250.0,258.0,225.0,231.0,262.0,302.0,97.0,140.0,232.0,140.0,134.0,90.0,119.0,171.0,90.0,232.0,115.0,120.0,121.0,121.0,91.0,107.0,116.0,140.0,98.0,101.0,305.0,318.0,304.0,351.0,225.0,250.0,200.0,232.0,85.0,98.0,90.0,91.0,225.0,250.0,250.0,258.0,97.0,85.0,97.0,140.0,130.0,318.0,120.0,156.0,168.0,350.0,350.0,302.0,318.0,98.0,111.0,79.0,122.0,85.0,305.0,260.0,318.0,302.0,250.0,231.0,225.0,250.0,400.0,350.0,400.0,351.0,97.0,151.0,97.0,140.0,98.0,98.0,97.0,97.0,146.0,121.0,80.0,90.0,98.0,78.0,85.0,91.0,260.0,318.0,302.0,231.0,200.0,200.0,140.0,225.0,232.0,231.0,200.0,225.0,258.0,305.0,231.0,302.0,318.0,98.0,134.0,119.0,105.0,134.0,156.0,151.0,119.0,131.0,163.0,121.0,163.0,89.0,98.0,231.0,200.0,140.0,232.0,225.0,305.0,302.0,351.0,318.0,350.0,351.0,267.0,360.0,89.0,86.0,98.0,121.0,183.0,350.0,141.0,260.0,105.0,105.0,85.0,91.0,151.0,173.0,173.0,151.0,98.0,89.0,98.0,86.0,151.0,140.0,151.0,225.0,97.0,134.0,120.0,119.0,108.0,86.0,156.0,85.0,90.0,90.0,121.0,146.0,91.0,85.0,97.0,89.0,168.0,70.0,122.0,140.0,107.0,135.0,151.0,156.0,173.0,135.0,79.0,86.0,81.0,97.0,85.0,89.0,91.0,105.0,98.0,98.0,105.0,100.0,107.0,108.0,119.0,120.0,141.0,145.0,168.0,146.0,231.0,350.0,200.0,225.0,112.0,112.0,112.0,112.0,135.0,151.0,140.0,151.0,105.0,91.0,91.0,105.0,98.0,120.0,107.0,108.0,91.0,91.0,91.0,181.0,262.0,156.0,232.0,144.0,135.0,151.0,140.0,97.0,135.0,120.0,119.0],\n",
"\"mpg\":[18.0,15.0,18.0,16.0,17.0,15.0,14.0,14.0,14.0,15.0,15.0,14.0,15.0,14.0,24.0,22.0,18.0,21.0,27.0,26.0,25.0,24.0,25.0,26.0,21.0,10.0,10.0,11.0,9.0,27.0,28.0,25.0,25.0,19.0,16.0,17.0,19.0,18.0,14.0,14.0,14.0,14.0,12.0,13.0,13.0,18.0,22.0,19.0,18.0,23.0,28.0,30.0,30.0,31.0,35.0,27.0,26.0,24.0,25.0,23.0,20.0,21.0,13.0,14.0,15.0,14.0,17.0,11.0,13.0,12.0,13.0,19.0,15.0,13.0,13.0,14.0,18.0,22.0,21.0,26.0,22.0,28.0,23.0,28.0,27.0,13.0,14.0,13.0,14.0,15.0,12.0,13.0,13.0,14.0,13.0,12.0,13.0,18.0,16.0,18.0,18.0,23.0,26.0,11.0,12.0,13.0,12.0,18.0,20.0,21.0,22.0,18.0,19.0,21.0,26.0,15.0,16.0,29.0,24.0,20.0,19.0,15.0,24.0,20.0,11.0,20.0,21.0,19.0,15.0,31.0,26.0,32.0,25.0,16.0,16.0,18.0,16.0,13.0,14.0,14.0,14.0,29.0,26.0,26.0,31.0,32.0,28.0,24.0,26.0,24.0,26.0,31.0,19.0,18.0,15.0,15.0,16.0,15.0,16.0,14.0,17.0,16.0,15.0,18.0,21.0,20.0,13.0,29.0,23.0,20.0,23.0,24.0,25.0,24.0,18.0,29.0,19.0,23.0,23.0,22.0,25.0,33.0,28.0,25.0,25.0,26.0,27.0,17.5,16.0,15.5,14.5,22.0,22.0,24.0,22.5,29.0,24.5,29.0,33.0,20.0,18.0,18.5,17.5,29.5,32.0,28.0,26.5,20.0,13.0,19.0,19.0,16.5,16.5,13.0,13.0,13.0,31.5,30.0,36.0,25.5,33.5,17.5,17.0,15.5,15.0,17.5,20.5,19.0,18.5,16.0,15.5,15.5,16.0,29.0,24.5,26.0,25.5,30.5,33.5,30.0,30.5,22.0,21.5,21.5,43.1,36.1,32.8,39.4,36.1,19.9,19.4,20.2,19.2,20.5,20.2,25.1,20.5,19.4,20.6,20.8,18.6,18.1,19.2,17.7,18.1,17.5,30.0,27.5,27.2,30.9,21.1,23.2,23.8,23.9,20.3,17.0,21.6,16.2,31.5,29.5,21.5,19.8,22.3,20.2,20.6,17.0,17.6,16.5,18.2,16.9,15.5,19.2,18.5,31.9,34.1,35.7,27.4,25.4,23.0,27.2,23.9,34.2,34.5,31.8,37.3,28.4,28.8,26.8,33.5,41.5,38.1,32.1,37.2,28.0,26.4,24.3,19.1,34.3,29.8,31.3,37.0,32.2,46.6,27.9,40.8,44.3,43.4,36.4,30.0,44.6,40.9,33.8,29.8,32.7,23.7,35.0,23.6,32.4,27.2,26.6,25.8,23.5,30.0,39.1,39.0,35.1,32.3,37.0,37.7,34.1,34.7,34.4,29.9,33.0,34.5,33.7,32.4,32.9,31.6,28.1,30.7,25.4,24.2,22.4,26.6,20.2,17.6,28.0,27.0,34.0,31.0,29.0,27.0,24.0,23.0,36.0,37.0,31.0,38.0,36.0,36.0,36.0,34.0,38.0,32.0,38.0,25.0,38.0,26.0,22.0,32.0,36.0,27.0,27.0,44.0,32.0,28.0,31.0],\n",
"\"pred\":[17.810280893272967,17.50875108694529,17.721391288573436,17.83621359769703,17.853933071868422,17.172376529669407,17.1006658753121,17.13931661048913,17.098040818859506,17.312985806182983,17.342871272396412,17.568794370082397,17.27298330397485,17.098040818859506,26.15336546008998,19.646058246111973,19.6145471698554,19.583507577085147,29.599626617538206,29.599626617538206,26.686937573754825,27.266015920937257,27.895926263777817,24.919444106175057,19.6145471698554,17.453641580473185,17.810280893272967,17.721391288573436,17.83621359769703,29.599626617538206,22.789949425483112,26.15336546008998,29.333708299764673,18.79226977606759,18.936941113827945,18.474455925992203,18.474455925992203,18.79226977606759,17.50875108694529,17.27298330397485,17.503027385668414,17.721391288573436,17.342871272396412,17.27298330397485,17.27298330397485,18.35407892113695,22.789949425483112,18.474455925992203,18.474455925992203,24.78203195614438,25.660653465456107,36.247715008785306,32.41551015171424,40.94795062771344,40.27363440131563,29.599626617538206,31.38361536097365,26.15336546008998,29.465644704856473,29.599626617538206,22.789949425483112,24.78203195614438,17.50875108694529,17.27298330397485,17.721391288573436,17.503027385668414,17.83621359769703,17.172376529669407,17.50875108694529,17.50875108694529,17.27298330397485,41.651372063118195,17.83621359769703,17.810280893272967,17.853933071868422,17.721391288573436,24.919444106175057,24.919444106175057,25.060305854087332,29.873898089733785,24.78203195614438,29.599626617538206,25.060305854087332,29.333708299764673,29.599626617538206,17.50875108694529,17.83621359769703,17.50875108694529,17.853933071868422,17.721391288573436,17.172376529669407,17.27298330397485,17.503027385668414,17.721391288573436,17.13931661048913,17.098040818859506,17.453641580473185,18.936941113827945,18.474455925992203,18.79226977606759,18.474455925992203,19.646058246111973,29.599626617538206,17.27298330397485,17.27298330397485,17.453641580473185,17.50875108694529,18.79226977606759,29.599626617538206,22.789949425483112,27.06762003740218,41.651372063118195,24.78203195614438,21.631964060633507,29.333708299764673,17.50875108694529,17.27298330397485,43.1524541415459,25.660653465456107,25.984800023075756,24.919444106175057,17.721391288573436,24.919444106175057,21.566416420718706,17.50875108694529,19.646058246111973,19.583507577085147,18.79226977606759,18.474455925992203,36.247715008785306,24.78203195614438,40.94795062771344,22.789949425483112,18.474455925992203,18.35407892113695,18.936941113827945,17.853933071868422,17.50875108694529,17.721391288573436,17.853933071868422,17.83621359769703,29.333708299764673,36.247715008785306,29.599626617538206,37.837289369081184,34.39044989226491,31.716137093499714,31.716137093499714,25.660653465456107,25.060305854087332,27.06762003740218,36.247715008785306,18.936941113827945,18.474455925992203,18.474455925992203,18.474455925992203,17.27298330397485,17.50875108694529,17.721391288573436,17.503027385668414,18.81213485635836,18.474455925992203,18.35407892113695,18.936941113827945,18.81213485635836,18.29798308785625,17.853933071868422,29.599626617538206,22.789949425483112,18.79226977606759,22.789949425483112,23.365582061115994,31.716137093499714,25.204733638605944,20.71702698040668,31.716137093499714,18.79226977606759,25.820612798260015,25.060305854087332,24.919444106175057,24.919444106175057,31.38361536097365,27.266015920937257,25.660653465456107,22.789949425483112,29.333708299764673,28.582796903545137,17.827484291851093,17.721391288573436,17.83621359769703,17.503027385668414,18.936941113827945,18.474455925992203,19.583507577085147,18.79226977606759,33.55858172724709,29.333708299764673,31.716137093499714,31.38361536097365,18.936941113827945,18.474455925992203,18.474455925992203,18.35407892113695,29.599626617538206,33.55858172724709,29.599626617538206,22.789949425483112,23.79440388547551,17.721391288573436,25.060305854087332,21.566416420718706,20.86887860127954,17.50875108694529,17.50875108694529,17.853933071868422,17.721391288573436,29.333708299764673,26.50426632019798,36.247715008785306,24.78203195614438,33.55858172724709,17.827484291851093,18.32570738107244,17.721391288573436,17.853933071868422,18.474455925992203,18.81213485635836,18.936941113827945,18.474455925992203,17.27298330397485,17.50875108694529,17.27298330397485,17.503027385668414,29.599626617538206,21.907306648659436,29.599626617538206,22.789949425483112,29.333708299764673,29.333708299764673,29.599626617538206,29.599626617538206,22.283816297845316,24.919444106175057,35.75717748885723,31.716137093499714,29.333708299764673,36.757240044060566,33.55858172724709,31.38361536097365,18.32570738107244,17.721391288573436,17.853933071868422,18.81213485635836,19.583507577085147,19.583507577085147,22.789949425483112,18.936941113827945,18.79226977606759,18.81213485635836,19.583507577085147,18.936941113827945,18.35407892113695,17.827484291851093,18.81213485635836,17.853933071868422,17.721391288573436,29.333708299764673,23.365582061115994,25.204733638605944,27.679947887092208,23.365582061115994,21.566416420718706,21.907306648659436,25.204733638605944,23.683506712610743,21.140843896963922,24.919444106175057,21.140843896963922,32.05993027743531,29.333708299764673,18.81213485635836,19.583507577085147,22.789949425483112,18.79226977606759,18.936941113827945,17.827484291851093,17.853933071868422,17.503027385668414,17.721391288573436,17.50875108694529,17.503027385668414,18.23137762751185,17.453641580473185,32.05993027743531,33.1642419200612,29.333708299764673,24.919444106175057,20.182463395103845,17.50875108694529,22.701086360895893,18.32570738107244,27.679947887092208,27.679947887092208,33.55858172724709,31.38361536097365,21.907306648659436,20.620151632660846,20.620151632660846,21.907306648659436,29.333708299764673,32.05993027743531,29.333708299764673,33.1642419200612,21.907306648659436,22.789949425483112,21.907306648659436,18.936941113827945,29.599626617538206,23.365582061115994,25.060305854087332,25.204733638605944,27.06762003740218,33.1642419200612,21.566416420718706,33.55858172724709,31.716137093499714,31.716137093499714,24.919444106175057,22.283816297845316,31.38361536097365,33.55858172724709,29.599626617538206,32.05993027743531,20.86887860127954,41.651372063118195,24.78203195614438,22.789949425483112,27.266015920937257,23.264287900595104,21.907306648659436,21.566416420718706,20.620151632660846,23.264287900595104,36.247715008785306,33.1642419200612,35.284695641614995,29.599626617538206,33.55858172724709,32.05993027743531,31.38361536097365,27.679947887092208,29.333708299764673,29.333708299764673,27.679947887092208,28.82560466952634,27.266015920937257,27.06762003740218,25.204733638605944,25.060305854087332,22.701086360895893,22.363829294549618,20.86887860127954,22.283816297845316,18.81213485635836,17.50875108694529,19.583507577085147,18.936941113827945,26.326466170039687,26.326466170039687,26.326466170039687,26.326466170039687,23.264287900595104,21.907306648659436,22.789949425483112,21.907306648659436,27.679947887092208,31.38361536097365,31.38361536097365,27.679947887092208,29.333708299764673,25.060305854087332,27.266015920937257,27.06762003740218,31.38361536097365,31.38361536097365,31.38361536097365,20.264231006896832,18.29798308785625,21.566416420718706,18.79226977606759,22.44551501003247,23.264287900595104,21.907306648659436,22.789949425483112,29.599626617538206,23.264287900595104,25.060305854087332,25.204733638605944]\n",
"},\n",
"\"mapping\":{\n",
"},\n",
"\"data_meta\":{\n",
"\"series_annotations\":[{\n",
"\"type\":\"str\",\n",
"\"column\":\"car_name\"\n",
"},{\n",
"\"type\":\"str\",\n",
"\"column\":\"cylinders\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"displacement\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"horsepower\"\n",
"},{\n",
"\"type\":\"int\",\n",
"\"column\":\"weight\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"acceleration\"\n",
"},{\n",
"\"type\":\"int\",\n",
"\"column\":\"model_year\"\n",
"},{\n",
"\"type\":\"int\",\n",
"\"column\":\"origin\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"mpg\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"d2_recip\"\n",
"},{\n",
"\"type\":\"float\",\n",
"\"column\":\"pred\"\n",
"}]\n",
"},\n",
"\"kind\":\"plot\",\n",
"\"scales\":[],\n",
"\"layers\":[{\n",
"\"geom\":\"point\",\n",
"\"mapping\":{\n",
"\"x\":\"displacement\",\n",
"\"y\":\"mpg\"\n",
"},\n",
"\"data_meta\":{\n",
"},\n",
"\"data\":{\n",
"}\n",
"},{\n",
"\"geom\":\"line\",\n",
"\"mapping\":{\n",
"\"x\":\"displacement\",\n",
"\"y\":\"pred\"\n",
"},\n",
"\"data_meta\":{\n",
"},\n",
"\"color\":\"blue\",\n",
"\"data\":{\n",
"}\n",
"}],\n",
"\"metainfo_list\":[],\n",
"\"spec_id\":\"6\"\n",
"};\n",
" \n",
" var toolbar = null;\n",
" var plotContainer = containerDiv; \n",
" \n",
" var options = {\n",
" sizing: {\n",
" width_mode: \"min\",\n",
" height_mode: \"scaled\",\n",
" width: width\n",
" }\n",
" };\n",
" var fig = LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer, options);\n",
" if (toolbar) {\n",
" toolbar.bind(fig);\n",
" }\n",
" \n",
" break;\n",
" }\n",
" }\n",
" });\n",
" \n",
" observer.observe(containerDiv);\n",
" \n",
" // ----------\n",
" })();\n",
" \n",
" </script>\n",
" </body>\n",
"</html>"
]
},
"metadata": {},
"execution_count": 18
}
],
"source": [
"autompg['pred'] = lm.predict(X)\n",
"\n",
"(\n",
" ggplot(autompg)\n",
" + geom_point(aes(x='displacement', y='mpg'))\n",
" + geom_line(aes(x='displacement', y='pred'), color='blue')\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "murach",
"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.11.7"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment