Kaggle competition: COVID19 Global Forecasting (Week 3)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-week-3<categorify>
warnings.filterwarnings("ignore" )
def embedding_input(name, n_in, n_out, reg):
inp = Input(shape=(1,), dtype="int64", name=name)
 return inp, Embedding(n_in, n_out, input_length=1, W_regularizer=l2(reg))(inp)
 def create_bias(inp, n_in):
x = Embedding(n_in, 1, input_length=1 )(inp)
 return Flatten()(x)
<categorify>
train = pd.read_csv('.. /input/covid19-global-forecasting-week-3/train.csv')
 test = pd.read_csv('.. /input/covid19-global-forecasting-week-3/test.csv' )
n_factors = 50
team1_in, t1 = embedding_input("team1_in", n, n_factors, 1e-4)
 team2_in, t2 = embedding_input("team2_in", n, n_factors, 1e-4)
 b1 = create_bias(team1_in, n)
 b2 = create_bias(team2_in, n)
<merge>
curr_date = train['Date'].max()
world_cum_confirmed = sum(train[train['Date'] == curr_date].ConfirmedCases)
 world_cum_fatal = sum(train[train['Date'] == curr_date].Fatalities)
 print('Number of Countires: ', len(train['Country_Region'].unique()))
print('End date in train dset: ', curr_date)
 print('Number of confirmed cases: ', world_cum_confirmed)
 print('Number of fatal cases: ', world_cum_fatal )
x = merge([t1, t2], mode="dot")
 x = Flatten()(x)
 x = merge([x, b1], mode="sum")
 x = merge([x, b2], mode="sum")
 x = Dense(1, activation="sigmoid" )(x)
 model = Model([team1_in, team2_in], x)
 model.compile(Adam(0.001), loss="binary_crossentropy")
<train_model>
top_country_c = train[train['Date'] == curr_date].groupby(['Date','Country_Region'] ).sum().sort_values(['ConfirmedCases'], ascending=False)
 top_country_c.head(20 )
history = model.fit([train[:, 0], train[:, 1]], train[:, 2], batch_size=64, nb_epoch=10, verbose=2)
<feature_engineering>
top_country_f = train[train['Date'] == curr_date].groupby(['Date','Country_Region'] ).sum().sort_values(['Fatalities'], ascending=False)
 top_country_f.head(20 )
sub = pd.read_csv(".. /input/SampleSubmission.csv")
 sub["team1"] = sub["Id"].apply(lambda x: trans_dict[int(x.split("_")[1])])
 sub["team2"] = sub["Id"].apply(lambda x: trans_dict[int(x.split("_")[2])])
 sub.head() 
<predict_on_test>
train['MortalityRate'] = train['Fatalities'] / train['ConfirmedCases']
train['MortalityRate'] = train['MortalityRate'].fillna(0.0 )
sub["pred"] = model.predict([sub.team1, sub.team2])
 sub = sub[["Id", "pred"]]
sub.head() 
<save_to_csv>
top_country_m = train[train['Date'] == curr_date].groupby(['Country_Region'] ).sum().sort_values(['MortalityRate'], ascending=False)
 top_country_m.head(10 )
sub.to_csv("CF.csv", index=False)
<load_from_csv>
for df in [test]:
df['Date'] = pd.to_datetime(df['Date'], infer_datetime_format=True)
 df['Date'] = pd.to_datetime(df['Date'], infer_datetime_format=True )
train_data = pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()<load_from_csv>
missed = "NA"
def State(state, country):
if state == missed: return country
return state
test_data = pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()<count_missing_values>
for df in [train, test]:
df['Province_State'].fillna(missed, inplace=True)
 df['Province_State'] = df.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : State(x['Province_State'], x['Country_Region']), axis=1)
 df.loc[:, 'Date'] = df.Date.dt.strftime("%m%d")
 df["Date"] = df["Date"].astype(int )
train_data.isnull().sum()<count_missing_values>
label_encoder = preprocessing.LabelEncoder()
for df in [train, test]:
df['Country_Region'] = label_encoder.fit_transform(df['Country_Region'])
 df['Province_State'] = label_encoder.fit_transform(df['Province_State'] )
test_data.isnull().sum()<count_values>
def RF() :
model = RandomForestRegressor(n_estimators = 100)
 return model
def XGB() :
model = XGBRegressor(n_estimators=1300)
 return model
def LGBM() :
model = LGBMRegressor(iterations=2)
 return model
train_data['IsBadBuy'].value_counts()<count_values>
sub = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 for country in range(len(unique_countries)) :
current_country_train = train.loc[train['Country_Region'] == unique_countries[country]]
current_country_test = test.loc[test['Country_Region'] == unique_countries[country]]
features = ['Country_Region', 'Province_State', 'Date']
X_train = current_country_train[features].to_numpy()
y1_train = current_country_train[['ConfirmedCases']].to_numpy()
y2_train = current_country_train[['Fatalities']].to_numpy()
X_test = current_country_test[features].to_numpy()
y1_train = y1_train.reshape(-1)
 y2_train = y2_train.reshape(-1)
 model1 = XGB()
model1.fit(X_train, y1_train)
 res_cnf_cls = np.round(model1.predict(X_test))
model2 = XGB()
model2.fit(X_train, y2_train)
 res_fac = np.round(model2.predict(X_test))
current_country_test_Id = current_country_test.loc[:, 'ForecastId']
pred = pd.DataFrame({'ForecastId': current_country_test_Id, 'ConfirmedCases': res_cnf_cls, 'Fatalities': res_fac})
 sub = pd.concat([sub, pred], axis=0 )
<drop_column><EOS> 
sub.ForecastId = sub.ForecastId.astype('int')
 sub.to_csv('submission.csv', index=False)
 sub