-
Notifications
You must be signed in to change notification settings - Fork 4
/
house-rental-prediction-machine-learning.py
273 lines (190 loc) · 7.91 KB
/
house-rental-prediction-machine-learning.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
# -*- coding: utf-8 -*-
"""Revisi 4.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1imi1VNZhzO_HcnZksgavC_6r8zSqeKpX
Nama : Irbah Labibah Nur Saidah
Dataset ini diambil dari : https://www.kaggle.com/datasets/iamsouravbanerjee/house-rent-prediction-dataset
# Data Collection
"""
# Commented out IPython magic to ensure Python compatibility.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# %matplotlib inline
!gdown --id "11vCKuk81QvTQcwibMX7Wo6k4q0HDzLOz"
df = pd.read_csv("/content/House_Rent_Dataset.csv")
df.head()
"""# Data Understanding & Removing Outlier"""
df.shape
df.info()
# Fitur Point of Contract dan Posted On tidak mempengaruhi harga sewa model sehingga akan didrop
df = df.drop(['Posted On', 'Point of Contact'], axis = 'columns')
"""## Univariate Analysis"""
df.groupby('Area Type')['Area Type'].agg('count')
# Fitur Area Type hanya terdapat 2 sample Built Area sehingga 2 sample tersebut akan dihapus
df.drop(df.index[df['Area Type'] == 'Built Area'], inplace = True)
df.groupby('Area Type')['Area Type'].agg('count')
df.groupby('City')['City'].agg('count')
df.groupby('Furnishing Status')['Furnishing Status'].agg('count')
df.groupby('Tenant Preferred')['Tenant Preferred'].agg('count')
df.groupby('Floor')['Floor'].agg('count')
df.groupby('Area Locality')['Area Locality'].agg('count')
# Fitur Floor dan Area Locality memiliki banyak sekali nilai unique sehingga akan di drop
df = df.drop(['Floor', 'Area Locality'], axis = 'columns')
df.head()
df.hist(bins=50, figsize=(10,10))
plt.ticklabel_format(useOffset=False, style='plain')
plt.show()
df.Rent.describe().apply(lambda x: format(x, 'f'))
"""## Multivariate Analysis"""
# Menambahkan fitur baru price per sqft
df['Price_per_sqft'] = df['Rent']*1000/df['Size']
df.head()
# Mendeteksi size per BHK outlier
# 100 sqft untuk 1 BHK itu tidak biasa sehingga anggap saja batasan tresholdnya 300 sqft/bhk
df[(df.Size/df.BHK) < 300].head()
df.shape
# Menghapus size per BHK outlier
df1 = df[~(df.Size/df.BHK < 300)]
df1.head()
df1.shape
# Mendeteksi price per sqft outlier
df1.Price_per_sqft.describe().apply(lambda x: format(x, 'f'))
"""Harga 571 per sqft sangat rendah dan harga 1400000 per sqft sangat tinggi"""
# Menghapus price per sqft outlier dengan mean dan one standard deviation
def remove_pps_outliers(df):
df_out = pd.DataFrame()
for key, subdf in df.groupby('City'):
m = np.mean(subdf.Price_per_sqft)
st = np.std(subdf.Price_per_sqft)
reduced_df = subdf[(subdf.Price_per_sqft>(m-st)) & (subdf.Price_per_sqft<=(m+st))]
df_out = pd.concat([df_out,reduced_df],ignore_index=True)
return df_out
df2 = remove_pps_outliers(df1)
df2.shape
# Mendeteksi bathroom outlier
# 2 BHK dengan 4 kamar mandi itu tidak biasa jadi anggap saja batasnya kamar mandi tidak boleh melebihi jumlah BHK + 2
df2[df2.Bathroom > df2.BHK + 2]
# Menghapus bathroom outlier
df2 = df2[~(df2.Bathroom > df2.BHK + 2)]
df2.head()
df2.shape
# Menghilangkan fitur price per sqft karena sudah tidak terpakai
df3 = df2.drop(['Price_per_sqft'], axis = 'columns')
# Melihat kolerasi antara fitur numerik dengan fitur target (harga)
plt.figure(figsize=(10, 8))
correlation_matrix = df3.corr().round(2)
# Untuk menge-print nilai di dalam kotak, gunakan parameter anot=True
sns.heatmap(data=correlation_matrix, annot=True, cmap='coolwarm', linewidths=0.5, )
plt.title("Correlation Matrix untuk Fitur Numerik ", size=20)
# Melihat kolerasi antara fitur kategorik dengan fitur target (harga)
cat_features = df2.select_dtypes(include='object').columns.to_list()
for col in cat_features:
sns.catplot(x=col, y="Rent", kind="bar", dodge=False, height = 4, aspect = 3, data=df2, palette="Set3")
plt.title("Rata-rata 'Rent' Relatif terhadap - {}".format(col))
"""# Data Preparation
## One hot encoding
"""
df3 = pd.get_dummies(data = df3, columns = ['Area Type'])
df3 = pd.get_dummies(data = df3, columns = ['City'])
df3 = pd.get_dummies(data = df3, columns = ['Furnishing Status'])
df3 = pd.get_dummies(data = df3, columns = ['Tenant Preferred'])
df3.head()
"""## Train Test Split"""
from sklearn.model_selection import train_test_split
X = df3.drop(["Rent"],axis =1)
y = df3["Rent"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.05, random_state=123)
print(f'Total # of sample in whole dataset: {len(X)}')
print(f'Total # of sample in train dataset: {len(X_train)}')
print(f'Total # of sample in test dataset: {len(X_test)}')
"""## Normalization"""
from sklearn.preprocessing import StandardScaler
# Normalisasi data train
numerical_features = ['BHK', 'Size', 'Bathroom']
scaler = StandardScaler()
scaler.fit(X_train[numerical_features])
X_train[numerical_features] = scaler.transform(X_train.loc[:, numerical_features])
X_train[numerical_features].head()
# Normalisasi data test
X_test.loc[:, numerical_features] = scaler.transform(X_test[numerical_features])
"""# Modeling
## Grid Search
"""
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import ShuffleSplit
def grid_search_model(X,y):
algos = {
'knn': {
'model': KNeighborsRegressor(),
'params': {
'n_neighbors': [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
}
},
'boosting': {
'model': AdaBoostRegressor(),
'params': {
'learning_rate' : [0.1, 0.05, 0.01, 0.05, 0.001],
'n_estimators': [25, 50, 75, 100],
'random_state': [11, 33, 55, 77]
}
},
'random_forest': {
'model': RandomForestRegressor(),
'params': {
'n_estimators': [25, 50, 75, 100],
'max_depth' : [8, 16, 32, 64],
'random_state': [11, 33, 55, 77],
}
}
}
scores = []
cv = ShuffleSplit(n_splits=5, test_size=0.05, random_state=123)
for algo_name, config in algos.items():
gs = GridSearchCV(config['model'], config['params'], cv=cv, return_train_score=False)
gs.fit(X,y)
scores.append({
'model': algo_name,
'best_score': gs.best_score_,
'best_params': gs.best_params_
})
return pd.DataFrame(scores,columns=['model','best_score','best_params'])
grid_search_model(X,y)
"""## Model with best parameter"""
acc = pd.DataFrame(index=['accuracy'])
from sklearn.metrics import mean_squared_error
knn = KNeighborsRegressor(n_neighbors = 7)
knn.fit(X_train, y_train)
acc.loc['accuracy', 'knn'] = knn.score(X_test,y_test)
knn.score(X_test,y_test)
rf = RandomForestRegressor(n_estimators = 50, max_depth = 8, random_state = 11)
rf.fit(X_train, y_train)
acc.loc['accuracy', 'rf'] = rf.score(X_test,y_test)
rf.score(X_test,y_test)
boosting = AdaBoostRegressor(n_estimators = 25, learning_rate = 0.001, random_state = 11)
boosting.fit(X_train, y_train)
acc.loc['accuracy', 'boosting'] = boosting.score(X_test,y_test)
boosting.score(X_test,y_test)
"""# Evaluation"""
# Akurasi dari model
acc
# Mean squared error dari model
mse = pd.DataFrame(columns=['train', 'test'], index=['KNN','RF','Boosting'])
model_dict = {'KNN': knn, 'RF': rf, 'Boosting': boosting}
for name, model in model_dict.items():
mse.loc[name, 'train'] = mean_squared_error(y_true=y_train, y_pred=model.predict(X_train))/1e3
mse.loc[name, 'test'] = mean_squared_error(y_true=y_test, y_pred=model.predict(X_test))/1e3
mse
fig, ax = plt.subplots()
mse.sort_values(by='test', ascending=False).plot(kind='barh', ax=ax, zorder=3)
ax.grid(zorder=0)
prediksi = X_test.iloc[5:10].copy()
pred_dict = {'y_true':y_test[5:10]}
for name, model in model_dict.items():
pred_dict['prediksi_'+name] = model.predict(prediksi).round(1)
pd.DataFrame(pred_dict)