Kaggle competition: New York City Taxi Fare Prediction
def jaccard(str1, str2):
a = set(str1.lower().split())
 b = set(str2.lower().split())
 if(len(a)==0)&(len(b)==0): return 0.5
c = a.intersection(b)
 return float(len(c)) /(len(a)+ len(b)- len(c))<define_variables>
warnings.filterwarnings("ignore")
 def preparedataset(datasetname):
datasetname['pickup_year']=0
datasetname['pickup_month']=0
datasetname['pickup_day']=0
datasetname['pickup_hour']=0
datasetname['dis'] =0
datasetname['x_dis']=0
datasetname['y_dis']=0
datasetname.head()
for k in range(len(datasetname.index)) :
datetime=dt.strptime(datasetname['pickup_datetime'][k].replace("UTC",''),"%Y-%m-%d %H:%M:%S ")
 datasetname['pickup_year'][k]=datetime.year
datasetname['pickup_month'][k]=datetime.month
datasetname['pickup_day'][k]=datetime.day
datasetname['pickup_hour'][k]=datetime.hour
datasetname['x_dis'] =(datasetname['dropoff_longitude'] - datasetname['pickup_longitude'])
 datasetname['y_dis'] =(datasetname['dropoff_latitude'] - datasetname['pickup_latitude'])
 datasetname['dis'] =(( datasetname['dropoff_longitude'] - datasetname['pickup_longitude'])**2 +(datasetname['dropoff_latitude']-datasetname['pickup_latitude'])**2)**.5
datasetname=datasetname.drop(['pickup_datetime'],axis=1)
 datasetname=datasetname.drop(['pickup_longitude'],axis=1)
 datasetname=datasetname.drop(['dropoff_latitude'],axis=1)
 datasetname=datasetname.drop(['dropoff_longitude'],axis=1)
 datasetname=datasetname.drop(['pickup_latitude'],axis=1)
 return datasetname
jac = []; VER='v0'; DISPLAY=1
oof_start = np.zeros(( input_ids.shape[0],MAX_LEN))
oof_end = np.zeros(( input_ids.shape[0],MAX_LEN))
preds_start = np.zeros(( input_ids_t.shape[0],MAX_LEN))
preds_end = np.zeros(( input_ids_t.shape[0],MAX_LEN))
skf = StratifiedKFold(n_splits=5,shuffle=True,random_state=777)
 for fold,(idxT,idxV)in enumerate(skf.split(input_ids,train.sentiment.values)) :
print('
print('
print('
K.clear_session()
model = build_model()
sv = tf.keras.callbacks.ModelCheckpoint(
 '%s-roberta-%i.h5'%(VER,fold), monitor='val_loss', verbose=1, save_best_only=True,
save_weights_only=True, mode='auto', save_freq='epoch')
 model.fit([input_ids[idxT,], attention_mask[idxT,], token_type_ids[idxT,]], [start_tokens[idxT,], end_tokens[idxT,]],
epochs=3, batch_size=32, verbose=DISPLAY, callbacks=[sv],
validation_data=([input_ids[idxV,],attention_mask[idxV,],token_type_ids[idxV,]],
[start_tokens[idxV,], end_tokens[idxV,]]))
print('Loading model...')
 model.load_weights('%s-roberta-%i.h5'%(VER,fold))
print('Predicting OOF...')
 oof_start[idxV,],oof_end[idxV,] = model.predict([input_ids[idxV,],attention_mask[idxV,],token_type_ids[idxV,]],verbose=DISPLAY)
 print('Predicting Test...')
 preds = model.predict([input_ids_t,attention_mask_t,token_type_ids_t],verbose=DISPLAY)
 preds_start += preds[0]/skf.n_splits
preds_end += preds[1]/skf.n_splits
all = []
for k in idxV:
a = np.argmax(oof_start[k,])
 b = np.argmax(oof_end[k,])
 if a>b:
st = train.loc[k,'text']
else:
text1 = " "+" ".join(train.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-1:b])
 all.append(jaccard(st,train.loc[k,'selected_text']))
jac.append(np.mean(all))
print('>>>> FOLD %i Jaccard ='%(fold+1),np.mean(all))
print()<feature_engineering>
df=preparedataset(dataset_train)
 df.head(50)
print('>>>> OVERALL 5Fold CV Jaccard =',np.mean(jac))<string_transform>
df.to_csv('train_preprocessed.csv' )
all = []
for k in range(input_ids_t.shape[0]):
a = np.argmax(preds_start[k,])
 b = np.argmax(preds_end[k,])
 if a>b:
st = test.loc[k,'text']
else:
text1 = " "+" ".join(test.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-1:b])
 all.append(st )<save_to_csv>
test_df=preparedataset(dataset_test)
 test_df.head(50 )
test['selected_text'] = all
test[['textID','selected_text']].to_csv('submission.csv',index=False)
 pd.set_option('max_colwidth', 60)
 test.sample(25 )<count_values>
test_df.to_csv('test_preprocessed.csv' )
print('Number of submissions above: ' + str(len(lbs)) )<import_modules>
y = df.fare_amount
X=df.drop('fare_amount',axis=1)
 X_train, X_valid, y_train, y_valid = train_test_split(X, y )
print('TF version',tf.__version__ )<define_variables>
warnings.filterwarnings("ignore")
 result={}
best_istemator=0
best_learing_rate=0
best_mae=100000000000
for lr in [X / 100 for X in range(10,50, 5)]:
for ns in range(200,701,50):
my_model = XGBRegressor(n_estimators=ns, learning_rate=lr,n_jobs=4)
 my_model.fit(X_train, y_train)
 predictions = my_model.predict(X_valid)
 mae=mean_absolute_error(predictions, y_valid)
 if(mae < best_mae):
best_mae=mae
best_istemator=ns
best_learing_rate=lr
print("better found")
 print(ns , lr, mae)
 result[(ns,lr)]=mae
my_model_2 = XGBRegressor(n_estimators=best_istemator, learning_rate=best_learing_rate, n_jobs=4)
 my_model_2.fit(X_train,y_train)
 predictions_2 = my_model_2.predict(X_valid)
 mae_2 = mean_absolute_error(y_valid, predictions_2)
 print("best_istemator:" , best_istemator)
 print("best_learing_rate:" , best_learing_rate)
 print("Mean Absolute Error:" , mae_2)
MAX_LEN = 96
PATH = '.. /input/tf-roberta/'
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file=PATH+'vocab-roberta-base.json',
merges_file=PATH+'merges-roberta-base.txt',
lowercase=True,
add_prefix_space=True 
)
 EPOCHS = 3
BATCH_SIZE = 32
PAD_ID = 1
SEED = 88888
LABEL_SMOOTHING = 0.1
tf.random.set_seed(SEED)
 np.random.seed(SEED)
 sentiment_id = {'positive': 1313, 'negative': 2430, 'neutral': 7974}
train = pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv' ).fillna('')
 train.head()<define_variables>
my_model_2 = XGBRegressor(n_estimators=700, learning_rate=0.2, n_jobs=4)
 my_model_2.fit(X_train,y_train)
 test_preds = my_model_2.predict(test_df)
 output = pd.DataFrame({'key': test_df.index,
'fare_amount': test_preds})
 output.to_csv('submission.csv', index=False )