-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSklearn Machine-Learning_AirBnB.py
More file actions
637 lines (513 loc) · 23.6 KB
/
Sklearn Machine-Learning_AirBnB.py
File metadata and controls
637 lines (513 loc) · 23.6 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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 27 15:02:33 2020
@author: Andreas Traut
@date: 07.04.2021
#%% #######################################################################
# 1. Initialize and Read the CSV File
# S. Split Training Data and Test Data
# S.1 Alternative 1: generate id with static data
# S.2 Alternative 2: generate stratified sampling
# S.3 verify if stratified sample is good
# 2. Discover and Visualize the Data to Gain Insights
# 3. Clean NULL-Values and Prepare for Machine Learning
# 3.1 find all NULL-values
# 3.2 remove all NULL-values
# 4. Model-Specific Preprocessing
# 4.1 Use "Imputer" to clean NaNs
# 4.2 Treat "Categorial" Inputs
# 5. Pipelines and Custom Transformer
# 5.1 Custom Transformer
# 5.2 Pipelines
# 6. Select and Train Model
# 6.1 LinearRegression model
# 6.2 DecisionTreeRegressor model
# 7. Crossvalidation
# 7.1 for DecisionTreeRegressor
# 7.2 for LinearRegression
# 7.3 for RandomForestRegressor
# 7.4 for ExtraTreesRegressor
# 8. Save Model
# 9. Optimize Model
# 9.1 GridSearchCV
# 9.1.1 GridSearchCV on RandomForestRegressor
# 9.1.2 GridSearchCV on LinearRegressor
# 9.2 Randomized Search
# 9.3 Analyze Best Models
# 10. Evaluate Final Model on Test Dataset
#%% #######################################################################
"""
# Common imports
from __future__ import division, print_function, unicode_literals
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import os
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import FunctionTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.compose import ColumnTransformer
import hashlib
from sklearn.model_selection import StratifiedShuffleSplit
from pandas.plotting import scatter_matrix
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestRegressor
import joblib
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint
from scipy import stats
#%% #######################################################################
#
# =============================================================================
# # 1. initialize and read the file
# =============================================================================
#%%
# Where to save the figures
PROJECT_ROOT_DIR = "."
myDataset_NAME = "AirBnB"
IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, "media")
myDataset_PATH = os.path.join("datasets", "AirBnB")
def save_fig(fig_id, prefix=myDataset_NAME, tight_layout=True, fig_extension="png", resolution=300):
path = os.path.join(IMAGES_PATH, prefix + "_" + fig_id + "." + fig_extension)
print("Saving figure", prefix + "_" + fig_id)
if tight_layout:
plt.tight_layout()
plt.savefig(path, format=fig_extension, dpi=resolution)
# Ignore useless warnings (see SciPy issue #5998)
import warnings
warnings.filterwarnings(action="ignore", message="^internal gelsd")
np.random.seed(42)
mpl.rc('axes', labelsize=14)
mpl.rc('xtick', labelsize=12)
mpl.rc('ytick', labelsize=12)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
#%% read the csv-file
def load_myDataset_data(myDataset_path=myDataset_PATH):
csv_path = os.path.join(myDataset_path, "listings.csv")
return pd.read_csv(csv_path)
myDataset = load_myDataset_data()
print(myDataset.head())
#%% remove unwanted columns
myDataset = myDataset.drop("id", axis=1)
#%% #######################################################################
#
# =============================================================================
# # 1. Split Training Data and Test Data
# =============================================================================
print("\n\n1. create index\n")
print(myDataset.info())
myDataset_with_id = myDataset.reset_index()
print(myDataset_with_id.head())
def test_set_check(identifier, test_ratio, hash=hashlib.md5):
return bytearray(hash(np.int64(identifier)).digest())[-1] < 256 * test_ratio
def split_train_test_by_id(data, test_ratio, id_column):
ids = data[id_column]
in_test_set = ids.apply(lambda id_: test_set_check(id_, test_ratio))
return data.loc[~in_test_set], data.loc[in_test_set]
#%%
# S.1 Alternative 1: generate id with static data
print("\n1.1 Alternative 1: generate id with static data\n")
myDataset_with_id["index"] = round(myDataset["longitude"],2) * 10000 + myDataset["latitude"]
train_set, test_set = split_train_test_by_id(myDataset_with_id, 0.2, "index")
print(myDataset_with_id.head())
print("train set: {0:7d}\ntest set : {1:7d}".format(len(train_set),len(test_set)))
#%%
# S.2 Alternative 2: generate stratified sampling
# Requirement: from sklearn.model_selection import StratifiedShuffleSplit
print("\n1.2 Alternative 2: generate stratified sampling\n")
myDataset["price"].hist()
myDataset["price_cat"] = pd.cut(myDataset["price"],
bins=[-1, 50, 100, 200, 400, np.inf],
labels=[50, 100, 200, 400, 500])
print("\nvalue_counts\n", myDataset["price_cat"].value_counts())
myDataset["price_cat"].hist()
split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)
for train_index, test_index in split.split(myDataset, myDataset["price_cat"]):
strat_train_set = myDataset.loc[train_index]
strat_test_set = myDataset.loc[test_index]
print("\nstrat_test_set\n", strat_test_set["price_cat"].value_counts() / len(strat_test_set))
print("\nmyDataset\n", myDataset["price_cat"].value_counts() / len(myDataset))
#%%
# S.3 verify if stratified example is good
print("\n1.3 verify if stratified sample is good \n")
def price_cat_proportions(data):
return data["price_cat"].value_counts() / len(data)
train_set, test_set = train_test_split(myDataset, test_size=0.2, random_state=42)
compare_props = pd.DataFrame({
"Overall": price_cat_proportions(myDataset),
"Stratified": price_cat_proportions(strat_test_set),
"Random": price_cat_proportions(test_set),
}).sort_index()
compare_props["Rand. %error"] = 100 * compare_props["Random"] / compare_props["Overall"] - 100
compare_props["Strat. %error"] = 100 * compare_props["Stratified"] / compare_props["Overall"] - 100
print(compare_props)
for set_ in (strat_train_set, strat_test_set):
set_.drop("price_cat", axis=1, inplace=True)
#%% #######################################################################
#
# =============================================================================
# # 2. Discover and Visualize the Data to Gain Insights
# =============================================================================
# Requirement: from pandas.plotting import scatter_matrix
print("\n\n2. Discover and visualize the data to gain insights \n")
# myDataset = strat_train_set.copy()
myDataset.plot(kind="scatter", x="longitude", y="latitude",
title="bad_visualization_plot")
save_fig("bad_visualization_plot")
attributes = ["price", "number_of_reviews", "host_id", "availability_365",
"reviews_per_month", "minimum_nights"]
scatter_matrix(myDataset[attributes], figsize=(12, 8))
plt.suptitle("scatter_matrix_plot")
save_fig("scatter_matrix_plot")
#%%
# myDataset = myDataset[(myDataset['longitude']>=13.32) & (myDataset['longitude']<=13.35)]
myDataset.plot(kind="scatter", x="longitude", y="latitude", alpha=0.4,
s=myDataset['price']/100, label="price", figsize=(10,7),
c="price", cmap=plt.get_cmap("jet"), colorbar=True,
sharex=False, title="prices_scatterplot")
plt.legend()
save_fig("prices_scatterplot")
#%%
corr_matrix = myDataset.corr()
print("correlation:\n", corr_matrix["price"].sort_values(ascending=False))
#%% #######################################################################
#
# =============================================================================
# # 3. Clean NULL-Values and Prepare for Machine Learning
# =============================================================================
print("\n\n3. prepare for Machine Learning\n")
myDataset = strat_train_set.drop("price", axis=1) # drop labels for training set
myDataset_labels = strat_train_set["price"].copy()
# 3.1 find all NULL-values
print("\n3.1 find all NULL-values\n")
print("\nHow many Non-NULLrows are there?\n")
print(myDataset.info())
print("\nAre there NULL values in the columns?\n", myDataset.isnull().any())
print("\nAre there NULLs in column reviews_per_month?\n", myDataset["reviews_per_month"].isnull().any())
print("\nShow some rows with NULL (head only):\n", myDataset[myDataset["reviews_per_month"].isnull()].head())
#%%
# 3.2 remove all NULL-values
print("\n3.2 remove all NULL-values \n")
sample_incomplete_rows = myDataset[myDataset.isnull().any(axis=1)]
# option 1: remove rows which contains NaNs
# sample_incomplete_rows.dropna(subset=["total_bedrooms"])
# option 2 : remove columns with contain NaNs
# sample_incomplete_rows.drop("total_bedrooms", axis=1)
# option 3 : replace NaN by median
median = myDataset["reviews_per_month"].median()
sample_incomplete_rows["reviews_per_month"].fillna(median, inplace=True)
print("sample_incomplete_rows\n", sample_incomplete_rows['reviews_per_month'].head())
#%% #######################################################################
#
# =============================================================================
# # 4. Model-Specific Preprocessing
# =============================================================================
# 4.1 Use "Imputer" to Clean NaN
# Requirement: from sklearn.impute import SimpleImputer
print("\n\n4.1. Use Imputer to Clean NaN\n")
imputer = SimpleImputer(strategy="median")
myDataset_num = myDataset.select_dtypes(include=[np.number]) #or: myDataset_num = myDataset.drop('ocean_proximity', axis=1)
imputer.fit(myDataset_num)
print("\nimputer.strategy\n", imputer.strategy)
print("\nimputer.statistics_\n", imputer.statistics_)
print("\nmyDataset_num.median\n", myDataset_num.median().values) # Check that this is the same as manually computing the median of each attribute:
print("\nmyDataset_num.mean\n", myDataset_num.mean().values) # Check that this is the same as manually computing the median of each attribute:
X = imputer.transform(myDataset_num) # Transform the training set:
myDataset_tr = pd.DataFrame(X, columns=myDataset_num.columns,
index=myDataset.index)
myDataset_tr.loc[sample_incomplete_rows.index.values]
#%%
# 4.2 Treat "Categorial" Inputs
# Requirement: from sklearn.preprocessing import OneHotEncoder
print("\n\n4.1. Treat Categorial Inputs\n")
myDataset_cat = myDataset[['room_type']]
print("myDataset_cat.head\n", myDataset_cat.head(10), "\n")
cat_encoder = OneHotEncoder()
myDataset_cat_1hot = cat_encoder.fit_transform(myDataset_cat)
print("\ncat_encoder.categories_:\n", cat_encoder.categories_)
print("\nmyDataset_cat_1hot.toarray():\n", myDataset_cat_1hot.toarray())
print("\nmyDataset_cat_1hot:\n", myDataset_cat_1hot)
#%% #######################################################################
#
# =============================================================================
# # 5. Pipelines and Custom Transformer
# =============================================================================
print("\n\n 5. custom transformer and pipelines \n")
# 5.1 custom transformer
# Requirement: from sklearn.preprocessing import FunctionTransformer
print("5.1 custom transformer\n")
print("myDataset.columns\n", myDataset_num.columns)
number_of_reviews_ix, availability_365_ix, calculated_host_listings_count_ix, reviews_per_month_ix = [
list(myDataset_num.columns).index(col)
for col in ("number_of_reviews", "availability_365", "calculated_host_listings_count", "reviews_per_month")]
def add_extra_features(X):
number_reviews_dot_revievs_per_month = X[:, number_of_reviews_ix] * X[:, reviews_per_month_ix]
return np.c_[X, number_reviews_dot_revievs_per_month]
attr_adder = FunctionTransformer(add_extra_features, validate=False)
myDataset_extra_attribs = attr_adder.fit_transform(myDataset_num.values)
myDataset_extra_attribs = pd.DataFrame(
myDataset_extra_attribs,
columns=list(myDataset_num.columns)+["number_reviews_dot_revievs_per_month"],
index=myDataset_num.index)
print("myDataset_extra_attribs.head()\n", myDataset_extra_attribs.head())
#%%
# 5.2 Pipelines
# Requirements: from sklearn.pipeline import Pipeline
# from sklearn.preprocessing import StandardScaler
# from sklearn.compose import ColumnTransformer
print("5.2 Pipelines \n")
num_pipeline = Pipeline([
('imputer', SimpleImputer(strategy="median")),
('attribs_adder', FunctionTransformer(add_extra_features,
validate=False)),
('std_scaler', StandardScaler())
])
myDataset_num_tr = num_pipeline.fit_transform(myDataset_num)
print("myDataset_num_tr\n", myDataset_num_tr)
num_attribs = list(myDataset_num)
cat_attribs = ["room_type"]
full_pipeline = ColumnTransformer([
("num", num_pipeline, num_attribs),
("cat", OneHotEncoder(), cat_attribs),
])
myDataset_prepared = full_pipeline.fit_transform(myDataset)
print("myDataset_prepared\n", myDataset_prepared)
#%% #######################################################################
#
# =============================================================================
# # 6. Select and Train Model
# =============================================================================
print("\n\n6. select and train model\n")
# 6.1 LinearRegression model
# Requirement: from sklearn.linear_model import LinearRegression
# from sklearn.metrics import mean_squared_error
print("6.1 LinearRegression model\n")
lin_reg = LinearRegression()
lin_reg.fit(myDataset_prepared, myDataset_labels)
some_data = myDataset.iloc[:10]
some_labels = myDataset_labels.iloc[:10]
some_data_prepared = full_pipeline.transform(some_data)
print("Predictions:\n", lin_reg.predict(some_data_prepared))
print("Labels:\n", list(some_labels)) # Compare against the actual values:
myDataset_predictions = lin_reg.predict(myDataset_prepared)
lin_mse = mean_squared_error(myDataset_labels, myDataset_predictions)
lin_rmse = np.sqrt(lin_mse)
print("lin_rmse\n", lin_rmse)
print("mean of labels:\n", myDataset_labels.mean())
print("std deviation of labels:\n", myDataset_labels.std())
myDataset_labels.hist()
#%%
# 6.2 DecisionTreeRegressor Model
# Requirement: from sklearn.tree import DecisionTreeRegressor
print("6.2 DecisionTreeRegressor Model\n")
tree_reg = DecisionTreeRegressor(random_state=42)
tree_reg.fit(myDataset_prepared, myDataset_labels)
myDataset_predictions = tree_reg.predict(myDataset_prepared)
tree_mse = mean_squared_error(myDataset_labels, myDataset_predictions)
tree_rmse = np.sqrt(tree_mse)
print("tree_rmse\n", tree_rmse)
#%% #######################################################################
#
# =============================================================================
# # 7. Crossvalidation
# =============================================================================
print("\n\n8. crossvalidation \n")
# 7.1 for DecisionTreeRegressor
# Requirement: from sklearn.model_selection import cross_val_score
print("7.1 for DecisionTreeRegressor\n")
scores = cross_val_score(tree_reg, myDataset_prepared,
myDataset_labels,
scoring="neg_mean_squared_error",
cv=10)
tree_rmse_scores = np.sqrt(-scores)
def display_scores(scores):
print("Scores:", scores)
print("Mean:", scores.mean())
print("Standard deviation:", scores.std())
display_scores(tree_rmse_scores)
#%%
# 7.2 for LinearRegression
print("7.2 for LinearRegression\n")
lin_scores = cross_val_score(lin_reg, myDataset_prepared,
myDataset_labels,
scoring="neg_mean_squared_error",
cv=10)
lin_rmse_scores = np.sqrt(-lin_scores)
display_scores(lin_rmse_scores)
#%%
# 7.3 for RandomForestRegressor
# Requirements: from sklearn.ensemble import RandomForestRegressor
# from sklearn.model_selection import cross_val_score
print("7.3 for RandomForestRegressor\n")
forest_reg = RandomForestRegressor(n_estimators=10, random_state=42)
forest_reg.fit(myDataset_prepared, myDataset_labels)
myDataset_predictions = forest_reg.predict(myDataset_prepared)
forest_mse = mean_squared_error(myDataset_labels, myDataset_predictions)
forest_rmse = np.sqrt(forest_mse)
print("forest_rmse\n", forest_rmse)
forest_scores = cross_val_score(forest_reg, myDataset_prepared,
myDataset_labels,
scoring="neg_mean_squared_error",
cv=10)
forest_rmse_scores = np.sqrt(-forest_scores)
display_scores(forest_rmse_scores)
#%%
# 7.4 for ExtraTreesRegressor
print("7.4 for ExtraTreesRegressor\n")
from sklearn.ensemble import ExtraTreesRegressor
extratree_reg = ExtraTreesRegressor(n_estimators=10,
random_state=42)
extratree_reg.fit(myDataset_prepared, myDataset_labels)
myDataset_predictions = extratree_reg.predict(myDataset_prepared)
extratree_mse = mean_squared_error(myDataset_labels, myDataset_predictions)
extratree_rmse = np.sqrt(extratree_mse)
print("extratree_rmse\n", extratree_rmse)
extratree_scores = cross_val_score(extratree_reg,
myDataset_prepared,
myDataset_labels,
scoring = "neg_mean_squared_error",
cv=10)
extratree_rmse_scores = np.sqrt(-extratree_scores)
display_scores(extratree_rmse_scores)
#%% #######################################################################
#
# =============================================================================
# # 8. Save Model
# =============================================================================
# Requirement: import joblib
print("\n\n8. Save Model\n")
joblib.dump(forest_reg, "forest_reg.pkl")
# und später...
my_model_loaded = joblib.load("forest_reg.pkl")
#%% #######################################################################
#
# =============================================================================
# # 9. Optimize Model
# =============================================================================
print("\n\n9. Optimize Model\n")
# 9.1 GridSearchCV
print("\n9.1 GridSearchCV on RandomForestRegressor\n")
# 9.1.1 GridSearchCV on RandomForestRegressor
# Requirement: from sklearn.model_selection import GridSearchCV
print("\n9.1.1 GridSearchCV on RandomForestRegressor\n")
param_grid = [
{'n_estimators': [30, 40, 50], 'max_features': [2, 4, 6, 8, 10]},
{'bootstrap': [False], 'n_estimators': [3, 10], 'max_features': [2, 3, 4]}
]
forest_reg = RandomForestRegressor(random_state=42)
grid_search = GridSearchCV(forest_reg, param_grid, cv=5,
scoring='neg_mean_squared_error',
return_train_score=True)
grid_search.fit(myDataset_prepared, myDataset_labels)
print("Best Params: ", grid_search.best_params_)
print("Best Estimator: ", grid_search.best_estimator_)
print("\nResults (mean_test_score and params):")
cvres = grid_search.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
print(np.sqrt(-mean_score), params)
print("\nResults (complete):")
a = pd.DataFrame(grid_search.cv_results_)
print(a)
#%%
# 9.1.2 GridSearchCV on LinearRegressor
# Requirement: from sklearn.model_selection import GridSearchCV
print("\n9.1.1 GridSearchCV on LinearRegressor\n")
param_grid = [
{'fit_intercept': [True], 'n_jobs': [2, 4, 6, 8, 10]},
{'normalize': [False], 'n_jobs': [3, 10]},
]
lin_reg = LinearRegression()
# train across 5 folds, that's a total of (12+6)*5=90 rounds of training
lin_grid_search = GridSearchCV(lin_reg, param_grid, cv=5,
scoring='neg_mean_squared_error',
return_train_score=True)
lin_grid_search.fit(myDataset_prepared, myDataset_labels)
print("Best Params: ", lin_grid_search.best_params_)
print("Best Estimator: ", lin_grid_search.best_estimator_)
print("\nResults (mean_test_score and params):")
cvres = lin_grid_search.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
print(np.sqrt(-mean_score), params)
print("\nResults (complete):")
a = pd.DataFrame(lin_grid_search.cv_results_)
print(a)
#%%
# 9.2 Randomized Search
# Requirements: from sklearn.model_selection import RandomizedSearchCV
# from scipy.stats import randint
print("\n9.2 Randomized Search\n")
param_distribs = {
'n_estimators': randint(low=1, high=200),
'max_features': randint(low=1, high=8),
}
forest_reg = RandomForestRegressor(random_state=42)
rnd_search = RandomizedSearchCV(forest_reg,
param_distributions=param_distribs,
n_iter=10, cv=5,
scoring='neg_mean_squared_error',
random_state=42)
rnd_search.fit(myDataset_prepared, myDataset_labels)
print("Best Params: ", rnd_search.best_params_)
print("Best Estimator: ", rnd_search.best_estimator_)
print("\nResults (mean_test_score and params):")
cvres = rnd_search.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
print(np.sqrt(-mean_score), params)
print("\nResults (complete):")
a = pd.DataFrame(rnd_search.cv_results_)
print(a)
#%%
# 9.3 Analyze best models
print("\n9.3 Analyze best models\n")
feature_importances = grid_search.best_estimator_.feature_importances_
print("feature_importances:\n", feature_importances)
extra_attribs = ["number_reviews_dot_revievs_per_month"]
cat_encoder = full_pipeline.named_transformers_["cat"]
cat_one_hot_attribs = list(cat_encoder.categories_[0])
attributes = num_attribs + extra_attribs + cat_one_hot_attribs
print("\nattributes:\n", attributes)
my_list = sorted(zip(feature_importances, attributes), reverse=True)
print("\nMost important features (think about removing features):")
print("\n".join('{}' for _ in range(len(my_list))).format(*my_list))
#%% #######################################################################
#
# =============================================================================
# # 10. Evaluate final model on test dataset
# =============================================================================
# Requirment: from scipy import stats
print("\n\n 10. Evaluate final model on test dataset\n")
final_model = grid_search.best_estimator_
print("final_model:\n", final_model)
X_test = strat_test_set.drop("price", axis=1)
y_test = strat_test_set["price"].copy()
X_test_prepared = full_pipeline.transform(X_test)
final_predictions = final_model.predict(X_test_prepared)
final_mse = mean_squared_error(y_test, final_predictions)
final_rmse = np.sqrt(final_mse)
print ("final_predictions:\n", final_predictions )
print ("final_rmse:\n", final_rmse )
confidence = 0.95
squared_errors = (final_predictions - y_test) ** 2
mean = squared_errors.mean()
m = len(squared_errors)
print("95% confidence interval: ",
np.sqrt(stats.t.interval(confidence, m - 1,
loc=np.mean(squared_errors),
scale=stats.sem(squared_errors)))
)
side_by_side = [(true, pred, (true-pred)/true)
for true, pred in
zip(list(y_test),
list(final_predictions))]
print(side_by_side)
test_set.insert(loc=1, column="final_prediction", value = final_predictions)
print(test_set)