-
Notifications
You must be signed in to change notification settings - Fork 0
/
Real Estate Price Prediction Model.py
378 lines (168 loc) · 5.27 KB
/
Real Estate Price Prediction Model.py
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
#!/usr/bin/env python
# coding: utf-8
# ## Real Estate - Price Prediction ML Model
# In[1]:
import pandas as pd
# In[2]:
housing= pd.read_csv("data.csv")
# In[3]:
housing.head()
# In[4]:
housing.info()
# In[5]:
housing['CHAS'].value_counts()
# In[6]:
housing.describe()
# In[7]:
#FOR PLOTTING HISTOGRAMS
# %matplotlib inline
# import matplotlib.pyplot as plt
# housing.hist(bins=50,figsize=(20,15))
# ## Training and Testing Splitting of Datasets
#
# In[8]:
import numpy as np
# In[9]:
#For Learning purpose
# def split_train_test(data,test_ratio):
# np.random.seed(42)
# shuffled=np.random.permutation(len(data))
# test_set_size=int(len(data)*test_ratio)
# test_indices=shuffled[:test_set_size]
# train_indices=shuffled[test_set_size:]
# return data.iloc[train_indices],data.iloc[test_indices]
# In[10]:
# train_set,test_set=split_train_test(housing,0.2)
# In[11]:
from sklearn.model_selection import train_test_split
train_set,test_set = train_test_split(housing,test_size=0.2,random_state=42)
print(f"Rows in training set {len(train_set)}\nRows in testing set {len(test_set)}")
# In[12]:
from sklearn.model_selection import StratifiedShuffleSplit
split= StratifiedShuffleSplit(n_splits=1,test_size=0.2,random_state=42)
for train_index,test_index in split.split(housing,housing['CHAS']):
strat_train_set=housing.loc[train_index]
strat_test_set=housing.loc[test_index]
# In[13]:
strat_train_set['CHAS'].value_counts()
# In[14]:
strat_test_set['CHAS'].value_counts()
# In[15]:
housing=strat_train_set.copy()
# ## Looking for correlations
# In[16]:
corr_matrix = housing.corr()
corr_matrix['MEDV'].sort_values(ascending=False)
# In[17]:
# from pandas.plotting import scatter_matrix
# attributes = ['MEDV','RM','ZN','LSTAT']
# scatter_matrix(housing[attributes],figsize=(12,8))
# In[18]:
# housing.plot(kind="scatter",x="RM",y="MEDV",alpha=0.8)
# In[19]:
# housing.plot(kind="scatter",x="LSTAT",y="MEDV",alpha=0.8)
# ## Trying out new attributes
# In[20]:
housing['TAXRM']=housing['TAX']/housing['RM']
# In[21]:
housing.head(2)
# In[22]:
corr_matrix = housing.corr()
corr_matrix['MEDV'].sort_values(ascending=False)
# In[23]:
# housing.plot(kind="scatter",x="TAXRM",y="MEDV",alpha=0.8)
# In[24]:
#Splitting the features and labels
housing=strat_train_set.drop("MEDV",axis=1)
housing_labels = strat_train_set["MEDV"].copy()
# ## Missing Attributes
# In[25]:
#We are placing the median of RM in the missing dataplaces
median = housing['RM'].median()
# In[26]:
housing['RM'].fillna(median)
# In[27]:
housing.shape
# In[28]:
from sklearn.impute import SimpleImputer
imputer= SimpleImputer(strategy="median")
imputer.fit(housing)
# In[29]:
imputer.statistics_
# In[30]:
#Now we create a transformed Dataframe that consists the imputed values
X= imputer.transform(housing)
housing_tr=pd.DataFrame(X,columns=housing.columns)
# In[31]:
housing_tr.describe()
# ## Creating Workflow Pipeline
# In[32]:
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
my_pipeline=Pipeline([('imputer',SimpleImputer(strategy="median")),
('std_Scaler',StandardScaler()),
])
#Can add many functions to this pipeline
# In[33]:
housing_num_tr=my_pipeline.fit_transform(housing_tr)
#This applies all the pipeline functions to the imputed np array
# In[34]:
housing_num_tr
#This is what we use as input for predictors
# ## Selecting a desired model for the problem
# In[35]:
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
# model = LinearRegression()
# model = DecisionTreeRegressor()
model = RandomForestRegressor()
model.fit(housing_num_tr,housing_labels)
# In[36]:
#Now we prepare some test data and test the accuracy of the model
some_data=housing.iloc[:5]
some_labels=housing_labels.iloc[:5]
# In[37]:
prepared_data=my_pipeline.transform(some_data)
# In[38]:
model.predict(prepared_data)
# In[39]:
list(some_labels)
# ## Evaluating the model
# In[40]:
from sklearn.metrics import mean_squared_error
housing_predictions=model.predict(housing_num_tr)
lin_mse=mean_squared_error(housing_labels,housing_predictions)
lin_rmse=np.sqrt(lin_mse)
# In[41]:
lin_mse
# ## Using better Evaluation techniques - Cross Validation
# In[42]:
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model,housing_num_tr,housing_labels,scoring="neg_mean_squared_error",cv=10)
rmse_scores=np.sqrt(-scores)
# In[43]:
rmse_scores
# In[44]:
#defining a function to print scores, mean and std
def print_scores(scores):
print("Scores:",scores)
print("Mean:",scores.mean())
print("Standard Deviation:",scores.std())
# In[45]:
print_scores(rmse_scores)
# ## Saving the Model
# In[46]:
from joblib import dump, load
dump(model,'RealEstatePricePredictor.joblib')
# ## Testing the model on test data
# In[47]:
X_test=strat_test_set.drop("MEDV",axis=1)
Y_test=strat_test_set["MEDV"].copy()
X_test_prepared=my_pipeline.transform(X_test)
final_predictions=model.predict(X_test_prepared)
final_mse=mean_squared_error(Y_test,final_predictions)
final_rmse=np.sqrt(final_mse)
# In[48]:
final_rmse
# In[ ]: