Kaggle competition: New York City Taxi Trip Duration
<SOS> metric: RMSLE Kaggle data source: new-york-city-taxi-trip-duration<create_dataframe>
%matplotlib inline
plt.rcParams['figure.figsize'] = [16, 10]
warnings.filterwarnings('ignore')
 print(os.listdir(".. /input"))
def clean_prediction(row):
data = row.PURCHASED_COUPONS
data = str("".join(str(data)) [2:-2].replace("', '"," "))
return data<groupby>
beluga = pd.DataFrame({'since_start_hour': [8.0, 8.0, 20.0, 28.0, 37.0, 45.0, 85.0, 85.0, 100.0, 134.0, 134.0, 143.0, 148.0],
'Score': [ 0.400, 0.398, 0.393, 0.390, 0.383, 0.380, 0.379, 0.377, 0.376, 0.376, 0.375, 0.371, 0.368]})
 others = pd.DataFrame({'since_start_hour': [0,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,68,69,70,71,72,73,74,75,76,77,78,79,80,81,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],
'BestScore': [.452,.452,.452,.452,.452,.452,.421,.421,.421,.406,.398,.398,.398,.398,.394,.394,.394,.394,.391,.391,.390,.390,.390,.390,.389,.389,.389,.389,.389,.389,.389,.385,.385,.385,.385,.385,.385,.385,.385,.385,.385,.385,.385,.383,.383,.383,.383,.383,.383,.383,.383,.383,.383,.383,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.378,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.377,.373,.373,.373]})
 with plt.xkcd() :
fig, ax = plt.subplots()
ax.plot(others.since_start_hour.values, others.BestScore.values, 'b', alpha=0.8, lw=5, label='others')
 ax.plot(beluga.since_start_hour.values, beluga.Score.values, 'k', alpha=0.8, lw=5, label='Mrig')
 ax.set_ylim(0.36, 0.4)
 ax.legend(loc=0)
 ax.set_xlabel('hours since start')
 ax.set_ylabel('RMSLE')
 ax.set_title('Race for the top')
 plt.show()
cosine_res = cosine_res.groupby('USER_ID_hash')['COUPON_ID_hash'].apply(list ).reset_index(name='PURCHASED_COUPONS')
 jaccard_res = jaccard_res.groupby('USER_ID_hash')['COUPON_ID_hash'].apply(list ).reset_index(name='PURCHASED_COUPONS')
 pearson_res = pearson_res.groupby('USER_ID_hash')['COUPON_ID_hash'].apply(list ).reset_index(name='PURCHASED_COUPONS' )<feature_engineering>
np.random.seed(1987)
 N = 100000
t0 = dt.datetime.now()
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 sample_submission = pd.read_csv('.. /input/sample_submission.csv' )
cosine_res['PURCHASED_COUPONS'] = cosine_res.apply(clean_prediction, axis=1)
 jaccard_res['PURCHASED_COUPONS'] = jaccard_res.apply(clean_prediction, axis=1)
 pearson_res['PURCHASED_COUPONS'] = pearson_res.apply(clean_prediction, axis=1 )<save_to_csv>
print('Id is unique.')if train.id.nunique() == train.shape[0] else print('oops')
 print('Train and test sets are distinct.')if len(np.intersect1d(train.id.values, test.id.values)) == 0 else print('oops')
 print('We do not need to worry about missing values.')if train.count().min() == train.shape[0] and test.count().min() == test.shape[0] else print('oops')
 print('The store_and_fwd_flag has only two values {}.'.format(str(set(train.store_and_fwd_flag.unique())| set(test.store_and_fwd_flag.unique()))))
cosine_res.to_csv('sub_cpp_turi_item_content_cosine.csv', index=False)
 jaccard_res.to_csv('sub_cpp_turi_item_content_jaccard.csv', index=False)
 pearson_res.to_csv('sub_cpp_turi_item_content_pearson.csv', index=False )<import_modules>
train['pickup_datetime'] = pd.to_datetime(train.pickup_datetime)
 test['pickup_datetime'] = pd.to_datetime(test.pickup_datetime)
 train.loc[:, 'pickup_date'] = train['pickup_datetime'].dt.date
test.loc[:, 'pickup_date'] = test['pickup_datetime'].dt.date
train['dropoff_datetime'] = pd.to_datetime(train.dropoff_datetime)
 train['store_and_fwd_flag'] = 1 *(train.store_and_fwd_flag.values == 'Y')
 test['store_and_fwd_flag'] = 1 *(test.store_and_fwd_flag.values == 'Y')
 train['check_trip_duration'] =(train['dropoff_datetime'] - train['pickup_datetime'] ).map(lambda x: x.total_seconds())
 duration_difference = train[np.abs(train['check_trip_duration'].values - train['trip_duration'].values)> 1]
print('Trip_duration and datetimes are ok.')if len(duration_difference[['pickup_datetime', 'dropoff_datetime', 'trip_duration', 'check_trip_duration']])== 0 else print('Ooops.' )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
from sklearn.model_selection import train_test_split
from lightgbm import LGBMRegressor
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import mean_absolute_error as mae 
<load_from_csv>
coords = np.vstack(( train[['pickup_latitude', 'pickup_longitude']].values,
train[['dropoff_latitude', 'dropoff_longitude']].values,
test[['pickup_latitude', 'pickup_longitude']].values,
test[['dropoff_latitude', 'dropoff_longitude']].values))
pca = PCA().fit(coords)
 train['pickup_pca0'] = pca.transform(train[['pickup_latitude', 'pickup_longitude']])[:, 0]
train['pickup_pca1'] = pca.transform(train[['pickup_latitude', 'pickup_longitude']])[:, 1]
train['dropoff_pca0'] = pca.transform(train[['dropoff_latitude', 'dropoff_longitude']])[:, 0]
train['dropoff_pca1'] = pca.transform(train[['dropoff_latitude', 'dropoff_longitude']])[:, 1]
test['pickup_pca0'] = pca.transform(test[['pickup_latitude', 'pickup_longitude']])[:, 0]
test['pickup_pca1'] = pca.transform(test[['pickup_latitude', 'pickup_longitude']])[:, 1]
test['dropoff_pca0'] = pca.transform(test[['dropoff_latitude', 'dropoff_longitude']])[:, 0]
test['dropoff_pca1'] = pca.transform(test[['dropoff_latitude', 'dropoff_longitude']])[:, 1]
sample_submission = pd.read_csv('/kaggle/input/predict-volcanic-eruptions-ingv-oe/sample_submission.csv')
 sample_submission.head(10 )<load_from_csv>
def haversine_array(lat1, lng1, lat2, lng2):
lat1, lng1, lat2, lng2 = map(np.radians,(lat1, lng1, lat2, lng2))
AVG_EARTH_RADIUS = 6371
lat = lat2 - lat1
lng = lng2 - lng1
d = np.sin(lat * 0.5)** 2 + np.cos(lat1)* np.cos(lat2)* np.sin(lng * 0.5)** 2
h = 2 * AVG_EARTH_RADIUS * np.arcsin(np.sqrt(d))
return h
def dummy_manhattan_distance(lat1, lng1, lat2, lng2):
a = haversine_array(lat1, lng1, lat1, lng2)
 b = haversine_array(lat1, lng1, lat2, lng1)
 return a + b
def bearing_array(lat1, lng1, lat2, lng2):
AVG_EARTH_RADIUS = 6371
lng_delta_rad = np.radians(lng2 - lng1)
 lat1, lng1, lat2, lng2 = map(np.radians,(lat1, lng1, lat2, lng2))
y = np.sin(lng_delta_rad)* np.cos(lat2)
 x = np.cos(lat1)* np.sin(lat2)- np.sin(lat1)* np.cos(lat2)* np.cos(lng_delta_rad)
 return np.degrees(np.arctan2(y, x))
train.loc[:, 'distance_haversine'] = haversine_array(train['pickup_latitude'].values, train['pickup_longitude'].values, train['dropoff_latitude'].values, train['dropoff_longitude'].values)
 train.loc[:, 'distance_dummy_manhattan'] = dummy_manhattan_distance(train['pickup_latitude'].values, train['pickup_longitude'].values, train['dropoff_latitude'].values, train['dropoff_longitude'].values)
 train.loc[:, 'direction'] = bearing_array(train['pickup_latitude'].values, train['pickup_longitude'].values, train['dropoff_latitude'].values, train['dropoff_longitude'].values)
 train.loc[:, 'pca_manhattan'] = np.abs(train['dropoff_pca1'] - train['pickup_pca1'])+ np.abs(train['dropoff_pca0'] - train['pickup_pca0'])
 test.loc[:, 'distance_haversine'] = haversine_array(test['pickup_latitude'].values, test['pickup_longitude'].values, test['dropoff_latitude'].values, test['dropoff_longitude'].values)
 test.loc[:, 'distance_dummy_manhattan'] = dummy_manhattan_distance(test['pickup_latitude'].values, test['pickup_longitude'].values, test['dropoff_latitude'].values, test['dropoff_longitude'].values)
 test.loc[:, 'direction'] = bearing_array(test['pickup_latitude'].values, test['pickup_longitude'].values, test['dropoff_latitude'].values, test['dropoff_longitude'].values)
 test.loc[:, 'pca_manhattan'] = np.abs(test['dropoff_pca1'] - test['pickup_pca1'])+ np.abs(test['dropoff_pca0'] - test['pickup_pca0'])
 train.loc[:, 'center_latitude'] =(train['pickup_latitude'].values + train['dropoff_latitude'].values)/ 2
train.loc[:, 'center_longitude'] =(train['pickup_longitude'].values + train['dropoff_longitude'].values)/ 2
test.loc[:, 'center_latitude'] =(test['pickup_latitude'].values + test['dropoff_latitude'].values)/ 2
test.loc[:, 'center_longitude'] =(test['pickup_longitude'].values + test['dropoff_longitude'].values)/ 2
train_set = pd.read_csv('/kaggle/input/volcano-stft-data/output_train_set.csv', index_col=0 ).reset_index(drop=True)
 train_set.head(10 )<load_from_csv>
train.loc[:, 'pickup_weekday'] = train['pickup_datetime'].dt.weekday
train.loc[:, 'pickup_hour_weekofyear'] = train['pickup_datetime'].dt.weekofyear
train.loc[:, 'pickup_hour'] = train['pickup_datetime'].dt.hour
train.loc[:, 'pickup_minute'] = train['pickup_datetime'].dt.minute
train.loc[:, 'pickup_dt'] =(train['pickup_datetime'] - train['pickup_datetime'].min() ).dt.total_seconds()
train.loc[:, 'pickup_week_hour'] = train['pickup_weekday'] * 24 + train['pickup_hour']
test.loc[:, 'pickup_weekday'] = test['pickup_datetime'].dt.weekday
test.loc[:, 'pickup_hour_weekofyear'] = test['pickup_datetime'].dt.weekofyear
test.loc[:, 'pickup_hour'] = test['pickup_datetime'].dt.hour
test.loc[:, 'pickup_minute'] = test['pickup_datetime'].dt.minute
test.loc[:, 'pickup_dt'] =(test['pickup_datetime'] - train['pickup_datetime'].min() ).dt.total_seconds()
test.loc[:, 'pickup_week_hour'] = test['pickup_weekday'] * 24 + test['pickup_hour']
test_set = pd.read_csv('/kaggle/input/volcano-stft-data/output_test_set.csv', index_col=0 ).reset_index(drop=True)
 test_set.head(10 )<filter>
sample_ind = np.random.permutation(len(coords)) [:500000]
kmeans = MiniBatchKMeans(n_clusters=100, batch_size=10000 ).fit(coords[sample_ind] )
sum(sample_submission['segment_id']==test_set['segment_id'])/ len(test_set )<feature_engineering>
train.loc[:, 'pickup_cluster'] = kmeans.predict(train[['pickup_latitude', 'pickup_longitude']])
 train.loc[:, 'dropoff_cluster'] = kmeans.predict(train[['dropoff_latitude', 'dropoff_longitude']])
 test.loc[:, 'pickup_cluster'] = kmeans.predict(test[['pickup_latitude', 'pickup_longitude']])
 test.loc[:, 'dropoff_cluster'] = kmeans.predict(test[['dropoff_latitude', 'dropoff_longitude']])
 t1 = dt.datetime.now()
print('Time till clustering: %i seconds' %(t1 - t0 ).seconds )
train_set['time_to_eruption_pc'] = train_set['time_to_eruption'] / train_set['time_to_eruption'].max()
NFOLDS=10
skf = StratifiedKFold(n_splits=NFOLDS)
 train_set['label_strat'] = np.round(train_set['time_to_eruption_pc'] * 20, 0)
 train_set['fold']=0
f=0
for trn_idx, val_idx in skf.split(train_set[['segment_id']], train_set['label_strat']):
train_set.loc[val_idx, 'fold']=f
f+=1
train_set['fold'].value_counts()<define_variables>
for gby_col in ['pickup_hour', 'pickup_date', 'pickup_dt_bin',
'pickup_week_hour', 'pickup_cluster', 'dropoff_cluster']:
gby = train.groupby(gby_col ).mean() [['avg_speed_h', 'avg_speed_m', 'log_trip_duration']]
gby.columns = ['%s_gby_%s' %(col, gby_col)for col in gby.columns]
train = pd.merge(train, gby, how='left', left_on=gby_col, right_index=True)
 test = pd.merge(test, gby, how='left', left_on=gby_col, right_index=True)
 for gby_cols in [['center_lat_bin', 'center_long_bin'],
['pickup_hour', 'center_lat_bin', 'center_long_bin'],
['pickup_hour', 'pickup_cluster'], ['pickup_hour', 'dropoff_cluster'],
['pickup_cluster', 'dropoff_cluster']]:
coord_speed = train.groupby(gby_cols ).mean() [['avg_speed_h']].reset_index()
coord_count = train.groupby(gby_cols ).count() [['id']].reset_index()
coord_stats = pd.merge(coord_speed, coord_count, on=gby_cols)
 coord_stats = coord_stats[coord_stats['id'] > 100]
coord_stats.columns = gby_cols + ['avg_speed_h_%s' % '_'.join(gby_cols), 'cnt_%s' % '_'.join(gby_cols)]
train = pd.merge(train, coord_stats, how='left', on=gby_cols)
 test = pd.merge(test, coord_stats, how='left', on=gby_cols )
DROP_FTS = ['segment_id', 'time_to_eruption','h:m:s', 'label_strat', 'fold', 'time_to_eruption_pc',]
SEL_FTS = [x for x in train_set.columns if x not in DROP_FTS]
LABEL = 'time_to_eruption'<train_model>
group_freq = '60min'
df_all = pd.concat(( train, test)) [['id', 'pickup_datetime', 'pickup_cluster', 'dropoff_cluster']]
train.loc[:, 'pickup_datetime_group'] = train['pickup_datetime'].dt.round(group_freq)
 test.loc[:, 'pickup_datetime_group'] = test['pickup_datetime'].dt.round(group_freq)
 df_counts = df_all.set_index('pickup_datetime')[['id']].sort_index()
df_counts['count_60min'] = df_counts.isnull().rolling(group_freq ).count() ['id']
train = train.merge(df_counts, on='id', how='left')
 test = test.merge(df_counts, on='id', how='left')
 dropoff_counts = df_all \ 
.set_index('pickup_datetime')\ 
.groupby([pd.TimeGrouper(group_freq), 'dropoff_cluster'])\ 
.agg({'id': 'count'})\ 
.reset_index().set_index('pickup_datetime')\ 
.groupby('dropoff_cluster' ).rolling('240min' ).mean() \ 
.drop('dropoff_cluster', axis=1)\ 
.reset_index().set_index('pickup_datetime' ).shift(freq='-120min' ).reset_index() \ 
.rename(columns={'pickup_datetime': 'pickup_datetime_group', 'id': 'dropoff_cluster_count'})
 train['dropoff_cluster_count'] = train[['pickup_datetime_group', 'dropoff_cluster']].merge(dropoff_counts, on=['pickup_datetime_group', 'dropoff_cluster'], how='left')['dropoff_cluster_count'].fillna(0)
 test['dropoff_cluster_count'] = test[['pickup_datetime_group', 'dropoff_cluster']].merge(dropoff_counts, on=['pickup_datetime_group', 'dropoff_cluster'], how='left')['dropoff_cluster_count'].fillna(0 )
optuna.logging.set_verbosity(optuna.logging.WARNING)
 OPTUNA_TRIALS = 30
class Optimizer:
def __init__(self, metric, trials=OPTUNA_TRIALS):
self.metric = metric
self.trials = trials
self.sampler = TPESampler(seed=42)
 def objective(self, trial):
model = create_model(trial)
 model.fit(X_train, y_train)
 preds = model.predict(X_val)
 if self.metric == 'mae':
return -mae(y_val, preds)
 else:
return -np.sqrt(mse(y_val, preds))
def optimize(self):
study = optuna.create_study(direction="maximize", sampler=self.sampler)
 study.optimize(self.objective, n_trials=self.trials)
 return study.best_params
def create_model(trial):
colsample_bytree = trial.suggest_uniform("colsample_bytree", 0.2, 0.95)
 subsample = trial.suggest_uniform("subsample", 0.2, 0.9)
 reg_alpha = trial.suggest_uniform("reg_alpha", 0.001, 100)
 max_depth = trial.suggest_int("max_depth", 1, 15)
 min_child_samples = trial.suggest_int('min_child_samples', 1, 15)
 n_estimators = trial.suggest_int('n_estimators', 100, 250)
 model = LGBMRegressor(random_state = 42,
max_depth = max_depth,
n_estimators = n_estimators,
colsample_bytree=colsample_bytree,
subsample=subsample,
reg_alpha=reg_alpha,
learning_rate = 0.05)
 return model<prepare_x_and_y>
param_outputs = []
FOLD_VALUES = [x for x in train_set['fold'].unique() ]
for fold in train_set['fold'].unique() :
trn_idx = train_set['fold']!=fold
val_idx = train_set['fold']==fold
X_train = train_set.loc[trn_idx,SEL_FTS].values
y_train = train_set.loc[trn_idx, LABEL].values
X_val = train_set.loc[val_idx,SEL_FTS].values
y_val = train_set.loc[val_idx, LABEL].values
optimizer = Optimizer('mse')
 lgb_params = optimizer.optimize()
param_outputs += [lgb_params]
lgb_params['random_state'] = 42
model_output = LGBMRegressor(**lgb_params)
 print(fold, lgb_params)
 model_output.fit(X_train, y_train)
 preds = model_output.predict(X_val)
 print(mae(y_val, preds))
param_headings = ['colsample_bytree','subsample', 'reg_alpha',
'max_depth', 'min_child_samples', 'n_estimators', 'random_state']
params_output_df = pd.DataFrame(columns=param_headings, index=FOLD_VALUES, data=0.0)
 for idx in params_output_df.index:
for h in param_headings:
params_output_df.at[idx, h] = param_outputs[idx][h]
print('saving CSV')
 params_output_df.to_csv('optuna_lgbm_params.csv', index=True)
 param_means = params_output_df.mean(axis=0)
 param_means<save_to_csv>
feature_stats = pd.DataFrame({'feature': feature_names})
 feature_stats.loc[:, 'train_mean'] = np.nanmean(train[feature_names].values, axis=0 ).round(4)
 feature_stats.loc[:, 'test_mean'] = np.nanmean(test[feature_names].values, axis=0 ).round(4)
 feature_stats.loc[:, 'train_std'] = np.nanstd(train[feature_names].values, axis=0 ).round(4)
 feature_stats.loc[:, 'test_std'] = np.nanstd(test[feature_names].values, axis=0 ).round(4)
 feature_stats.loc[:, 'train_nan'] = np.mean(np.isnan(train[feature_names].values), axis=0 ).round(3)
 feature_stats.loc[:, 'test_nan'] = np.mean(np.isnan(test[feature_names].values), axis=0 ).round(3)
 feature_stats.loc[:, 'train_test_mean_diff'] = np.abs(feature_stats['train_mean'] - feature_stats['test_mean'])/ np.abs(feature_stats['train_std'] + feature_stats['test_std'])* 2
feature_stats.loc[:, 'train_test_nan_diff'] = np.abs(feature_stats['train_nan'] - feature_stats['test_nan'])
 feature_stats = feature_stats.sort_values(by='train_test_mean_diff')
 feature_stats[['feature', 'train_test_mean_diff']].tail()
sample_submission['time_to_eruption'] = test_predictions
sample_submission.to_csv('submission.csv', index=False )<install_modules>
feature_stats = feature_stats.sort_values(by='train_test_nan_diff')
 feature_stats[['feature', 'train_nan', 'test_nan', 'train_test_nan_diff']].tail()
!pip install -q https://github.com/pfnet-research/xfeat/archive/master.zip<set_options>
Xtr, Xv, ytr, yv = train_test_split(train[feature_names].values, y, test_size=0.2, random_state=1987)
 dtrain = xgb.DMatrix(Xtr, label=ytr)
 dvalid = xgb.DMatrix(Xv, label=yv)
 dtest = xgb.DMatrix(test[feature_names].values)
 watchlist = [(dtrain, 'train'),(dvalid, 'valid')]
xgb_pars = {'min_child_weight': 50, 'eta': 0.3, 'colsample_bytree': 0.3, 'max_depth': 10,
'subsample': 0.8, 'lambda': 1., 'nthread': 4, 'booster' : 'gbtree', 'silent': 1,
'eval_metric': 'rmse', 'objective': 'reg:linear'}
xfeat.utils.cudf_is_available()<define_variables>
model = xgb.train(xgb_pars, dtrain, 60, watchlist, early_stopping_rounds=50,
maximize=False, verbose_eval=10 )
PATH_TRAIN_CSV = ".. /input/bnp-paribas-cardif-claims-management/train.csv.zip"
PATH_TEST_CSV = ".. /input/bnp-paribas-cardif-claims-management/test.csv.zip"
PATH_SUB_CSV = ".. /input/bnp-paribas-cardif-claims-management/sample_submission.csv.zip"
USECOLS = [
"v10", "v12", "v14", "v21", "v22", "v24", "v30", "v31", "v34", "v38",
"v40", "v47", "v50", "v52", "v56", "v62", "v66", "v72", "v75", "v79",
"v91", "v112", "v113", "v114", "v129", "target"
]
def preload() :
xfeat.utils.compress_df(pd.concat([
pd.read_csv(PATH_TRAIN_CSV),
pd.read_csv(PATH_TEST_CSV),
], sort=False)).reset_index(drop=True)[USECOLS].to_feather(
 ".. /working/train_test.ftr")
 preload()<load_from_disk>
print('Modeling RMSLE %.5f' % model.best_score)
 t1 = dt.datetime.now()
print('Training time: %i seconds' %(t1 - t0 ).seconds )
print("(1)Save numerical features")
 xfeat.SelectNumerical().fit_transform(
 pd.read_feather(".. /working/train_test.ftr")
 ).reset_index(drop=True ).to_feather(".. /working/feature_num_features.ftr" )<categorify>
ytest = model.predict(dtest)
 print('Test shape OK.')if test.shape[0] == ytest.shape[0] else print('Oops')
 test['trip_duration'] = np.exp(ytest)- 1
test[['id', 'trip_duration']].to_csv('mrig_xgb_submission.csv', index=False)
 print('Valid prediction mean: %.3f' % ypred.mean())
 print('Test prediction mean: %.3f' % ytest.mean())
 fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
 sns.distplot(ypred, ax=ax[0], color='blue', label='validation prediction')
 sns.distplot(ytest, ax=ax[1], color='green', label='test prediction')
 ax[0].legend(loc=0)
 ax[1].legend(loc=0)
 plt.show()
t1 = dt.datetime.now()
print('Total time: %i seconds' %(t1 - t0 ).seconds )
print("(2)Categorical encoding using label encoding: 13 features")
 xfeat.Pipeline([
xfeat.SelectCategorical() , xfeat.LabelEncoder(output_suffix="")]
).fit_transform(
 pd.read_feather(".. /working/train_test.ftr")
 ).reset_index(drop=True ).to_feather(".. /working/feature_1way_label_encoding.ftr" )<categorify>
FOREVER_COMPUTING_FLAG = False
xgb_pars = []
for MCW in [10, 20, 50, 75, 100]:
for ETA in [0.05, 0.1, 0.15]:
for CS in [0.3, 0.4, 0.5]:
for MD in [6, 8, 10, 12, 15]:
for SS in [0.5, 0.6, 0.7, 0.8, 0.9]:
for LAMBDA in [0.5, 1., 1.5, 2., 3.]:
xgb_pars.append({'min_child_weight': MCW, 'eta': ETA,
'colsample_bytree': CS, 'max_depth': MD,
'subsample': SS, 'lambda': LAMBDA,
'nthread': -1, 'booster' : 'gbtree', 'eval_metric': 'rmse',
'silent': 1, 'objective': 'reg:linear'})
 while FOREVER_COMPUTING_FLAG:
xgb_par = np.random.choice(xgb_pars, 1)[0]
print(xgb_par)
 model = xgb.train(xgb_par, dtrain, 2000, watchlist, early_stopping_rounds=50,
maximize=False, verbose_eval=100)
 print('Modeling RMSLE %.5f' % model.best_score )