You are on page 1of 8

{"metadata":{"kernelspec":{"language":"python","display_name":"Python

3","name":"python3"},"language_info":
{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_
extension":".py","codemirror_mode":
{"name":"ipython","version":3},"name":"python","mimetype":"text/x-
python"}},"nbformat_minor":4,"nbformat":4,"cells":
[{"cell_type":"markdown","source":"# House Price Prediction","metadata":
{"id":"ysV4SS4uews3"}},{"cell_type":"markdown","source":"# Importing
Dependencies","metadata":{}},{"cell_type":"code","source":"import pandas as pd\
nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom
sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import
StandardScaler\nfrom sklearn.metrics import r2_score,
mean_absolute_error,mean_squared_error\nfrom sklearn.linear_model import
LinearRegression\nfrom sklearn.linear_model import Lasso\nfrom sklearn.ensemble
import RandomForestRegressor\nfrom sklearn.svm import SVR\nimport xgboost as xg\n\n
%matplotlib inline\nimport warnings\
nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:11:54.33126Z","iopub.execute_input":"2023-09-
28T05:11:54.331806Z","iopub.status.idle":"2023-09-
28T05:11:56.349909Z","shell.execute_reply.started":"2023-09-
28T05:11:54.331766Z","shell.execute_reply":"2023-09-
28T05:11:56.348383Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"### Loading Dataset","metadata":{}},
{"cell_type":"code","source":"dataset =
pd.read_csv('/kaggle/input/usa-housing/USA_Housing.csv')","metadata":
{"id":"NoZiIyxxe9z9","execution":{"iopub.status.busy":"2023-09-
28T05:11:56.352547Z","iopub.execute_input":"2023-09-
28T05:11:56.352926Z","iopub.status.idle":"2023-09-
28T05:11:56.403657Z","shell.execute_reply.started":"2023-09-
28T05:11:56.352893Z","shell.execute_reply":"2023-09-
28T05:11:56.401883Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"# Data Exploration","metadata":{}},
{"cell_type":"code","source":"dataset","metadata":
{"id":"yQyf_iRbfOhd","outputId":"de9891f0-cd0d-4ebe-a33a-860c6a29d138","execution":
{"iopub.status.busy":"2023-09-28T05:11:56.405469Z","iopub.execute_input":"2023-09-
28T05:11:56.406005Z","iopub.status.idle":"2023-09-
28T05:11:56.441208Z","shell.execute_reply.started":"2023-09-
28T05:11:56.405931Z","shell.execute_reply":"2023-09-
28T05:11:56.439727Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"dataset.info()","metadata":
{"id":"hn6TotD8fUA9","outputId":"db582509-ba4d-4ced-cf2f-6ed7bc16d80e","execution":
{"iopub.status.busy":"2023-09-28T05:11:56.443052Z","iopub.execute_input":"2023-09-
28T05:11:56.443538Z","iopub.status.idle":"2023-09-
28T05:11:56.476991Z","shell.execute_reply.started":"2023-09-
28T05:11:56.443493Z","shell.execute_reply":"2023-09-
28T05:11:56.476025Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"dataset.describe()","metadata":
{"id":"ybu4UcaWfZuk","outputId":"b186d0d6-e5e6-46d2-b6bf-5e691f317d88","execution":
{"iopub.status.busy":"2023-09-28T05:11:56.480001Z","iopub.execute_input":"2023-09-
28T05:11:56.481005Z","iopub.status.idle":"2023-09-
28T05:11:56.521596Z","shell.execute_reply.started":"2023-09-
28T05:11:56.480931Z","shell.execute_reply":"2023-09-
28T05:11:56.520294Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"dataset.columns","metadata":
{"id":"fwLd0YWBfdbs","outputId":"e91e32ba-52ca-4798-87e3-63ea2016f1ac","execution":
{"iopub.status.busy":"2023-09-28T05:11:56.523381Z","iopub.execute_input":"2023-09-
28T05:11:56.52374Z","iopub.status.idle":"2023-09-
28T05:11:56.532673Z","shell.execute_reply.started":"2023-09-
28T05:11:56.52371Z","shell.execute_reply":"2023-09-
28T05:11:56.531467Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"# Visualisation and Pre-Processing of
Data","metadata":{}},{"cell_type":"code","source":"sns.histplot(dataset, x='Price',
bins=50, color='y')","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:11:56.535116Z","iopub.execute_input":"2023-09-
28T05:11:56.536219Z","iopub.status.idle":"2023-09-
28T05:11:56.99267Z","shell.execute_reply.started":"2023-09-
28T05:11:56.536172Z","shell.execute_reply":"2023-09-
28T05:11:56.991432Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"sns.boxplot(dataset, x='Price',
palette='Blues')","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:11:56.994437Z","iopub.execute_input":"2023-09-
28T05:11:56.995701Z","iopub.status.idle":"2023-09-
28T05:11:57.228726Z","shell.execute_reply.started":"2023-09-
28T05:11:56.995644Z","shell.execute_reply":"2023-09-
28T05:11:57.227338Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"sns.jointplot(dataset, x='Avg. Area House Age',
y='Price', kind='hex')","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:11:57.230608Z","iopub.execute_input":"2023-09-
28T05:11:57.231083Z","iopub.status.idle":"2023-09-
28T05:11:58.324749Z","shell.execute_reply.started":"2023-09-
28T05:11:57.231033Z","shell.execute_reply":"2023-09-
28T05:11:58.323215Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"sns.jointplot(dataset, x='Avg. Area Income',
y='Price')","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:11:58.32674Z","iopub.execute_input":"2023-09-
28T05:11:58.32743Z","iopub.status.idle":"2023-09-
28T05:11:59.68517Z","shell.execute_reply.started":"2023-09-
28T05:11:58.327392Z","shell.execute_reply":"2023-09-
28T05:11:59.683696Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"plt.figure(figsize=(12,8))\
nsns.pairplot(dataset)","metadata":{"id":"YmWgq5hcfqH9","outputId":"1153c150-c6ba-
4c0d-b34c-4a8e33f52454","execution":{"iopub.status.busy":"2023-09-
28T05:11:59.686664Z","iopub.execute_input":"2023-09-
28T05:11:59.687042Z","iopub.status.idle":"2023-09-
28T05:12:13.501118Z","shell.execute_reply.started":"2023-09-
28T05:11:59.687008Z","shell.execute_reply":"2023-09-
28T05:12:13.499024Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"dataset.hist(figsize=(10,8))","metadata":
{"id":"8G3oWFcziLzM","outputId":"85b6f344-41b2-430c-fdde-df34f9d1f4bb","execution":
{"iopub.status.busy":"2023-09-28T05:12:13.502516Z","iopub.execute_input":"2023-09-
28T05:12:13.502862Z","iopub.status.idle":"2023-09-
28T05:12:15.111986Z","shell.execute_reply.started":"2023-09-
28T05:12:13.50283Z","shell.execute_reply":"2023-09-
28T05:12:15.11067Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Visualising Correlation ","metadata":{}},
{"cell_type":"code","source":"dataset.corr(numeric_only=True)","metadata":
{"execution":{"iopub.status.busy":"2023-09-
28T05:12:15.113636Z","iopub.execute_input":"2023-09-
28T05:12:15.114135Z","iopub.status.idle":"2023-09-
28T05:12:15.136032Z","shell.execute_reply.started":"2023-09-
28T05:12:15.114092Z","shell.execute_reply":"2023-09-
28T05:12:15.134206Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"plt.figure(figsize=(10,5))\
nsns.heatmap(dataset.corr(numeric_only = True), annot=True)","metadata":
{"id":"fsGGAu1_f5JF","outputId":"3c4ab08c-f947-4ba7-80d6-c8261e5d2176","execution":
{"iopub.status.busy":"2023-09-28T05:12:15.141913Z","iopub.execute_input":"2023-09-
28T05:12:15.142285Z","iopub.status.idle":"2023-09-
28T05:12:15.683992Z","shell.execute_reply.started":"2023-09-
28T05:12:15.142257Z","shell.execute_reply":"2023-09-
28T05:12:15.683002Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"# Dividing Dataset in to features and target
variable","metadata":{}},{"cell_type":"code","source":"X = dataset[['Avg. Area
Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',\n 'Avg. Area
Number of Bedrooms', 'Area Population']]\nY = dataset['Price']","metadata":
{"id":"IlwWfa_DgOD9","execution":{"iopub.status.busy":"2023-09-
28T05:12:15.685547Z","iopub.execute_input":"2023-09-
28T05:12:15.686558Z","iopub.status.idle":"2023-09-
28T05:12:15.69339Z","shell.execute_reply.started":"2023-09-
28T05:12:15.686521Z","shell.execute_reply":"2023-09-
28T05:12:15.692221Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"# Using Train Test Split","metadata":{}},
{"cell_type":"code","source":"X_train, X_test, Y_train, Y_test =
train_test_split(X, Y, test_size=0.2, random_state=101)\n","metadata":
{"id":"zIEMiNvWhF-E","execution":{"iopub.status.busy":"2023-09-
28T05:12:15.694668Z","iopub.execute_input":"2023-09-
28T05:12:15.695766Z","iopub.status.idle":"2023-09-
28T05:12:15.712487Z","shell.execute_reply.started":"2023-09-
28T05:12:15.695721Z","shell.execute_reply":"2023-09-
28T05:12:15.710949Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"Y_train.head()","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:15.714568Z","iopub.execute_input":"2023-09-
28T05:12:15.714984Z","iopub.status.idle":"2023-09-
28T05:12:15.729264Z","shell.execute_reply.started":"2023-09-
28T05:12:15.71492Z","shell.execute_reply":"2023-09-
28T05:12:15.727983Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:15.731283Z","iopub.execute_input":"2023-09-
28T05:12:15.731645Z","iopub.status.idle":"2023-09-
28T05:12:15.744191Z","shell.execute_reply.started":"2023-09-
28T05:12:15.731615Z","shell.execute_reply":"2023-09-
28T05:12:15.74263Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"Y_test.head()","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:15.745763Z","iopub.execute_input":"2023-09-
28T05:12:15.746161Z","iopub.status.idle":"2023-09-
28T05:12:15.760668Z","shell.execute_reply.started":"2023-09-
28T05:12:15.746128Z","shell.execute_reply":"2023-09-28T05:12:15.759321Z"},"tru
sted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"Y_test.shape","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:15.762791Z","iopub.execute_input":"2023-09-
28T05:12:15.763198Z","iopub.status.idle":"2023-09-
28T05:12:15.774232Z","shell.execute_reply.started":"2023-09-
28T05:12:15.763165Z","shell.execute_reply":"2023-09-
28T05:12:15.773084Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"# Standardizing the data","metadata":{}},
{"cell_type":"code","source":"sc = StandardScaler()\nX_train_scal =
sc.fit_transform(X_train)\nX_test_scal = sc.fit_transform(X_test)","metadata":
{"execution":{"iopub.status.busy":"2023-09-
28T05:12:15.777138Z","iopub.execute_input":"2023-09-
28T05:12:15.777629Z","iopub.status.idle":"2023-09-
28T05:12:15.793663Z","shell.execute_reply.started":"2023-09-
28T05:12:15.777586Z","shell.execute_reply":"2023-09-
28T05:12:15.79274Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"# Model Building and Evaluation","metadata":{}},
{"cell_type":"markdown","source":"## Model 1 - Linear Regression","metadata":{}},
{"cell_type":"code","source":"model_lr=LinearRegression()","metadata":
{"id":"FgWH2V4Gixac","execution":{"iopub.status.busy":"2023-09-
28T05:12:15.794813Z","iopub.execute_input":"2023-09-
28T05:12:15.795228Z","iopub.status.idle":"2023-09-
28T05:12:15.801275Z","shell.execute_reply.started":"2023-09-
28T05:12:15.795197Z","shell.execute_reply":"2023-09-
28T05:12:15.800288Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"model_lr.fit(X_train_scal, Y_train)","metadata":
{"id":"WxcPD34TjGPu","outputId":"3f96e335-6708-458f-dac9-accb2a1b3837","execution":
{"iopub.status.busy":"2023-09-28T05:12:15.802456Z","iopub.execute_input":"2023-09-
28T05:12:15.802827Z","iopub.status.idle":"2023-09-
28T05:12:15.84717Z","shell.execute_reply.started":"2023-09-
28T05:12:15.802771Z","shell.execute_reply":"2023-09-
28T05:12:15.846254Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Predicting Prices","metadata":{}},
{"cell_type":"code","source":"Prediction1 =
model_lr.predict(X_test_scal)","metadata":{"id":"p6RcjGiHj-R0","execution":
{"iopub.status.busy":"2023-09-28T05:12:15.848189Z","iopub.execute_input":"2023-09-
28T05:12:15.848499Z","iopub.status.idle":"2023-09-
28T05:12:15.855184Z","shell.execute_reply.started":"2023-09-
28T05:12:15.848471Z","shell.execute_reply":"2023-09-
28T05:12:15.854125Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Evaluation of Predicted Data","metadata":{}},
{"cell_type":"code","source":" plt.figure(figsize=(12,6))\n
plt.plot(np.arange(len(Y_test)), Y_test, label='Actual Trend')\n
plt.plot(np.arange(len(Y_test)), Prediction1, label='Predicted Trend')\n
plt.xlabel('Data')\n plt.ylabel('Trend')\n plt.legend()\n
plt.title('Actual vs Predicted')","metadata":
{"id":"ieEfszuykQqU","outputId":"03545449-ba53-4d77-aac4-81581ec424e5","execution":
{"iopub.status.busy":"2023-09-28T05:12:15.856677Z","iopub.execute_input":"2023-09-
28T05:12:15.857181Z","iopub.status.idle":"2023-09-
28T05:12:16.364164Z","shell.execute_reply.started":"2023-09-
28T05:12:15.85714Z","shell.execute_reply":"2023-09-
28T05:12:16.362895Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"sns.histplot((Y_test-Prediction1),
bins=50)","metadata":{"id":"vMoNYG_7URlf","outputId":"213a92e6-2515-4958-ad34-
9d4442d6c34a","execution":{"iopub.status.busy":"2023-09-
28T05:12:16.365851Z","iopub.execute_input":"2023-09-
28T05:12:16.366301Z","iopub.status.idle":"2023-09-
28T05:12:16.783157Z","shell.execute_reply.started":"2023-09-
28T05:12:16.36626Z","shell.execute_reply":"2023-09-
28T05:12:16.781738Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"print(r2_score(Y_test, Prediction1))\
nprint(mean_absolute_error(Y_test, Prediction1))\nprint(mean_squared_error(Y_test,
Prediction1))","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:12:16.785094Z","iopub.execute_input":"2023-09-
28T05:12:16.785686Z","iopub.status.idle":"2023-09-
28T05:12:16.794084Z","shell.execute_reply.started":"2023-09-
28T05:12:16.78565Z","shell.execute_reply":"2023-09-
28T05:12:16.792832Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Model 2 - Support Vector
Regressor","metadata":{}},{"cell_type":"code","source":"model_svr =
SVR()","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:12:16.795584Z","iopub.execute_input":"2023-09-
28T05:12:16.795918Z","iopub.status.idle":"2023-09-
28T05:12:16.810956Z","shell.execute_reply.started":"2023-09-
28T05:12:16.795889Z","shell.execute_reply":"2023-09-
28T05:12:16.809641Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"model_svr.fit(X_train_scal, Y_train)","metadata":
{"execution":{"iopub.status.busy":"2023-09-
28T05:12:16.812749Z","iopub.execute_input":"2023-09-
28T05:12:16.81323Z","iopub.status.idle":"2023-09-
28T05:12:17.606612Z","shell.execute_reply.started":"2023-09-
28T05:12:16.813188Z","shell.execute_reply":"2023-09-
28T05:12:17.605476Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Predicting Prices","metadata":{}},
{"cell_type":"code","source":"Prediction2 =
model_svr.predict(X_test_scal)","metadata":{"execution":{"iopub.status.busy":"2023-
09-28T05:12:17.608265Z","iopub.execute_input":"2023-09-
28T05:12:17.609438Z","iopub.status.idle":"2023-09-
28T05:12:17.8063Z","shell.execute_reply.started":"2023-09-
28T05:12:17.609389Z","shell.execute_reply":"2023-09-
28T05:12:17.804949Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Evaluation of Predicted Data","metadata":{}},
{"cell_type":"code","source":" plt.figure(figsize=(12,6))\n
plt.plot(np.arange(len(Y_test)), Y_test, label='Actual Trend')\n
plt.plot(np.arange(len(Y_test)), Prediction2, label='Predicted Trend')\n
plt.xlabel('Data')\n plt.ylabel('Trend')\n plt.legend()\n
plt.title('Actual vs Predicted')","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:17.807808Z","iopub.execute_input":"2023-09-
28T05:12:17.808139Z","iopub.status.idle":"2023-09-
28T05:12:18.273572Z","shell.execute_reply.started":"2023-09-
28T05:12:17.808112Z","shell.execute_reply":"2023-09-
28T05:12:18.272088Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"sns.histplot((Y_test-Prediction2),
bins=50)","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:12:18.276244Z","iopub.execute_input":"2023-09-
28T05:12:18.276851Z","iopub.status.idle":"2023-09-
28T05:12:18.68124Z","shell.execute_reply.started":"2023-09-
28T05:12:18.276801Z","shell.execute_reply":"2023-09-
28T05:12:18.679878Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"print(r2_score(Y_test, Prediction2))\
nprint(mean_absolute_error(Y_test, Prediction2))\nprint(mean_squared_error(Y_test,
Prediction2))","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:12:18.683065Z","iopub.execute_input":"2023-09-
28T05:12:18.683955Z","iopub.status.idle":"2023-09-
28T05:12:18.693543Z","shell.execute_reply.started":"2023-09-
28T05:12:18.683871Z","shell.execute_reply":"2023-09-
28T05:12:18.692448Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Model 3 - Lasso Regression","metadata":{}},
{"cell_type":"code","source":"model_lar = Lasso(alpha=1)","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:18.69542Z","iopub.execute_input":"2023-09-
28T05:12:18.695822Z","iopub.status.idle":"2023-09-
28T05:12:18.710828Z","shell.execute_reply.started":"2023-09-
28T05:12:18.69579Z","shell.execute_reply":"2023-09-
28T05:12:18.709587Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"model_lar.fit(X_train_scal,Y_train)","metadata":
{"execution":{"iopub.status.busy":"2023-09-
28T05:12:18.712472Z","iopub.execute_input":"2023-09-
28T05:12:18.712806Z","iopub.status.idle":"2023-09-
28T05:12:18.736985Z","shell.execute_reply.started":"2023-09-
28T05:12:18.712778Z","shell.execute_reply":"2023-09-
28T05:12:18.735333Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Predicting Prices","metadata":{}},
{"cell_type":"code","source":"Prediction3 =
model_lar.predict(X_test_scal)","metadata":{"execution":{"iopub.status.busy":"2023-
09-28T05:12:18.739596Z","iopub.execute_input":"2023-09-
28T05:12:18.740323Z","iopub.status.idle":"2023-09-
28T05:12:18.748413Z","shell.execute_reply.started":"2023-09-
28T05:12:18.740248Z","shell.execute_reply":"2023-09-
28T05:12:18.746681Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Evaluation of Predicted Data","metadata":{}},
{"cell_type":"code","source":" plt.figure(figsize=(12,6))\n
plt.plot(np.arange(len(Y_test)), Y_test, label='Actual Trend')\n
plt.plot(np.arange(len(Y_test)), Prediction3, label='Predicted Trend')\n
plt.xlabel('Data')\n plt.ylabel('Trend')\n plt.legend()\n
plt.title('Actual vs Predicted')","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:18.751292Z","iopub.execute_input":"2023-09-
28T05:12:18.751957Z","iopub.status.idle":"2023-09-
28T05:12:19.303512Z","shell.execute_reply.started":"2023-09-
28T05:12:18.75189Z","shell.execute_reply":"2023-09-
28T05:12:19.302098Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"sns.histplot((Y_test-Prediction3),
bins=50)","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:12:19.305256Z","iopub.execute_input":"2023-09-
28T05:12:19.30562Z","iopub.status.idle":"2023-09-
28T05:12:19.712421Z","shell.execute_reply.started":"2023-09-
28T05:12:19.305588Z","shell.execute_reply":"2023-09-
28T05:12:19.711041Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"print(r2_score(Y_test,
Prediction2))\nprint(mean_absolute_error(Y_test, Prediction2))\
nprint(mean_squared_error(Y_test, Prediction2))","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:19.7141Z","iopub.execute_input":"2023-09-
28T05:12:19.714484Z","iopub.status.idle":"2023-09-
28T05:12:19.725304Z","shell.execute_reply.started":"2023-09-
28T05:12:19.714449Z","shell.execute_reply":"2023-09-
28T05:12:19.724017Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Model 4 - Random Forest Regressor","metadata":
{}},{"cell_type":"code","source":"model_rf =
RandomForestRegressor(n_estimators=50)","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:19.727527Z","iopub.execute_input":"2023-09-
28T05:12:19.728238Z","iopub.status.idle":"2023-09-
28T05:12:19.739665Z","shell.execute_reply.started":"2023-09-
28T05:12:19.7282Z","shell.execute_reply":"2023-09-
28T05:12:19.738386Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"model_rf.fit(X_train_scal, Y_train)","metadata":
{"execution":{"iopub.status.busy":"2023-09-
28T05:12:19.749966Z","iopub.execute_input":"2023-09-
28T05:12:19.751591Z","iopub.status.idle":"2023-09-
28T05:12:20.668315Z","shell.execute_reply.started":"2023-09-
28T05:12:19.751519Z","shell.execute_reply":"2023-09-
28T05:12:20.667047Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Predicting Prices","metadata":{}},
{"cell_type":"code","source":"Prediction4 =
model_rf.predict(X_test_scal)","metadata":{"execution":{"iopub.status.busy":"2023-
09-28T05:12:20.670318Z","iopub.execute_input":"2023-09-
28T05:12:20.671229Z","iopub.status.idle":"2023-09-
28T05:12:20.700185Z","shell.execute_reply.started":"2023-09-
28T05:12:20.67119Z","shell.execute_reply":"2023-09-
28T05:12:20.698959Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Evaluation of Predicted Data","metadata":{}},
{"cell_type":"code","source":" plt.figure(figsize=(12,6))\n
plt.plot(np.arange(len(Y_test)), Y_test, label='Actual Trend')\n
plt.plot(np.arange(len(Y_test)), Prediction4, label='Predicted Trend')\n
plt.xlabel('Data')\n plt.ylabel('Trend')\n plt.legend()\n
plt.title('Actual vs Predicted')","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:20.701864Z","iopub.execute_input":"2023-09-
28T05:12:20.702271Z","iopub.status.idle":"2023-09-
28T05:12:21.179553Z","shell.execute_reply.started":"2023-09-
28T05:12:20.702239Z","shell.execute_reply":"2023-09-
28T05:12:21.178279Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"sns.histplot((Y_test-Prediction4),
bins=50)","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:12:21.180876Z","iopub.execute_input":"2023-09-
28T05:12:21.18126Z","iopub.status.idle":"2023-09-
28T05:12:21.840782Z","shell.execute_reply.started":"2023-09-
28T05:12:21.181221Z","shell.execute_reply":"2023-09-
28T05:12:21.839589Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"print(r2_score(Y_test, Prediction2))\
nprint(mean_absolute_error(Y_test, Prediction2))\nprint(mean_squared_error(Y_test,
Prediction2))","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:12:21.842564Z","iopub.execute_input":"2023-09-
28T05:12:21.842899Z","iopub.status.idle":"2023-09-
28T05:12:21.852214Z","shell.execute_reply.started":"2023-09-
28T05:12:21.84287Z","shell.execute_reply":"2023-09-
28T05:12:21.850953Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Model 5 - XGboost Regressor","metadata":{}},
{"cell_type":"code","source":"model_xg = xg.XGBRegressor()","metadata":
{"execution":{"iopub.status.busy":"2023-09-
28T05:12:21.854021Z","iopub.execute_input":"2023-09-
28T05:12:21.854354Z","iopub.status.idle":"2023-09-
28T05:12:21.864688Z","shell.execute_reply.started":"2023-09-
28T05:12:21.854328Z","shell.execute_reply":"2023-09-
28T05:12:21.863431Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"model_xg.fit(X_train_scal, Y_train)","metadata":
{"execution":{"iopub.status.busy":"2023-09-
28T05:12:21.866742Z","iopub.execute_input":"2023-09-
28T05:12:21.867175Z","iopub.status.idle":"2023-09-
28T05:12:22.347273Z","shell.execute_reply.started":"2023-09-
28T05:12:21.867138Z","shell.execute_reply":"2023-09-
28T05:12:22.346062Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Predicting Prices","metadata":{}},
{"cell_type":"code","source":"Prediction5 =
model_xg.predict(X_test_scal)","metadata":{"execution":{"iopub.status.busy":"2023-
09-28T05:12:22.351534Z","iopub.execute_input":"2023-09-
28T05:12:22.352449Z","iopub.status.idle":"2023-09-
28T05:12:22.363772Z","shell.execute_reply.started":"2023-09-
28T05:12:22.352409Z","shell.execute_reply":"2023-09-
28T05:12:22.362692Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"## Evaluation of Predicted Data","metadata":{}},
{"cell_type":"code","source":" plt.figure(figsize=(12,6))\n
plt.plot(np.arange(len(Y_test)), Y_test, label='Actual Trend')\n
plt.plot(np.arange(len(Y_test)), Prediction5, label='Predicted Trend')\n
plt.xlabel('Data')\n plt.ylabel('Trend')\n plt.legend()\n
plt.title('Actual vs Predicted')","metadata":{"execution":
{"iopub.status.busy":"2023-09-28T05:12:22.368301Z","iopub.execute_input":"2023-09-
28T05:12:22.371874Z","iopub.status.idle":"2023-09-
28T05:12:22.86614Z","shell.execute_reply.started":"2023-09-
28T05:12:22.371816Z","shell.execute_reply":"2023-09-
28T05:12:22.864953Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"sns.histplot((Y_test-Prediction4),
bins=50)","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:12:22.867512Z","iopub.execute_input":"2023-09-
28T05:12:22.867843Z","iopub.status.idle":"2023-09-
28T05:12:23.313165Z","shell.execute_reply.started":"2023-09-
28T05:12:22.867813Z","shell.execute_reply":"2023-09-
28T05:12:23.311711Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"code","source":"print(r2_score(Y_test, Prediction2))\
nprint(mean_absolute_error(Y_test, Prediction2))\nprint(mean_squared_error(Y_test,
Prediction2))","metadata":{"execution":{"iopub.status.busy":"2023-09-
28T05:12:23.314732Z","iopub.execute_input":"2023-09-
28T05:12:23.315101Z","iopub.status.idle":"2023-09-
28T05:12:23.325665Z","shell.execute_reply.started":"2023-09-
28T05:12:23.31507Z","shell.execute_reply":"2023-09-
28T05:12:23.324128Z"},"trusted":true},"execution_count":null,"outputs":[]},
{"cell_type":"markdown","source":"### Linear Regression is giving us best
Accuracy.","metadata":{}}]}

You might also like