Kaggle competition: New York City Taxi Trip Duration
bot.load_model(bot.best_performers[0][1] )<predict_on_test>
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )
def predict(loader, *, return_y=False):
model.eval()
outputs, y_global = [], []
with torch.set_grad_enabled(False):
for input_tensors in loader:
input_tensors = [x.to(model.device)for x in input_tensors if x is not None]
outputs.append(bot.predict_batch(input_tensors ).cpu())
 outputs = torch.cat(outputs, dim=0)
 return outputs<predict_on_test>
df_sample = pd.read_csv('.. /input/sample_submission.csv')
 df_sample.head()
preds = predict(test_loader )<import_modules>
len(df_train.index)== df_train.index.nunique()
from torch.autograd import Variable 
<feature_engineering>
len(df_test.index)== df_test.index.nunique()
def exx(row):
if(row.A > row.B and row.A > row.NEITHER):
row.A = 1
row.B = 0
row.NEITHER = 0
elif(row.B > row.A and row.B > row.NEITHER):
row.A = 0
row.B = 1
row.NEITHER = 0
else:
row.A = 0
row.B = 0
row.NEITHER = 1
return row<save_to_csv>
df_train.isnull().values.any()
df_sub = pd.DataFrame(torch.softmax(preds, -1 ).cpu().numpy().clip(1e-3, 1-1e-3), columns=["A", "B", "NEITHER"])
 df_sub["ID"] = df_test.ID
df_sub.to_csv("submission.csv", index=False)
 df_sub.head()<import_modules>
df_test.isnull().values.any()
import os
import gc
import numpy as np
import pandas as pd
from tqdm import tqdm
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import log_loss<load_from_url>
df_train['pickup_datetime'] = pd.to_datetime(df_train['pickup_datetime'])
 df_train['dropoff_datetime'] = pd.to_datetime(df_train['dropoff_datetime'])
 df_test['pickup_datetime'] = pd.to_datetime(df_test['pickup_datetime'] )
!wget -q https://raw.githubusercontent.com/google-research-datasets/gap-coreference/master/gap-development.tsv
!wget -q https://raw.githubusercontent.com/google-research-datasets/gap-coreference/master/gap-validation.tsv
!wget -q https://raw.githubusercontent.com/google-research-datasets/gap-coreference/master/gap-test.tsv
!wget -q https://raw.githubusercontent.com/google-research/bert/master/modeling.py
!wget -q https://raw.githubusercontent.com/google-research/bert/master/extract_features.py
!wget -q https://raw.githubusercontent.com/google-research/bert/master/tokenization.py<import_modules>
df_train.loc[:, 'pickup_month'] = df_train['pickup_datetime'].dt.month
df_train.loc[:, 'pickup_weekday'] = df_train['pickup_datetime'].dt.weekday
df_train.loc[:, 'pickup_day'] = df_train['pickup_datetime'].dt.day
df_train.loc[:, 'pickup_hour'] = df_train['pickup_datetime'].dt.hour
df_train.loc[:, 'pickup_minute'] = df_train['pickup_datetime'].dt.minute
df_train.loc[:, 'dropoff_month'] = df_train['dropoff_datetime'].dt.month
df_train.loc[:, 'dropoff_weekday'] = df_train['dropoff_datetime'].dt.weekday
df_train.loc[:, 'dropoff_day'] = df_train['dropoff_datetime'].dt.day
df_train.loc[:, 'dropoff_hour'] = df_train['dropoff_datetime'].dt.hour
df_train.loc[:, 'dropoff_minute'] = df_train['dropoff_datetime'].dt.minute
df_test.loc[:, 'pickup_month'] = df_test['pickup_datetime'].dt.month
df_test.loc[:, 'pickup_weekday'] = df_test['pickup_datetime'].dt.weekday
df_test.loc[:, 'pickup_day'] = df_test['pickup_datetime'].dt.day
df_test.loc[:, 'pickup_hour'] = df_test['pickup_datetime'].dt.hour
df_test.loc[:, 'pickup_minute'] = df_test['pickup_datetime'].dt.minute
import modeling
import extract_features
import tokenization<load_from_csv>
def calculate_distance(longitude1, latitude1, longitude2, latitude2):
R = 6373.0
lat1 = radians(latitude1)
 lon1 = radians(longitude1)
 lat2 = radians(latitude2)
 lon2 = radians(longitude2)
 dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1)* cos(lat2)* sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
return R * c
def add_distance(df):
return df.apply(lambda row: calculate_distance(
 row.pickup_longitude,
row.pickup_latitude,
row.dropoff_longitude,
row.dropoff_latitude 
), axis=1)
 df_train['distance_km'] = add_distance(df_train)
 df_test['distance_km'] = add_distance(df_test)
 df_train.head()
val_df = pd.read_table('gap-validation.tsv', index_col='ID' ).reset_index(drop=True)
 test_df = pd.read_table('gap-validation.tsv', index_col='ID' ).reset_index(drop=True)
 dev_df = pd.read_table('gap-development.tsv', index_col='ID' ).reset_index(drop=True )<load_from_zip>
le = LabelEncoder()
le.fit(df_train['store_and_fwd_flag'])
 df_train['store_and_fwd_flag'] = le.transform(df_train['store_and_fwd_flag'])
 df_test['store_and_fwd_flag'] = le.transform(df_test['store_and_fwd_flag'])
 le.fit(df_train['vendor_id'])
 df_train['vendor_id'] = le.transform(df_train['vendor_id'])
 df_test['vendor_id'] = le.transform(df_test['vendor_id'] )
!wget -q https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip
!unzip uncased_L-12_H-768_A-12.zip<define_variables>
train_variables = ["vendor_id","passenger_count","store_and_fwd_flag","pickup_longitude","pickup_latitude","dropoff_longitude","dropoff_latitude","pickup_month","pickup_weekday","pickup_day","pickup_hour",'pickup_minute',"distance_km"]
def count_char(text, offset):
count = 0
for pos in range(offset):
if text[pos] != " ": count +=1
return count
def candidate_length(candidate):
count = 0
for i in range(len(candidate)) :
if candidate[i] != " ": count += 1
return count
def count_token_length_special(token):
count = 0
special_token = ["
for i in range(len(token)) :
if token[i] not in special_token: count+=1
return count
def embed_by_bert(df, path_to_bert='uncased_L-12_H-768_A-12', embed_size=768, batch_size=8,
layers='-1', max_seq_length=256):
text = df['Text']
text.to_csv('input.txt', index=False, header=False)
 os.system(f"python3 extract_features.py \
--input_file=input.txt \
--output_file=output.jsonl \
--vocab_file={path_to_bert}/vocab.txt \
--bert_config_file={path_to_bert}/bert_config.json \
--init_checkpoint={path_to_bert}/bert_model.ckpt \
--layers={layers} \
--max_seq_length={max_seq_length} \
--batch_size={batch_size}")
 bert_output = pd.read_json("output.jsonl", lines=True)
 bert_output.head()
os.system("rm input.txt")
 os.system("rm output.jsonl")
 index = df.index
columns = ["emb_A", "emb_B", "emb_P", "label"]
emb = pd.DataFrame(index = index, columns = columns)
 emb.index.name = "ID"
for i in tqdm(range(len(text))):
features = bert_output.loc[i, "features"]
P_char_start = count_char(df.loc[i, 'Text'], df.loc[i, 'Pronoun-offset'])
 A_char_start = count_char(df.loc[i, 'Text'], df.loc[i, 'A-offset'])
 B_char_start = count_char(df.loc[i, 'Text'], df.loc[i, 'B-offset'])
 A_length = candidate_length(df.loc[i, 'A'])
 B_length = candidate_length(df.loc[i, 'B'])
 emb_A, emb_B, emb_P = np.zeros(embed_size), np.zeros(embed_size), np.zeros(embed_size)
 char_count, cnt_A, cnt_B = 0, 0, 0
for j in range(2, len(features)) :
token = features[j]["token"]
token_length = count_token_length_special(token)
 if char_count == P_char_start:
emb_P += np.asarray(features[j]["layers"][0]['values'])
 if char_count in range(A_char_start, A_char_start + A_length):
emb_A += np.asarray(features[j]["layers"][0]['values'])
 cnt_A += 1
if char_count in range(B_char_start, B_char_start + B_length):
emb_B += np.asarray(features[j]["layers"][0]['values'])
 cnt_B += 1
char_count += token_length
if cnt_A > 0:
emb_A /= cnt_A
if cnt_B > 0:
emb_B /= cnt_B
label = "Neither"
if(df.loc[i,"A-coref"] == True):
label = "A"
if(df.loc[i,"B-coref"] == True):
label = "B"
emb.iloc[i] = [emb_A, emb_B, emb_P, label]
return emb<categorify>
y = np.log1p(df_train['trip_duration'])
 df_train["trip_duration"]
X = df_train[train_variables]
X.shape, y.shape
%%time
val_bert_emb = embed_by_bert(val_df)
 test_bert_emb = embed_by_bert(test_df)
 dev_bert_emb = embed_by_bert(dev_df )<data_type_conversions>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1337)
 X_train.shape, y_train.shape, X_test.shape, y_test.shape
val_bert_emb["emb_A"].head().map(np.asarray ).values[0].astype('float' ).shape<categorify>
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, random_state=1337)
 X_train.shape, y_train.shape, X_valid.shape, y_valid.shape
def featurize(embedding_df):
pronoun_embs, a_embs, b_embs, labels = [], [], [], []
for i in tqdm(range(len(embedding_df))):
pronoun_embs.append(embedding_df.loc[i, "emb_P"])
 a_embs.append(embedding_df.loc[i, "emb_A"])
 b_embs.append(embedding_df.loc[i, "emb_B"])
 label_map = {'A': 0, 'B': 1, 'Neither': 2}
labels.append(label_map[embedding_df.loc[i, "label"]])
 a_embs = np.asarray(a_embs ).astype('float')
 b_embs = np.asarray(b_embs ).astype('float')
 pronoun_embs = np.asarray(pronoun_embs ).astype('float')
 return np.concatenate([a_embs, b_embs, pronoun_embs], axis=1), np.asarray(labels )<prepare_x_and_y>
rf = RandomForestRegressor(random_state=1337,n_jobs=-1,bootstrap=True,n_estimators=20)
 fit = rf.fit(X_train, y_train )
X_train, y_train = featurize(pd.concat([val_bert_emb, dev_bert_emb] ).sort_index().reset_index() )<choose_model_class>
score = rf.score(X_valid, y_valid)
 print(score )
logit = LogisticRegression(C=1e-2, random_state=17, solver='lbfgs',
multi_class='multinomial', max_iter=100,
n_jobs=4 )<train_model>
test_columns = X_train.columns
predictions = rf.predict(df_test[test_columns] )
%%time
logit.fit(X_train, y_train )<set_options>
submission = pd.DataFrame({'id': df_test.id, 'trip_duration': np.expm1(predictions)})
 submission.head()
<load_from_csv><EOS> 
submission.to_csv("submission_florian_coupechoux_nyc_taxi.csv", index=False)