-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCV 4thQ.py
446 lines (268 loc) · 10.7 KB
/
CV 4thQ.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
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
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 8 20:29:28 2017
@author: darshan
"""
#########
#
## CV bfor 1 LigthGBM and 2 XGB
### Train set: Month 1 to 9
### Val set: Month 10 to 12
# Parameters
#XGB_WEIGHT = 0.6700
#BASELINE_WEIGHT = 0.0056
#OLS_WEIGHT = 0.0550
XGB_WEIGHT = 0.6415
BASELINE_WEIGHT = 0.0050
OLS_WEIGHT = 0.0828
XGB1_WEIGHT = 0.8083 # Weight of first in combination of two XGB models
BASELINE_PRED = 0.0115 # Baseline based on mean of training data, per Oleg
#XGB_WEIGHT = 0.6415
#BASELINE_WEIGHT = 0.0056
#OLS_WEIGHT = 0.0828
#XGB1_WEIGHT = 0.8083 # Weight of first in combination of two XGB models
#BASELINE_PRED = 0.0115 # Baseline based on mean of training data, per Oleg
import numpy as np
import pandas as pd
import xgboost as xgb
from sklearn.preprocessing import LabelEncoder
import lightgbm as lgb
import gc
from sklearn.linear_model import LinearRegression
import random
import datetime as dt
from sklearn.metrics import mean_absolute_error
##### READ IN RAW DATA
print( "\nReading data from disk ...")
prop = pd.read_csv('../input/properties_2016.csv')
train = pd.read_csv("../input/train_2016_v2.csv")
print( "\nProcessing data for LightGBM ..." )
for c, dtype in zip(prop.columns, prop.dtypes):
if dtype == np.float64:
prop[c] = prop[c].astype(np.float32)
print("\nPreparing train set...")
#########################################################################################3
####Ofert1: LIGTH to 0.0652813
ofert1 = prop.groupby(['yearbuilt', 'bedroomcnt', 'regionidcity'], as_index=False)['parcelid'].count()
ofert1=pd.DataFrame(ofert1)
ofert1.columns.values[3] = 'count_ParcelId'
prop= pd.merge(prop,ofert1, on=['yearbuilt', 'bedroomcnt', 'regionidcity'], how='left')
####Ofert2: v12
ofert2 = prop.groupby(['yearbuilt', 'roomcnt', 'regionidcity'], as_index=False)['parcelid'].count()
ofert2=pd.DataFrame(ofert2)
ofert2.columns.values[3] = 'count_ParcelId_Of2'
prop= pd.merge(prop,ofert2, on=['yearbuilt', 'roomcnt', 'regionidcity'], how='left')
####Tax1: ####Ofert1: LIGTH to 0.0652813
Tax1 = prop.groupby(['yearbuilt', 'bedroomcnt', 'regionidcity'], as_index=False)['taxamount'].mean()
Tax1=pd.DataFrame(Tax1)
Tax1.columns.values[3] = 'mean_TaxAmount'
prop= pd.merge(prop,Tax1, on=['yearbuilt', 'bedroomcnt', 'regionidcity'], how='left')
#######################################################################################
print
df_train = train.merge(prop, how='left', on='parcelid')
df_train.fillna(df_train.median(),inplace = True)
df_train["transactiondate"] = pd.to_datetime(df_train["transactiondate"])
df_train["Month"] = df_train["transactiondate"].dt.month
x_trainT = df_train.drop(['parcelid', 'propertyzoningdesc',
'propertycountylandusecode', 'fireplacecnt', 'fireplaceflag',
'transactiondate'], axis=1)
x_train = x_trainT[x_trainT["Month"]<10]
xval = x_trainT[x_trainT["Month"]>=10]
y_train = x_train['logerror'].values
print(x_train.shape, y_train.shape)
x_train2 = x_train.drop("logerror", axis=1)
train_columns = x_train2.columns
for c in x_train2.dtypes[x_train2.dtypes == object].index.values:
x_train2[c] = (x_train2[c] == True)
x_train2 = x_train2.values.astype(np.float32, copy=False)
d_train = lgb.Dataset(x_train2, label=y_train)
print("\nRunning LIGHTGBM.......")
##### RUN LIGHTGBM
params = {}
params['max_bin'] = 10
params['learning_rate'] = 0.0021 # shrinkage_rate
params['boosting_type'] = 'gbdt'
params['objective'] = 'regression'
params['metric'] = 'l1' # or 'mae'
params['sub_feature'] = 0.3 # feature_fraction (small values => use very different submodels)
params['bagging_fraction'] = 0.85 # sub_row
params['bagging_freq'] = 40
params['num_leaves'] = 512 # num_leaf
params['min_data'] = 500 # min_data_in_leaf
params['min_hessian'] = 0.05 # min_sum_hessian_in_leaf
params['verbose'] = 0
params['feature_fraction_seed'] = 2
params['bagging_seed'] = 3
np.random.seed(0)
random.seed(0)
print("\nFitting LightGBM model ...")
clf = lgb.train(params, d_train, 430)
print(" ...")
df_test = xval
#df_test['Ratio_1'] = df_test['taxvaluedollarcnt']/df_test['taxamount']
x_test = df_test[train_columns]
print(" ...")
del df_test; gc.collect()
print(" Preparing x_test...")
for c in x_test.dtypes[x_test.dtypes == object].index.values:
x_test[c] = (x_test[c] == True)
print(" ...")
x_test = x_test.values.astype(np.float32, copy=False)
print("\nStart LightGBM prediction ...")
p_test = clf.predict(x_test)
logReal=xval.logerror
del x_test; gc.collect()
print( "\nUnadjusted LightGBM predictions:" )
print( pd.DataFrame(p_test).head() )
maelgh=mean_absolute_error(logReal, p_test)
print("##########")
print("CV: MAE for LIGHTGBM is", maelgh)
print("##########")
################
################
## XGBoost ##
################
################
del prop
print( "\nRe-reading properties file ...")
properties =pd.read_csv('../input/properties_2016.csv')
#########################################################################################3
####Ofert1: LIGTH to 0.0652813
#ofert1 = properties.groupby(['yearbuilt', 'bedroomcnt', 'regionidcity'], as_index=False)['parcelid'].count()
#ofert1=pd.DataFrame(ofert1)
#ofert1.columns.values[3] = 'count_ParcelId'
#properties= pd.merge(properties,ofert1, on=['yearbuilt', 'bedroomcnt', 'regionidcity'], how='left')
####Ofert2: v12
#ofert2 = properties.groupby(['yearbuilt', 'roomcnt', 'regionidcity'], as_index=False)['parcelid'].count()
#ofert2=pd.DataFrame(ofert2)
#ofert2.columns.values[3] = 'count_ParcelId_Of2'
#properties= pd.merge(properties,ofert2, on=['yearbuilt', 'roomcnt', 'regionidcity'], how='left')
####Ofert3:v12
#ofert3 = properties.groupby(['yearbuilt', 'bathroomcnt', 'regionidcity'], as_index=False)['parcelid'].count()
#ofert3=pd.DataFrame(ofert3)
#ofert3.columns.values[3] = 'count_ParcelId_Of3'
#properties= pd.merge(properties,ofert3, on=['yearbuilt', 'bathroomcnt', 'regionidcity'], how='left')
####Ofert4: v12
#ofert4 = properties.groupby(['yearbuilt', 'finishedsquarefeet12', 'regionidcity'], as_index=False)['parcelid'].count()
#ofert4=pd.DataFrame(ofert4)
#ofert4.columns.values[3] = 'count_ParcelId_Of4'
#properties= pd.merge(properties,ofert4, on=['yearbuilt', 'finishedsquarefeet12', 'regionidcity'], how='left')
####Tax1: ####Ofert1: LIGTH to 0.0652813
#Tax1 = properties.groupby(['yearbuilt', 'bedroomcnt', 'regionidcity'], as_index=False)['taxamount'].mean()
#Tax1=pd.DataFrame(Tax1)
#Tax1.columns.values[3] = 'mean_TaxAmount'
#properties= pd.merge(properties,Tax1, on=['yearbuilt', 'bedroomcnt', 'regionidcity'], how='left')
#######################################################################################
print( "\nProcessing data for XGBoost ...")
for c in properties.columns:
properties[c]=properties[c].fillna(-1)
if properties[c].dtype == 'object':
lbl = LabelEncoder()
lbl.fit(list(properties[c].values))
properties[c] = lbl.transform(list(properties[c].values))
train_df = train.merge(properties, how='left', on='parcelid')
train_df["transactiondate"] = pd.to_datetime(train_df["transactiondate"])
train_df["Month"] = train_df["transactiondate"].dt.month
x_trainT = train_df.drop(['parcelid', 'propertyzoningdesc',
'propertycountylandusecode', 'fireplacecnt', 'fireplaceflag','transactiondate'], axis=1)
#Subset train y validation set
x_train = x_trainT[x_trainT["Month"]<10]
x_val = x_trainT[x_trainT["Month"]>=10]
y_train = x_train['logerror'].values
print(x_train.shape, y_train.shape)
len(y_train)
len(x_train)
# shape
print('Shape train: {}\nShape test: {}'.format(x_train.shape, x_val.shape))
# drop out ouliers
x_train=x_train[x_train.logerror > -0.4 ]
x_train=x_train[x_train.logerror < 0.419 ]
#####
x_val2 = x_val.drop("logerror", axis=1)
x_train2=x_train.drop(['logerror'], axis=1)
train_columns = x_train2.columns
y_train = x_train['logerror'].values
y_mean = np.mean(y_train)
print('After removing outliers:')
print('Shape train: {}\nShape test: {}'.format(x_train.shape, x_val.shape))
##### RUN XGBOOST
print("\nSetting up data for XGBoost ...")
# xgboost params
xgb_params = {
'eta': 0.037,
'max_depth': 5,
'subsample': 0.80,
'objective': 'reg:linear',
'eval_metric': 'mae',
'lambda': 0.8,
'alpha': 0.4,
'base_score': y_mean,
'silent': 1
}
len(x_train2)
dtrain = xgb.DMatrix(x_train2, y_train)
dtest = xgb.DMatrix(x_val2)
num_boost_rounds = 500
print("num_boost_rounds="+str(num_boost_rounds))
# train model
print( "\nTraining XGBoost ...")
model = xgb.train(dict(xgb_params, silent=1), dtrain, num_boost_round=num_boost_rounds)
print( "\nPredicting with XGBoost ...")
xgb_pred1 = model.predict(dtest)
print( "\nFirst XGBoost predictions:" )
print( pd.DataFrame(xgb_pred1).head() )
maexgb=mean_absolute_error(x_val.logerror,xgb_pred1)
print("##########")
print("CV: MAE FIRST XGB is:", maexgb)
print("##########")
#### RUN XGBOOST AGAIN
print("\nSetting up data for XGBoost ...")
# xgboost params
xgb_params = {
'eta': 0.033,
'max_depth': 6,
'subsample': 0.80,
'objective': 'reg:linear',
'eval_metric': 'mae',
'base_score': y_mean,
'silent': 1
}
num_boost_rounds = 500
print("num_boost_rounds="+str(num_boost_rounds))
print( "\nTraining XGBoost again ...")
model = xgb.train(dict(xgb_params, silent=1), dtrain, num_boost_round=num_boost_rounds)
print( "\nPredicting with XGBoost again ...")
xgb_pred2 = model.predict(dtest)
print( "\nSecond XGBoost predictions:" )
print( pd.DataFrame(xgb_pred2).head() )
maexgb2=mean_absolute_error(x_val.logerror,xgb_pred2)
print("##########")
print("CV: MAE SECOND XGB is:", maexgb2)
print("##########")
##### COMBINE XGBOOST RESULTS
xgb_pred = XGB1_WEIGHT*xgb_pred1 + (1-XGB1_WEIGHT)*xgb_pred2
#xgb_pred = xgb_pred1
print( "\nCombined XGBoost predictions:" )
print( pd.DataFrame(xgb_pred).head() )
maexgbC=mean_absolute_error(x_val.logerror,xgb_pred)
print("CV es", maexgbC)
print( "\nCombining XGBoost, LightGBM, and baseline predicitons ..." )
lgb_weight = (1 - XGB_WEIGHT - BASELINE_WEIGHT) / (1 - OLS_WEIGHT)
xgb_weight0 = XGB_WEIGHT / (1 - OLS_WEIGHT)
baseline_weight0 = BASELINE_WEIGHT / (1 - OLS_WEIGHT)
pred0 = xgb_weight0*xgb_pred + baseline_weight0*BASELINE_PRED + lgb_weight*p_test
maexgb3=mean_absolute_error(x_val.logerror,pred0)
print("##########")
print("CV: FINAL MAE is:", maexgb3)
print("##########")
#####
#Version 1: 0.064840
######
#Version10: 0.0648305 with Feat Eng.
#Tax1+Ofert1---->. Transport info to Or kernel. Make submission.
######
##V17 : CV: FINAL MAE is: 0.0648308712264
######
#V21LB0.0643556
###v222
###