Kaggle competition: COVID19 Global Forecasting (Week 1)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-1)<init_hyperparams>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
data_dir = Path('.. /input/covid19-global-forecasting-week-1')
 pio.templates.default = 'ggplot2'
params={'learning_rate': 0.01,
'objective':'regression',
'metric':'rmse',
'num_leaves': 31,
'verbose': 1,
'bagging_fraction': 0.9,
'feature_fraction': 0.9,
"random_state":42,
'max_depth': 5,
"bagging_seed" : 42,
"verbosity" : -1,
"bagging_frequency" : 5,
'lambda_l2': 0.5,
'lambda_l1': 0.5,
'min_child_samples': 36
}
xgb_params = {
'objective': 'reg:linear',
'booster': 'gbtree',
'learning_rate': 0.02,
'max_depth': 22,
'min_child_weight': 57,
'gamma' : 1.45,
'alpha': 0.0,
'lambda': 0.0,
'subsample': 0.67,
'colsample_bytree': 0.054,
'colsample_bylevel': 0.50,
'n_jobs': -1,
'random_state': 456
}
cat_param = {
'learning_rate' :0.03,
'depth' :10,
'eval_metric' :'RMSE',
'od_type' :'Iter',
'metric_period ' : 50,
'od_wait' : 20,
'seed' : 42
}<prepare_x_and_y>
folds = get_folds(df=full_data[['totals.pageviews']].reset_index() , n_splits=5)
 oof_reg_preds = np.zeros(full_data.shape[0])
 oof_reg_preds1 = np.zeros(full_data.shape[0])
 oof_reg_preds2 = np.zeros(full_data.shape[0])
 merge_pred = 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(**params,n_estimators=1100)
 xgb = XGBRegressor(**xgb_params, n_estimators=1000)
 cat = CatBoostRegressor(iterations=1000,learning_rate=0.03,
depth=10,
eval_metric='RMSE',
random_seed = 42,
bagging_temperature = 0.2,
od_type='Iter',
metric_period = 50,
od_wait=20)
 print("-"* 20 + "LightGBM Training" + "-"* 20)
 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')
 print("-"* 20 + "XGboost Training" + "-"* 20)
 xgb.fit(trn_x, np.log1p(trn_y),eval_set=[(val_x, np.log1p(val_y)) ],early_stopping_rounds=50,eval_metric='rmse',verbose=100)
 print("-"* 20 + "Catboost Training" + "-"* 20)
 cat.fit(trn_x, np.log1p(trn_y), eval_set=[(val_x, np.log1p(val_y)) ],early_stopping_rounds=50,use_best_model=True,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_reg_preds[val_] = reg.predict(val_x, num_iteration=reg.best_iteration_)
 oof_reg_preds[oof_reg_preds < 0] = 0
lgb_preds = reg.predict(test[train_features], num_iteration=reg.best_iteration_)
 lgb_preds[lgb_preds < 0] = 0
oof_reg_preds1[val_] = xgb.predict(val_x)
 oof_reg_preds1[oof_reg_preds1 < 0] = 0
xgb_preds = xgb.predict(test[train_features])
 xgb_preds[xgb_preds < 0] = 0
oof_reg_preds2[val_] = cat.predict(val_x)
 oof_reg_preds1[oof_reg_preds2 < 0] = 0
cat_preds = cat.predict(test[train_features])
 cat_preds[xgb_preds < 0] = 0
merge_pred[val_] = oof_reg_preds[val_] * 0.6 + oof_reg_preds1[val_] * 0.3 +oof_reg_preds2[val_] * 0.1
sub_reg_preds += np.expm1(_preds)/ len(folds)
 sub_preds +=(lgb_preds / len(folds)) * 0.6 +(xgb_preds / len(folds)) * 0.3 +(cat_preds / len(folds)) * 0.1
print("LGBM Result ", mean_squared_error(np.log1p(trn_user_target['target']), oof_reg_preds)**.5)
 print("XGBoost Result", mean_squared_error(np.log1p(trn_user_target['target']), oof_reg_preds1)**.5)
 print("CatBoost Result", mean_squared_error(np.log1p(trn_user_target['target']), oof_reg_preds2)**.5 )<save_to_csv>
data = pd.read_csv(data_dir/'train.csv')
 data_test = pd.read_csv(data_dir/'test.csv')
sub_full_data['PredictedLogRevenue'] = sub_preds
sub_full_data[['PredictedLogRevenue']].to_csv('new_test.csv', index=True )<set_options>
day_min = pd.to_datetime(data['Date'].min() ,format='%Y-%m-%d')
 t =(pd.DatetimeIndex(data['Date'])- day_min ).days
pd.DatetimeIndex(data['Date'] ).dayofweek
warnings.filterwarnings('ignore' )<set_options>
l_use_date_int = True
l_use_days_num = True
methods = ['use_date_int','use_days_num','use_converter']
c_method = methods[1]
if c_method == methods[0]:
data["Date"] = data["Date"].apply(lambda x: x.replace("-",""))
data["Date"] = data["Date"].astype(int)
 data_test["Date"] = data_test["Date"].apply(lambda x: x.replace("-",""))
data_test["Date"] = data_test["Date"].astype(int)
 features = ['Lat','Long','Date']
elif c_method == methods[1]:
day_min = pd.to_datetime(data['Date'].min() ,format='%Y-%m-%d')
 t =(pd.DatetimeIndex(data['Date'])- day_min ).days
data.loc[:,'days'] = pd.Series(t)
 t =(pd.DatetimeIndex(data_test['Date'])- day_min ).days
data_test.loc[:,'days'] = pd.Series(t)
 features = ['Lat','Long','days']
elif c_method == methods[2]:
t =(pd.DatetimeIndex(data['Date'])- pd.DatetimeIndex([data_test['Date'].min() ])[0] ).days
data['year'] = pd.DatetimeIndex(data['Date'] ).year
data['month'] = pd.DatetimeIndex(data['Date'] ).month
data['day'] = pd.DatetimeIndex(data['Date'] ).day
data_test['year'] = pd.DatetimeIndex(data_test['Date'] ).year
data_test['month'] = pd.DatetimeIndex(data_test['Date'] ).month
data_test['day'] = pd.DatetimeIndex(data_test['Date'] ).day
features = ['Lat','Long','year','month','day']
features += ['dayofweek']
data['dayofweek'] = pd.DatetimeIndex(data['Date'] ).dayofweek
data_test['dayofweek'] = pd.DatetimeIndex(data_test['Date'] ).dayofweek
X = data[features]
X1 = data[features]
y = data.ConfirmedCases
y1 = data.Fatalities
X_test = data_test[features]
X_train, X_valid, y_train, y_valid = train_test_split(X, y,
train_size=0.8, test_size=0.2,
random_state=0)
 X1_train, X1_valid, y1_train, y1_valid = train_test_split(X1, y1,
train_size=0.8, test_size=0.2,
random_state=0)
def seed_everything(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 os.environ['PYTHONHASHSEED'] = str(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed = 42
seed_everything(seed )<feature_engineering>
categorical_cols = [cname for cname in data.columns if
data[cname].nunique() < 10 and
data[cname].dtype == "object"]
numerical_cols = [cname for cname in X.columns if
X[cname].dtype in ['int64', 'float64']]
print(categorical_cols, numerical_cols )
class TweetDataset(torch.utils.data.Dataset):
def __init__(self, df, max_len=96):
self.df = df
self.max_len = max_len
self.labeled = 'selected_text' in df
self.tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file='.. /input/roberta-base/vocab.json',
merges_file='.. /input/roberta-base/merges.txt',
lowercase=True,
add_prefix_space=True)
 def __getitem__(self, index):
data = {}
row = self.df.iloc[index]
ids, masks, tweet, offsets = self.get_input_data(row)
 data['ids'] = ids
data['masks'] = masks
data['tweet'] = tweet
data['offsets'] = offsets
if self.labeled:
start_idx, end_idx = self.get_target_idx(row, tweet, offsets)
 data['start_idx'] = start_idx
data['end_idx'] = end_idx
return data
def __len__(self):
return len(self.df)
 def get_input_data(self, row):
tweet = " " + " ".join(row.text.lower().split())
 encoding = self.tokenizer.encode(tweet)
 sentiment_id = self.tokenizer.encode(row.sentiment ).ids
ids = [0] + sentiment_id + [2, 2] + encoding.ids + [2]
offsets = [(0, 0)] * 4 + encoding.offsets + [(0, 0)]
pad_len = self.max_len - len(ids)
 if pad_len > 0:
ids += [1] * pad_len
offsets += [(0, 0)] * pad_len
ids = torch.tensor(ids)
 masks = torch.where(ids != 1, torch.tensor(1), torch.tensor(0))
offsets = torch.tensor(offsets)
 return ids, masks, tweet, offsets
def get_target_idx(self, row, tweet, offsets):
selected_text = " " + " ".join(row.selected_text.lower().split())
 len_st = len(selected_text)- 1
idx0 = None
idx1 = None
for ind in(i for i, e in enumerate(tweet)if e == selected_text[1]):
if " " + tweet[ind: ind+len_st] == selected_text:
idx0 = ind
idx1 = ind + len_st - 1
break
char_targets = [0] * len(tweet)
 if idx0 != None and idx1 != None:
for ct in range(idx0, idx1 + 1):
char_targets[ct] = 1
target_idx = []
for j,(offset1, offset2)in enumerate(offsets):
if sum(char_targets[offset1: offset2])> 0:
target_idx.append(j)
 start_idx = target_idx[0]
end_idx = target_idx[-1]
return start_idx, end_idx
def get_test_loader(df, batch_size=32):
loader = torch.utils.data.DataLoader(
 TweetDataset(df),
batch_size=batch_size,
shuffle=False,
num_workers=2)
 return loader<load_pretrained>
[cname for cname in data.columns]
for cname in data.columns:
print(cname, data[cname].nunique() < 10, data[cname].dtype == "object" )
class TweetModel(nn.Module):
def __init__(self):
super(TweetModel, self ).__init__()
config = RobertaConfig.from_pretrained(
 '.. /input/roberta-base/config.json', output_hidden_states=True)
 self.roberta = RobertaModel.from_pretrained(
 '.. /input/roberta-base/pytorch_model.bin', config=config)
 self.dropout = nn.Dropout(0.5)
 self.fc = nn.Linear(config.hidden_size, 2)
 nn.init.normal_(self.fc.weight, std=0.02)
 nn.init.normal_(self.fc.bias, 0)
 def forward(self, input_ids, attention_mask):
_, _, hs = self.roberta(input_ids, attention_mask)
 x = torch.stack([hs[-1], hs[-2], hs[-3], hs[-4]])
 x = torch.mean(x, 0)
 x = self.dropout(x)
 x = self.fc(x)
 start_logits, end_logits = x.split(1, dim=-1)
 start_logits = start_logits.squeeze(-1)
 end_logits = end_logits.squeeze(-1)
 return start_logits, end_logits<string_transform>
model0 = RandomForestRegressor(n_estimators=110, random_state=0)
 model1 = RandomForestRegressor(n_estimators=50, random_state=0)
 model0 = DecisionTreeClassifier(criterion='entropy')
 model1 = DecisionTreeClassifier(criterion='entropy')
 model0 = DecisionTreeRegressor(random_state = 0)
 model1 = DecisionTreeRegressor(random_state = 0)
 my_pipeline0 = Pipeline(steps=[ 
('preprocessor', SimpleImputer()), 
('model', model0)
 ])
 my_pipeline1 = Pipeline(steps=[ 
('preprocessor', SimpleImputer()), 
('model', model1)
 ] )
def get_selected_text(text, start_idx, end_idx, offsets):
selected_text = ""
for ix in range(start_idx, end_idx + 1):
selected_text += text[offsets[ix][0]: offsets[ix][1]]
if(ix + 1)< len(offsets)and offsets[ix][1] < offsets[ix + 1][0]:
selected_text += " "
return selected_text<load_from_csv>
scores = -1 * cross_val_score(my_pipeline0, X, y,
cv=5,
scoring='neg_mean_absolute_error')
 print("Average MAE score:", scores.mean() )
%%time
test_df = pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv')
 test_df['text'] = test_df['text'].astype(str)
 test_loader = get_test_loader(test_df)
 predictions = []
models = []
skf = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
 for fold in range(skf.n_splits):
model = TweetModel()
model.cuda()
model.load_state_dict(torch.load(f'.. /input/tweet-sentiment-roberta-pytorch-train/roberta_fold{fold+1}.pth'))
model.eval()
models.append(model)
 for data in test_loader:
ids = data['ids'].cuda()
masks = data['masks'].cuda()
tweet = data['tweet']
offsets = data['offsets'].numpy()
start_logits = []
end_logits = []
for model in models:
with torch.no_grad() :
output = model(ids, masks)
 start_logits.append(torch.softmax(output[0], dim=1 ).cpu().detach().numpy())
 end_logits.append(torch.softmax(output[1], dim=1 ).cpu().detach().numpy())
 start_logits = np.mean(start_logits, axis=0)
 end_logits = np.mean(end_logits, axis=0)
 for i in range(len(ids)) :
start_pred = np.argmax(start_logits[i])
 end_pred = np.argmax(end_logits[i])
 if start_pred > end_pred:
pred = tweet[i]
else:
pred = get_selected_text(tweet[i], start_pred, end_pred, offsets[i])
 predictions.append(pred )<save_to_csv>
scores = -1 * cross_val_score(my_pipeline1, X, y1,
cv=5,
scoring='neg_mean_absolute_error')
 print("Average MAE score:", scores.mean() )
sub_df = pd.read_csv('.. /input/tweet-sentiment-extraction/sample_submission.csv')
 sub_df['selected_text'] = predictions
sub_df['selected_text'] = sub_df['selected_text'].apply(lambda x: x.replace('!!!!', '!')if len(x.split())==1 else x)
 sub_df['selected_text'] = sub_df['selected_text'].apply(lambda x: x.replace('.. ', '.')if len(x.split())==1 else x)
 sub_df['selected_text'] = sub_df['selected_text'].apply(lambda x: x.replace('...', '.')if len(x.split())==1 else x)
 sub_df.to_csv('submission.csv', index=False)
 sub_df.head()<import_modules>
def get_score(yy, n_estimators):

 my_pipeline = Pipeline(steps=[ 
('preprocessor', SimpleImputer()), 
('model',RandomForestRegressor(n_estimators=n_estimators,random_state=0))
])
 score_cross_valids = -1 * cross_val_score(my_pipeline, X, yy,
cv=3,
scoring='neg_mean_absolute_error')
 return score_cross_valids.mean()
import pandas as pd
import numpy as np
import tensorflow as tf
import tensorflow.keras.backend as K
from transformers import *
import tokenizers
import os
from tokenizers import BertWordPieceTokenizer
from transformers import BertTokenizer, TFBertModel, BertConfig
from sklearn.model_selection import StratifiedShuffleSplit
from transformers import BertTokenizer, TFBertForQuestionAnswering
from sklearn.model_selection import train_test_split
import gc
from keras.callbacks import ModelCheckpoint
from sklearn.model_selection import StratifiedKFold
from keras.utils import plot_model 
<define_variables>
my_pipeline0.fit(X, y)
 my_pipeline1.fit(X, y1)
max_len = 168
train_mode = False
tokenizer = tokenizers.ByteLevelBPETokenizer(vocab_file = '.. /input/tf-roberta/vocab-roberta-base.json',
merges_file = '.. /input/tf-roberta/merges-roberta-base.txt',
add_prefix_space = True,
lowercase = True )<load_from_csv>
rf_predictions = my_pipeline0.predict(X_valid)
 rf_val_mae = mean_absolute_error(rf_predictions, y_valid)
 print("Validation MAE for Random Forest Model: {}".format(rf_val_mae))
sentiment_id = {'positive': 1313, 'negative': 2430, 'neutral': 7974}
x_train = pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv' ).fillna('')
 x_train.head()<prepare_x_and_y>
test_preds0 = my_pipeline0.predict(X_test)
 test_preds1 = my_pipeline1.predict(X_test)
 t0 = np.round(test_preds0 ).astype(int)
 t1 = np.round(test_preds1 ).astype(int)
x_train['text'].replace('', np.nan, inplace=True)
 x_train.dropna(subset=['text'], inplace=True)
 x_train.reset_index(drop=True, inplace=True )<define_variables>
output = pd.DataFrame({'ForecastId': data_test.ForecastId,
'ConfirmedCases':t0,
'Fatalities': t1})
 output.to_csv('submission.csv', index=False )
tweet = 'WHY DO WE FALL? SO THAT WE CAN LEARN TO PICK OURSELVES BACK UP.'
selected_text = 'LEARN TO PICK OURSELVES BACK UP'
sentiment = 'positive'
print('text',tweet)
 print('select_text:',selected_text)
 print('sentiment:', sentiment)
 idx0 = None
idx1 = None
st_len = len(selected_text)
 tweet = " " + tweet
for i in range(len(tweet)) :
if(tweet[i:i+st_len]==selected_text):
idx0 = i
idx1 = i + st_len -1
break
char_targets = [0]*len(tweet)
 for i in range(len(tweet)) :
if idx0 != None and idx1!=None:
if i>=idx0 and i<=idx1:
char_targets[i] = 1
print('char_targets:',char_targets)
 tok_tweet = tokenizer.encode(tweet)
 input_ids = tok_tweet.ids
offsets = tok_tweet.offsets
print('input_ids:',input_ids)
 print('offsets:',offsets)
 targets_index = []
for i,(off1,off2)in enumerate(offsets):
if sum(char_targets[off1:off2])>0:
targets_index.append(i)
 target_start = targets_index[0]
target_end = targets_index[-1]
print('target_start:',target_start)
 print('target_end:', target_end)
 ids = [0] + input_ids + [2,2] + [sentiment_id[sentiment]] + [2]
token_type_ids = [0]*(len(ids))
mask = [1] * len(ids)
 target_start+=1
target_end+=1
offsets = [(0,0)]*1 + offsets + [(0,0)]*4
padding_length = max_len - len(ids)
 if padding_length > 0:
ids = ids +([0] * padding_length)
 mask = mask +([0] * padding_length)
 token_type_ids = token_type_ids +([0] * padding_length)
 offsets = offsets +([(0, 0)] * padding_length)
<categorify>
data['log_ConfirmedCases'] = np.log(data['ConfirmedCases']+1)
 data['log_Fatalities'] = np.log(data['Fatalities']+1)
 y_pred2 = data['log_ConfirmedCases']
y1_pred2 = data['log_Fatalities']
day_min = pd.to_datetime(data['Date'].min() ,format='%Y-%m-%d')
 t = data['days'] < data_test['days'].min()
X_train = X[t]
X_valid = X[~t]
y_train = y_pred2[t]
y_valid = y_pred2[~t]
def text_process(tweet, sentiment, tokenizer, max_len, selected_text=None):

 if selected_text!=None:
tweet = " " + tweet
idx0 = None
idx1 = None
st_len = len(selected_text)
 for i in range(len(tweet)) :
if(tweet[i:i+st_len]==selected_text):
idx0 = i
idx1 = i + st_len -1
break
char_targets = [0]*len(tweet)
 for i in range(len(tweet)) :
if idx0 != None and idx1!=None:
if i>=idx0 and i<=idx1:
char_targets[i] = 1
tok_tweet = tokenizer.encode(tweet)
 input_ids = tok_tweet.ids
offsets = tok_tweet.offsets
targets_index = []
for i,(off1,off2)in enumerate(offsets):
if sum(char_targets[off1:off2])>0:
targets_index.append(i)
 target_start = targets_index[0]
target_end = targets_index[-1]
ids = [0] + input_ids + [2,2] + [sentiment_id[sentiment]] + [2]
token_type_ids = [0]*(len(ids))
mask = [1] * len(ids)
 target_start+=1
target_end+=1
offsets = [(0,0)]*1 + offsets + [(0,0)]*4
padding_length = max_len - len(ids)
 if padding_length > 0:
ids = ids +([0] * padding_length)
 mask = mask +([0] * padding_length)
 token_type_ids = token_type_ids +([0] * padding_length)
 offsets = offsets +([(0, 0)] * padding_length)
 return {
'ids': ids,
'token_type_ids':token_type_ids,
'mask':mask,
'target_start':target_start,
'target_end':target_end,
'offsets':offsets
}
else:
tweet = " " + tweet
tok_tweet = tokenizer.encode(tweet)
 input_ids = tok_tweet.ids
offsets = tok_tweet.offsets
ids = [0] + input_ids + [2,2] + [sentiment_id[sentiment]] + [2]
token_type_ids = [0]*(len(ids))
mask = [1] * len(ids)
 offsets = [(0,0)]*1 + offsets + [(0,0)]*4
padding_length = max_len - len(ids)
 if padding_length > 0:
ids = ids +([0] * padding_length)
 mask = mask +([0] * padding_length)
 token_type_ids = token_type_ids +([0] * padding_length)
 offsets = offsets +([(0, 0)] * padding_length)
 return {
'ids': ids,
'token_type_ids':token_type_ids,
'mask':mask,
'offsets':offsets
} 
<categorify>
model0 = RandomForestRegressor(n_estimators=300, random_state=0,verbose=True)
 model1 = RandomForestRegressor(n_estimators=100, random_state=0)
 my_pipeline0 = Pipeline(steps=[ 
('preprocessor', SimpleImputer()), 
('model', model0)
 ])
 my_pipeline1 = Pipeline(steps=[ 
('preprocessor', SimpleImputer()), 
('model', model1)
 ])
rows = x_train.shape[0]
train_ids = np.zeros(( rows,max_len), dtype = 'int32')
 train_mask = np.zeros(( rows,max_len), dtype = 'int32')
 train_type_ids = np.zeros(( rows,max_len), dtype = 'int32')
 train_start_idx = np.zeros(( rows,max_len), dtype = 'int32')
 train_end_idx = np.zeros(( rows,max_len), dtype = 'int32')
 for i in range(x_train.shape[0]):
encoding = text_process(x_train.loc[i,'text'], x_train.loc[i,'sentiment'], tokenizer, max_len,x_train.loc[i,'selected_text'])
 train_ids[i] = encoding['ids']
train_start_idx[i,encoding['target_start']] = 1
train_end_idx[i, encoding['target_end']] = 1
train_type_ids[i] = encoding['token_type_ids']
train_mask[i] = encoding['mask']<categorify>
my_pipeline0.fit(X_train, y_train )
i = 10
encoding = text_process(x_train.loc[i,'text'], x_train.loc[i,'sentiment'], tokenizer, max_len,x_train.loc[i,'selected_text'])
 train_ids[i] = encoding['ids']
train_start_idx[i,encoding['target_start']] = 1
train_end_idx[i, encoding['target_end']] = 1
train_type_ids[i] = encoding['token_type_ids']
train_mask[i] = encoding['mask']
tweet = x_train.loc[i,'text']
select_text = x_train.loc[i,'selected_text']
target_start = np.argmax(train_start_idx[i,])
 target_end = np.argmax(train_end_idx[i, ])
 offsets = encoding['offsets']
print('tweet:',tweet)
 print('selected_text:',select_text)
 print(tweet[offsets[target_start][0]:offsets[target_end][-1]] )<string_transform>
rf_predictions = my_pipeline0.predict(X_valid)
 rf_val_mae = mean_absolute_error(rf_predictions, y_valid)
 print("Validation MAE for Random Forest Model: {}".format(rf_val_mae))
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))<load_pretrained>
def get_score1(n_est):
model = RandomForestRegressor(n_estimators=n_est, random_state=0)
 my_pipeline = Pipeline(steps=[ 
('preprocessor', SimpleImputer()), 
('model', model)
 ])
 score_cross_valids = -1 * cross_val_score(my_pipeline0, X_train, y_train,
cv=5,
scoring='neg_mean_absolute_error')
 return score_cross_valids.mean()
config = RobertaConfig.from_pretrained('.. /input/tf-roberta/config-roberta-base.json')
 print(config )<load_pretrained>
my_pipeline0.fit(X, y_pred2)
 my_pipeline1.fit(X, y1_pred2)
def build_model() :
ids = tf.keras.layers.Input(( max_len,), dtype=tf.int32)
 att = tf.keras.layers.Input(( max_len,), dtype=tf.int32)
 tok = tf.keras.layers.Input(( max_len,), dtype=tf.int32)
 roberta = TFRobertaForQuestionAnswering.from_pretrained('.. /input/tf-roberta/pretrained-roberta-base.h5', config = config)
 x = roberta(ids, attention_mask = att, token_type_ids = tok)
 x1 = tf.keras.layers.Dropout(0.3 )(x[0])
 x1 = tf.keras.layers.Activation('softmax' )(x1)
 x2 = tf.keras.layers.Dropout(0.3 )(x[1])
 x2 = tf.keras.layers.Activation('softmax' )(x2)
 model = tf.keras.models.Model(inputs = [ids, att, tok], outputs=[x1, x2])
 optimizer = tf.keras.optimizers.Adam(learning_rate = 3e-5)
 model.compile(loss= 'categorical_crossentropy', optimizer=optimizer)
 return model<compute_train_metric>
test_preds2 = my_pipeline0.predict(X_test)
 test_preds3 = my_pipeline1.predict(X_test)
 t0 = np.round(np.exp(test_preds2)-1 ).astype(int)
 t1 = np.round(np.exp(test_preds3)-1 ).astype(int)
def lr_scheduler(epoch):
return 3e-5 * 0.2**epoch<set_options>
output = pd.DataFrame({'ForecastId': data_test.ForecastId,
'ConfirmedCases':t0,
'Fatalities': t1})
 output.to_csv('submission.csv', index=False )