Kaggle competition: New York City Taxi Fare Prediction
<SOS> metric: RMSE Kaggle data source: new-york-city-taxi-fare-prediction<train_model>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
X_train = X_train.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS)
 X_test = X_test.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS )<train_model>
df = pd.read_csv('.. /input/train.csv',nrows=2_000_000, usecols=[1,2,3,4,5,6,7] )
print(f'Training images: {X_train.shape}')
 print(f'Testing images: {X_test.shape}' )<data_type_conversions>
df['pickup_datetime'] = df['pickup_datetime'].str.slice(0, 16)
 df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'], utc=True, format='%Y-%m-%d %H:%M' )
Y_train = Y_train.to_numpy()<init_hyperparams>
df.dropna(how='any', axis='rows', inplace=True)
 mask = df['pickup_longitude'].between(-75, -73)
 mask &= df['dropoff_longitude'].between(-75, -73)
 mask &= df['pickup_latitude'].between(40, 42)
 mask &= df['dropoff_latitude'].between(40, 42)
 mask &= df['passenger_count'].between(0, 8)
 mask &= df['fare_amount'].between(0, 250)
 df = df[mask]
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=8,
zoom_range = 0.15,
width_shift_range=0.15,
height_shift_range=0.15,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<choose_model_class>
def dist(pickup_lat, pickup_long, dropoff_lat, dropoff_long):
distance = np.abs(dropoff_lat - pickup_lat)+ np.abs(dropoff_long - pickup_long)
 return distance
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu', input_shape=(28, 28, 1)))
 model.add(Conv2D(filters=32, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(BatchNormalization(momentum=0.15))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(filters=32, kernel_size=(5, 5), padding='SAME', activation='relu'))
model.add(Dropout(rate=0.3))
model.add(Conv2D(filters=64, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(3, 3), padding='SAME', activation='relu'))
model.add(BatchNormalization(momentum=0.15))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(filters=64, kernel_size=(5, 5), padding='SAME', activation='relu'))
model.add(Dropout(rate=0.3))
model.add(Flatten())
 model.add(Dense(128, activation = "relu"))
model.add(Dropout(0.40))
model.add(Dense(64, activation = "relu"))
model.add(Dropout(0.40))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
def transform(data):
data['hour'] = data['pickup_datetime'].dt.hour
data['day'] = data['pickup_datetime'].dt.day
data['month'] = data['pickup_datetime'].dt.month
data['year'] = data['pickup_datetime'].dt.year
data = data.drop('pickup_datetime', axis=1)
 nyc =(-74.0063889, 40.7141667)
 jfk =(-73.7822222222, 40.6441666667)
 ewr =(-74.175, 40.69)
 lgr =(-73.87, 40.77)
 data['distance_to_center'] = dist(nyc[1], nyc[0],
data['pickup_latitude'], data['pickup_longitude'])
 data['pickup_distance_to_jfk'] = dist(jfk[1], jfk[0],
data['pickup_latitude'], data['pickup_longitude'])
 data['dropoff_distance_to_jfk'] = dist(jfk[1], jfk[0],
data['dropoff_latitude'], data['dropoff_longitude'])
 data['pickup_distance_to_ewr'] = dist(ewr[1], ewr[0],
data['pickup_latitude'], data['pickup_longitude'])
 data['dropoff_distance_to_ewr'] = dist(ewr[1], ewr[0],
data['dropoff_latitude'], data['dropoff_longitude'])
 data['pickup_distance_to_lgr'] = dist(lgr[1], lgr[0],
data['pickup_latitude'], data['pickup_longitude'])
 data['dropoff_distance_to_lgr'] = dist(lgr[1], lgr[0],
data['dropoff_latitude'], data['dropoff_longitude'])
 data['long_dist'] = data['pickup_longitude'] - data['dropoff_longitude']
data['lat_dist'] = data['pickup_latitude'] - data['dropoff_latitude']
data['dist'] = dist(data['pickup_latitude'], data['pickup_longitude'],
data['dropoff_latitude'], data['dropoff_longitude'])
 return data
df = transform(df )
model.compile(optimizer="adam", loss=['categorical_crossentropy'], metrics=['accuracy'] )<choose_model_class>
import xgboost as xgb
from bayes_opt import BayesianOptimization
from sklearn.metrics import mean_squared_error
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<define_variables>
X_train, X_test, y_train, y_test = train_test_split(df.drop('fare_amount', axis=1),
df['fare_amount'], test_size=0.25)
 del(df)
 dtrain = xgb.DMatrix(X_train, label=y_train)
 del(X_train)
 dtest = xgb.DMatrix(X_test)
 del(X_test )
batch_size=32
epochs = 25<train_model>
def xgb_evaluate(max_depth, gamma, colsample_bytree):
params = {'eval_metric': 'rmse',
'max_depth': int(max_depth),
'subsample': 0.8,
'eta': 0.1,
'gamma': gamma,
'colsample_bytree': colsample_bytree}
cv_result = xgb.cv(params, dtrain, num_boost_round=100, nfold=3)
 return -1.0 * cv_result['test-rmse-mean'].iloc[-1]
history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_test,Y_test),
steps_per_epoch=X_train.shape[0] // batch_size,
callbacks=[learning_rate_reduction])
<create_dataframe>
xgb_bo = BayesianOptimization(xgb_evaluate, {'max_depth':(3, 7),
'gamma':(0, 1),
'colsample_bytree':(0.3, 0.9)})
 xgb_bo.maximize(init_points=3, n_iter=5, acq='ei' )
cfm = confusion_matrix(np.argmax(Y_test.to_numpy() , axis=1), np.argmax(model.predict(X_test), axis=1))
cfm = pd.DataFrame(cfm,index=range(0,10),columns=range(0,10))
cfm<predict_on_test>
params = xgb_bo.res['max']['max_params']
params['max_depth'] = int(params['max_depth'] )
preds = model.predict(X_pred.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS))<prepare_output>
model2 = xgb.train(params, dtrain, num_boost_round=250)
 y_pred = model2.predict(dtest)
 y_train_pred = model2.predict(dtrain)
 print(np.sqrt(mean_squared_error(y_test, y_pred)))
 print(np.sqrt(mean_squared_error(y_train, y_train_pred)) )
pred_df['label'] = np.argmax(preds, axis=1 )<prepare_output>
test = pd.read_csv('.. /input/test.csv' ).set_index('key')
 test['pickup_datetime'] = test['pickup_datetime'].str.slice(0, 16)
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'], utc=True, format='%Y-%m-%d %H:%M')
 test = transform(test)
 dtest = xgb.DMatrix(test)
 y_pred_test = model2.predict(dtest )
<save_to_csv><EOS> 
holdout = pd.DataFrame({'key': test.index, 'fare_amount': y_pred_test})
 holdout.to_csv('submission.csv', index=False )