Kaggle competition: New York City Taxi Trip Duration
<SOS> metric: RMSLE Kaggle data source: new-york-city-taxi-trip-duration<feature_engineering>
%matplotlib inline
print('Creating df_test...')
 sample['parcelid'] = sample['ParcelId']
print("Merging Sample with property data...")
 df_test = sample.merge(prop, on='parcelid', how='left')
 df_test["transactiondate"] = pd.to_datetime('2016-11-15')
 df_test["transactiondate_year"] = df_test["transactiondate"].dt.year
df_test["transactiondate_month"] = df_test["transactiondate"].dt.month
df_test['transactiondate_quarter'] = df_test['transactiondate'].dt.quarter
df_test["transactiondate"] = df_test["transactiondate"].dt.day
x_test = df_test[train_columns]
print('Shape of x_test:', x_test.shape)
 print("Preparing x_test...")
 for c in x_test.dtypes[x_test.dtypes == object].index.values:
x_test[c] =(x_test[c] == True)
<normalization>
import os
print("
Preprocessing neural network data...")
 imputer= Imputer()
imputer.fit(x_train.iloc[:, :])
 x_train = imputer.transform(x_train.iloc[:, :])
 imputer.fit(x_test.iloc[:, :])
 x_test = imputer.transform(x_test.iloc[:, :])
 sc = StandardScaler()
x_train = sc.fit_transform(x_train)
 x_test = sc.transform(x_test)
 len_x=int(x_train.shape[1])
 print("len_x is:",len_x)
<choose_model_class>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 submission = pd.read_csv('.. /input/sample_submission.csv' )
print("
Setting up neural network model...")
 nn = Sequential()
nn.add(Dense(units = 400 , kernel_initializer = 'normal', input_dim = len_x))
nn.add(PReLU())
 nn.add(Dropout (.4))
nn.add(Dense(units = 160 , kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.6))
nn.add(Dense(units = 64 , kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.5))
nn.add(Dense(units = 26, kernel_initializer = 'normal'))
nn.add(PReLU())
 nn.add(BatchNormalization())
 nn.add(Dropout (.6))
nn.add(Dense(1, kernel_initializer='normal'))
nn.compile(loss='mae', optimizer=Adam(lr=4e-3, decay=1e-4))<train_model>
import datetime as dt
print("
Fitting neural network model...")
 nn.fit(np.array(x_train), np.array(y_train), batch_size = 32, epochs = 52, verbose=2)
 print("
Predicting with neural network model...")
 y_pred_ann = nn.predict(x_test)
 print("
Preparing results for write...")
 nn_pred = y_pred_ann.flatten()
print("Type of nn_pred is ", type(nn_pred))
print("Shape of nn_pred is ", nn_pred.shape)
 print("
Neural Network predictions:")
 print(pd.DataFrame(nn_pred ).head() )<drop_column>
train['pickup_datetime']= pd.to_datetime(train['pickup_datetime'])
 train['month'] = train['pickup_datetime'].dt.month
train['weekday'] = train['pickup_datetime'].dt.weekday
train['hours'] = train['pickup_datetime'].dt.hour
test['pickup_datetime']= pd.to_datetime(test['pickup_datetime'])
 test['month'] = test['pickup_datetime'].dt.month
test['weekday'] = test['pickup_datetime'].dt.weekday
test['hours'] = test['pickup_datetime'].dt.hour
del train
del prop
del sample
del x_train
del x_test
del df_train
del df_test
del y_pred_ann
gc.collect()<load_from_csv>
train = train[(train.trip_duration < 5000)]
np.random.seed(17)
 random.seed(17)
 print("
Processing data for OLS...")
 train = pd.read_csv(".. /input/train_2016_v2.csv", parse_dates=["transactiondate"])
 properties = pd.read_csv('.. /input/properties_2016.csv')
 submission = pd.read_csv(".. /input/sample_submission.csv")
 print(len(train),len(properties),len(submission))<feature_engineering>
train = train[(train.pickup_longitude > -100)]
train = train[(train.pickup_latitude < 50)]
def get_features(df):
df["transactiondate"] = pd.to_datetime(df["transactiondate"])
 df["transactiondate_year"] = df["transactiondate"].dt.year
df["transactiondate_month"] = df["transactiondate"].dt.month
df['transactiondate'] = df['transactiondate'].dt.quarter
df = df.fillna(-1.0)
 return df
def MAE(y, ypred):
return np.sum([abs(y[i]-ypred[i])for i in range(len(y)) ])/ len(y )<merge>
train = train[(train.passenger_count > 0)]
train = pd.merge(train, properties, how='left', on='parcelid')
 y = train['logerror'].values
test = pd.merge(submission, properties, how='left', left_on='ParcelId', right_on='parcelid')
 properties = []
exc = [train.columns[c] for c in range(len(train.columns)) if train.dtypes[c] == 'O'] + ['logerror','parcelid']
col = [c for c in train.columns if c not in exc]
train = get_features(train[col])
 test['transactiondate'] = '2016-01-01'
test = get_features(test[col] )<train_model>
X_train = train.loc[:,['month','weekday','hours','passenger_count','pickup_longitude','pickup_latitude','dropoff_longitude','dropoff_latitude']]
X_test = test.loc[:,['month','weekday','hours','passenger_count','pickup_longitude','pickup_latitude','dropoff_longitude','dropoff_latitude']]
y_train = train['trip_duration']
X_train.shape, y_train.shape
print("
Fitting OLS...")
 reg = LinearRegression(n_jobs=-1)
 reg.fit(train, y); print('fit...')
 print(MAE(y, reg.predict(train)))
 train = []; y = []
test_dates = ['2016-10-01','2016-11-01','2016-12-01','2017-10-01','2017-11-01','2017-12-01']
test_columns = ['201610','201611','201612','201710','201711','201712'] 
<define_variables>
import xgboost as xgb
from xgboost.sklearn import XGBRegressor
print("
Combining XGBoost, LightGBM, NN, and baseline predicitons...")
 lgb_weight = 1 - XGB_WEIGHT - BASELINE_WEIGHT - NN_WEIGHT - OLS_WEIGHT
lgb_weight0 = lgb_weight /(1 - OLS_WEIGHT)
 xgb_weight0 = XGB_WEIGHT /(1 - OLS_WEIGHT)
 baseline_weight0 = BASELINE_WEIGHT /(1 - OLS_WEIGHT)
 nn_weight0 = NN_WEIGHT /(1 - OLS_WEIGHT)
 pred0 = 0
pred0 += xgb_weight0*xgb_pred
pred0 += baseline_weight0*BASELINE_PRED
pred0 += lgb_weight0*p_test
pred0 += nn_weight0*nn_pred 
<predict_on_test>
xgb_model = xgb.XGBRegressor(max_depth=15, learning_rate=0.1, n_estimators=700, silent=True, objective='reg:linear', booster='gbtree', n_jobs=1, nthread=None, gamma=0, min_child_weight=1, max_delta_step=0, subsample=1, colsample_bytree=1, colsample_bylevel=1, reg_alpha=0, reg_lambda=1, scale_pos_weight=1, base_score=0.5, random_state=0, seed=None, missing=None, importance_type='gain',)
 xgb_model.fit(X_train, y_train)
print("
Combined XGB/LGB/NN/baseline predictions:")
 print(pd.DataFrame(pred0 ).head())
 print("
Predicting with OLS and combining with XGB/LGB/NN/baseline predicitons:...")
 for i in range(len(test_dates)) :
test['transactiondate'] = test_dates[i]
pred = FUDGE_FACTOR *(OLS_WEIGHT*reg.predict(get_features(test)) +(1-OLS_WEIGHT)*pred0)
 submission[test_columns[i]] = [float(format(x, '.4f')) for x in pred]
print('predict...', i)
 print("
Combined XGB/LGB/NN/baseline/OLS predictions:")
 print(submission.head() )<save_to_csv>
y_test_pred = xgb_model.predict(X_test )
print("
Writing results to disk...")
 submission.to_csv('sub{}.csv'.format(datetime.now().strftime('%Y%m%d_%H%M%S')) , index=False)
 print("
Finished..." )<import_modules>
submission['trip_duration'] = y_test_pred
submission.head()
<define_variables><EOS> 
submission.to_csv("submition.csv", index=False )