Kaggle competition: Google Analytics Customer Revenue Prediction
<SOS> metric: RMSE Kaggle data source: google-analytics-customer-revenue-prediction<string_transform>

 pass
def simple_fix(a):
a = str(a)
 a = a.replace("n`****", "n`t it")
 a = a.replace("`", "'")
 a = a.replace('"', "'")
 a = a.replace("Â", "_")
 a = a.replace("ï¿½", "_'_")
 a = ' '.join(str(a ).split())
 return a<create_dataframe>
warnings.simplefilter('error', SettingWithCopyWarning)
 gc.enable()
%matplotlib inline
final_output = []
MAX_LEN = 92
test_dataset = TweetDataset(
 tweet=df_test.text.values,
sentiment=df_test.sentiment.values,
selected_text=df_test.selected_text.values 
)
 data_loader = torch.utils.data.DataLoader(
 test_dataset,
shuffle=False,
batch_size=VALID_BATCH_SIZE,
num_workers=1 
)
 with torch.no_grad() :
tk0 = tqdm(data_loader, total=len(data_loader))
for bi, d in enumerate(tk0):
ids = d["ids"]
token_type_ids = d["token_type_ids"]
mask = d["mask"]
sentiment = d["sentiment"]
orig_selected = d["orig_selected"]
orig_tweet = d["orig_tweet"]
targets_start = d["targets_start"]
targets_end = d["targets_end"]
offsets = d["offsets"].numpy()
ids = ids.to(device, dtype=torch.long)
 token_type_ids = token_type_ids.to(device, dtype=torch.long)
 mask = mask.to(device, dtype=torch.long)
 targets_start = targets_start.to(device, dtype=torch.long)
 targets_end = targets_end.to(device, dtype=torch.long)
 outputs_start, outputs_end = model5(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids 
)
 outputs_start = torch.softmax(outputs_start, dim=1 ).cpu().detach().numpy()
outputs_end = torch.softmax(outputs_end, dim=1 ).cpu().detach().numpy()
jaccard_scores = []
for px, tweet in enumerate(orig_tweet):
selected_tweet = orig_selected[px]
tweet_sentiment = sentiment[px]
_, output_sentence = calculate_jaccard_score(
 original_tweet=tweet,
target_string=selected_tweet,
sentiment_val=tweet_sentiment,
idx_start=np.argmax(outputs_start[px, :]),
idx_end=np.argmax(outputs_end[px, :]),
offsets=offsets[px] 
)
 final_output.append(output_sentence )<feature_engineering>
train = pd.read_csv('.. /input/create-extracted-json-fields-dataset/extracted_fields_train.gz',
dtype={'date': str, 'fullVisitorId': str, 'sessionId':str}, nrows=None)
 test = pd.read_csv('.. /input/create-extracted-json-fields-dataset/extracted_fields_test.gz',
dtype={'date': str, 'fullVisitorId': str, 'sessionId':str}, nrows=None)
 train.shape, test.shape
df_test.loc[:, 'raw_output_n'] = final_output<feature_engineering>
y_reg = train['totals.transactionRevenue'].fillna(0)
 del train['totals.transactionRevenue']
if 'totals.transactionRevenue' in test.columns:
del test['totals.transactionRevenue']
def unfix(orig, text, raw_output):
orig = ' '.join(str(orig ).split())
 text = ' '.join(str(text ).split())
 raw_output = ' '.join(str(raw_output ).split())
 pos = text.find(raw_output)
 if pos >= 0:
return orig[pos:pos + len(str(raw_output)) ]
else:
return raw_output
df_test['selected_text_1'] = df_test.apply(lambda r: unfix(r.original_text, r.text, r.raw_output), axis=1)
 df_test['selected_text_2'] = df_test.apply(lambda r: unfix(r.original_text, r.text, r.raw_output_n), axis=1)
 def get_text(senti, s1, s2):
if senti == 'neutral':
return s2
else:
return s1
df_test['selected_text'] = df_test.apply(lambda r: get_text(r.sentiment, r.selected_text_1, r.selected_text_2), axis=1)
 df_test.head(5 )<save_to_csv>
train['target'] = y_reg
for df in [train, test]:
df['vis_date'] = pd.to_datetime(df['visitStartTime'], unit='s')
 df['sess_date_dow'] = df['vis_date'].dt.dayofweek
df['sess_date_hours'] = df['vis_date'].dt.hour
df['sess_date_dom'] = df['vis_date'].dt.day
df.sort_values(['fullVisitorId', 'vis_date'], ascending=True, inplace=True)
 df['next_session_1'] =(
 df['vis_date'] - df[['fullVisitorId', 'vis_date']].groupby('fullVisitorId')['vis_date'].shift(1)
 ).astype(np.int64)// 1e9 // 60 // 60
df['next_session_2'] =(
 df['vis_date'] - df[['fullVisitorId', 'vis_date']].groupby('fullVisitorId')['vis_date'].shift(-1)
 ).astype(np.int64)// 1e9 // 60 // 60
df['nb_pageviews'] = df['date'].map(
 df[['date', 'totals.pageviews']].groupby('date')['totals.pageviews'].sum() 
)
 df['ratio_pageviews'] = df['totals.pageviews'] / df['nb_pageviews']
y_reg = train['target']
del train['target']
sample = pd.read_csv(".. /input/tweet-sentiment-extraction/sample_submission.csv")
 sample['selected_text'] = df_test.selected_text.values
sample.to_csv("submission.csv", index=False )<define_variables>
excluded_features = [
'date', 'fullVisitorId', 'sessionId', 'totals.transactionRevenue',
'visitId', 'visitStartTime', 'vis_date', 'nb_sessions', 'max_visits'
]
categorical_features = [
_f for _f in train.columns
if(_f not in excluded_features)&(train[_f].dtype == 'object')
 ]
%%time
!python.. /input/tweet-inference-scripts/inference_distilbert.py<define_variables>
for f in categorical_features:
train[f], indexer = pd.factorize(train[f])
 test[f] = indexer.get_indexer(test[f] )
%%time
!python.. /input/tweet-inference-scripts/inference_bert_wwm.py<define_variables>
folds = get_folds(df=train, n_splits=5)
 train_features = [_f for _f in train.columns if _f not in excluded_features]
print(train_features)
 importances = pd.DataFrame()
oof_reg_preds = np.zeros(train.shape[0])
 sub_reg_preds = np.zeros(test.shape[0])
 for fold_,(trn_, val_)in enumerate(folds):
trn_x, trn_y = train[train_features].iloc[trn_], y_reg.iloc[trn_]
val_x, val_y = train[train_features].iloc[val_], y_reg.iloc[val_]
reg = lgb.LGBMRegressor(
 num_leaves=31,
learning_rate=0.03,
n_estimators=1000,
subsample=.9,
colsample_bytree=.9,
random_state=1 
)
 reg.fit(
 trn_x, np.log1p(trn_y),
eval_set=[(val_x, np.log1p(val_y)) ],
early_stopping_rounds=50,
verbose=100,
eval_metric='rmse' 
)
 imp_df = pd.DataFrame()
imp_df['feature'] = train_features
imp_df['gain'] = reg.booster_.feature_importance(importance_type='gain')
 imp_df['fold'] = fold_ + 1
importances = pd.concat([importances, imp_df], axis=0, sort=False)
 oof_reg_preds[val_] = reg.predict(val_x, num_iteration=reg.best_iteration_)
 oof_reg_preds[oof_reg_preds < 0] = 0
_preds = reg.predict(test[train_features], num_iteration=reg.best_iteration_)
 _preds[_preds < 0] = 0
sub_reg_preds += np.expm1(_preds)/ len(folds)
 mean_squared_error(np.log1p(y_reg), oof_reg_preds)**.5
%%time
!python.. /input/tweet-inference-scripts/inference_albert.py<install_modules>
train['predictions'] = np.expm1(oof_reg_preds)
 test['predictions'] = sub_reg_preds
%%time
!pip install /kaggle/input/bertweet-libs/sacrebleu-1.4.10-py3-none-any.whl
!cp -R /kaggle/input/bertweet-libs/fairseq-0.9.0/fairseq-0.9.0 /kaggle/working
!cp -R /kaggle/input/bertweet-libs/fastBPE-0.1.0/fastBPE-0.1.0/ /kaggle/working
!pip install /kaggle/working/fairseq-0.9.0/
!pip install /kaggle/working/fastBPE-0.1.0/
!python.. /input/tweet-inference-scripts/inference_bertweet.py<define_variables>
trn_data = train[train_features + ['fullVisitorId']].groupby('fullVisitorId' ).mean()
tmp = train[train_features + ['fullVisitorId']].groupby('fullVisitorId' ).std()
tmp.columns = ['_' + name for name in tmp.columns]
trn_data = pd.concat(( trn_data,tmp), axis=1, sort=False )
%%time
!python.. /input/tweet-inference-scripts/inference_roberta_anton.py<load_pretrained>
%%time
trn_pred_list = train[['fullVisitorId', 'predictions']].groupby('fullVisitorId')\ 
.apply(lambda df: list(df.predictions)) \ 
.apply(lambda x: {'pred_'+str(i): pred for i, pred in enumerate(x)} )
%%time
!python.. /input/tweet-inference-scripts/inference_roberta_large_hiki.py<define_variables>
trn_all_predictions = pd.DataFrame(list(trn_pred_list.values), index=trn_data.index)
 trn_feats = trn_all_predictions.columns
trn_all_predictions['t_mean'] = np.log1p(trn_all_predictions[trn_feats].mean(axis=1))
trn_all_predictions['t_max'] = np.log1p(trn_all_predictions[trn_feats].max(axis=1))
trn_all_predictions['t_min'] = np.log1p(trn_all_predictions[trn_feats].min(axis=1))
trn_all_predictions['t_std'] = np.log1p(trn_all_predictions[trn_feats].std(axis=1))
trn_all_predictions['t_median'] = np.log1p(trn_all_predictions[trn_feats].median(axis=1))
trn_all_predictions['t_sum_log'] = np.log1p(trn_all_predictions[trn_feats] ).sum(axis=1)
 trn_all_predictions['t_sum_act'] = np.log1p(trn_all_predictions[trn_feats].fillna(0 ).sum(axis=1))
trn_all_predictions['t_nb_sess'] = trn_all_predictions[trn_feats].isnull().sum(axis=1)
 full_data = pd.concat([trn_data, trn_all_predictions], axis=1)
 del trn_data, trn_all_predictions
gc.collect()
full_data.shape
%%time
!python.. /input/tweet-inference-scripts/inference_roberta_hiki.py<categorify>
%%time
sub_pred_list = test[['fullVisitorId', 'predictions']].groupby('fullVisitorId')\ 
.apply(lambda df: list(df.predictions)) \ 
.apply(lambda x: {'pred_'+str(i): pred for i, pred in enumerate(x)} )
def string_from_preds_char_level(texts, preds):
selected_texts = []
n_models = len(preds)
 for idx in range(len(texts)) :
data = texts[idx]
start_probas = np.mean(
 [preds[i][0][idx] for i in range(n_models)], 0)
 end_probas = np.mean(
 [preds[i][1][idx] for i in range(n_models)], 0)
 start_idx = np.argmax(start_probas)
 end_idx = np.argmax(end_probas)
 if end_idx < start_idx:
selected_text = data
else:
selected_text = data[start_idx: end_idx]
selected_texts.append(selected_text.strip())
 return selected_texts
df_test = pd.read_csv(
 '.. /input/tweet-sentiment-extraction/test.csv' ).fillna('')
 df_test['selected_text'] = ''
sub = pd.read_csv(
 '.. /input/tweet-sentiment-extraction/sample_submission.csv')
 preds_1 = np.load('preds_char_test_cnn_2_2.npy')
 preds_2 = np.load('preds_char_test_wavenet_4_3.npy')
 preds_3 = np.load('preds_char_test_rnn_5_3.npy')
 preds_4 = np.load('preds_char_test_wavenet_0_3.npy')
 test_preds =(preds_1 + preds_2 + preds_3 + preds_4)/ 4
selected_texts = string_from_preds_char_level(
 df_test['text'].values, test_preds)
 sub['selected_text'] = selected_texts
df_test['selected_text'] = selected_texts
sub.to_csv('submission.csv', index=False )<import_modules>
sub_data = test[train_features + ['fullVisitorId']].groupby('fullVisitorId' ).mean()
tmp = test[train_features + ['fullVisitorId']].groupby('fullVisitorId' ).std()
tmp.columns = ['_' + name for name in tmp.columns]
sub_data = pd.concat(( sub_data,tmp), axis=1, sort=False)
 sub_all_predictions = pd.DataFrame(list(sub_pred_list.values), index=sub_data.index)
 for f in trn_feats:
if f not in sub_all_predictions.columns:
sub_all_predictions[f] = np.nan
sub_all_predictions['t_mean'] = np.log1p(sub_all_predictions[trn_feats].mean(axis=1))
sub_all_predictions['t_median'] = np.log1p(sub_all_predictions[trn_feats].median(axis=1))
sub_all_predictions['t_sum_log'] = np.log1p(sub_all_predictions[trn_feats] ).sum(axis=1)
 sub_all_predictions['t_min'] = np.log1p(sub_all_predictions[trn_feats].min(axis=1))
sub_all_predictions['t_max'] = np.log1p(sub_all_predictions[trn_feats].max(axis=1))
sub_all_predictions['t_std'] = np.log1p(sub_all_predictions[trn_feats].std(axis=1))
sub_all_predictions['t_sum_act'] = np.log1p(sub_all_predictions[trn_feats].fillna(0 ).sum(axis=1))
sub_all_predictions['t_nb_sess'] = sub_all_predictions[trn_feats].isnull().sum(axis=1)
 sub_full_data = pd.concat([sub_data, sub_all_predictions], axis=1)
 del sub_data, sub_all_predictions
gc.collect()
sub_full_data.shape
import pandas as pd
import os
import re<define_variables>
trn_cat = pd.read_csv('.. /input/mode-of-ga/train_cat_mode.csv', dtype={'fullVisitorId': 'str'}, index_col='fullVisitorId')
 test_cat = pd.read_csv('.. /input/mode-of-ga/test_cat_mode.csv', dtype={'fullVisitorId': 'str'}, index_col='fullVisitorId' )
data_path = '/kaggle/input/tweet-sentiment-extraction'
config_path = '/kaggle/input/submission'<load_from_csv>
trn_cat = trn_cat.astype('category')
 test_cat = test_cat.astype('category' )
train_dataframe = pd.read_csv(os.path.join(data_path, 'train.csv'))
test_dataframe = pd.read_csv(os.path.join(data_path, 'test.csv'))<define_variables>
full_data = pd.concat(( full_data, trn_cat), axis=1, sort=False )
rob_distil_tozer = RobertaTokenizerFast(vocab_file=os.path.join(config_path, 'roberta-base-vocab.json'),
merges_file=os.path.join(config_path, 'roberta-base-merges.txt'),
add_prefix_space=True )<categorify>
sub_full_data = pd.concat(( sub_full_data, test_cat), axis=1, sort=False )
def robdist_tokenize(tweet, tozer = None, offset_map=True):
if not isinstance(tweet, str):
raise TypeError
if tozer is not None and not isinstance(tozer, RobertaTokenizerFast):
raise TypeError
temp = tozer.encode_plus(tweet, return_offsets_mapping=offset_map, add_special_tokens=True)if tozer is not None else rob_distil_tozer.encode_plus(tweet, return_offsets_mapping=offset_map, add_special_tokens=True)
 return {'input_ids': torch.tensor(temp['input_ids']),
'att_mask': torch.tensor(temp['attention_mask']),
'offsets': temp['offset_mapping']}
def robdist_detokenize(token_ids, tozer = None, skip_special_tok=True):
if not isinstance(token_ids, torch.LongTensor)and not isinstance(token_ids, torch.cuda.LongTensor):
raise TypeError('First argument is not type tensor.long')
 if tozer is not None and isinstance(tozer, RobertaTokenizerFast):
raise TypeError('Second argument is not type TokenizerFast')
 return tozer.decode(token_ids.tolist() , skip_special_tokens=skip_special_tok)[1:] if tozer is not None else rob_distil_tozer.decode(token_ids.tolist() , skip_special_tokens=skip_special_tok)[1:]<categorify>
train['target'] = y_reg
trn_user_target = train[['fullVisitorId', 'target']].groupby('fullVisitorId' ).sum()
def offset_paddings(offset: list, maxtweetlen, prefix_space=3):
try:
assert offset[0] == offset[-1]
except AssertionError:
raise AssertionError("Offsets already padded or None type at tails!")
 offset[0] =(offset[1][0], offset[1][0])
 while offset[-1] is None or offset[-1] ==(0, 0):
del offset[-1]
offset = offset + [(offset[-1][-1],offset[-1][-1])for i in range(maxtweetlen - len(offset)- prefix_space)]
offset = [(0,0)for i in range(prefix_space)] + offset
return torch.tensor(offset )<categorify>
folds = get_folds(df=full_data[['totals.pageviews']].reset_index() , n_splits=5)
 oof_preds = np.zeros(full_data.shape[0])
 sub_preds = np.zeros(sub_full_data.shape[0])
 vis_importances = pd.DataFrame()
for fold_,(trn_, val_)in enumerate(folds):
trn_x, trn_y = full_data.iloc[trn_], trn_user_target['target'].iloc[trn_]
val_x, val_y = full_data.iloc[val_], trn_user_target['target'].iloc[val_]
reg = lgb.LGBMRegressor(
 num_leaves=31,
learning_rate=0.03,
n_estimators=1000,
subsample=.9,
colsample_bytree=.9,
random_state=1 
)
 reg.fit(
 trn_x, np.log1p(trn_y),
eval_set=[(trn_x, np.log1p(trn_y)) ,(val_x, np.log1p(val_y)) ],
eval_names=['TRAIN', 'VALID'],
early_stopping_rounds=50,
eval_metric='rmse',
verbose=100 
)
 imp_df = pd.DataFrame()
imp_df['feature'] = trn_x.columns
imp_df['gain'] = reg.booster_.feature_importance(importance_type='gain')
 imp_df['fold'] = fold_ + 1
vis_importances = pd.concat([vis_importances, imp_df], axis=0, sort=False)
 oof_preds[val_] = reg.predict(val_x, num_iteration=reg.best_iteration_)
 oof_preds[oof_preds < 0] = 0
_preds = reg.predict(sub_full_data[full_data.columns], num_iteration=reg.best_iteration_)
 _preds[_preds < 0] = 0
sub_preds += _preds / len(folds)
 mean_squared_error(np.log1p(trn_user_target['target']), oof_preds)**.5
def sentiment_padding(tweet, sent_st, max_tweet_len):
tweet_dump = " " + str(' '.join(tweet.split()))
sent_st_dump = " " + str(' '.join(sent_st.split()))
st_tensor = torch.zeros(max_tweet_len, dtype=torch.long)
 st_len = len(sent_st_dump)
 idx_start = None
idx_end = None
for idx in(idx for idx, letter in enumerate(tweet_dump[1:])if letter==sent_st_dump[1]):
if tweet_dump[idx + 1:idx + st_len] == sent_st_dump[1:]:
idx_start = idx + 1
idx_end = idx + st_len
st_tensor[idx_start: idx_end + 1] = st_tensor[idx_start: idx_end + 1] + 1
break
try:
assert(idx_start != None and idx_end != None)
 assert(st_len != 0)
 except AssertionError:
print('sentiment_padding : Either improper selected sentiment support or sentiment support doesnt have length!')
 return -1
return [idx_start], [idx_end], st_tensor<define_variables>
sub_full_data['PredictedLogRevenue'] = sub_preds
sub_full_data[['PredictedLogRevenue']].to_csv('test_with_cat_mode.csv', index=True )