Az4 Ipynb

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 17

{

"cells": [
{
"cell_type": "code",
"execution_count": 47,
"id": "3f7ebd6a-c0e4-446a-a0ea-9e3f5d77c8b2",
"metadata": {},
"outputs": [],
"source": [
"#Import python libraries\n",
"\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "2259d9d1-5564-45eb-880b-1a366259b2f9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 506 entries, 0 to 505\n",
"Data columns (total 14 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 crim 506 non-null float64\n",
" 1 zn 506 non-null float64\n",
" 2 indus 506 non-null float64\n",
" 3 chas 506 non-null int64 \n",
" 4 nox 506 non-null float64\n",
" 5 rm 501 non-null float64\n",
" 6 age 506 non-null float64\n",
" 7 dis 506 non-null float64\n",
" 8 rad 506 non-null int64 \n",
" 9 tax 506 non-null int64 \n",
" 10 ptratio 506 non-null float64\n",
" 11 b 506 non-null float64\n",
" 12 lstat 506 non-null float64\n",
" 13 medv 506 non-null float64\n",
"dtypes: float64(11), int64(3)\n",
"memory usage: 55.5 KB\n"
]
}
],
"source": [
"#Load the dataset and store it in a dataframe.\n",
"\n",
"df = pd.read_csv(\"BostonHousing.csv\")\n",
"df.info()"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "ccb0496d-a5ec-46ed-9402-62b50d4a2a0a",
"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>crim</th>\n",
" <th>zn</th>\n",
" <th>indus</th>\n",
" <th>chas</th>\n",
" <th>nox</th>\n",
" <th>rm</th>\n",
" <th>age</th>\n",
" <th>dis</th>\n",
" <th>rad</th>\n",
" <th>tax</th>\n",
" <th>ptratio</th>\n",
" <th>b</th>\n",
" <th>lstat</th>\n",
" <th>medv</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.00632</td>\n",
" <td>18.0</td>\n",
" <td>2.31</td>\n",
" <td>0</td>\n",
" <td>0.538</td>\n",
" <td>6.575</td>\n",
" <td>65.2</td>\n",
" <td>4.0900</td>\n",
" <td>1</td>\n",
" <td>296</td>\n",
" <td>15.3</td>\n",
" <td>396.90</td>\n",
" <td>4.98</td>\n",
" <td>24.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.02731</td>\n",
" <td>0.0</td>\n",
" <td>7.07</td>\n",
" <td>0</td>\n",
" <td>0.469</td>\n",
" <td>6.421</td>\n",
" <td>78.9</td>\n",
" <td>4.9671</td>\n",
" <td>2</td>\n",
" <td>242</td>\n",
" <td>17.8</td>\n",
" <td>396.90</td>\n",
" <td>9.14</td>\n",
" <td>21.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.02729</td>\n",
" <td>0.0</td>\n",
" <td>7.07</td>\n",
" <td>0</td>\n",
" <td>0.469</td>\n",
" <td>7.185</td>\n",
" <td>61.1</td>\n",
" <td>4.9671</td>\n",
" <td>2</td>\n",
" <td>242</td>\n",
" <td>17.8</td>\n",
" <td>392.83</td>\n",
" <td>4.03</td>\n",
" <td>34.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.03237</td>\n",
" <td>0.0</td>\n",
" <td>2.18</td>\n",
" <td>0</td>\n",
" <td>0.458</td>\n",
" <td>6.998</td>\n",
" <td>45.8</td>\n",
" <td>6.0622</td>\n",
" <td>3</td>\n",
" <td>222</td>\n",
" <td>18.7</td>\n",
" <td>394.63</td>\n",
" <td>2.94</td>\n",
" <td>33.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.06905</td>\n",
" <td>0.0</td>\n",
" <td>2.18</td>\n",
" <td>0</td>\n",
" <td>0.458</td>\n",
" <td>7.147</td>\n",
" <td>54.2</td>\n",
" <td>6.0622</td>\n",
" <td>3</td>\n",
" <td>222</td>\n",
" <td>18.7</td>\n",
" <td>396.90</td>\n",
" <td>5.33</td>\n",
" <td>36.2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" crim zn indus chas nox rm age dis rad tax
ptratio \\\n",
"0 0.00632 18.0 2.31 0 0.538 6.575 65.2 4.0900 1 296
15.3 \n",
"1 0.02731 0.0 7.07 0 0.469 6.421 78.9 4.9671 2 242
17.8 \n",
"2 0.02729 0.0 7.07 0 0.469 7.185 61.1 4.9671 2 242
17.8 \n",
"3 0.03237 0.0 2.18 0 0.458 6.998 45.8 6.0622 3 222
18.7 \n",
"4 0.06905 0.0 2.18 0 0.458 7.147 54.2 6.0622 3 222
18.7 \n",
"\n",
" b lstat medv \n",
"0 396.90 4.98 24.0 \n",
"1 396.90 9.14 21.6 \n",
"2 392.83 4.03 34.7 \n",
"3 394.63 2.94 33.4 \n",
"4 396.90 5.33 36.2 "
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "00493fc9-e841-4bd3-8a09-d906f232a2bd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(506, 14)"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "8ae5366e-ffed-43e6-8d54-3a2a06d3766d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"crim 0\n",
"zn 0\n",
"indus 0\n",
"chas 0\n",
"nox 0\n",
"rm 5\n",
"age 0\n",
"dis 0\n",
"rad 0\n",
"tax 0\n",
"ptratio 0\n",
"b 0\n",
"lstat 0\n",
"medv 0\n",
"dtype: int64"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.isnull().sum()"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "f9129532-947c-402b-b369-6c9deeabbb07",
"metadata": {},
"outputs": [],
"source": [
"#Removing null value by replacing it with mean\n",
"\n",
"df['rm'] = df['rm'].fillna(df['rm'].mean())"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "631a94fb-27e8-4f90-9700-a8e2976aac12",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"crim 0\n",
"zn 0\n",
"indus 0\n",
"chas 0\n",
"nox 0\n",
"rm 0\n",
"age 0\n",
"dis 0\n",
"rad 0\n",
"tax 0\n",
"ptratio 0\n",
"b 0\n",
"lstat 0\n",
"medv 0\n",
"dtype: int64"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.isnull().sum()"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "3736b4b2-cda4-45a0-a3aa-65e1b77ebeee",
"metadata": {},
"outputs": [],
"source": [
"#Here we seperate the first 13 columns in X and the last column to be
predicted as Y.\n",
"\n",
"X = df[['crim', 'zn', 'indus', 'chas', 'nox', 'rm', 'age', 'dis', 'rad',
'tax',\n",
" 'ptratio', 'b', 'lstat']]\n",
"Y = df['medv']"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "2250c47e-4015-422e-9b21-085ed1c5c59d",
"metadata": {},
"outputs": [],
"source": [
"#Splitting the data into training and testing data using train_test_split\n",
"\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X,Y, test_size = 0.3,
random_state = 42)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "6bf6b07b-c049-4f6f-a04b-f5f02de571a3",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>#sk-container-id-3 {\n",
" /* Definition of color scheme common for light and dark mode */\n",
" --sklearn-color-text: black;\n",
" --sklearn-color-line: gray;\n",
" /* Definition of color scheme for unfitted estimators */\n",
" --sklearn-color-unfitted-level-0: #fff5e6;\n",
" --sklearn-color-unfitted-level-1: #f6e4d2;\n",
" --sklearn-color-unfitted-level-2: #ffe0b3;\n",
" --sklearn-color-unfitted-level-3: chocolate;\n",
" /* Definition of color scheme for fitted estimators */\n",
" --sklearn-color-fitted-level-0: #f0f8ff;\n",
" --sklearn-color-fitted-level-1: #d4ebff;\n",
" --sklearn-color-fitted-level-2: #b3dbfd;\n",
" --sklearn-color-fitted-level-3: cornflowerblue;\n",
"\n",
" /* Specific color for light theme */\n",
" --sklearn-color-text-on-default-background: var(--sg-text-color, var(--
theme-code-foreground, var(--jp-content-font-color1, black)));\n",
" --sklearn-color-background: var(--sg-background-color, var(--theme-
background, var(--jp-layout-color0, white)));\n",
" --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-
foreground, var(--jp-content-font-color1, black)));\n",
" --sklearn-color-icon: #696969;\n",
"\n",
" @media (prefers-color-scheme: dark) {\n",
" /* Redefinition of color scheme for dark theme */\n",
" --sklearn-color-text-on-default-background: var(--sg-text-color, var(--
theme-code-foreground, var(--jp-content-font-color1, white)));\n",
" --sklearn-color-background: var(--sg-background-color, var(--theme-
background, var(--jp-layout-color0, #111)));\n",
" --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-
foreground, var(--jp-content-font-color1, white)));\n",
" --sklearn-color-icon: #878787;\n",
" }\n",
"}\n",
"\n",
"#sk-container-id-3 {\n",
" color: var(--sklearn-color-text);\n",
"}\n",
"\n",
"#sk-container-id-3 pre {\n",
" padding: 0;\n",
"}\n",
"\n",
"#sk-container-id-3 input.sk-hidden--visually {\n",
" border: 0;\n",
" clip: rect(1px 1px 1px 1px);\n",
" clip: rect(1px, 1px, 1px, 1px);\n",
" height: 1px;\n",
" margin: -1px;\n",
" overflow: hidden;\n",
" padding: 0;\n",
" position: absolute;\n",
" width: 1px;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-dashed-wrapped {\n",
" border: 1px dashed var(--sklearn-color-line);\n",
" margin: 0 0.4em 0.5em 0.4em;\n",
" box-sizing: border-box;\n",
" padding-bottom: 0.4em;\n",
" background-color: var(--sklearn-color-background);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-container {\n",
" /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
" but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
" so we also need the `!important` here to be able to override the\n",
" default hidden behavior on the sphinx rendered scikit-learn.org.\n",
" See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
" display: inline-block !important;\n",
" position: relative;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-text-repr-fallback {\n",
" display: none;\n",
"}\n",
"\n",
"div.sk-parallel-item,\n",
"div.sk-serial,\n",
"div.sk-item {\n",
" /* draw centered vertical line to link estimators */\n",
" background-image: linear-gradient(var(--sklearn-color-text-on-default-
background), var(--sklearn-color-text-on-default-background));\n",
" background-size: 2px 100%;\n",
" background-repeat: no-repeat;\n",
" background-position: center center;\n",
"}\n",
"\n",
"/* Parallel-specific style estimator block */\n",
"\n",
"#sk-container-id-3 div.sk-parallel-item::after {\n",
" content: \"\";\n",
" width: 100%;\n",
" border-bottom: 2px solid var(--sklearn-color-text-on-default-
background);\n",
" flex-grow: 1;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-parallel {\n",
" display: flex;\n",
" align-items: stretch;\n",
" justify-content: center;\n",
" background-color: var(--sklearn-color-background);\n",
" position: relative;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-parallel-item {\n",
" display: flex;\n",
" flex-direction: column;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-parallel-item:first-child::after {\n",
" align-self: flex-end;\n",
" width: 50%;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-parallel-item:last-child::after {\n",
" align-self: flex-start;\n",
" width: 50%;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-parallel-item:only-child::after {\n",
" width: 0;\n",
"}\n",
"\n",
"/* Serial-specific style estimator block */\n",
"\n",
"#sk-container-id-3 div.sk-serial {\n",
" display: flex;\n",
" flex-direction: column;\n",
" align-items: center;\n",
" background-color: var(--sklearn-color-background);\n",
" padding-right: 1em;\n",
" padding-left: 1em;\n",
"}\n",
"\n",
"\n",
"/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer
box that is\n",
"clickable and can be expanded/collapsed.\n",
"- Pipeline and ColumnTransformer use this feature and define the default
style\n",
"- Estimators will overwrite some part of the style using the `sk-estimator`
class\n",
"*/\n",
"\n",
"/* Pipeline and ColumnTransformer style (default) */\n",
"\n",
"#sk-container-id-3 div.sk-toggleable {\n",
" /* Default theme specific background. It is overwritten whether we have
a\n",
" specific estimator or a Pipeline/ColumnTransformer */\n",
" background-color: var(--sklearn-color-background);\n",
"}\n",
"\n",
"/* Toggleable label */\n",
"#sk-container-id-3 label.sk-toggleable__label {\n",
" cursor: pointer;\n",
" display: block;\n",
" width: 100%;\n",
" margin-bottom: 0;\n",
" padding: 0.5em;\n",
" box-sizing: border-box;\n",
" text-align: center;\n",
"}\n",
"\n",
"#sk-container-id-3 label.sk-toggleable__label-arrow:before {\n",
" /* Arrow on the left of the label */\n",
" content: \"▸\";\n",
" float: left;\n",
" margin-right: 0.25em;\n",
" color: var(--sklearn-color-icon);\n",
"}\n",
"\n",
"#sk-container-id-3 label.sk-toggleable__label-arrow:hover:before {\n",
" color: var(--sklearn-color-text);\n",
"}\n",
"\n",
"/* Toggleable content - dropdown */\n",
"\n",
"#sk-container-id-3 div.sk-toggleable__content {\n",
" max-height: 0;\n",
" max-width: 0;\n",
" overflow: hidden;\n",
" text-align: left;\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-toggleable__content.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-toggleable__content pre {\n",
" margin: 0.2em;\n",
" border-radius: 0.25em;\n",
" color: var(--sklearn-color-text);\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-toggleable__content.fitted pre {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-3 input.sk-toggleable__control:checked~div.sk-
toggleable__content {\n",
" /* Expand drop-down */\n",
" max-height: 200px;\n",
" max-width: 100%;\n",
" overflow: auto;\n",
"}\n",
"\n",
"#sk-container-id-3 input.sk-toggleable__control:checked~label.sk-
toggleable__label-arrow:before {\n",
" content: \"▾\";\n",
"}\n",
"\n",
"/* Pipeline/ColumnTransformer-specific style */\n",
"\n",
"#sk-container-id-3 div.sk-label input.sk-
toggleable__control:checked~label.sk-toggleable__label {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-label.fitted input.sk-
toggleable__control:checked~label.sk-toggleable__label {\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Estimator-specific style */\n",
"\n",
"/* Colorize estimator box */\n",
"#sk-container-id-3 div.sk-estimator input.sk-
toggleable__control:checked~label.sk-toggleable__label {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-estimator.fitted input.sk-
toggleable__control:checked~label.sk-toggleable__label {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-label label.sk-toggleable__label,\n",
"#sk-container-id-3 div.sk-label label {\n",
" /* The background is the default theme color */\n",
" color: var(--sklearn-color-text-on-default-background);\n",
"}\n",
"\n",
"/* On hover, darken the color of the background */\n",
"#sk-container-id-3 div.sk-label:hover label.sk-toggleable__label {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"/* Label box, darken color on hover, fitted */\n",
"#sk-container-id-3 div.sk-label.fitted:hover label.sk-
toggleable__label.fitted {\n",
" color: var(--sklearn-color-text);\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Estimator label */\n",
"\n",
"#sk-container-id-3 div.sk-label label {\n",
" font-family: monospace;\n",
" font-weight: bold;\n",
" display: inline-block;\n",
" line-height: 1.2em;\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-label-container {\n",
" text-align: center;\n",
"}\n",
"\n",
"/* Estimator-specific */\n",
"#sk-container-id-3 div.sk-estimator {\n",
" font-family: monospace;\n",
" border: 1px dotted var(--sklearn-color-border-box);\n",
" border-radius: 0.25em;\n",
" box-sizing: border-box;\n",
" margin-bottom: 0.5em;\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-0);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-estimator.fitted {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-0);\n",
"}\n",
"\n",
"/* on hover */\n",
"#sk-container-id-3 div.sk-estimator:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-2);\n",
"}\n",
"\n",
"#sk-container-id-3 div.sk-estimator.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-2);\n",
"}\n",
"\n",
"/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
"\n",
"/* Common style for \"i\" and \"?\" */\n",
"\n",
".sk-estimator-doc-link,\n",
"a:link.sk-estimator-doc-link,\n",
"a:visited.sk-estimator-doc-link {\n",
" float: right;\n",
" font-size: smaller;\n",
" line-height: 1em;\n",
" font-family: monospace;\n",
" background-color: var(--sklearn-color-background);\n",
" border-radius: 1em;\n",
" height: 1em;\n",
" width: 1em;\n",
" text-decoration: none !important;\n",
" margin-left: 1ex;\n",
" /* unfitted */\n",
" border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
" color: var(--sklearn-color-unfitted-level-1);\n",
"}\n",
"\n",
".sk-estimator-doc-link.fitted,\n",
"a:link.sk-estimator-doc-link.fitted,\n",
"a:visited.sk-estimator-doc-link.fitted {\n",
" /* fitted */\n",
" border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-1);\n",
"}\n",
"\n",
"/* On hover */\n",
"div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
".sk-estimator-doc-link:hover,\n",
"div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
".sk-estimator-doc-link:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-3);\n",
" color: var(--sklearn-color-background);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
".sk-estimator-doc-link.fitted:hover,\n",
"div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
".sk-estimator-doc-link.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-3);\n",
" color: var(--sklearn-color-background);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"/* Span, style for the box shown on hovering the info icon */\n",
".sk-estimator-doc-link span {\n",
" display: none;\n",
" z-index: 9999;\n",
" position: relative;\n",
" font-weight: normal;\n",
" right: .2ex;\n",
" padding: .5ex;\n",
" margin: .5ex;\n",
" width: min-content;\n",
" min-width: 20ex;\n",
" max-width: 50ex;\n",
" color: var(--sklearn-color-text);\n",
" box-shadow: 2pt 2pt 4pt #999;\n",
" /* unfitted */\n",
" background: var(--sklearn-color-unfitted-level-0);\n",
" border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link.fitted span {\n",
" /* fitted */\n",
" background: var(--sklearn-color-fitted-level-0);\n",
" border: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"\n",
".sk-estimator-doc-link:hover span {\n",
" display: block;\n",
"}\n",
"\n",
"/* \"?\"-specific style due to the `<a>` HTML tag */\n",
"\n",
"#sk-container-id-3 a.estimator_doc_link {\n",
" float: right;\n",
" font-size: 1rem;\n",
" line-height: 1em;\n",
" font-family: monospace;\n",
" background-color: var(--sklearn-color-background);\n",
" border-radius: 1rem;\n",
" height: 1rem;\n",
" width: 1rem;\n",
" text-decoration: none;\n",
" /* unfitted */\n",
" color: var(--sklearn-color-unfitted-level-1);\n",
" border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
"}\n",
"\n",
"#sk-container-id-3 a.estimator_doc_link.fitted {\n",
" /* fitted */\n",
" border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
" color: var(--sklearn-color-fitted-level-1);\n",
"}\n",
"\n",
"/* On hover */\n",
"#sk-container-id-3 a.estimator_doc_link:hover {\n",
" /* unfitted */\n",
" background-color: var(--sklearn-color-unfitted-level-3);\n",
" color: var(--sklearn-color-background);\n",
" text-decoration: none;\n",
"}\n",
"\n",
"#sk-container-id-3 a.estimator_doc_link.fitted:hover {\n",
" /* fitted */\n",
" background-color: var(--sklearn-color-fitted-level-3);\n",
"}\n",
"</style><div id=\"sk-container-id-3\" class=\"sk-top-container\"><div
class=\"sk-text-repr-fallback\"><pre>LinearRegression()</pre><b>In a Jupyter
environment, please rerun this cell to show the HTML representation or trust the
notebook. <br />On GitHub, the HTML representation is unable to render, please try
loading this page with nbviewer.org.</b></div><div class=\"sk-container\"
hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-
toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-
estimator-id-3\" type=\"checkbox\" checked><label for=\"sk-estimator-id-3\"
class=\"sk-toggleable__label fitted sk-toggleable__label-arrow
fitted\">&nbsp;&nbsp;LinearRegression<a class=\"sk-estimator-doc-link fitted\"
rel=\"noreferrer\" target=\"_blank\"
href=\"https://scikit-learn.org/1.4/modules/generated/sklearn.linear_model.LinearRe
gression.html\">?<span>Documentation for LinearRegression</span></a><span
class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></label><div
class=\"sk-toggleable__content fitted\"><pre>LinearRegression()</pre></div>
</div></div></div></div>"
],
"text/plain": [
"LinearRegression()"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Importing and loading the Linear Regression model on the data.\n",
"\n",
"from sklearn.linear_model import LinearRegression\n",
"\n",
"lr = LinearRegression()\n",
"lr.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "8d1e0a3f-4baa-411d-b5eb-90153a03c89d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[28.63252461 36.48602086 15.4189334 25.39319615 18.85542687 23.14196315\n",
" 17.38803819 14.06783335 23.04060004 20.58567038 24.8304966 18.54903944\n",
" -6.85534914 21.79886221 19.22195173 26.18174676 20.2844755 5.62300497\n",
" 40.43768792 17.57865999 27.42731845 30.15917866 10.93074566 23.99706963\n",
" 18.07925258 15.94318816 23.12256494 14.55801939 22.33614573 19.31522404\n",
" 22.15661595 25.17988256 25.29889979 18.5274093 16.62345866 17.52123633\n",
" 30.9352234 20.18058657 23.9016633 24.85660195 13.92840266 31.80230979\n",
" 42.55820862 17.62362431 26.99706912 17.19500817 13.7957965 26.0889121\n",
" 20.31719055 30.07798888 21.32984607 34.14817303 15.58954428 26.08619306\n",
" 39.29980093 22.99527802 18.95885672 33.04454222 24.83380987 12.93150916\n",
" 20.74721067 30.80662141 31.61362771 16.28330058 21.09350412 16.58402988\n",
" 20.35925216 26.14458437 31.05928094 11.99175967 20.41681948 27.55532026\n",
" 10.94472185 16.84280526 23.92147147 5.28087568 21.41455878 41.32710238\n",
" 18.21375852 9.49359688 21.19692567 12.94799555 21.67192035 9.39238193\n",
" 23.05296377 31.94739255 19.16416096 25.58389244 29.3467935 20.12397296\n",
" 25.5587241 5.43290922 20.2289884 15.20011603 14.062659 20.9084651\n",
" 24.82046202 -0.46940268 13.70345263 15.68603072 22.05258067 24.63507033\n",
" 10.7441925 19.6756647 23.64149838 12.07610712 18.46679762 25.51108845\n",
" 20.92417535 24.71305511 7.58933577 18.9987648 21.9349782 27.19865355\n",
" 32.16661077 15.28139453 34.35494559 12.95721321 21.0021746 28.57541619\n",
" 15.88124605 24.84735958 3.38197795 23.89839525 25.80726248 23.09995905\n",
" 25.34942887 33.32792365 20.62475134 38.46095259 13.97842334 25.20383548\n",
" 17.80061349 20.62556377 9.81967929 21.07548805 22.32082393 32.34312881\n",
" 31.48594411 15.45764717 16.8507661 29.00837324 24.96783449 16.73899638\n",
" 6.12761957 26.66264105 23.35932155 17.40141528 13.37640178 39.94608459\n",
" 16.68209254 18.28708111]\n"
]
}
],
"source": [
"y_pred = lr.predict(X_test)\n",
"print(y_pred)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "ea89df63-5a04-435d-9459-93751f5e34b0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Root Mead squared Error is:\n",
"4.647395902281432\n"
]
}
],
"source": [
"#Root Mean Squared Error:\n",
"\n",
"from sklearn.metrics import mean_squared_error\n",
"\n",
"rmse = np.sqrt(mean_squared_error(y_test, y_pred))\n",
"print(\"Root Mead squared Error is:\")\n",
"print(rmse)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "a6d4b86d-35f1-4def-8f42-87eac5313502",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Training accuracy is:\n"
]
},
{
"data": {
"text/plain": [
"0.7429813753968331"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Training Accuracy:\n",
"\n",
"print(\"Training accuracy is:\")\n",
"lr.score(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "36a255f0-459b-4b53-8da1-da9dae98938a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Testing accuracy is:\n"
]
},
{
"data": {
"text/plain": [
"0.7101410361770519"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Testing Accuracy:\n",
"\n",
"print(\"Testing accuracy is:\")\n",
"lr.score(X_test, y_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e51b3e74-f325-4b82-97fe-08746b66e471",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

You might also like