Kaggle competition: New York City Taxi Fare Prediction
titanictest['Fare'] = titanictest['Fare'].fillna(35)
 titanictest.isna().sum()<split>
preTrainedModel = False
loadOnlyWeights = False
useBestModel = True
EPOCH = 32
BATCH_SIZE = 1024
VALIDATION_SIZE = 500000
TRAINING_SIZE = 6000000
INTEL_AI_DEV_CLOUD = False
X = titanic[['Pclass',
'Sex_label',
'Age',
'SibSp',
'Parch',
'Fare',
'Embarked_C',
'Embarked_Q',
'Embarked_S']]
y = titanic['Survived']
X_train, X_test,y_train,y_test = train_test_split(X,
y,
test_size = 0.3,
random_state = 789 )<predict_on_test>
if INTEL_AI_DEV_CLOUD:
config = tf.ConfigProto(intra_op_parallelism_threads = 6,
inter_op_parallelism_threads = 2,
allow_soft_placement = True,
device_count = {'CPU': 6 })
 session = tf.Session(config = config)
 K.set_session(session)
 os.environ["OMP_NUM_THREADS"] = "6"
os.environ["KMP_BLOCKTIME"] = "30"
os.environ["KMP_SETTINGS"] = "1"
os.environ["KMP_AFFINITY"]= "granularity=fine,verbose,compact,1,0"
logreg = LogisticRegression(random_state=789)
 logreg.fit(X_train, y_train)
 y_predicted = logreg.predict(titanictest)
 THRESHOLD = 0.58
y_predicted = np.where(logreg.predict_proba(titanictest)[:,1] > THRESHOLD, 1, 0 )<rename_columns>
print(keras.__version__)
pd.DataFrame(y_predicted ).rename(columns = {0 : 'Survived'} )<load_from_csv>
JFK =(-73.7822222222, 40.6441666667)
 NYC =(-74.0063889, 40.7141667)
 EWR =(-74.175, 40.6897222222)
 LGA =(-73.8739659, 40.7769271 )
dfkagglesubmission = pd.read_csv('.. /input/titanic/test.csv')
 dfid = dfkagglesubmission[['PassengerId']]
dfkagglesubmission = pd.concat([dfid, pd.DataFrame(y_predicted ).rename(columns = {0 : 'Survived'})], axis=1)
 dfkagglesubmission<count_values>
def minkowski_distance(x1, x2, y1, y2, p):
return(( abs(x2 - x1)** p)+(abs(y2 - y1)) ** p)**(1 / p )
dfkagglesubmission['Survived'].value_counts()<save_to_csv>
def minkowski_distance_3D(x1, x2, y1, y2, z1, z2, p):
return(( abs(x2 - x1)** p)+(abs(y2 - y1)** p)+(abs(z2 - z1)** p)) **(1 / p )
dfkagglesubmission.to_csv('Titanic_submission.csv',index=False )<define_variables>
def calculate_direction(d_lon, d_lat):
result = np.zeros(len(d_lon))
l = np.sqrt(d_lon**2 + d_lat**2)
 result[d_lon>0] =(180/np.pi)*np.arcsin(d_lat[d_lon>0]/l[d_lon>0])
 idx =(d_lon<0)&(d_lat>0)
 result[idx] = 180 -(180/np.pi)*np.arcsin(d_lat[idx]/l[idx])
 idx =(d_lon<0)&(d_lat<0)
 result[idx] = -180 -(180/np.pi)*np.arcsin(d_lat[idx]/l[idx])
 return result
f_lstm_glove = '.. /input/improved-lstm-baseline-glove-dropout/submission.csv'
f_nbsvm = '.. /input/nb-svm-strong-linear-baseline/submission.csv'
f_lstm_fast = '.. /input/improved-lstm-baseline-fasttext-dropout/submission.csv'
f_bi_gru_dual = '.. /input/improved-lstm-baseline-bi-gru-dual-embedding/submission.csv'<load_from_csv>
def time_type_of_the_day(hour):
time_of_the_day = 2
if(hour >= 4 and hour < 7):
time_of_the_day = 1
elif(hour >= 7 and hour < 12):
time_of_the_day = 2
elif(hour >= 12 and hour < 16):
time_of_the_day = 3
elif(hour >= 16 and hour < 20):
time_of_the_day = 4
elif(hour >= 20 and hour < 23):
time_of_the_day = 5
elif(hour >= 23 and hour < 4):
time_of_the_day = 6
else:
time_of_the_day = 2
return time_of_the_day
p_lstm_glove = pd.read_csv(f_lstm_glove)
 p_nbsvm = pd.read_csv(f_nbsvm)
 p_lstm_fast = pd.read_csv(f_lstm_fast)
 p_bi_gru_dual = pd.read_csv(f_bi_gru_dual )<feature_engineering>
def peak_hours_of_the_day(hour, weekday):
peak_hours = 0
if(0 <= weekday <= 4)and(16 <= hour <= 20):
peak_hours = 1
return peak_hours
label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
p_res = p_lstm_glove.copy()
p_res[label_cols] =(p_nbsvm[label_cols] + p_lstm_glove[label_cols] + p_lstm_fast[label_cols] + p_bi_gru_dual[label_cols])/ 4<save_to_csv>
def surcharge_hours_of_the_day(hour):
surcharge_hours = 0
if(hour >= 20 or hour <= 6):
surcharge_hours = 1
return surcharge_hours
p_res.to_csv('submission.csv', index=False )<import_modules>
def add_travel_vector_features(df):
df['delta_longitude'] = df.pickup_longitude - df.dropoff_longitude
df['delta_latitude'] = df.pickup_latitude - df.dropoff_latitude
df['pickup_x'] = np.cos(df.pickup_latitude)* np.cos(df.pickup_longitude)
 df['pickup_y'] = np.cos(df.pickup_latitude)* np.sin(df.pickup_longitude)
 df['pickup_z'] = np.sin(df.pickup_latitude)
 df['dropoff_x'] = np.cos(df.dropoff_latitude)* np.cos(df.dropoff_longitude)
 df['dropoff_y'] = np.cos(df.dropoff_latitude)* np.sin(df.dropoff_longitude)
 df['dropoff_z'] = np.sin(df.dropoff_latitude)
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score<import_modules>
def parse_and_extract_date_time_components(df):
df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'], format='%Y-%m-%d %H:%M:%S %Z')
 df['year'] = df['pickup_datetime'].apply(lambda x: x.year)
 df['month'] = df['pickup_datetime'].apply(lambda x: x.month)
 df['day'] = df['pickup_datetime'].apply(lambda x: x.day)
 df['hour'] = df['pickup_datetime'].apply(lambda x: x.hour)
 df['minute'] = df['pickup_datetime'].apply(lambda x: x.minute)
 df['weekday'] = df['pickup_datetime'].apply(lambda x: x.dayofweek)
 df['weekofyear'] = df['pickup_datetime'].apply(lambda x: x.weekofyear)
 df['dayofyear'] = df['pickup_datetime'].apply(lambda x: x.dayofyear)
 df['quarterofyear'] = df['pickup_datetime'].apply(lambda x: x.quarter)
 df['days_in_month'] = df['pickup_datetime'].apply(lambda x: x.days_in_month)
 df['linear_time'] = df['pickup_datetime'].apply(lambda x: x.value // 10 ** 9)
 df['time_type_of_the_day'] = df['pickup_datetime'].apply(lambda x: time_type_of_the_day(x.hour))
df['peak_hours'] = df['pickup_datetime'].apply(lambda x: peak_hours_of_the_day(x.hour, x.dayofweek))
df['surcharge'] = df['pickup_datetime'].apply(lambda x: surcharge_hours_of_the_day(x.hour))
df['hour_sin'] = np.sin(df.hour *(2.* np.pi / 24))
df['hour_cos'] = np.cos(df.hour *(2.* np.pi / 24))
df['minute_sin'] = np.sin(df.minute *(2.* np.pi / 60))
df['minute_cos'] = np.cos(df.minute *(2.* np.pi / 60))
df['time_type_of_day_sin'] = np.sin(df.time_type_of_the_day *(2.* np.pi / 6))
df['time_type_of_day_cos'] = np.cos(df.time_type_of_the_day *(2.* np.pi / 6))
df['month_sin'] = np.sin(( df.month - 1)*(2.* np.pi / 12))
df['month_cos'] = np.cos(( df.month - 1)*(2.* np.pi / 12))
df['day_sin'] = np.sin(( df.day - 1)*(2.* np.pi / df.days_in_month))
df['day_cos'] = np.cos(( df.day - 1)*(2.* np.pi / df.days_in_month))
df['weekday_sin'] = np.sin(( df.weekday - 1)*(2.* np.pi / 7))
df['weekday_cos'] = np.cos(( df.weekday - 1)*(2.* np.pi / 7))
df['weekofyear_sin'] = np.sin(( df.weekofyear - 1)*(2.* np.pi / 52))
df['weekofyear_cos'] = np.cos(( df.weekofyear - 1)*(2.* np.pi / 52))
df['dayofyear_sin'] = np.sin(( df.dayofyear - 1)*(2.* np.pi / 365))
df['dayofyear_cos'] = np.cos(( df.dayofyear - 1)*(2.* np.pi / 365))
df['quarterofyear_sin'] = np.sin(( df.quarterofyear - 1)*(2.* np.pi / 4))
df['quarterofyear_cos'] = np.cos(( df.quarterofyear - 1)*(2.* np.pi / 4))
del df['hour']
del df['minute']
del df['month']
del df['day']
del df['weekday']
del df['days_in_month']
del df['time_type_of_the_day']
del df['weekofyear']
del df['quarterofyear']
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Model
from keras.layers import Input, Dense, Embedding, SpatialDropout1D, concatenate, GRU, Bidirectional, GlobalAveragePooling1D, GlobalMaxPooling1D
from keras.callbacks import Callback, ModelCheckpoint, EarlyStopping
import keras.backend as K<define_variables>
def haversine_np(lon1, lat1, lon2, lat2):

 lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
 dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat/2.0)**2 + np.cos(lat1)* np.cos(lat2)* np.sin(dlon/2.0)**2
c = 2 * np.arcsin(np.sqrt(a))
miles = 3959 * c
return miles
EMBEDDING_FILE = '.. /input/fasttext-crawl-300d-2m/crawl-300d-2M.vec'<load_from_csv>
fname = 'train.csv'
train_df = pd.read_csv('.. /input/train.csv', nrows = TRAINING_SIZE)
 print(train_df.isnull().sum())
 print(fname + ' Old size: %d' % len(train_df))
train_df = train_df.dropna(how = 'any', axis = 'rows')
 print(fname + ' New size: %d' % len(train_df))
train = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/train.csv')
 test = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/test.csv')
 submission = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/sample_submission.csv' )<prepare_x_and_y>
add_travel_vector_features(train_df)
 parse_and_extract_date_time_components(train_df)
 print(fname + ' Before dropping Zero pickup longitude and latitude - Old size: %d' % len(train_df))
train_df = train_df[(train_df.pickup_longitude != 0.) &(train_df.pickup_latitude != 0.) ]
print(fname + ' After dropping Zero pickup longitude and latitude - New size: %d' % len(train_df))
print(fname + ' Before dropping Zero dropoff longitude and latitude - Old size: %d' % len(train_df))
train_df = train_df[(train_df.dropoff_longitude != 0.) &(train_df.dropoff_latitude != 0.) ]
print(fname + ' After dropping Zero dropoff longitude and latitude - New size: %d' % len(train_df))
print(fname + ' Before dropping really large lat and lon Outliers - Old size: %d' % len(train_df))
train_df = train_df[(train_df.delta_longitude.abs() < 5.0)&(train_df.delta_latitude.abs() < 5.0)]
print(fname + ' After dropping really large lat and lon Outliers - New size: %d' % len(train_df))
print(fname + ' Before dropping zero delta lat and long values - Old size: %d' % len(train_df))
train_df = train_df[(train_df.delta_longitude != 0.) &(train_df.delta_latitude != 0.) ]
print(fname + ' After dropping delta lat and long values - New size: %d' % len(train_df))
print(fname + ' Before dropping NEGATIVE FARE VALUES - Old size: %d' % len(train_df))
train_df = train_df[(train_df.fare_amount > 0.) ]
print(fname + ' After dropping NEGATIVE FARE VALUES - New size: %d' % len(train_df))
print(fname + ' Before dropping Zero Passenger Counts - Old size: %d' % len(train_df))
train_df = train_df[(train_df.passenger_count > 0)&(train_df.passenger_count < 7)]
print(fname + ' After dropping Zero Passenger Counts - New size: %d' % len(train_df))
X_train = train['comment_text'].values
y_train = train[['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']].values
X_test = test['comment_text'].values<count_unique_values>
train_df['distance_in_miles'] = haversine_np(train_df.dropoff_longitude, train_df.dropoff_latitude, train_df.pickup_longitude, train_df.pickup_latitude)
 train_df['direction'] = calculate_direction(train_df.delta_longitude, train_df.delta_latitude)
 train_df['pickup_to_JFK_distance'] = haversine_np(train_df.pickup_longitude, train_df.pickup_latitude, JFK[0], JFK[1])
 train_df['dropoff_to_JFK_distance'] = haversine_np(train_df.dropoff_longitude, train_df.dropoff_latitude, JFK[0], JFK[1])
 train_df['pickup_to_EWR_distance'] = haversine_np(train_df.pickup_longitude, train_df.pickup_latitude, EWR[0], EWR[1])
 train_df['dropoff_to_EWR_distance'] = haversine_np(train_df.dropoff_longitude, train_df.dropoff_latitude, EWR[0], EWR[1])
 train_df['pickup_to_LGA_distance'] = haversine_np(train_df.pickup_longitude, train_df.pickup_latitude, LGA[0], LGA[1])
 train_df['dropoff_to_LGA_distance'] = haversine_np(train_df.dropoff_longitude, train_df.dropoff_latitude, LGA[0], LGA[1])
 train_df['pickup_to_NYC_distance'] = haversine_np(train_df.pickup_longitude, train_df.pickup_latitude, NYC[0], NYC[1])
 train_df['dropoff_to_NYC_distance'] = haversine_np(train_df.dropoff_longitude, train_df.dropoff_latitude, NYC[0], NYC[1])
 train_df['manhattan'] = minkowski_distance(train_df.pickup_longitude, train_df.dropoff_longitude,
train_df.pickup_latitude, train_df.dropoff_latitude, 1)
 train_df['euclidean'] = minkowski_distance(train_df.pickup_longitude, train_df.dropoff_longitude,
train_df.pickup_latitude, train_df.dropoff_latitude, 2)
 train_df['euclidean_3D'] = minkowski_distance_3D(train_df.pickup_x, train_df.dropoff_x,
train_df.pickup_y, train_df.dropoff_y,
train_df.pickup_z, train_df.dropoff_z, 2)
 train_df['manhattan_3D'] = minkowski_distance_3D(train_df.pickup_x, train_df.dropoff_x,
train_df.pickup_y, train_df.dropoff_y,
train_df.pickup_z, train_df.dropoff_z, 1)

print(fname + ' Before dropping HIGH FARE AMOUNTS - Old size: %d' % len(train_df))
train_df = train_df[(train_df.fare_amount > 1.) &(train_df.fare_amount < 251.) ]
print(fname + ' After dropping HIGH FARE AMOUNTS - New size: %d' % len(train_df))
unique_words = {word for sent in X_train for word in sent.split() }.union({word for sent in X_test for word in sent.split() })
 print(f'number of unique words in the corpus {len(unique_words)}' )<split>
training_set = train_df.loc[:, [
'passenger_count',
'distance_in_miles',
'pickup_x',
'pickup_y',
'pickup_z',
'dropoff_x',
'dropoff_y',
'dropoff_z',
'pickup_to_JFK_distance',
'dropoff_to_JFK_distance',
'pickup_to_NYC_distance',
'dropoff_to_NYC_distance',
'delta_longitude',
'delta_latitude',
'direction',
'year',
'linear_time',
'peak_hours',
'surcharge',
'hour_sin',
'hour_cos',
'minute_sin',
'minute_cos',
'time_type_of_day_sin',
'time_type_of_day_cos',
'month_sin',
'month_cos',
'day_sin',
'day_cos',
'weekday_sin',
'weekday_cos',
'weekofyear_sin',
'weekofyear_cos',
'quarterofyear_sin',
'quarterofyear_cos'
]].values
training_label = train_df.loc[:, 'fare_amount'].values
mean = training_set.mean(axis = 0)
 training_set -= mean
std = training_set.std(axis = 0)
 training_set /= std
max(map(lambda x: len(x.split()), X_train))<define_variables>
num_validation_samples = VALIDATION_SIZE
if num_validation_samples >= len(training_set):
num_validation_samples = int(len(training_set)* 0.2)
 validation_data = training_set[:num_validation_samples]
training_set = training_set[num_validation_samples:]
validation_targets = training_label[:num_validation_samples]
training_label = training_label[num_validation_samples:]
max_words = 40000
max_len = 100
embedding_size = 300<train_model>
NN_INPUT_SIZE = training_set.shape[1]
if preTrainedModel == True:
model = load_model('./GoodModelRepo/0_NYCTFP_Model_8.h5')
 else:
model = models.Sequential()
model.add(layers.Dense(128, kernel_initializer = 'uniform', activation = 'relu', input_shape =(NN_INPUT_SIZE,)))
 model.add(layers.Dense(128, kernel_initializer = 'uniform', activation = 'relu'))
model.add(layers.Dense(64, kernel_initializer = 'uniform', activation = 'relu'))
model.add(layers.Dense(32, kernel_initializer = 'uniform', activation = 'relu'))

 model.add(layers.Dense(1))
model.compile(optimizer = 'rmsprop', loss = 'mse', metrics = ['mae'])
 if loadOnlyWeights == True:
model.load_weights('./GoodModelRepo/0_NYCTFP_Weights_8.h5')
tokenizer = Tokenizer(num_words=max_words)
 tokenizer.fit_on_texts(list(X_train)+ list(X_test))
for token, idx in zip(list(tokenizer.word_index.keys())[:5], list(tokenizer.word_index.values())[:5]):
print(f'tokenized {token} ------> {idx}' )<string_transform>
checkpointer = ModelCheckpoint(filepath = 'NYCTFP_Ref_Model.h5', monitor = 'val_mean_absolute_error', verbose = 1, save_best_only = True, mode = 'min')
 test_history = model.fit(training_set, training_label, shuffle=True,
validation_data =(validation_data, validation_targets),
epochs = EPOCH, batch_size = BATCH_SIZE,
callbacks = [checkpointer])
X_train_tokenized = tokenizer.texts_to_sequences(X_train)
 X_test_tokenized = tokenizer.texts_to_sequences(X_test)
 for text, tokenized in zip(X_train[:5], X_train_tokenized[:5]):
print(f'{text.split() [:5]}...------> {tokenized[:5]}...')
 X_train = X_train_tokenized
X_test = X_test_tokenized<categorify>
model.save('NYCTFP_Model_12.h5')
 model.save_weights('NYCTFP_Weights_12.h5')
 pd.DataFrame(test_history.history ).to_csv("epoch_testing_history.csv", header = True, index = False)
X_train_paddded = pad_sequences(X_train, maxlen=max_len, padding='post')
 X_test_paddded = pad_sequences(X_test, maxlen=max_len, padding='post')
 for train_unpadded, train_padded in zip(X_train[:2], X_train_paddded[:2]):
print(f'{train_unpadded} ------> {len(train_unpadded)} values + {max_len - len(train_unpadded)} zeros')
 X_train = X_train_paddded
X_test = X_test_paddded<feature_engineering>
test_df = pd.read_csv('.. /input/test.csv')
 print(test_df.isnull().sum())
 print('test.csv Old size: %d' % len(test_df))
test_df = test_df.dropna(how = 'any', axis = 'rows')
 print('test.csv New size: %d' % len(test_df))
word_index = tokenizer.word_index
tokenized_words = set(list(word_index.keys())[:max_words])
 embedding_matrix = np.zeros(( len(tokenized_words)+1, embedding_size))
found = 0
with open(EMBEDDING_FILE)as f:
for line in f:
word, coord = line.split(' ', 1)
 if word in tokenized_words:
embedding_matrix[word_index[word]] = np.asarray(coord.split() , dtype='float32')
 found += 1
print(f'found {found} of {len(tokenized_words)} words' )<compute_train_metric>
add_travel_vector_features(test_df)
 parse_and_extract_date_time_components(test_df)
class RocAucCallback(Callback):
def __init__(self, validation_data=() , interval=1):
super(Callback, self ).__init__()
self.interval = interval
self.X_val, self.y_val = validation_data
def on_epoch_end(self, epoch, logs={}):
if epoch % self.interval == 0:
y_pred = self.model.predict(self.X_val, verbose=0)
 score - roc_auc_score(self.y_val, y_pred)
 print(f'
ROC-AUC - epoch {epoch+1} - score: {score}' )<normalization>
test_df['distance_in_miles'] = haversine_np(test_df.dropoff_longitude, test_df.dropoff_latitude, test_df.pickup_longitude, test_df.pickup_latitude)
 test_df['direction'] = calculate_direction(test_df.delta_longitude, test_df.delta_latitude)
 test_df['pickup_to_JFK_distance'] = haversine_np(test_df.pickup_longitude, test_df.pickup_latitude, JFK[0], JFK[1])
 test_df['dropoff_to_JFK_distance'] = haversine_np(test_df.dropoff_longitude, test_df.dropoff_latitude, JFK[0], JFK[1])
 test_df['pickup_to_EWR_distance'] = haversine_np(test_df.pickup_longitude, test_df.pickup_latitude, EWR[0], EWR[1])
 test_df['dropoff_to_EWR_distance'] = haversine_np(test_df.dropoff_longitude, test_df.dropoff_latitude, EWR[0], EWR[1])
 test_df['pickup_to_LGA_distance'] = haversine_np(test_df.pickup_longitude, test_df.pickup_latitude, LGA[0], LGA[1])
 test_df['dropoff_to_LGA_distance'] = haversine_np(test_df.dropoff_longitude, test_df.dropoff_latitude, LGA[0], LGA[1])
 test_df['pickup_to_NYC_distance'] = haversine_np(test_df.pickup_longitude, test_df.pickup_latitude, NYC[0], NYC[1])
 test_df['dropoff_to_NYC_distance'] = haversine_np(test_df.dropoff_longitude, test_df.dropoff_latitude, NYC[0], NYC[1])
 test_df['manhattan'] = minkowski_distance(test_df.pickup_longitude, test_df.dropoff_longitude,
test_df.pickup_latitude, test_df.dropoff_latitude, 1)
 test_df['euclidean'] = minkowski_distance(test_df.pickup_longitude, test_df.dropoff_longitude,
test_df.pickup_latitude, test_df.dropoff_latitude, 2)
 test_df['euclidean_3D'] = minkowski_distance_3D(test_df.pickup_x, test_df.dropoff_x,
test_df.pickup_y, test_df.dropoff_y,
test_df.pickup_z, test_df.dropoff_z, 2)
 test_df['manhattan_3D'] = minkowski_distance_3D(test_df.pickup_x, test_df.dropoff_x,
test_df.pickup_y, test_df.dropoff_y,
test_df.pickup_z, test_df.dropoff_z, 1)

print('Done with Test Data File Feature Generations' )
class GlobalAveragePooling1DMasked(GlobalAveragePooling1D):
def call(self, x, mask=None):
if mask != None:
return K.sum(x, axis=1)/ K.clip(K.tf.cast(K.tf.count_nonzero(x, axis=1), dtype=K.tf.float32), 1, K.int_shape(x)[1])
 else:
return super().call(x )<choose_model_class>
testing_set = test_df.loc[:, [
'passenger_count',
'distance_in_miles',
'pickup_x',
'pickup_y',
'pickup_z',
'dropoff_x',
'dropoff_y',
'dropoff_z',
'pickup_to_JFK_distance',
'dropoff_to_JFK_distance',
'pickup_to_NYC_distance',
'dropoff_to_NYC_distance',
'delta_longitude',
'delta_latitude',
'direction',
'year',
'linear_time',
'peak_hours',
'surcharge',
'hour_sin',
'hour_cos',
'minute_sin',
'minute_cos',
'time_type_of_day_sin',
'time_type_of_day_cos',
'month_sin',
'month_cos',
'day_sin',
'day_cos',
'weekday_sin',
'weekday_cos',
'weekofyear_sin',
'weekofyear_cos',
'quarterofyear_sin',
'quarterofyear_cos'
]].values
testing_set -= mean
testing_set /= std
def get_model() :
inp = Input(shape=(max_len,))
x = Embedding(max_words+1, embedding_size, weights=[embedding_matrix], mask_zero=True )(inp)
 x = SpatialDropout1D(0.2 )(x)
 x = Bidirectional(GRU(80, return_sequences=True))(x)
 avg_pool = GlobalAveragePooling1DMasked()(x)
 max_pool = GlobalMaxPooling1DMasked()(x)
 conc = concatenate([avg_pool, max_pool])
 outp = Dense(6, activation="sigmoid" )(conc)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model
model = get_model()<train_model>
if useBestModel == True:
del model
model = load_model('NYCTFP_Ref_Model.h5')
 predictions = model.predict(testing_set)
 outputKeys = np.reshape(test_df.iloc[:, 0].values,(len(test_df), 1))
outputdata = np.concatenate(( outputKeys, predictions), axis = 1)
 submission = pd.DataFrame(outputdata, columns = ['key', 'fare_amount'])
 submission.to_csv('submission.csv', index = False)
<save_to_csv><EOS>