Kaggle competition: StumbleUpon Evergreen Classification Challenge
<SOS> metric: AUC Kaggle data source: stumbleupon-evergreen-classification-challenge<filter>
%matplotlib inline
pred_df.loc[pred_df["country"] == "Israel"]<filter>
df_train=pd.read_csv('/kaggle/input/stumbleupon/train.tsv',sep='\t')
 df_test=pd.read_csv('/kaggle/input/stumbleupon/test.tsv',sep='\t',usecols=['urlid','boilerplate'] )
pred_df.loc[pred_df["country"] == "Argentina"]<filter>
df_train['boilerplate'].replace(to_replace=r'"title":', value="",inplace=True,regex=True)
 df_train['boilerplate'].replace(to_replace=r'"url":',value="",inplace=True,regex=True)
 df_train['boilerplate'].replace(to_replace=r'{|}',value="",inplace=True,regex=True)
 df_train['boilerplate']=df_train['boilerplate'].str.lower()
df_test['boilerplate'].replace(to_replace=r'"title":', value="",inplace=True,regex=True)
 df_test['boilerplate'].replace(to_replace=r'"url":',value="",inplace=True,regex=True)
 df_test['boilerplate'].replace(to_replace=r'{|}',value="",inplace=True,regex=True)
 df_test['boilerplate']=df_test['boilerplate'].str.lower()
pred_df.loc[pred_df["country"] == "Uruguay"]<merge>
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
 bert=TFAutoModel.from_pretrained('bert-base-uncased' )
test = pd.merge(test, pred_df[["state", "country", "Date", "ConfirmedCases", "Fatalities"]], on=["state", "country", "Date"], how="left" )<save_to_csv>
SEQ_length=512
Xids=np.zeros(( df_train.shape[0],SEQ_length))
Xmask=np.zeros(( df_train.shape[0],SEQ_length))
y=np.zeros(( df_train.shape[0],1))
Xids_test=np.zeros(( df_test.shape[0],SEQ_length))
Xmask_test=np.zeros(( df_test.shape[0],SEQ_length))
Xids
test[["ForecastId", "ConfirmedCases", "Fatalities"]].to_csv("submission.csv", index=False )<import_modules>
for i,sequence in enumerate(df_train['boilerplate']):
tokens=tokenizer.encode_plus(sequence,max_length=SEQ_length,padding='max_length',add_special_tokens=True,
truncation=True,return_token_type_ids=False,return_attention_mask=True,
return_tensors='tf')
 Xids[i,:],Xmask[i,:],y[i,0]=tokens['input_ids'],tokens['attention_mask'],df_train.loc[i,'label']
for i,sequence in enumerate(df_test['boilerplate']):
tokens=tokenizer.encode_plus(sequence,max_length=SEQ_length,padding='max_length',add_special_tokens=True,
truncation=True,return_token_type_ids=False,return_attention_mask=True,
return_tensors='tf')
 Xids_test[i,:],Xmask_test[i,:]=tokens['input_ids'],tokens['attention_mask']
from sklearn.tree import DecisionTreeRegressor
from sklearn import preprocessing<load_from_csv>
tf.config.get_visible_devices()
df = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/train.csv')
 df.head()<categorify>
dataset=tf.data.Dataset.from_tensor_slices(( Xids,Xmask,y))
def map_func(input_ids,mask,labels):
return {'input_ids':input_ids,'attention_mask':mask},labels
dataset=dataset.map(map_func)
 dataset=dataset.shuffle(100000 ).batch(64 ).prefetch(1000)
 DS_size=len(list(dataset))
train=dataset.take(round(DS_size*0.90))
val=dataset.skip(round(DS_size*0.90))
df['Date'] = pd.to_datetime(df['Date'])
 df['Date'] = pd.to_timedelta(df.Date ).dt.total_seconds().astype(int)
 df = df.replace(np.nan, '', regex=True)
 le_cr = preprocessing.LabelEncoder()
le_ps = preprocessing.LabelEncoder()
le_cr.fit(df['Country_Region'])
 le_ps.fit(df['Province_State'])
 df['Country_Region'] = le_cr.transform(df['Country_Region'])
 df['Province_State'] = le_ps.transform(df['Province_State'] )<train_model>
dataset_test=tf.data.Dataset.from_tensor_slices(( Xids_test,Xmask_test))
def map_func(input_ids,mask):
return {'input_ids':input_ids,'attention_mask':mask}
dataset_test=dataset_test.map(map_func)
 dataset_test=dataset_test.batch(64 ).prefetch(1000 )
regressor_cc = DecisionTreeRegressor(random_state = 0, max_features="sqrt")
 regressor_f = DecisionTreeRegressor(random_state = 0, max_features="sqrt")
 regressor_cc.fit(df[['Country_Region','Province_State','Date']], df[['ConfirmedCases']])
 regressor_f.fit(df[['Country_Region','Province_State','Date']], df[['Fatalities']] )<load_from_csv>
distil_bert = 'distilbert-base-uncased'
config = DistilBertConfig(dropout=0.4, attention_dropout=0.4)
 config.output_hidden_states = False
transformer_model = TFDistilBertModel.from_pretrained(distil_bert, config = config)
 input_ids_in = tf.keras.layers.Input(shape=(SEQ_length,), name='input_ids', dtype='int32')
 input_masks_in = tf.keras.layers.Input(shape=(SEQ_length,), name='attention_mask', dtype='int32')
 embedding_layer = transformer_model(input_ids_in, attention_mask=input_masks_in)[0]
X = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(100, return_sequences=True, dropout=0.4, recurrent_dropout=0.4))(embedding_layer)
 X = tf.keras.layers.GlobalMaxPool1D()(X)
 X = tf.keras.layers.Dense(64, activation='relu' )(X)
 X = tf.keras.layers.Dropout(0.3 )(X)
 X = tf.keras.layers.Dense(32, activation='relu' )(X)
 X = tf.keras.layers.Dropout(0.3 )(X)
 X = tf.keras.layers.Dense(1, activation='sigmoid' )(X)
 model = tf.keras.Model(inputs=[input_ids_in, input_masks_in], outputs = X)
 for layer in model.layers[:3]:
layer.trainable = False
df_test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/test.csv' )<data_type_conversions>
model.compile(loss=tf.keras.losses.BinaryCrossentropy() ,
optimizer='adam',metrics=[tf.keras.metrics.AUC() ,tf.keras.metrics.Precision() ,tf.keras.metrics.Recall()
] )
df_test['Date'] = pd.to_datetime(df_test['Date'])
 df_test['Date'] = pd.to_timedelta(df_test.Date ).dt.total_seconds().astype(int)
 df_test = df_test.replace(np.nan, '', regex=True)
 df_test['Country_Region'] = le_cr.transform(df_test['Country_Region'])
 df_test['Province_State'] = le_ps.transform(df_test['Province_State'] )<predict_on_test>
history=model.fit(train,validation_data=val,epochs=10 )
df_s = pd.DataFrame()
df_s['ForecastId'] = df_test['ForecastId']
df_s['ConfirmedCases'] = regressor_cc.predict(df_test[['Country_Region','Province_State','Date']])
 df_s['Fatalities'] = regressor_f.predict(df_test[['Country_Region','Province_State','Date']] )<save_to_csv>
predictions=model.predict(dataset_test)
 df_test['label']=predictions
df_test.to_csv('submission.csv',columns=['urlid','label'],index=False )
df_s.to_csv('submission.csv', index=False )<import_modules>
input_x=tf.data.Dataset.from_tensor_slices(( Xids,Xmask,y))
def map_func(input_ids,mask,labels):
return {'input_ids':input_ids,'attention_mask':mask}
input_x=input_x.map(map_func)
 input_x=input_x.shuffle(100000 ).batch(32 ).prefetch(1000)
 y_true = y
y_true
import numpy as np
import pandas as pd
import math<load_from_csv>
y_pred=model.predict(dataset)
 y_pred
y_pred = np.round(y_pred)
 y_pred
print(metrics.classification_report(y_true, y_pred))