-
Notifications
You must be signed in to change notification settings - Fork 1
/
model-03-build.py
317 lines (259 loc) · 13.6 KB
/
model-03-build.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
import time
import pickle
import itertools
import numpy as np
import pandas as pd
import lightgbm as lgb
import matplotlib.pyplot as plt
from sklearn.model_selection import KFold
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
from utilities import cal_mape
from utilities import cal_score, convert_types, create_dt_feats
# PARS #
is_log_target = True
is_per_area_target = True
map_target = {
(True, True): 'log_per_area_total_price',
(True, False): 'log_total_price',
(False, True): 'per_area_total_price',
(False, False): 'total_price'
}
col_target = map_target[(is_log_target, is_per_area_target)]
cols_num_add = ['building_per_land_area']
cols_cat_add = ['encoded_latlon']
cols_feats_rm = [] # TODO: implement before using it
#['day_in_year_txn_dt', 'day_in_year_building_complete_dt', 'dt_diff_building_complete_dt_txn_dt'] # dt num feats
#['dow_txn_dt', 'dow_building_complete_dt', 'month_txn_dt', 'month_building_complete_dt',
# 'quarter_txn_dt', 'quarter_building_complete_dt', 'year_txn_dt', 'year_building_complete_dt'] # dt cat feats
# Analysis
def info_value_woe(feature, target):
''' calculate information value and weight of evidence
read: https://www.slideshare.net/0xdata/feature-engineering-83511751
notice that it's implemented as non-event/event; if opposite, woe changes sign
'''
nT = np.sum(target)
nF = np.sum(~target)
df = pd.concat([feature,target],axis=1)
df.columns = ['feature','target']
group = df.groupby('feature')['target']
cal_woe = lambda y: np.log( ((np.sum(~y)+0.5)/nF) / ((np.sum(y)+0.5)/nT) )
woe = group.agg(cal_woe).rename(columns={'target':'woe'})
cal_diff = lambda y: np.sum(~y)/nF - np.sum(y)/nT
diff_pct = group.agg(cal_diff).rename(columns={'target':'diff'})
info_value = np.sum(woe * diff_pct)
return woe, info_value
# Processing
def pre_processing(df, cols_num, cols_cat, cols_feats):
# Convert types
df[cols_num] = df[cols_num].astype('float32')
df['total_price'] = df['total_price'].astype('float32')
# generate target
if 'total_price' in df:
df['log_total_price'] = np.log1p(df['total_price'])
df['per_area_total_price'] = df['total_price'] / df['building_area']
df['log_per_area_total_price'] = np.log1p(df['per_area_total_price'])
df['target'] = df[col_target]
# generate lat/lon encoded
kmeans = pickle.load(open('./object_kmeans_k6_combined.pkl', 'rb'))
df['encoded_latlon'] = kmeans.predict(df[['lat','lon']])
# generate dt feats
df = create_dt_feats(df, 'txn_dt')
df = create_dt_feats(df, 'building_complete_dt')
df['dt_diff_building_complete_dt_txn_dt'] = df['txn_dt'] - df['building_complete_dt']
# generate building to land ratio
df['building_per_land_area'] = df['building_area'] / df['land_area']
return df, cols_feats
from feature_engineering import CategoricalColumnsEncoder
from feature_engineering import TargetMeanEncoding
from feature_engineering import MultiLabelEncoding
class FeatureEngineering():
def __init__(self, cols_num, cols_cat, cols_feats, col_target, n_ran_encode=4):
# cols
self.cols_num = cols_num
self.cols_cat = cols_cat
self.cols_feats = cols_feats
self.col_target = col_target
# Numbers
self.n_ran_encode = n_ran_encode
# encoders
#self.tar_encoder = TargetMeanEncoding(col_target = self.col_target)
#self.ran_encoder = MultiLabelEncoding(self.n_ran_encode)
self.cat_astyper = CategoricalColumnsEncoder(mode='pandas')
def fit_transform(self, df):
df = df.copy()
# encoded1 = self.tar_encoder.fit_transform(df[self.cols_cat], df[self.col_target])
# encoded2 = self.ran_encoder.fit_transform(df[self.cols_cat])
self.cat_astyper.fit_transform(df, self.cols_cat)
return df
# return pd.concat([df, encoded1, encoded2], axis=1)
def transform(self, df):
df = df.copy()
# encoded1 = self.tar_encoder.transform(df[self.cols_cat])
# encoded2 = self.ran_encoder.transform(df[self.cols_cat]) # TODO: add generated feats into cols_feats
self.cat_astyper.transform(df)
return df
# return pd.concat([df, encoded1, encoded2], axis=1)
def post_processing(y_pred, df):
y_pred_final = pd.Series(y_pred.copy())
if is_log_target:
y_pred_final = np.expm1(y_pred_final)
y_pred_final = np.clip(y_pred_final, 0, None)
if is_per_area_target:
y_pred_final = y_pred_final * df['building_area'].reset_index(drop=True)
return y_pred_final.values
# Columns
cols_num = ['txn_dt', 'total_floor', 'building_complete_dt', 'parking_area', 'parking_price', 'txn_floor',
'land_area', 'building_area', 'lat', 'lon', 'village_income_median', 'town_population', 'town_area',
'town_population_density', 'doc_rate', 'master_rate', 'bachelor_rate', 'jobschool_rate',
'highschool_rate', 'junior_rate', 'elementary_rate', 'born_rate', 'death_rate', 'marriage_rate',
'divorce_rate', 'N_50', 'N_500', 'N_1000', 'N_5000', 'N_10000', 'I_10', 'I_50', 'I_100', 'I_250',
'I_500', 'I_1000', 'I_5000', 'I_10000', 'I_MIN', 'II_10', 'II_50', 'II_100', 'II_250', 'II_500',
'II_1000', 'II_5000', 'II_10000', 'II_MIN', 'III_10', 'III_50', 'III_100', 'III_250', 'III_500',
'III_1000', 'III_5000', 'III_10000', 'III_MIN', 'IV_10', 'IV_50', 'IV_100', 'IV_250', 'IV_500',
'IV_1000', 'IV_5000', 'IV_10000', 'IV_MIN', 'V_10', 'V_50', 'V_100', 'V_250', 'V_500', 'V_1000',
'V_5000', 'V_10000', 'V_MIN', 'VI_50', 'VI_100', 'VI_250', 'VI_500', 'VI_1000', 'VI_5000', 'VI_10000',
'VI_MIN', 'VII_10', 'VII_50', 'VII_100', 'VII_250', 'VII_500', 'VII_1000', 'VII_5000', 'VII_10000',
'VII_MIN', 'VIII_10', 'VIII_50', 'VIII_100', 'VIII_250', 'VIII_500', 'VIII_1000', 'VIII_5000',
'VIII_10000', 'VIII_MIN', 'IX_10', 'IX_50', 'IX_100', 'IX_250', 'IX_500', 'IX_1000', 'IX_5000',
'IX_10000', 'IX_MIN', 'X_10', 'X_50', 'X_100', 'X_250', 'X_500', 'X_1000', 'X_5000', 'X_10000', 'X_MIN',
'XI_10', 'XI_50', 'XI_100', 'XI_250', 'XI_500', 'XI_1000', 'XI_5000', 'XI_10000', 'XI_MIN', 'XII_10',
'XII_50', 'XII_100', 'XII_250', 'XII_500', 'XII_1000', 'XII_5000', 'XII_10000', 'XII_MIN', 'XIII_10',
'XIII_50', 'XIII_100', 'XIII_250', 'XIII_500', 'XIII_1000', 'XIII_5000', 'XIII_10000', 'XIII_MIN',
'XIV_10', 'XIV_50', 'XIV_100', 'XIV_250', 'XIV_500', 'XIV_1000', 'XIV_5000', 'XIV_10000', 'XIV_MIN']
cols_cat = ['building_material', 'city', 'building_type', 'building_use', 'parking_way', 'town', 'village',
'I_index_50', 'I_index_500', 'I_index_1000', 'II_index_50', 'II_index_500', 'II_index_1000',
'III_index_50', 'III_index_500', 'III_index_1000', 'IV_index_50', 'IV_index_500', 'IV_index_1000',
'IV_index_5000', 'V_index_50', 'V_index_500', 'V_index_1000', 'VI_10', 'VI_index_50', 'VI_index_500',
'VI_index_1000', 'VII_index_50', 'VII_index_500', 'VII_index_1000', 'VIII_index_50', 'VIII_index_500',
'VIII_index_1000', 'IX_index_50', 'IX_index_500', 'IX_index_1000', 'IX_index_5000', 'X_index_50',
'X_index_500', 'X_index_1000', 'XI_index_50', 'XI_index_500', 'XI_index_1000', 'XI_index_5000',
'XI_index_10000', 'XII_index_50', 'XII_index_500', 'XII_index_1000', 'XIII_index_50', 'XIII_index_500',
'XIII_index_1000', 'XIII_index_5000', 'XIII_index_10000', 'XIV_index_50', 'XIV_index_500',
'XIV_index_1000']
cols_feats = cols_num + cols_cat
# informational cols
cols_dt = ['txn_dt', 'building_complete_dt']
cols_bin = ['I_index_50', 'I_index_500', 'I_index_1000', 'II_index_50', 'II_index_500', 'II_index_1000',
'III_index_50', 'III_index_500', 'III_index_1000', 'IV_index_50', 'IV_index_500', 'IV_index_1000',
'IV_index_5000', 'V_index_50', 'V_index_500', 'V_index_1000', 'VI_10', 'VI_index_50', 'VI_index_500',
'VI_index_1000', 'VII_index_50', 'VII_index_500', 'VII_index_1000', 'VIII_index_50', 'VIII_index_500',
'VIII_index_1000', 'IX_index_50', 'IX_index_500', 'IX_index_1000', 'IX_index_5000', 'X_index_50',
'X_index_500', 'X_index_1000', 'XI_index_50', 'XI_index_500', 'XI_index_1000', 'XI_index_5000',
'XI_index_10000', 'XII_index_50', 'XII_index_500', 'XII_index_1000', 'XIII_index_50', 'XIII_index_500',
'XIII_index_1000', 'XIII_index_5000', 'XIII_index_10000', 'XIV_index_50', 'XIV_index_500',
'XIV_index_1000']
# Read data
df = pd.read_csv('dataset/train.csv', dtype=object)
print(df.head(5))
# Preprocessing
df, cols_feats = pre_processing(df, cols_num, cols_cat, cols_feats)
# Construct feats cols
cols_num = cols_num + [c for c in cols_num_add if c not in cols_num]
cols_cat = cols_cat + [c for c in cols_cat_add if c not in cols_cat]
cols_feats_origin = cols_num + cols_cat
# Remove feats based on feat imp exp
from vars_03 import score_base_per_area, shuffle_results_per_area
feat_imp = pd.DataFrame({'feats': shuffle_results_per_area[:,0],'avg':shuffle_results_per_area[:,1]})
feat_imp['diff'] = feat_imp['avg'] - score_base_per_area
feat_imp = feat_imp.sort_values(by='diff', ascending=False)
print(feat_imp.reset_index().values)
feat_imp = feat_imp[feat_imp['feats'] != 'none']
# Grid search
# grid search
params_fix = {'task': 'train',
'boosting_type': 'gbdt',
'objective': 'mse',
'metric': 'mape',
'learning_rate': 0.01 }
lgb_other_params = {'num_boost_round': 20000,
'verbose_eval': 2000,
'early_stopping_rounds': 1000 }
# prvs baseline best estimator
# ('objective', 'mse'), ('metric', 'mape'),
# ('learning_rate', 0.015), ('num_leaves', 255),
# ('feature_fraction', 0.5), ('min_data_in_leaf', 5),
# ('lambda_l1', 0.1), ('lambda_l2', 0.1)),
# 5524.863045737181,
# [5528.862755759428, 5530.862894274216, 5514.863487177898]]
#params_gsearch = {'max_depth': [16],
# 'num_leaves': [300],
# 'feature_fraction': [0.5],
# 'min_data_in_leaf': [10],
# 'lambda_l1': [0.01],
# 'lambda_l2': [0.1],
# }
params_gsearch = {'max_depth': [-1, 16, 24],
'num_leaves': [300], # may reduce in dim-reduction exp
'feature_fraction': [0.5],
'min_data_in_leaf': [10],
'lambda_l1': [0, 0.01, 0.1],
'lambda_l2': [0, 0.01, 0.1],
'n_feats_rm': [25, 50, 75, 100, 125, 150, 175]
}
N_fold = 3
N_iter = 189
score_low_bound = 5600 # if score smaller than this, skip the param set
# Round 1
rand_params_set = []
for i in range(N_iter):
params_rand = {key: np.random.choice(params_gsearch[key]) for key in params_gsearch}
while params_rand in rand_params_set:
params_rand = {key: np.random.choice(params_gsearch[key]) for key in params_gsearch}
rand_params_set.append(params_rand)
gsearch = {}
t0 = time.time()
gsearch = {}
folds = KFold(n_splits=N_fold, shuffle=True, random_state=123)
for i_fold, (itrain, ival) in enumerate(folds.split(df)): # kfold
print('==== Fold', i_fold+1, '====')
# split train, val
df_train = df.loc[itrain]
df_val = df.loc[ival]
# feat eng
feat_eng = FeatureEngineering(cols_num, cols_cat, cols_feats_origin, col_target)
df_train = feat_eng.fit_transform(df_train)
df_val = feat_eng.transform(df_val)
# random sample - grid search
rand_params_next = []
for params_rand in rand_params_set:
n_feats_rm = params_rand['n_feats_rm']
params = params_fix.copy()
params.update( params_rand )
params_lgb = params.copy()
del params_lgb['n_feats_rm']
feats_rm = list(feat_imp.iloc[:n_feats_rm]['feats'].values)
cols_feats = list(set(cols_feats_origin) - set(feats_rm))
print('N feats (after):', len(cols_feats))
# Construct lgb dataset
lgb_train = lgb.Dataset(df_train[cols_feats], df_train[col_target]).construct()
lgb_val = lgb.Dataset(df_val[cols_feats], df_val[col_target], reference=lgb_train).construct()
model = lgb.train(params_lgb, lgb_train, valid_sets=lgb_val, **lgb_other_params)
y_pred = model.predict(df_val[cols_feats])
mse = mean_squared_error(df_val[col_target], y_pred)
mae = mean_absolute_error(df_val[col_target], y_pred)
mape = cal_mape(df_val[col_target], y_pred)
y_pred_final = post_processing(y_pred, df_val)
score = cal_score(df_val['total_price'], y_pred_final)
print('params:', params)
print('score, mse, mae, mape: %.5f, %.5f, %.5f, %.5f.' % (score, mse, mae, mape))
if score < score_low_bound:
continue
rand_params_next.append(params_rand)
tuple_params = tuple(params.items())
if tuple_params not in gsearch:
gsearch[tuple_params] = [[], [], [], [], []]
gsearch[tuple_params][0].append(model.best_iteration)
gsearch[tuple_params][1].append(score)
gsearch[tuple_params][2].append(mse)
gsearch[tuple_params][3].append(mae)
gsearch[tuple_params][4].append(mape)
rand_params_set = rand_params_next
# aggregate, sort gsearch results
gsearch_results = [[key, *[np.mean(val_i) for val_i in val[1:]], *val] for key, val in gsearch.items()]
gsearch_results.sort(key= lambda x: x[1], reverse=True)
t1 = time.time()
print('Total computing time:', t1-t0)
for row in gsearch_results:
print(*[x for i, x in enumerate(row) if i != 5])