Kaggle competition: Tweet Sentiment Extraction
cols = ['home_ownership','purpose','title','addr_state','earliest_cr_line','initial_list_status','zip_code']
target = 'loan_condition'
X_temp = pd.concat([X_train, y_train], axis=1)
 for col in cols:
summary = X_temp.groupby([col])[target].mean()
X_test[col] = X_test[col].map(summary)
 skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 enc_train = Series(np.zeros(len(X_train)) , index=X_train.index)
 for i,(train_ix, val_ix)in enumerate(( skf.split(X_train, y_train))):
X_train_, _ = X_temp.iloc[train_ix], y_train.iloc[train_ix]
X_val, _ = X_temp.iloc[val_ix], y_train.iloc[val_ix]
summary = X_train_.groupby([col])[target].mean()
enc_train.iloc[val_ix] = X_val[col].map(summary)
 X_train[col] = enc_train<prepare_x_and_y>
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.layers import Dense,Input,Embedding
from tensorflow.keras.models import Model
from tensorflow.keras.models import load_model
import keras.backend as K
from transformers import *
import tokenizers
from tokenizers import ByteLevelBPETokenizer
import os
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import LabelEncoder
y_pred_test = np.zeros(len(X_test))
y_pred = np.zeros(len(X_test))<categorify>
train = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/train.csv', keep_default_na=False)
 test = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/test.csv', keep_default_na=False )
TXT_train = X_train.emp_title.copy()
TXT_test = X_test.emp_title.copy()
TXT_train = TXT_train.str.lower()
TXT_test = TXT_test.str.lower()
X_train = X_train.drop('emp_title',axis=1)
 X_test = X_test.drop('emp_title',axis=1)
 tfidf = TfidfVectorizer(max_features=1000, use_idf=True)
 TXT_train_enc = tfidf.fit_transform(TXT_train)
 TXT_test_enc = tfidf.transform(TXT_test)
 X_train = sp.sparse.hstack([X_train.values, TXT_train_enc])
 X_test = sp.sparse.hstack([X_test.values, TXT_test_enc])
 X_train = X_train.tocsr()
X_test = X_test.tocsr()<train_model>
Max_length = 120
scores = []
skf = StratifiedKFold(n_splits=5, random_state=71, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=9434, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=94, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=34, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=35, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=77, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=101, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=373, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=365, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=1000, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=2, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=3, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=4, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=5, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=6, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=7, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=8, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=9, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=10, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
skf = StratifiedKFold(n_splits=5, random_state=11, shuffle=True)
 for i,(train_ix, test_ix)in enumerate(tqdm(skf.split(X_train, y_train))):
X_train_, y_train_ = X_train[train_ix,:], y_train.iloc[train_ix]
X_val, y_val = X_train[test_ix,:], y_train.iloc[test_ix]
clf = lgb.LGBMClassifier(colsample_bytree=0.7000000000000001, learning_rate=0.05,
max_depth=10, subsample=0.9929417385040324,n_estimators=9999)
 clf.fit(X_train_, y_train_, early_stopping_rounds=200, eval_metric='auc', eval_set=[(X_val, y_val)])
 y_pred = clf.predict_proba(X_val)[:,1]
scores.append(roc_auc_score(y_val, y_pred))
y_pred_test += clf.predict_proba(X_test)[:,1]
scores = np.array(scores)
 print('Ave.CV score is %f' % scores.mean())
 y_pred_test /= 100<save_to_csv>
def list_in(a, b):
y_seq = np.zeros(( len(b)) ,dtype='int32')
 for i in range(0,len(b)- len(a)+ 1):
if b[i:i + len(a)] == a:
y_seq[i:i+len(a)] = 1
if b[i-1] == " ":
y_seq[i-1] = 1
break
return y_seq
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))
submission = pd.read_csv('.. /input/homework-for-students4plus/sample_submission.csv', index_col=0)
 submission.loan_condition = y_pred_test
submission.to_csv('submission.csv' )<import_modules>
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file = "/kaggle/input/roberta-base/vocab-roberta-base.json" ,
merges_file = "/kaggle/input/roberta-base/merges-roberta-base.txt",
lowercase=True,
add_prefix_space=True )
<train_model>
Max_length = 120
obs = train.shape[0]
input_tokens = np.ones(( obs,Max_length),dtype='int32')
 mask = np.zeros(( obs,Max_length),dtype='int32')
 seg = np.zeros(( obs,Max_length),dtype='int32')
 st_vec = np.zeros(( obs,Max_length),dtype='int32')
 end_vec = np.zeros(( obs,Max_length),dtype='int32')
 for i in range(0,obs):
overlap_char = list_in(" ".join(train.selected_text.values[i].split())," "+" ".join(train.text.values[i].split()))
tweet_ex = tokenizer.encode(" "+" ".join(train.text.values[i].split()))
tweet_ex_ids = tweet_ex.ids
input_tokens[i,0:len(tweet_ex_ids)+5] = [0]+tweet_ex_ids+[2,2]+tokenizer.encode(train.sentiment.values[i] ).ids + [2]
mask[i,0:len(tweet_ex_ids)+5] = 1
tweet_ex_off = tweet_ex.offsets
selected_text_tweet = tokenizer.encode(" "+" ".join(train.selected_text.values[i].split()))
tokens = []
for j,(a,b)in enumerate(tweet_ex_off):
if sum(overlap_char[a:b])> 0:
tokens.append(j)
 if len(tokens)>0:
st_vec[i,tokens[0]+1] = 1
end_vec[i,tokens[-1]+1] = 1
<import_modules>
obs1 = test.shape[0]
input_tokens_test = np.ones(( obs1,Max_length),dtype='int32')
 mask_test = np.zeros(( obs1,Max_length),dtype='int32')
 seg_test = np.zeros(( obs1,Max_length),dtype='int32')
 for i in range(0,obs1):
tweet_ex_test = tokenizer.encode(" "+" ".join(test.text.values[i].split()))
tweet_ex_ids_test = tweet_ex_test.ids
input_tokens_test[i,0:len(tweet_ex_ids_test)+5] = [0]+tweet_ex_ids_test+[2,2]+tokenizer.encode(test.sentiment.values[i] ).ids + [2]
mask_test[i,0:len(tweet_ex_ids_test)+5] = 1
import plotly.graph_objects as go
import matplotlib.pyplot as plt
from tqdm import tqdm
import time
from datetime import datetime
from pathlib import Path
from sklearn import preprocessing
import keras.backend as K
from keras.models import Sequential
from keras.layers import Dense, LSTM, RNN, Dropout
from keras.callbacks import EarlyStopping
from keras import optimizers
from sklearn.preprocessing import StandardScaler, MinMaxScaler<load_from_csv>
def Create_Model() :
token_inputs = Input(( Max_length,), dtype=tf.int32, name='token_inputs')
 mask_inputs = Input(( Max_length,), dtype=tf.int32, name='mask_inputs')
 seg_inputs = Input(( Max_length,), dtype=tf.int32, name='seg_inputs')
 config = BertConfig.from_pretrained("/kaggle/input/roberta-base/config-roberta-base.json")
 model = TFRobertaModel.from_pretrained("/kaggle/input/roberta-base/pretrained-roberta-base.h5",config=config)
 pool_output,seq_out = model([token_inputs, mask_inputs, seg_inputs])
 x1 = tf.keras.layers.Dropout(0.1 )(pool_output)
 x1 = tf.keras.layers.Conv1D(128, 2,padding='same' )(x1)
 x1 = tf.keras.layers.LeakyReLU()(x1)
 x1 = tf.keras.layers.Conv1D(64,2,padding='same' )(x1)
 x1 = tf.keras.layers.Dense(1 )(x1)
 x1 = tf.keras.layers.Flatten()(x1)
 x1 = tf.keras.layers.Activation('softmax' )(x1)
 x2 = tf.keras.layers.Dropout(0.1 )(pool_output)
 x2 = tf.keras.layers.Conv1D(128, 2, padding='same',activation='relu' )(x2)
 x2 = tf.keras.layers.LeakyReLU(0.1 )(x2)
 x2 = tf.keras.layers.Conv1D(64,2,padding='same',activation='relu' )(x2)
 x2 = tf.keras.layers.Dense(1 )(x2)
 x2 = tf.keras.layers.Flatten()(x2)
 x2 = tf.keras.layers.Activation('softmax' )(x2)
 bert_model1 = Model([token_inputs, mask_inputs, seg_inputs],[x1,x2])
 return bert_model1
train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/train.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/test.csv")
 train.tail()<define_variables>
def prediction_fn(tokens_test,mask_test,seg_test,sentiment,text):
tokens_tst = []
mask_tst = []
seg_tst = []
txt = []
for i in range(0,len(tokens_test)) :
if sentiment[i] != "neutral":
tokens_tst.append(tokens_test[i])
 mask_tst.append(mask_test[i])
 seg_tst.append(seg_test[i])
 txt.append(text[i])
 tokens_tst = np.asarray(tokens_tst, dtype=np.int32)
 mask_tst = np.asarray(mask_tst, dtype=np.int32)
 seg_tst = np.asarray(seg_tst, dtype=np.int32)
 prediction = bert_model1.predict([tokens_tst,mask_tst,seg_tst],verbose=1)
 prediction1 =[]
for i in range(0,len(prediction[0])) :
a = np.argmax(prediction[0][i])
 b = np.argmax(prediction[1][i])
 if b>=a:
text1 = " "+" ".join(txt[i].split())
 enc = tokenizer.encode(text1)
 predict1 = tokenizer.decode(enc.ids[a-1:b])
 else:
predict1 = txt[i]
prediction1.append(predict1)
 return {'prediction_string':prediction1,
'prediction':prediction}
mask = train['Date'].max()
world_cum_confirmed = sum(train[train['Date'] == mask].ConfirmedCases)
 world_cum_fatal = sum(train[train['Date'] == mask].Fatalities )<train_model>
def evaluation(prediction_string,test_text,sentiment,text):
flag = 0
total_prediction_string = []
for i in range(0,len(test_text)) :
if sentiment[i] == 'neutral':
total_prediction_string.append(text[i])
 if sentiment[i] != 'neutral':
total_prediction_string.append(prediction_string[flag])
 flag+=1
jaccard_metric = []
for i in range(0,len(total_prediction_string)) :
jaccard_metric.append(jaccard(total_prediction_string[i],test_text[i]))
return sum(jaccard_metric)/len(jaccard_metric )
print('Number of Countires are: ', len(train['Country_Region'].unique()))
print('Training dataset ends at: ', mask)
 print('Number of cumulative confirmed cases worldwide are: ', world_cum_confirmed)
 print('Number of cumulative fatal cases worldwide are: ', world_cum_fatal )<sort_values>
models_path = '/kaggle/input/roberta-model-v16/'
length = len(test[test.sentiment!='neutral'].sentiment)
 pred0 = np.zeros(( length,Max_length))
pred1 = np.zeros(( length,Max_length))
for i in range(0,5):
bert_model1 = Create_Model()
print('loading model weights')
 bert_model1.load_weights(models_path + 'v16-roberta-%i.h5'%(i))
print('making predictions')
 predictions = prediction_fn(input_tokens_test,mask_test,seg_test,test.sentiment.values,test.text.values)["prediction"]
prediction_string = prediction_fn(input_tokens_test,mask_test,seg_test,test.sentiment.values,test.text.values)["prediction_string"]
pred0 += predictions[0]/5
pred1 += predictions[1]/5
cum_per_country = train[train['Date'] == mask].groupby(['Date','Country_Region'] ).sum().sort_values(['ConfirmedCases'], ascending=False)
 cum_per_country[:10]<groupby>
prediction1 = []
flag = 0
for i in range(0,len(test.text)) :
if test.sentiment.values[i] != 'neutral':
a = np.argmax(pred0[flag])
 b = np.argmax(pred1[flag])
 flag += 1
if b>=a:
predict1 = tokenizer.decode(input_tokens_test[i][a:b+1])
 else:
predict1 = test.text[i]
prediction1.append(predict1)
 if test.sentiment.values[i] == 'neutral':
prediction1.append(test.text.iloc[i] )
date = train['Date'].unique()
cc_us = train[train['Country_Region'] == 'US'].groupby(['Date'] ).sum().ConfirmedCases
ft_us = train[train['Country_Region'] == 'US'].groupby(['Date'] ).sum().Fatalities
cc_ity = train[train['Country_Region'] == 'Italy'].groupby(['Date'] ).sum().ConfirmedCases
ft_ity = train[train['Country_Region'] == 'Italy'].groupby(['Date'] ).sum().Fatalities
cc_spn = train[train['Country_Region'] == 'Spain'].groupby(['Date'] ).sum().ConfirmedCases
ft_spn = train[train['Country_Region'] == 'Spain'].groupby(['Date'] ).sum().Fatalities
cc_gmn = train[train['Country_Region'] == 'Germany'].groupby(['Date'] ).sum().ConfirmedCases
ft_gmn = train[train['Country_Region'] == 'Germany'].groupby(['Date'] ).sum().Fatalities
cc_frc = train[train['Country_Region'] == 'France'].groupby(['Date'] ).sum().ConfirmedCases
ft_frc = train[train['Country_Region'] == 'France'].groupby(['Date'] ).sum().Fatalities
fig = go.Figure()
fig.add_trace(go.Scatter(x=date, y=cc_us, name='US'))
fig.add_trace(go.Scatter(x=date, y=cc_ity, name='Italy'))
fig.add_trace(go.Scatter(x=date, y=cc_spn, name='Spain'))
fig.add_trace(go.Scatter(x=date, y=cc_gmn, name='Germany'))
fig.add_trace(go.Scatter(x=date, y=cc_frc, name='France'))
fig.update_layout(title="Plot of Cumulative Cases for Top 5 countires(except China)",
xaxis_title="Date",
yaxis_title="Cases")
 fig.update_xaxes(nticks=30)
 fig.show()<data_type_conversions>
submission = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/sample_submission.csv' )
train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'])
 train['Country_Region'] = train['Country_Region'].astype(str)
 test['Country_Region'] = test['Country_Region'].astype(str)
<feature_engineering>
submission.selected_text = prediction1
<categorify><EOS> 
submission.to_csv("submission.csv",index=False )