Kaggle competition: March Machine Learning Mania 2016
%%time
train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-2/train.csv' )<drop_column>
%matplotlib inline
junk =['Id','Date','Province_State']
train.drop(junk, axis=1, inplace=True )<feature_engineering>
dr = pd.read_csv(".. /input/RegularSeasonDetailedResults.csv")
train['Country_Region'] = c<data_type_conversions>
simple_df_1 = pd.DataFrame()
simple_df_1[["team1", "team2"]] =dr[["Wteam", "Lteam"]].copy()
simple_df_1["pred"] = 1
simple_df_2 = pd.DataFrame()
simple_df_2[["team1", "team2"]] =dr[["Lteam", "Wteam"]]
simple_df_2["pred"] = 0
simple_df = pd.concat(( simple_df_1, simple_df_2), axis=0)
 simple_df.head()
train['ConfirmedCases'] = train['ConfirmedCases'].astype(int)
 train['Fatalities'] = train['Fatalities'].astype(int )<drop_column>
n = simple_df.team1.nunique()
n
def prep_data(train):
X_train = train[train.ConfirmedCases >0]
X_train.reset_index(inplace = True, drop = True)
 train.reset_index(inplace = True, drop = True)
 return(X_train, train )<concatenate>
trans_dict = {t: i for i, t in enumerate(simple_df.team1.unique())}
simple_df["team1"] = simple_df["team1"].apply(lambda x: trans_dict[x])
 simple_df["team2"] = simple_df["team2"].apply(lambda x: trans_dict[x])
 simple_df.head()
def Calculate_Table(X_train):
diff_conf, conf_old = [], 0
diff_fat, fat_old = [], 0
dd_conf, dc_old = [], 0
dd_fat, df_old = [], 0
ratios = []
for row in X_train.values:
diff_conf.append(row[1]-conf_old)
 conf_old = row[1]
diff_fat.append(row[2]-fat_old)
 fat_old = row[2]
dd_conf.append(diff_conf[-1]-dc_old)
 dc_old = diff_conf[-1]
dd_fat.append(diff_fat[-1]-df_old)
 df_old = diff_fat[-1]
ratios.append(fat_old / conf_old)
 ratio = fat_old / conf_old
return diff_conf, conf_old, diff_fat, fat_old, dd_conf, dc_old, dd_fat, df_old, ratios, ratio<feature_engineering>
train = simple_df.values
np.random.shuffle(train)
def populate_df_features(X_train,diff_conf, diff_fat, dd_conf, dd_fat, ratios):
pd.options.mode.chained_assignment = None
X_train['diff_confirmed'] = diff_conf
X_train['diff_fatalities'] = diff_fat
X_train['dd_confirmed'] = dd_conf
X_train['dd_fatalities'] = dd_fat
X_train['ratios'] = ratios
return X_train<categorify>
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)
def fill_nan(variable):
if math.isnan(variable):
return 0
else:
return variable<statistical_test>
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)
def Cal_Series_Avg(X_train,ratio):
d_c = fill_nan(X_train.diff_confirmed[X_train.diff_confirmed != 0].mean())
 dd_c = fill_nan(X_train.dd_confirmed[X_train.dd_confirmed != 0].mean())
 d_f = fill_nan(X_train.diff_fatalities[X_train.diff_fatalities != 0].mean())
 dd_f = fill_nan(X_train.dd_fatalities[X_train.dd_fatalities != 0].mean())
 rate = fill_nan(X_train.ratios[X_train.ratios != 0].mean())
 rate = max(rate,ratio)
 return d_c, dd_c, d_f, dd_f, rate<data_type_conversions>
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")
def apply_taylor(train, d_c, dd_c, d_f, dd_f, rate):
pred_c, pred_f = list(train.ConfirmedCases.loc[57:69].astype(int)) , list(train.Fatalities.loc[57:69].astype(int))
for i in range(1, 31):
pred_c.append(int(( train.ConfirmedCases[69] + d_c*i + 0.5*dd_c*(i**2))))
pred_f.append(pred_c[-1]*rate)
 return pred_c, pred_f<data_type_conversions>
history = model.fit([train[:, 0], train[:, 1]], train[:, 2], batch_size=64, nb_epoch=10, verbose=2)
def apply_taylor2(train, d_c, dd_c, d_f, dd_f, rate):
pred_c, pred_f = list(train.ConfirmedCases.loc[57:58].astype(int)) , list(train.Fatalities.loc[57:58].astype(int))
for i in range(1, 42):
pred_c.append(int(( train.ConfirmedCases[58] + d_c*i + 0.5*dd_c*(i**2))))
pred_f.append(pred_c[-1]*rate)
 return pred_c, pred_f<groupby>
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()
pc = []
pf = []
pc2 = []
pf2 = []
pred_c = []
pred_f = []
pred_c2 = []
pred_f2 = []
for i,country in enumerate(country_list):
country_data = train[train['Country_Region'] == country]
X_train, country_data = prep_data(country_data)
 if(len(X_train)> 0):
diff_conf, conf_old, diff_fat, fat_old, dd_conf, dc_old, dd_fat, df_old, ratios, ratio = Calculate_Table(X_train)
 X_train = populate_df_features(X_train,diff_conf, diff_fat, dd_conf, dd_fat, ratios)
 d_c, dd_c, d_f, dd_f, rate = Cal_Series_Avg(X_train, ratio)
 pred_c, pred_f = apply_taylor(country_data, d_c, dd_c, d_f, dd_f, rate)
 pred_c2, pred_f2 = apply_taylor2(country_data, d_c, dd_c, d_f, dd_f, rate)
 else:
pred_c = list(np.zeros(43))
pred_f = list(np.zeros(43))
pred_c2 = list(np.zeros(43))
pred_f2 = list(np.zeros(43))
pc += pred_c
pf += pred_f
pc2 += pred_c2
pf2 += pred_f2<categorify>
sub["pred"] = model.predict([sub.team1, sub.team2])
 sub = sub[["Id", "pred"]]
sub.head()
pc = list(map(int, pc))
pf = list(map(int, pf))
pc2 = list(map(int, pc2))
pf2 = list(map(int, pf2))<import_modules>
sub.to_csv("CF.csv", index=False)