Kaggle competition: COVID19 Global Forecasting (Week 4)
<choose_model_class>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import nltk
from sklearn.preprocessing import LabelBinarizer,LabelEncoder,StandardScaler,MinMaxScaler
from sklearn.linear_model import LogisticRegression,SGDClassifier,LinearRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from sklearn.model_selection import train_test_split
import keras
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from keras.models import Sequential
from keras.layers import Dense,LSTM
import tensorflow as tf
<choose_model_class>
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )
<choose_model_class>
train_df.isna().sum()
def make_model(embedding_matrix, embed_size=300, loss='binary_crossentropy'):
inp = Input(shape=(max_len,))
embed= Embedding(length_to_keep, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 spatial_drop = SpatialDropout1D(0.3 )(embed)
 lstm1=(Bidirectional(CuDNNLSTM(256,return_sequences=True)) )(spatial_drop)
 lstm2=(Bidirectional(CuDNNGRU(256,return_sequences=True)) )(lstm1)
 maxpool1 = GlobalMaxPooling1D()(lstm1)
 maxpool2 = GlobalMaxPooling1D()(lstm2)
 conc = Concatenate(axis=1 )([maxpool1,maxpool2])
 dense1 = Dense(128,activation='relu' )(conc)
 output=Dense(1,activation='sigmoid' )(dense1)
 model = Model(inputs=inp, outputs=output)
 model.compile(loss=loss, optimizer=Adam(lr=0.001), metrics=['accuracy', f1])
 return model<choose_model_class>
test_df.isna().sum()
early_stopping = EarlyStopping(patience=5, verbose=1, monitor='val_loss', mode='min')
 checkpoints_treated = ModelCheckpoint('treated_weights.hdf5', monitor="val_loss", mode="min", verbose=1, save_best_only=True)
 reduce_lr_treated = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=2, verbose=1, min_lr=0.00001, mode='min' )<define_variables>
train_df['Country_Region'] = train_df['Country_Region'] + ' ' + train_df['Province_State']
test_df['Country_Region'] = test_df['Country_Region'] + ' ' + test_df['Province_State']
del train_df['Province_State']
del test_df['Province_State']
epochs = 4
batch_size = 128 
<define_variables>
def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
train_df.Date = train_df.Date.apply(split_date)
 test_df.Date = test_df.Date.apply(split_date )
a=3<train_model>
year = []
month = []
day = []
for i in train_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )
history = model_treated.fit(X_treated, y, batch_size=batch_size,
epochs=epochs,
validation_data=[X_t_chk, y_chk_treated],
verbose=2, callbacks=[checkpoints_treated, reduce_lr_treated, early_stopping] )<predict_on_test>
train_df['Year'] = year
train_df['Month'] = month
train_df['Day'] = day
del train_df['Date']
y_hat = model_treated.predict(X_test_treated,verbose=True )<find_best_params>
year = []
month = []
day = []
for i in test_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )
pred_val_y = model_treated.predict([X_t_chk], batch_size=1024, verbose=1)
 thresholds = []
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 res = f1_score(y_chk_treated,(pred_val_y > thresh ).astype(int))
thresholds.append([thresh, res])
 print("F1 score at threshold {0} is {1}".format(thresh, res))
thresholds.sort(key=lambda x: x[1], reverse=True)
 best_thresh_1 = thresholds[0][0]
print("Best threshold: ", best_thresh_1)
<load_from_csv>
test_df['Year'] = year
test_df['Month'] = month
test_df['Day'] = day
del test_df['Date']
del train_df['Id']
del test_df['ForecastId']
submission = pd.read_csv('.. /input/quora-insincere-questions-classification/sample_submission.csv', encoding='utf-8' )<save_to_csv>
del train_df['Year']
del test_df['Year']
y_pred =(y_hat > best_thresh_1 ).astype(int)
 submission['prediction'] = y_pred
submission.to_csv('submission.csv', index=False )<import_modules>
train_df['ConfirmedCases'] = train_df['ConfirmedCases'].apply(int)
 train_df['Fatalities'] = train_df['Fatalities'].apply(int )
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt<load_from_csv>
cases = train_df.ConfirmedCases
fatalities = train_df.Fatalities
del train_df['ConfirmedCases']
del train_df['Fatalities']
train = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv" )<data_type_conversions>
lb = LabelEncoder()
train_df['Country_Region'] = lb.fit_transform(train_df['Country_Region'])
 test_df['Country_Region'] = lb.transform(test_df['Country_Region'] )
train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'] )<data_type_conversions>
scaler = MinMaxScaler()
x_train = scaler.fit_transform(train_df.values)
 x_test = scaler.transform(test_df.values )
train = train[train['Date']<='2020-04-14']
train['Location'] = train['Province_State'].astype(str)+ train['Country_Region'].astype(str)
 test['Location'] = test['Province_State'].astype(str)+ test['Country_Region'].astype(str)
 test = test.merge(train[['ConfirmedCases','Fatalities','Location','Date']],how='left',on=['Location','Date'])
 train = train[train['Date']<='2020-04-01']
data = pd.concat([train,test],axis=0)
 data = data.sort_values(['Country_Region','Date'] )<drop_column>
from xgboost import XGBRegressor
data.drop(['Id','Province_State','Country_Region'],axis=1,inplace=True )<set_options>
rf = XGBRegressor(n_estimators = 2100 , random_state = 0 , max_depth = 22)
 rf.fit(x_train,cases )
del train,test
gc.collect()<choose_model_class>
cases_pred = rf.predict(x_test)
 cases_pred
poly = PolynomialFeatures(degree=4)
<train_on_grid>
cases_pred = np.around(cases_pred,decimals = 0)
 cases_pred
Res = None
for i in tqdm(locations):
df = data[data['Location']==i].reset_index().drop(['index'],axis=1 ).reset_index()
X_c = poly.fit_transform(np.array(df['index'] ).reshape(-1,1))
df[[0,1,2,3,4]] = pd.DataFrame(X_c)
 train = df[df['Date']<='2020-04-14'][['ForecastId','ConfirmedCases','Fatalities',0,1,2,3,4]]
test = df[df['Date']>='2020-04-15'][['ForecastId','ConfirmedCases','Fatalities',0,1,2,3,4]]
model = LinearRegression()
model.fit(train.drop(['ConfirmedCases','ForecastId','Fatalities'],axis=1),train['ConfirmedCases'])
 test['ConfirmedCases']=model.predict(test.drop(['ConfirmedCases','ForecastId','Fatalities'],axis=1))
model = LinearRegression()
model.fit(train.drop(['ConfirmedCases','ForecastId','Fatalities'],axis=1),train['Fatalities'])
 test['Fatalities']=model.predict(test.drop(['ConfirmedCases','ForecastId','Fatalities'],axis=1))
pred = pd.concat([train[-13:][['ForecastId','ConfirmedCases','Fatalities']],test[['ForecastId','ConfirmedCases','Fatalities']]])
 Res = pd.concat([Res,pred],axis=0 )<data_type_conversions>
x_train_cas = []
for i in range(len(x_train)) :
x = list(x_train[i])
 x.append(cases[i])
 x_train_cas.append(x)
 x_train_cas[0]
Res['ForecastId'] = Res['ForecastId'].astype(int )<save_to_csv>
x_train_cas = np.array(x_train_cas )
Res.to_csv("submission.csv",index=False )<load_from_csv>
rf = XGBRegressor(n_estimators = 2100 , random_state = 0 , max_depth = 22)
 rf.fit(x_train_cas,fatalities )
df_train=pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 df_test=pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 df_sub=pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv")
 print(df_train.shape)
 print(df_test.shape)
 print(df_sub.shape )<count_unique_values>
x_test_cas = []
for i in range(len(x_test)) :
x = list(x_test[i])
 x.append(cases_pred[i])
 x_test_cas.append(x)
 x_test_cas[0]
print(len(df_train.Country_Region.unique()), len(df_train.Province_State.unique()))
print(len(df_test.Country_Region.unique()), len(df_test.Province_State.unique()))<feature_engineering>
fatalities_pred = rf.predict(x_test_cas)
 fatalities_pred
df_train["Unique_Region"] = df_train.Country_Region
df_train.Unique_Region[df_train.Province_State.isna() == False] = df_train.Province_State+" , "+df_train.Country_Region
df_train.sample(5 )<drop_column>
fatalities_pred = np.around(fatalities_pred,decimals = 0)
 fatalities_pred
df_train.drop(labels=["Id","Province_State","Country_Region"], axis=1, inplace=True )<feature_engineering>
submission['ConfirmedCases'] = cases_pred
submission['Fatalities'] = fatalities_pred
df_test["Unique_Region"] = df_test.Country_Region
df_test.Unique_Region[df_test.Province_State.isna() == False] = df_test.Province_State+" , "+df_test.Country_Region
df_test.sample(5 )<drop_column>
submission.to_csv("submission.csv" , index = False )