Kaggle competition: COVID19 Global Forecasting (Week 4)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-4)<save_to_csv>
train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/train.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-4/test.csv" )
y_test = model.predict([x_test], batch_size=1024, verbose=1)
 y_test =(y_test > 0.4 ).astype(int)
 test_df = pd.DataFrame({"qid": test_df["qid"].values})
 test_df['prediction'] = y_test
test_df.to_csv("submission.csv", index=False )<import_modules>
train["Province_State"] = train["Province_State"].fillna('')
 test["Province_State"] = test["Province_State"].fillna('')
 train["Month"], train["Day"] = 0, 0
for i in range(len(train)) :
train["Month"][i] =(train["Date"][i] ).split("-")[1]
train["Day"][i] =(train["Date"][i] ).split("-")[2]
test["Month"], test["Day"] = 0, 0
for i in range(len(test)) :
test["Month"][i] =(test["Date"][i] ).split("-")[1]
test["Day"][i] =(test["Date"][i] ).split("-")[2]
tqdm.pandas(desc='Progress')
<define_variables>
for i in range(len(train)) :
if train["Province_State"][i] != '':
train["Country_Region"][i] = train["Province_State"][i] + "(" + str(train["Country_Region"][i])+ ")"
for i in range(len(test)) :
if test["Province_State"][i] != '':
test["Country_Region"][i] = test["Province_State"][i] + "(" + str(test["Country_Region"][i])+ ")"
train.drop(columns = "Province_State", inplace=True)
 test.drop(columns = "Province_State", inplace=True)
 train.rename(columns = {"Country_Region" : "Country/State"}, inplace=True)
 test.rename(columns = {"Country_Region" : "Country/State"}, inplace=True)
embed_size = 300
max_features = 120000
maxlen = 70
batch_size = 512
n_epochs = 5
n_splits = 5
SEED = 1029<set_options>
i = 0
for value in train["Country/State"].unique() :
if i < len(train):
j = 1
while(train["Country/State"][i] == value):
train["Day"][i] = j
j += 1; i += 1
if i == len(train):
break
i = 0
for value in test["Country/State"].unique() :
if i < len(test):
j = 72
while(test["Country/State"][i] == value):
test["Day"][i] = j
j += 1; i += 1
if i == len(test):
break
train.rename(columns = {"Day" : "No.of days"}, inplace = True)
 test.rename(columns = {"Day" : "No.of days"}, inplace = True )
def seed_everything(seed=1029):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()<features_selection>
train["Country/State"].loc[train["Country/State"] == "Taiwan*"] = "Taiwan"
test["Country/State"].loc[test["Country/State"] == "Taiwan*"] = "Taiwan"
train = train.drop(columns = ["Date"])
 test = test.drop(columns = ["Date"])
 countriesorstates = train["Country/State"].unique()
print(countriesorstates )
def load_glove(word_index):
EMBEDDING_FILE = '.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')[:300]
embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE))
all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = -0.005838499,0.48782197
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix
def load_fasttext(word_index):
EMBEDDING_FILE = '.. /input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE)if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix
def load_para(word_index):
EMBEDDING_FILE = '.. /input/embeddings/paragram_300_sl999/paragram_300_sl999.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE, encoding="utf8", errors='ignore')if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = -0.0053247833,0.49346462
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix<load_from_csv>
random_picks = random.choices(countriesorstates, k = 5)
 for value in random_picks:
train_temp = train.loc[train["Country/State"] == value]
test_temp = test.loc[test["Country/State"] == value]
train_temp_cc = train_temp["ConfirmedCases"].loc[train["Country/State"] == value]
train_temp_ft = train_temp["Fatalities"].loc[train["Country/State"] == value]
train_temp_X = train_temp.iloc[:, 5]
test_temp_X = test_temp.iloc[:, 3]
x = train_temp_X.to_numpy(dtype = float)
 y_1 = train_temp_cc.to_numpy(dtype = float)
 y_2= train_temp_ft.to_numpy(dtype = float)
 x = x.reshape(-1,)
 plt.plot(x, y_1, color ='red', label = value)
 plt.xlabel("No.of days")
 plt.ylabel('Confirmed cases')
 plt.legend()
plt.title("Place: " + value)
 plt.show()
plt.plot(x, y_2, color ='black', label = value)
 plt.xlabel("No.of days")
 plt.ylabel('Fatalities')
 plt.legend()
plt.title("Place: " + value)
 plt.show()
df_train = pd.read_csv(".. /input/train.csv")
 df_test = pd.read_csv(".. /input/test.csv")
 df = pd.concat([df_train ,df_test],sort=True )<feature_engineering>
poly_reg_cc = PolynomialFeatures(degree = 4)
 poly_reg_ft = PolynomialFeatures(degree = 4)
 reg_cc = LinearRegression()
reg_ft = LinearRegression()
sub = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 for value in countriesorstates:
train_temp = train.loc[train["Country/State"] == value]
test_temp = test.loc[test["Country/State"] == value]
train_temp_cc = train_temp["ConfirmedCases"].loc[train["Country/State"] == value].to_frame()
train_temp_ft = train_temp["Fatalities"].loc[train["Country/State"] == value].to_frame()
train_temp_X = train_temp.iloc[:, 4:6]
test_temp_X = test_temp.iloc[:, 2:4]
sc1 = StandardScaler()
train_temp_X = sc1.fit_transform(train_temp_X)
 test_temp_X = sc1.transform(test_temp_X)
 sc_cc = StandardScaler()
sc_ft = StandardScaler()
train_temp_cc = sc_cc.fit_transform(train_temp_cc)
 train_temp_ft = sc_ft.fit_transform(train_temp_ft)
 X_poly = poly_reg_cc.fit_transform(train_temp_X)
 reg_cc.fit(X_poly, train_temp_cc)
 test_cc = sc_cc.inverse_transform(reg_cc.predict(poly_reg_cc.fit_transform(test_temp_X)))
 X_poly = poly_reg_ft.fit_transform(train_temp_X)
 reg_ft.fit(X_poly, train_temp_ft)
 test_ft = sc_ft.inverse_transform(reg_ft.predict(poly_reg_ft.fit_transform(test_temp_X)))
 a = int(train["No.of days"].loc[train["Country/State"] == "India"].max())
 b = int(a - test_temp["No.of days"].min())
 test_cc[0:b+1] = sc_cc.inverse_transform(train_temp_cc)[(a-b-1):(a)]
test_ft[0:b+1] = sc_ft.inverse_transform(train_temp_ft)[(a-b-1):(a)]
test_cc = test_cc.flatten()
test_ft = test_ft.flatten()
sub_temp = pd.DataFrame({'ForecastId': test_temp["ForecastId"].loc[test["Country/State"] == value],
'ConfirmedCases': test_cc, 'Fatalities': test_ft})
 sub = pd.concat([sub, sub_temp], axis = 0 )
<define_variables><EOS> 
sub.ForecastId = sub.ForecastId.astype('int')
 for i in range(len(sub)) :
sub["ConfirmedCases"][i] = int(round(sub["ConfirmedCases"][i]))
sub["Fatalities"][i] = int(round(sub["Fatalities"][i]))
sub.to_csv("submission.csv", index = False )