Kaggle competition: Chh-OLA
<categorify>
!sed 's/\+AF8-//g' /kaggle/input/chh-ola/train.csv > train.csv
!sed 's/_//g' /kaggle/input/chh-ola/test.csv > test.csv
encoder = LabelEncoder()
sc = StandardScaler()
def encode(df):
cat_df = [col for col in df.columns if df[col].dtype not in ['int','float']]
for col in cat_df:
df[col] = encoder.fit_transform(df[col])
 df_ = sc.fit_transform(df)
 df = pd.DataFrame(data=df_, columns = df.columns)
 return df
test,train=encode(test),encode(train )<split>
class Ut:
@staticmethod
def to_timestamp(dt):
return dt_parse(dt, dayfirst=False ).timestamp()
@staticmethod
def flag_to_num(vl):
if vl == 'N':
return 0
else:
return 1
@staticmethod
def to_float(vl):
try:
if type(vl)== type('str'):
idx = vl.find('-')
 if idx != -1:
txt = vl.split('-')
 return float(txt[1])
 return float(vl)
 except:
print(vl)
 return float(0)
 @staticmethod
def rmse(predictions, targets):

 return np.sqrt(mean_squared_error(np.exp(predictions), np.exp(targets)) )
X = train.copy()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.35, random_state=16 )<train_on_grid>
train_set = pd.read_csv('train.csv', low_memory=False, dtype=str)
 train_set.dropna(inplace=True)
 train_set.reset_index(drop=True, inplace=True)
 test_set = pd.read_csv('test.csv', low_memory=False, dtype=str)
 test_set['totalamount'] = 0
train_set['PROPOSITO'] = 1
test_set['PROPOSITO'] = 0
all_set = pd.concat([train_set, test_set], ignore_index=True)
 del(train_set)
 del(test_set)
 all_set['mtatax'] = 0.5
all_set['storedflag'] = all_set['storedflag'].apply(Ut.flag_to_num)
 all_set['pickuptime'] = all_set['pickuptime'].apply(Ut.to_timestamp)
 all_set['droptime'] = all_set['droptime'].apply(Ut.to_timestamp)
 all_set['drivertip'] = all_set['drivertip'].apply(Ut.to_float)
 all_set['mtatax'] = all_set['mtatax'].apply(Ut.to_float)
 all_set['tollamount'] = all_set['tollamount'].apply(Ut.to_float)
 all_set['extracharges'] = all_set['extracharges'].apply(Ut.to_float)
 all_set['improvementcharge'] = all_set['improvementcharge'].apply(Ut.to_float)
 all_set['totalamount'] = all_set['totalamount'].apply(Ut.to_float )
params = {
'colsample_bytree': [0.4],
'gamma': [0.0],
'learning_rate': [0.01],
'max_depth': [4],
'min_child_weight': [2],
'n_estimators': [4000],
'seed': [36],
'subsample': [0.5],
'verbosity':[0]
}
gbm = XGBRegressor()
reg_cv = GridSearchCV(gbm, params, verbose = 0)
 reg_cv.fit(X_train,y_train)
 reg_cv.best_params_
XGBoost = XGBRegressor(**reg_cv.best_params_)
 XGBoost.fit(X_train,y_train,verbose = False)
 xgboost_predictions = XGBoost.predict(X_test)
 print('XGBoost \tRMSE: \t{}$'.format(int(np.sqrt(metrics.mean_squared_error(y_test, xgboost_predictions)))) )<find_best_params>
all_set['totaltime'] = all_set['droptime'] - all_set['pickuptime']
all_set['taxes'] = all_set['drivertip'] + all_set['mtatax'] + all_set['tollamount'] + all_set['extracharges'] + all_set['improvementcharge']
reg_cv.best_params_<train_on_grid>
features_cat = ['vendorid', 'paymentmethod', 'ratecode', 'storedflag']
features_num = ['drivertip', 'pickuploc', 'droploc', 'mtatax', 'distance', 'pickuptime', 'droptime', 'numpassengers',
'tollamount', 'extracharges', 'improvementcharge', 'totalamount', 'totaltime', 'taxes']
target = 'totalamount'
for col in features_num:
all_set[col] = all_set[col].astype(float)
 for col in features_cat:
all_set[col] = all_set[col].astype(float)
 all_set[col] = all_set[col].astype(str)
 all_set['PROPOSITO'] = all_set['PROPOSITO'].astype(int)
 all_set['ID'] = all_set['ID'].astype(int )
parameters = {
'kernel':('linear','poly'),
'C':[120],
'gamma': [.0003],
'epsilon':[0.1]
}
svr = svm.SVR()
clf = GridSearchCV(svr, parameters)
 clf.fit(X_train,y_train)
 clf.best_params_
supv = XGBRegressor(**clf.best_params_)
 supv.fit(X_train,y_train,verbose = False)
 supv_predictions = supv.predict(X_test)
 print(int(np.sqrt(metrics.mean_squared_error(y_test,supv_predictions))))<find_best_params>
all_dum = pd.get_dummies(all_set)
 train_df = all_dum[all_dum['PROPOSITO'] == 1].copy()
test_df = all_dum[all_dum['PROPOSITO'] == 0].copy()
del(all_dum)
 train_df.drop(columns=['PROPOSITO'], inplace=True)
 test_df.drop(columns=['PROPOSITO'], inplace=True)
 train_df = train_df[train_df['pickuploc'] != train_df['droploc']]
train_df = train_df[train_df['totalamount'] > 0]
test_df.drop(columns=[target], inplace=True )
clf.best_params_<train_model>
features_to_keep = [
'taxes',
'pickuploc',
'ratecode_2.0',
'ratecode_1.0',
'ratecode_5.0',
'storedflag_0.0',
'ratecode_4.0',
'totaltime',
'ratecode_3.0',
'droploc',
'numpassengers',
'distance',
'storedflag_1.0',
'vendorid_2.0',
'paymentmethod_1.0',
'vendorid_1.0',
'paymentmethod_2.0'
]
X = train_df[features_to_keep].copy()
y = train_df[target].copy()
normalizer = Normalizer()
norm_X = normalizer.fit_transform(X)
 y = np.log(y )
lasso.fit(X_train,y_train)
 lasso_predictions = lasso.predict(X_test)
 print('Lasso \t \tRMSE: \t{}$'.format(int(np.sqrt(metrics.mean_squared_error(y_test, lasso_predictions)))) )<train_on_grid>
if False:
params = {
'colsample_bytree':[0.9],
'gamma':[0.3],
'max_depth': [9],
'min_child_weight':[2],
'subsample':[0.9],
'n_estimators': [50],
'objective': ['reg:squarederror'],
'n_jobs': [8],
}
eval_model = xgb.XGBRegressor(nthread=-1)
 grid = GridSearchCV(eval_model, params, cv=2)
 grid.fit(train_X, train_y)
 pred_y = grid.predict(test_X)
 print('RMSE Test = ', Ut.rmse(pred_y, test_y))
print(grid)
 print(grid.best_params_ )
%%time
parameters = {
'objective':['regression'],
'num_leaves':[4],
'learning_rate':[0.01],
'n_estimators':[9000],
'max_bin':[500],
'bagging_fraction':[.5,.3],
'bagging_freq':[5],
'bagging_seed':[7],
'feature_fraction':[.2],
'feature_fraction_seed':[7],
'verbose':[-1]
}
light = LGBMRegressor()
clf = GridSearchCV(light, parameters)
 clf.fit(X_train, y_train)
 clf.best_params_
lightgbm = LGBMRegressor(**clf.best_params_)
 lightgbm.fit(X_train,y_train)
 lightgbm_predictions = lightgbm.predict(X_test )<find_best_params>
if False:
params = {
'min_child_weight': st.randint(2, 9),
'gamma': st.uniform(0.1, 0.9),
'subsample': st.uniform(0.1, 0.9),
'colsample_bytree': st.uniform(0.1, 0.9),
'max_depth': st.randint(3, 9),
'n_estimators': [50],
'objective': ['reg:squarederror'],
}
eval_model = xgb.XGBRegressor(nthread=-1)
 grid = RandomizedSearchCV(eval_model, params, cv=2, n_jobs=1, n_iter=10)
 grid.fit(train_X, train_y)
 pred_y = grid.predict(test_X)
 print('RMSE Test = ', Ut.rmse(pred_y, test_y))
print(grid )
clf.best_params_<train_on_grid>
train_X, test_X, train_y, test_y = train_test_split(norm_X, y, test_size=0.2, random_state=42)
 params = {
'objective': 'reg:squarederror',
'n_estimators': 1000,
'subsample': 0.9,
'min_child_weight': 1,
'max_depth': 9,
'gamma': 0.3,
'colsample_bytree': 0.9,
'n_jobs': 8,
'verbose_eval':'False',
}
model = xgb.XGBRegressor(**params)
 model.fit(train_X,train_y)
 pred_y = model.predict(test_X)
 print('RMSE Test = ', Ut.rmse(pred_y, test_y))
%%time
alphas_ela = [0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008]
l1 = [0.70, 0.80, 0.85, 0.90, 0.95, 0.99]
elasticnet = make_pipeline(RobustScaler() , ElasticNetCV(max_iter=5000, cv=kfolds, alphas=alphas_ela, l1_ratio=l1)
 elasticnet.fit(X_train,y_train)
 elasticnet_predictions = np.expm1(elasticnet.predict(X_test))
print('Elasticnet \tRMSE: \t{}$'.format(int(np.sqrt(metrics.mean_squared_error(y_test,elasticnet_predictions)))) )<train_on_grid>
real_X = normalizer.transform(test_df[features_to_keep].copy())
 model = xgb.XGBRegressor(**params)
 model.fit(norm_X, y)
 predictions = np.exp(model.predict(real_X))
parameters = {
'n_estimators':[7000],
'learning_rate':[0.01],
'max_depth':[4],
'max_features':['sqrt'],
'min_samples_leaf':[10],
'min_samples_split':[10],
'loss':['huber'],
'random_state' :[16]
}
gbr = GradientBoostingRegressor()
clf = GridSearchCV(gbr, parameters)
 clf.fit(X_train, y_train)
 clf.best_params_
gbr = GradientBoostingRegressor(**clf.best_params_)
 gbr.fit(X_train,y_train)
 gbr_predictions = np.expm1(gbr.predict(X_test))<find_best_params>
result = []
for idx in range(test_df.shape[0]):
result.append([idx, predictions[idx]])
 result = pd.DataFrame(result, columns=['ID', 'total_amount'])
 result.to_csv('result.csv', index=False )
<train_on_grid><EOS> 
!rm train.csv
!rm test.csv