-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriceprediction.html
More file actions
446 lines (352 loc) · 18.7 KB
/
priceprediction.html
File metadata and controls
446 lines (352 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<!DOCTYPE html>
<html lang="en">
<head>
<!-- META TAGS-->
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Price Prediction Model Python ~ Tanmay's Portfolio</title>
<meta name="description" content="Tanmay's Career Portfolio・タンマイのキャリアポートフォリオ">
<meta name="twitter:image" content="photos/Helloname.jpeg">
<meta property="og:image" content="photos/Helloname.jpeg">
<!-- All the important imports-->
<link rel="icon" type="image/x-icon" href="photos/Favicon.png">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abel">
<link href='https://fonts.googleapis.com/css?family=Rubik' rel='stylesheet'>
<link rel="stylesheet" href="https://fonts.googleapis.com/earlyaccess/kokoro.css">
<link href="https://fonts.googleapis.com/css?family=Noto+Sans+JP" rel="stylesheet">
<link rel="stylesheet" href="css_folder/styles.css">
<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
<link rel="stylesheet" href="css_folder/priceprediction_styles.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/styles/atom-one-dark.min.css">
</head>
<body class="fade-in-text">
<!-- NAV BAR-->
<header class="nav-bar">
<div id="navitem"><h3><a href="index.html">Home</a></h3></div>
<div id="navitem"><h3><a href="#top">Top of Page</a></h3></div>
</header>
<h1>Price Prediction machine learning project</h1>
<h3 class="tottori_japan_title">日本語での説明はこちら</h3>
<div class="data_intro">
<p class="intro">
The technology of Machine Learning has enabled us to create exceptionally intiuitive Regression models in Python! This project looks into making one predicting housing prices in a particular area
</p>
<img class="data_intro_photo" src="photos/housing_price.png" alt="Photo_of_man_finding_home" >
</div>
<div class="intro_div">
<p class="intro_desc">
The current project will look into housing prices of cities of Mumbai, India and Delhi, India. Do correlation analysis on several factors of housing prices and make a machine learning model to predict the housing prices in either of the cities
</p>
<p class="intro_desc_jp">日本語説明</p>
<p id="date">Made on 6th April 2023</p>
</div>
<!-- Article photo-->
<img src="photos/mumbai.jpeg" alt="MumbaiStation Photo" class="tottoristation">
<!-- Beginning question-->
<h2 class="prediction_body question">What is a Regression model?</h2>
<p class="prediction_body answer">A regression model provides a function that describes the relationship between one or more independent variables and a response, dependent, or target variable. </p>
<p class="prediction_body body_padding">For this project we are going to focus on linear regression and focus on the correlation between two variables: 1) Housing prices and 2) Area of Land</p>
<p class="prediction_body body_padding prediction_question">To give context, what are the housing in big cities like Mumbai and Delhi like? Do they depend on Quality of the building? Size of the room? Or the amount of appliances in the room? If so, how does it work?</p>
<p class="prediction_body body_padding">One way to find out, is to first check the correlation between all the variables which we think can have effect on the housing prices and check how they react on a graph</p>
<p class="prediction_body body_padding">I have tried to solve the problem according to the following steps:
<ol class="prediction_body projectlist">
<li>Find a dataset which has all the data on housing prices and other variables for Mumbai and Delhi</li>
<li>Clean the dataset and check for correlations in the variables</li>
<li>Plot the relavant variables and check the regression model</li>
<li>Train the ML model</li>
<li>Check the Predicton Model on a dataset</li>
</ol>
</p>
<p class="prediction_body">Let's get started with first finding a suitable dataset!</p>
<p class="prediction_body body_padding">I have found this suitable dataset already ready for analysis on Kaggle</p>
<br>
<a class="kaggle_dataset" href="https://www.kaggle.com/datasets/ruchi798/housing-prices-in-metropolitan-areas-of-india">Get the dataset here</a>
<br>
<br>
<p class="prediction_body body_padding"> I took the <span>mumbai.csv</span> and <span>delhi.csv</span> from Kaggle for this project. However, you
can any other dataset for this project as well!
</p>
<p class="prediction_body">Step one is to load the important packages for this project and read the CSV file in the project</p>
<pre>
<code>
import pandas as pd
import numpy as np
import plotly.express as px
data=pd.read_csv("mumbai.csv")
print(data.head())
</code>
</pre>
<p class="prediction_body">Loading the body will have an output which looks like this</p>
<pre>
<code>
Price Area Location No. of Bedrooms Resale MaintenanceStaff \
0 4850000 720 Kharghar 1 1 1
1 4500000 600 Kharghar 1 1 1
2 6700000 650 Kharghar 1 1 1
3 4500000 650 Kharghar 1 1 1
4 5000000 665 Kharghar 1 1 1
Gymnasium SwimmingPool LandscapedGardens JoggingTrack ... \
0 0 0 0 0 ...
1 1 1 0 1 ...
2 1 1 0 1 ...
3 0 0 1 0 ...
4 0 0 1 0 ...
LiftAvailable BED VaastuCompliant Microwave GolfCourse TV \
0 1 0 1 0 0 0
1 1 0 1 0 0 0
2 1 0 1 0 0 0
3 1 1 1 0 0 0
4 1 0 1 0 0 0
DiningTable Sofa Wardrobe Refrigerator
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
3 0 0 1 0
4 0 0 0 0
[5 rows x 40 columns]
</code>
</pre>
<p class="prediction_body">The head is rather long because of the data we have for this project
<br>
We now check for the data type and the titles of every column with the following code
</p>
<pre>
<code>
data.info()
</code>
</pre>
<p class="prediction_body">The output will info on all of the columns and all the data-types in it which looks something like this</p>
<pre>
<code>
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7719 entries, 0 to 7718
Data columns (total 40 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Price 7719 non-null int64
1 Area 7719 non-null int64
2 Location 7719 non-null object
3 No. of Bedrooms 7719 non-null int64
4 Resale 7719 non-null int64
5 MaintenanceStaff 7719 non-null int64
6 Gymnasium 7719 non-null int64
7 SwimmingPool 7719 non-null int64
8 LandscapedGardens 7719 non-null int64
</code>
</pre>
<p class="prediction_body">
Next we check the values and their ranges
</p>
<pre>
<code>
data.describe()
</code>
</pre>
<p class="prediction_body">The output will describe the values such as the Max value, Minimum Value, Mean and much more
</p>
<pre>
<code>
Price Area No. of Bedrooms Resale MaintenanceStaff Gymnasium SwimmingPool LandscapedGardens JoggingTrack RainWaterHarvesting ... LiftAvailable BED VaastuCompliant Microwave GolfCourse TV DiningTable Sofa Wardrobe Refrigerator
count 7.719000e+03 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000 ... 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000 7719.000000
mean 1.506165e+07 998.409250 1.913331 0.647105 7.498899 7.473896 7.437881 7.441638 7.439435 7.477005 ... 7.518331 7.417930 7.454722 7.372069 7.379712 7.379065 7.373624 7.374530 7.383988 7.374789
std 2.052100e+07 550.967809 0.855376 0.477901 3.197923 3.252095 3.328245 3.320401 3.325002 3.245418 ... 3.155041 3.369523 3.292904 3.462108 3.446892 3.448185 3.459020 3.457217 3.438345 3.456702
min 2.000000e+06 200.000000 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ... 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
25% 5.300000e+06 650.000000 1.000000 0.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 ... 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000
50% 9.500000e+06 900.000000 2.000000 1.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 ... 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000
75% 1.700000e+07 1177.000000 2.000000 1.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 ... 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000
max 4.200000e+08 8511.000000 7.000000 1.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 ... 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000 9.000000
8 rows × 39 columns
</code>
</pre>
<p class="prediction_body">It is time to load all the machine learning related libraries</p>
<pre>
<code>
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.metrics import mean_absolute_error
from sklearn.impute import SimpleImputer
from sklearn.pipeline import make_pipeline
from ipywidgets import IntSlider, interact
from glob import glob
</code>
</pre>
<p class="prediction_body">We will clean the data and clear all the NULL values from the dataset and </p>
<pre>
<code>
data.replace(9, np.nan, inplace=True)
data.dropna(inplace=True)
data.isnull().sum()/len(data)*100
</code>
</pre>
<p class="prediction_body">We will do some additional clean and check for the correlation between the values</p>
<pre>
<code>
#Removing values which do not have a numberical value
number_columns = data.select_dtypes(exclude='object')
#Checking correlations between the values
number_columns.corr()
</code>
</pre>
<p class="prediction_body">The output will look something like this, all the variables being checked for correlation against the other variable</p>
<img src="photos/Correlation.png" alt="correlation_graph_table.png" class="prediction_body imageforarticle">
<p class="prediction_body">Find the whole table in the Jupyter of the project <a href="https://github.com/happygoluckycodeeditor/pricepredictionmodel/blob/main/pricepredictionmodel.ipynb">here</a></p>
<p class="prediction_body">It is little difficult to figure out which variables are worth considering for the analysis, we can thus create a visualisation to feel the aspect of correlation more effectively.
Let's create a heat map.
</p>
<pre>
<code>
numbers_correlation = number_columns.corr()
plt.figure(figsize=(40,32))
sns.heatmap(numbers_correlation, annot=True, square=True)
</code>
</pre>
<p class="prediction_body">The output will create a beautiful visualisation which looks like this</p>
<img src="photos/correlation_heatmap.png" alt="correlation_heatmap_prediction_analysis" class="prediction_body imageforarticle">
<p class="prediction_body">We can see that variable Prices V/s Area has a relatively good correlation, thus we can do linear regression against these values and check how they plot on a graph</p>
<p class="prediction_body"><span>Usually a correlation of 60% might not be a good indicator of a good correlation, but for this project, this is the only available option</span></p>
<p class="prediction_body body_padding">Let's plot all the values of Price V/s Area</p>
<pre>
<code>
plt.scatter(x = data['Price'], y = data['Area'])
plt.xlabel('Price of a property')
plt.ylabel('Area of the property')
plt.title('Mumbai, India: Price v/s Area')
</code>
</pre>
<p class="prediction_body">The resulting plot will look like this:</p>
<img src="photos/price_vs_area_1.png" alt="price_vs_area_1" class="prediction_body imageforarticle correl_1">
<p class="prediction_body">Let's do regression analysis and plot the most suitable line</p>
<pre>
<code>
sns.regplot(x = data['Price'], y = data['Area'])
plt.xlabel('Price of a property')
plt.ylabel('Area of the property')
plt.title('Mumbai, India: Price v/s Area')
</code>
</pre>
<p class="prediction_body">Resuting Graph will look something like this</p>
<img src="photos/price_vs_area_2.png" alt="price_vs_area_2" class="prediction_body imageforarticle correl_1">
<p class="prediction_body">Now let us remove some outliers, so the plot can does not get affected by them</p>
<pre>
<code>
normal_house_range = data["Price"]< 100000000
#Seperate those values and visualise them on the scatterplot
sns.scatterplot(x = data['Price'], y = data['Area'], hue = normal_house_range)
plt.xlabel('Price of the property')
plt.ylabel('Area of the property')
plt.title('Mumbai, India: Price v/s Area')
</code>
</pre>
<p class="prediction_body">Resulting graph will look something like this</p>
<img src="photos/price_vs_area_3.png" alt="price_vs_area_3" class="prediction_body imageforarticle correl_1">
<p class="prediction_body">Now let's train a model and define a baseline for the data! Anything or any value below the baseline can be considered as not reliable
</p>
<pre>
<code>
#Removing the outliers and revising the dataset
data = data[normal_house_range]
# 'Price' as the target of the prediction
target = "Price"
# 'Area' as the feature of the prediction
feature = ['Area']
X_train = data[feature]
y_train = data[target]
y_mean = y_train.mean()
y_pred_baseline = [y_mean] * len(y_train)
len(y_pred_baseline) == len(y_train)
plt.plot( X_train.values, y_pred_baseline, color="red")
plt.scatter(x = X_train, y = y_train)
plt.xlabel("Area of the property")
plt.ylabel("Price of the property")
plt.title("Mumbai, India: Price v/s Area")
</code>
</pre>
<p class="prediction_body">The resulting body will look something like this:</p>
<img src="photos/price_vs_area_4.png" alt="price_vs_area_4" class="prediction_body imageforarticle correl_1">
<p class="prediction_body">We need to check if our values are reliable. One way is to check for mean prediction error</p>
<pre>
<code>
mae_baseline = mean_absolute_error(y_train, y_pred_baseline)
print("Mean Apartment Price:", round(y_mean, 2))
print("Baseline MAE:", round(mae_baseline, 2))
</code>
</pre>
<p class="prediction_body">The output looks something like this:</p>
<p class="prediction_body prediction_question body_padding">
Mean Apartment Price: 13451545.18 <br>
Baseline MAE: 8324779.64
</p>
<p class="prediction_body">We will now train the machine learning model</p>
<pre>
<code>
#Buidling a machine learning model
model = LinearRegression()
model.fit(X_train, y_train)
#Training the model
y_pred_training = model.predict(X_train)
y_pred_training[:5]
#Checking the MAE for the training Model
mae_training = mean_absolute_error(y_train, y_pred_training)
print("Trainng MAE:", round(mae_training, 2))
</code>
</pre>
<p class="prediction_body prediction_question body_padding">
The training MAE will look like this: <br>
Trainng MAE: 5566140.1
</p>
<p class="prediction_body body_padding">The model is ready, and looks relatively good. We will now use this model on a target dataset and check for our equations. In this case
we will use the <span>Delhi.csv</span>
</p>
<pre>
<code>
delhi = pd.read_csv("Delhi.csv")
X_test = delhi[['Area']]
y_pred_test = pd.Series(model.predict(X_test))
y_pred_test.head()
</code>
</pre>
<p class="prediction_body">We now check for the intercept and coefficient</p>
<pre>
<code>
intercept = round(model.intercept_, 2)
print("Model intercept:", intercept)
coefficient = round(model.coef_[0], 2)
print('Model coefficient for "area":', coefficient)
</code>
</pre>
<p class="prediction_body body_padding">The Model coefficient for "area": 16015.84 and the Model intercept: -3383620.23
<br>
That will give us the equation for the linear regression as:
</p>
<p class="prediction_body prediction_question body_padding">
apartment_price = -3383620.23 + 16015.84 * area
</p>
<p class="prediction_body">Plotting the data for the city of Delhi along with the line will look something like this:</p>
<img src="photos/price_vs_area_5.png" alt="price_vs_area_5" class="prediction_body imageforarticle correl_1">
<p class="prediction_body body_padding">With the help of the Equation up above, we can also create a creative slider and use it to create Interactive app which can predict house prices
<br>
You can find the script for creating a Python App in the project Files!
</p>
<h2 class="ending">Thank you very much for going through the project!</h2>
<a href="https://github.com/happygoluckycodeeditor/pricepredictionmodel" class="priceprediction_repo">Check project files</a>
<!-- Footer of the page-->
<footer class="foot">
<img src="photos/logo2.png" class="logo2">
<p>©Tanmay Bagwe</p>
<p>Thank you for visiting</p>
<p>Any Questions? Contact me <a href="mailto:tanmay.bagwe.tb@gmail.com">here!</a></p>
</footer>
<!-- Adding AOS Script (Global settings)-->
<script src="https://unpkg.com/aos@next/dist/aos.js"></script>
<script>
AOS.init({
once: true,
});
</script>
<!-- Adding Highlight.js Script-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
</body>
</html>