Kaggle competition: Mechanisms of Action (MoA) Prediction
DAYS_SINCE_CASES = [1, 10, 50, 100, 500, 1000, 5000, 10000]
min_date_train = np.min(df[~df.Id.isna() ].Date)
 max_date_train = np.max(df[~df.Id.isna() ].Date)
 min_date_test = np.min(df[~df.ForecastId.isna() ].Date)
 max_date_test = np.max(df[~df.ForecastId.isna() ].Date)
 n_dates_test = len(df[~df.ForecastId.isna() ].Date.unique())
 print("Train date range:", str(min_date_train), " - ", str(max_date_train))
print("Test date range:", str(min_date_test), " - ", str(max_date_test))
for lag in range(1, 41):
df[f"lag_{lag}_cc"] = df.groupby("geo")["ConfirmedCases"].shift(lag)
 df[f"lag_{lag}_ft"] = df.groupby("geo")["Fatalities"].shift(lag)
 df[f"lag_{lag}_rc"] = df.groupby("geo")["Recoveries"].shift(lag)
 for case in DAYS_SINCE_CASES:
df = df.merge(df[df.ConfirmedCases >= case].groupby("geo")["Date"].min().reset_index().rename(columns = {"Date": f"case_{case}_date"}), on = "geo", how = "left")
 df.shape<feature_engineering>
warnings.filterwarnings("ignore")
 pd.set_option('display.max_columns', 300)
 pd.set_option('display.max_rows', 300)
 pd.set_option('display.max_colwidth', 300)
 pd.options.display.float_format = '{:.3f}'.format
%matplotlib inline
def prepare_features(df, gap):
df["perc_1_ac"] =(df[f"lag_{gap}_cc"] - df[f"lag_{gap}_ft"] - df[f"lag_{gap}_rc"])/ df[f"lag_{gap}_cc"]
df["perc_1_cc"] = df[f"lag_{gap}_cc"] / df.population
df["diff_1_cc"] = df[f"lag_{gap}_cc"] - df[f"lag_{gap + 1}_cc"]
df["diff_2_cc"] = df[f"lag_{gap + 1}_cc"] - df[f"lag_{gap + 2}_cc"]
df["diff_3_cc"] = df[f"lag_{gap + 2}_cc"] - df[f"lag_{gap + 3}_cc"]
df["diff_1_ft"] = df[f"lag_{gap}_ft"] - df[f"lag_{gap + 1}_ft"]
df["diff_2_ft"] = df[f"lag_{gap + 1}_ft"] - df[f"lag_{gap + 2}_ft"]
df["diff_3_ft"] = df[f"lag_{gap + 2}_ft"] - df[f"lag_{gap + 3}_ft"]
df["diff_123_cc"] =(df[f"lag_{gap}_cc"] - df[f"lag_{gap + 3}_cc"])/ 3
df["diff_123_ft"] =(df[f"lag_{gap}_ft"] - df[f"lag_{gap + 3}_ft"])/ 3
df["diff_change_1_cc"] = df.diff_1_cc / df.diff_2_cc
df["diff_change_2_cc"] = df.diff_2_cc / df.diff_3_cc
df["diff_change_1_ft"] = df.diff_1_ft / df.diff_2_ft
df["diff_change_2_ft"] = df.diff_2_ft / df.diff_3_ft
df["diff_change_12_cc"] =(df.diff_change_1_cc + df.diff_change_2_cc)/ 2
df["diff_change_12_ft"] =(df.diff_change_1_ft + df.diff_change_2_ft)/ 2
df["change_1_cc"] = df[f"lag_{gap}_cc"] / df[f"lag_{gap + 1}_cc"]
df["change_2_cc"] = df[f"lag_{gap + 1}_cc"] / df[f"lag_{gap + 2}_cc"]
df["change_3_cc"] = df[f"lag_{gap + 2}_cc"] / df[f"lag_{gap + 3}_cc"]
df["change_1_ft"] = df[f"lag_{gap}_ft"] / df[f"lag_{gap + 1}_ft"]
df["change_2_ft"] = df[f"lag_{gap + 1}_ft"] / df[f"lag_{gap + 2}_ft"]
df["change_3_ft"] = df[f"lag_{gap + 2}_ft"] / df[f"lag_{gap + 3}_ft"]
df["change_123_cc"] = df[f"lag_{gap}_cc"] / df[f"lag_{gap + 3}_cc"]
df["change_123_ft"] = df[f"lag_{gap}_ft"] / df[f"lag_{gap + 3}_ft"]
for case in DAYS_SINCE_CASES:
df[f"days_since_{case}_case"] =(df[f"case_{case}_date"] - df.Date ).astype("timedelta64[D]")
 df.loc[df[f"days_since_{case}_case"] < gap, f"days_since_{case}_case"] = np.nan
df["country_flag"] = df.Province_State.isna().astype(int)
 df["density"] = df.population / df.area
df["target_cc"] = np.log1p(df.ConfirmedCases)- np.log1p(df[f"lag_{gap}_cc"])
 df["target_ft"] = np.log1p(df.Fatalities)- np.log1p(df[f"lag_{gap}_ft"])
 features = [
f"lag_{gap}_cc",
f"lag_{gap}_ft",
f"lag_{gap}_rc",
"perc_1_ac",
"perc_1_cc",
"diff_1_cc",
"diff_2_cc",
"diff_3_cc",
"diff_1_ft",
"diff_2_ft",
"diff_3_ft",
"diff_123_cc",
"diff_123_ft",
"diff_change_1_cc",
"diff_change_2_cc",
"diff_change_1_ft",
"diff_change_2_ft",
"diff_change_12_cc",
"diff_change_12_ft",
"change_1_cc",
"change_2_cc",
"change_3_cc",
"change_1_ft",
"change_2_ft",
"change_3_ft",
"change_123_cc",
"change_123_ft",
"days_since_1_case",
"days_since_10_case",
"days_since_50_case",
"days_since_100_case",
"days_since_500_case",
"days_since_1000_case",
"days_since_5000_case",
"days_since_10000_case",
"country_flag",
"lat",
"lon",
"continent",
"population",
"area",
"density",
"target_cc",
"target_ft"
]
return df[features]<predict_on_test>
path = "/kaggle/"
df_Train = pd.read_csv(path+"input/lish-moa/train_features.csv")
 df_Test = pd.read_csv(path+"input/lish-moa/test_features.csv")
 df_Train_target_nonscored = pd.read_csv(".. /input/lish-moa/train_targets_nonscored.csv")
 df_Train_target_scored = pd.read_csv(path+"input/lish-moa/train_targets_scored.csv")
 sample_submission = pd.read_csv(path+"input/lish-moa/sample_submission.csv")
 target_cols = df_Train_target_scored.columns[1:]
def build_predict_lgbm(df_train, df_test, gap):
df_train.dropna(subset = ["target_cc", "target_ft", f"lag_{gap}_cc", f"lag_{gap}_ft"], inplace = True)
 target_cc = df_train.target_cc
target_ft = df_train.target_ft
test_lag_cc = df_test[f"lag_{gap}_cc"].values
test_lag_ft = df_test[f"lag_{gap}_ft"].values
df_train.drop(["target_cc", "target_ft"], axis = 1, inplace = True)
 df_test.drop(["target_cc", "target_ft"], axis = 1, inplace = True)
 categorical_features = ["continent"]
dtrain_cc = lgb.Dataset(df_train, label = target_cc, categorical_feature = categorical_features)
 dtrain_ft = lgb.Dataset(df_train, label = target_ft, categorical_feature = categorical_features)
 model_cc = lgb.train(LGB_PARAMS, train_set = dtrain_cc, num_boost_round = 200)
 model_ft = lgb.train(LGB_PARAMS, train_set = dtrain_ft, num_boost_round = 200)
 y_pred_cc = np.expm1(model_cc.predict(df_test, num_boost_round = 200)+ np.log1p(test_lag_cc))
y_pred_ft = np.expm1(model_ft.predict(df_test, num_boost_round = 200)+ np.log1p(test_lag_ft))
return y_pred_cc, y_pred_ft, model_cc, model_ft<predict_on_test>
class Config() :
is_debug = False
is_kaggle = True
is_pretrain = False
do_variancethreshold = False
do_kmeans = False
do_filter = True
do_feature_squared = True
do_feature_stats = True
do_feature_pca = True
do_feature_svd = True
do_feature_fa = True
seed = 42
n_gene_comp = 70
n_cell_comp = 10
n_gene_kmeans_cluster = 30
n_cell_kmeans_cluster = 5
n_variance_threshold = 0.7
scaler = 'Rankgauss'
epochs = 80
seed_avg = [0, 101, 202, 303 ,404, 999]
nfold = 7
verbose = 0
lr = 1e-3
weight_decay = 1e-5
batch_size = 128
config = Config()
def predict_mad(df_test, gap, val = False):
df_test["avg_diff_cc"] =(df_test[f"lag_{gap}_cc"] - df_test[f"lag_{gap + 3}_cc"])/ 3
df_test["avg_diff_ft"] =(df_test[f"lag_{gap}_ft"] - df_test[f"lag_{gap + 3}_ft"])/ 3
if val:
y_pred_cc = df_test[f"lag_{gap}_cc"] + gap * df_test.avg_diff_cc -(1 - MAD_FACTOR)* df_test.avg_diff_cc * np.sum([x for x in range(gap)])/ VAL_DAYS
y_pred_ft = df_test[f"lag_{gap}_ft"] + gap * df_test.avg_diff_ft -(1 - MAD_FACTOR)* df_test.avg_diff_ft * np.sum([x for x in range(gap)])/ VAL_DAYS
else:
y_pred_cc = df_test[f"lag_{gap}_cc"] + gap * df_test.avg_diff_cc -(1 - MAD_FACTOR)* df_test.avg_diff_cc * np.sum([x for x in range(gap)])/ n_dates_test
y_pred_ft = df_test[f"lag_{gap}_ft"] + gap * df_test.avg_diff_ft -(1 - MAD_FACTOR)* df_test.avg_diff_ft * np.sum([x for x in range(gap)])/ n_dates_test
return y_pred_cc, y_pred_ft<init_hyperparams>
def HighestCorrelation(corrmat):
c = corrmat.abs()
shape = c.shape
s = c.unstack()
so = s.sort_values(kind="quicksort")
 return so[-(shape[0] + 10):-shape[0]]
def data_filter(train, test):

 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test[test['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1)
 return train, test
def one_hot_encoder(df, cols):

 for col in cols:
ohe = OneHotEncoder(sparse=False)
 ohe_df = pd.DataFrame(ohe.fit_transform(df[[col]])).add_prefix(col + '_ohe_')
 df = pd.concat([df, ohe_df], axis=1)
 df = df.drop(col, axis=1)
 return df
def feature_stats(df):

 df.loc[:, 'g-sum'] = df[GENES].sum(axis=1)
 df.loc[:, 'g-mean'] = df[GENES].mean(axis=1)
 df.loc[:, 'g-std'] = df[GENES].std(axis=1)
 df.loc[:, 'g-kurt'] = df[GENES].kurtosis(axis=1)
 df.loc[:, 'g-skew'] = df[GENES].skew(axis=1)
 df.loc[:, 'c-sum'] = df[CELLS].sum(axis=1)
 df.loc[:, 'c-mean'] = df[CELLS].mean(axis=1)
 df.loc[:, 'c-std'] = df[CELLS].std(axis=1)
 df.loc[:, 'c-kurt'] = df[CELLS].kurtosis(axis=1)
 df.loc[:, 'c-skew'] = df[CELLS].skew(axis=1)
 df.loc[:, 'gc-sum'] = df[GENES + CELLS].sum(axis=1)
 df.loc[:, 'gc-mean'] = df[GENES + CELLS].mean(axis=1)
 df.loc[:, 'gc-std'] = df[GENES + CELLS].std(axis=1)
 df.loc[:, 'gc-kurt'] = df[GENES + CELLS].kurtosis(axis=1)
 df.loc[:, 'gc-skew'] = df[GENES + CELLS].skew(axis=1)
 return df
def feature_pca(df, col_list, n_comp, col_type='g', seed=config.seed):

 pca =(PCA(n_components=n_comp, random_state=seed ).fit_transform(df[col_list]))
pca_df = pd.DataFrame(pca, columns=[f'{col_type}-pca_{i}' for i in range(n_comp)])
 df = pd.concat([df, pca_df], axis=1)
 return df
def feature_svd(df, col_list, n_comp, col_type='g', seed=config.seed):

 svd =(TruncatedSVD(n_components=n_comp, random_state=seed ).fit_transform(df[col_list]))
svd_df = pd.DataFrame(svd, columns=[f'{col_type}-svd_{i}' for i in range(n_comp)])
 df = pd.concat([df, svd_df], axis=1)
 return df
def feature_fa(df, col_list, n_comp, col_type='g', seed=config.seed):
svd =(FactorAnalysis(n_components=n_comp, random_state=seed ).fit_transform(df[col_list]))
svd_df = pd.DataFrame(svd, columns=[f'{col_type}-fa_{i}' for i in range(n_comp)])
 df = pd.concat([df, svd_df], axis=1)
 return df
def feature_squared(df, cols_list):
for feature in cols_list:
df.loc[:, f'{feature}_squared'] = df[feature] ** 2
return df
def variance_threshold(df, n):
var_thresh = VarianceThreshold(threshold=n)
 df = pd.DataFrame(var_thresh.fit_transform(df))
return df
def rankgauss(df, cols, seed=config.seed):
for col in cols:
transformer = QuantileTransformer(n_quantiles=100, random_state=seed, output_distribution="normal")
 vec_len = len(df[col].values)
 raw_vec = df[col].values.reshape(vec_len, 1)
 transformer.fit(raw_vec)
 df[col] = transformer.transform(raw_vec ).reshape(1, vec_len)[0]
return df
def feature_engineering(train_features, test_features):
global GENES, CELLS
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]
cat_columns = ['cp_time', 'cp_dose']
if config.do_filter:
print('do filter')
 train, test = data_filter(train_features, test_features)
 df = pd.concat([train, test])
 df = df.reset_index(drop=True)
 if config.do_feature_stats:
print('do feature_stats')
 df = feature_stats(df)
 if config.do_feature_squared:
print('do feature_squared')
 df = feature_squared(df, CELLS)
 if config.do_feature_pca:
print('do feature_pca')
 df = feature_pca(df, GENES, n_comp=config.n_gene_comp, col_type='g')
 df = feature_pca(df, CELLS, n_comp=config.n_cell_comp, col_type='c')
 if config.do_feature_svd:
print('do feature_svd')
 df = feature_svd(df, GENES, n_comp=config.n_gene_comp, col_type='g')
 df = feature_svd(df, CELLS, n_comp=config.n_cell_comp, col_type='c')
 if config.do_feature_fa:
print('do feature_fa')
 df = feature_fa(df, GENES, n_comp=config.n_gene_comp, col_type='g')
 df = feature_fa(df, CELLS, n_comp=config.n_cell_comp, col_type='c')
 cat_df = df[['sig_id'] + cat_columns]
num_df = df.drop(['sig_id'] + cat_columns, axis=1)
 if config.do_variancethreshold:
print('do variancethreshold')
 num_df = variance_threshold(num_df, n=config.n_variance_threshold)
 if config.scaler == 'Rankgauss':
print('do Rankgauss')
 df = rankgauss(df, num_df.columns.tolist())
 elif config.scaler == 'Standard':
print('do Standard')
 sscaler = StandardScaler()
num_df.iloc[:, :] = sscaler.fit_transform(num_df)
 elif config.scaler == 'Robust':
print('do Robust')
 rscaler = RobustScaler()
num_df.iloc[:, :] = rscaler.fit_transform(num_df)
 elif config.scaler == 'MinMax':
print('do MinMax')
 mmscaler = MinMaxScaler()
num_df.iloc[:, :] = mmscaler.fit_transform(num_df)
 cat_df = one_hot_encoder(cat_df, cat_columns)
 df = pd.concat([cat_df, num_df], axis=1)
 train = df.iloc[:len(train), :]
test = df.iloc[len(train):, :]
train = train.reset_index(drop=True)
 test = test.reset_index(drop=True)
 return train, test
SEED = 23
LGB_PARAMS = {"objective": "regression",
"num_leaves": 6,
"learning_rate": 0.013,
"bagging_fraction": 0.91,
"feature_fraction": 0.81,
"reg_alpha": 0.13,
"reg_lambda": 0.13,
"metric": "rmse",
"seed": SEED
}
VAL_DAYS = 7
MAD_FACTOR = 0.5<filter>
train, test = feature_engineering(df_Train, df_Test)
df_train = df[~df.Id.isna() ]
df_test_full = df[~df.ForecastId.isna() ]
df_preds_val = []
df_preds_test = []
for date in df_test_full.Date.unique() :
print("[INFO] Date:", date)
 if date in df_train.Date.values:
df_pred_test = df_test_full.loc[df_test_full.Date == date, ["ForecastId", "ConfirmedCases", "Fatalities"]].rename(columns = {"ConfirmedCases": "ConfirmedCases_test", "Fatalities": "Fatalities_test"})
 else:
df_test = df_test_full[df_test_full.Date == date]
gap =(pd.Timestamp(date)- max_date_train ).days
if gap <= VAL_DAYS:
val_date = max_date_train - pd.Timedelta(VAL_DAYS, "D")+ pd.Timedelta(gap, "D")
 df_build = df_train[df_train.Date < val_date]
df_val = df_train[df_train.Date == val_date]
X_build = prepare_features(df_build, gap)
 X_val = prepare_features(df_val, gap)
 y_val_cc_lgb, y_val_ft_lgb, _, _ = build_predict_lgbm(X_build, X_val, gap)
 y_val_cc_mad, y_val_ft_mad = predict_mad(df_val, gap, val = True)
 df_pred_val = pd.DataFrame({"Id": df_val.Id.values,
"ConfirmedCases_val_lgb": y_val_cc_lgb,
"Fatalities_val_lgb": y_val_ft_lgb,
"ConfirmedCases_val_mad": y_val_cc_mad,
"Fatalities_val_mad": y_val_ft_mad,
})
 df_preds_val.append(df_pred_val)
 X_train = prepare_features(df_train, gap)
 X_test = prepare_features(df_test, gap)
 y_test_cc_lgb, y_test_ft_lgb, model_cc, model_ft = build_predict_lgbm(X_train, X_test, gap)
 y_test_cc_mad, y_test_ft_mad = predict_mad(df_test, gap)
 if gap == 1:
model_1_cc = model_cc
model_1_ft = model_ft
features_1 = X_train.columns.values
elif gap == 14:
model_14_cc = model_cc
model_14_ft = model_ft
features_14 = X_train.columns.values
elif gap == 28:
model_28_cc = model_cc
model_28_ft = model_ft
features_28 = X_train.columns.values
df_pred_test = pd.DataFrame({"ForecastId": df_test.ForecastId.values,
"ConfirmedCases_test_lgb": y_test_cc_lgb,
"Fatalities_test_lgb": y_test_ft_lgb,
"ConfirmedCases_test_mad": y_test_cc_mad,
"Fatalities_test_mad": y_test_ft_mad,
})
 df_preds_test.append(df_pred_test )<merge>
target_cols = df_Train_target_scored.drop('sig_id', axis=1 ).columns.values.tolist()
target_cols_non_scored = df_Train_target_nonscored.drop('sig_id', axis=1 ).columns.values.tolist()
feature_cols = [c for c in train.columns if c not in ['sig_id']]
train = train.merge(df_Train_target_scored, on='sig_id')
 target = train[df_Train_target_scored.columns]
train_non_scored = train[['sig_id'] + feature_cols].merge(df_Train_target_nonscored, on='sig_id')
 target_non_scored = train_non_scored[df_Train_target_nonscored.columns]
df = df.merge(pd.concat(df_preds_val, sort = False), on = "Id", how = "left")
 df = df.merge(pd.concat(df_preds_test, sort = False), on = "ForecastId", how = "left")
 rmsle_cc_lgb = np.sqrt(mean_squared_error(np.log1p(df[~df.ConfirmedCases_val_lgb.isna() ].ConfirmedCases), np.log1p(df[~df.ConfirmedCases_val_lgb.isna() ].ConfirmedCases_val_lgb)))
 rmsle_ft_lgb = np.sqrt(mean_squared_error(np.log1p(df[~df.Fatalities_val_lgb.isna() ].Fatalities), np.log1p(df[~df.Fatalities_val_lgb.isna() ].Fatalities_val_lgb)))
 rmsle_cc_mad = np.sqrt(mean_squared_error(np.log1p(df[~df.ConfirmedCases_val_mad.isna() ].ConfirmedCases), np.log1p(df[~df.ConfirmedCases_val_mad.isna() ].ConfirmedCases_val_mad)))
 rmsle_ft_mad = np.sqrt(mean_squared_error(np.log1p(df[~df.Fatalities_val_mad.isna() ].Fatalities), np.log1p(df[~df.Fatalities_val_mad.isna() ].Fatalities_val_mad)))
 print("LGB CC RMSLE Val of", VAL_DAYS, "days for CC:", round(rmsle_cc_lgb, 2))
print("LGB FT RMSLE Val of", VAL_DAYS, "days for FT:", round(rmsle_ft_lgb, 2))
print("LGB Overall RMSLE Val of", VAL_DAYS, "days:", round(( rmsle_cc_lgb + rmsle_ft_lgb)/ 2, 2))
print("MAD CC RMSLE Val of", VAL_DAYS, "days for CC:", round(rmsle_cc_mad, 2))
print("MAD FT RMSLE Val of", VAL_DAYS, "days for FT:", round(rmsle_ft_mad, 2))
print("MAD Overall RMSLE Val of", VAL_DAYS, "days:", round(( rmsle_cc_mad + rmsle_ft_mad)/ 2, 2))<feature_engineering>
def seed_everything(seed=config.seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()
test = df.loc[~df.ForecastId.isna() , ["ForecastId", "Country_Region", "Province_State", "Date",
"ConfirmedCases_test", "ConfirmedCases_test_lgb", "ConfirmedCases_test_mad",
"Fatalities_test", "Fatalities_test_lgb", "Fatalities_test_mad"]].reset_index()
test["ConfirmedCases"] = 0.15 * test.ConfirmedCases_test_lgb + 0.85 * test.ConfirmedCases_test_mad
test["Fatalities"] = 0.1 * test.Fatalities_test_lgb + 0.9 * test.Fatalities_test_mad
test.loc[test.Country_Region.isin(["China", "US", "Diamond Princess"]), "ConfirmedCases"] = test[test.Country_Region.isin(["China", "US", "Diamond Princess"])].ConfirmedCases_test_mad.values
test.loc[test.Country_Region.isin(["China", "US", "Diamond Princess"]), "Fatalities"] = test[test.Country_Region.isin(["China", "US", "Diamond Princess"])].Fatalities_test_mad.values
test.loc[test.Date.isin(df_train.Date.values), "ConfirmedCases"] = test[test.Date.isin(df_train.Date.values)].ConfirmedCases_test.values
test.loc[test.Date.isin(df_train.Date.values), "Fatalities"] = test[test.Date.isin(df_train.Date.values)].Fatalities_test.values
sub0 = test[["ForecastId", "ConfirmedCases", "Fatalities"]]
sub0.ForecastId = sub0.ForecastId.astype(int)
 sub0.head()<filter>
def mean_log_loss(y_true, y_pred):
y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)
 metrics = []
for target in range(len(target_cols)) :
metrics.append(log_loss(y_true[:, target], y_pred[:, target]))
return np.mean(metrics)
 def create_model_3l(feature_len, target_len):
inp = tf.keras.layers.Input(shape =(feature_len))
x = tf.keras.layers.BatchNormalization()(inp)
 x = tf.keras.layers.Dropout(0.4914099166744246 )(x)
 x = tfa.layers.WeightNormalization(tf.keras.layers.Dense(1159, activation = 'relu'))(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dropout(0.18817607797795838 )(x)
 x = tfa.layers.WeightNormalization(tf.keras.layers.Dense(960, activation = 'relu'))(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dropout(0.12542057776853896 )(x)
 x = tfa.layers.WeightNormalization(tf.keras.layers.Dense(1811, activation = 'relu'))(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dropout(0.20175242230280122 )(x)
 out = tfa.layers.WeightNormalization(tf.keras.layers.Dense(target_len, activation = 'sigmoid'))(x)
 model = tf.keras.models.Model(inputs = inp, outputs = out)
 opt = tf.optimizers.Adam(learning_rate = LEARNING_RATE)
 opt = tfa.optimizers.Lookahead(opt, sync_period = 10)
 model.compile(optimizer = opt,
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing = 0.0015),
metrics = tf.keras.metrics.BinaryCrossentropy())
 return model
test.loc[test["Country_Region"]=="India", ["ForecastId", "Date", "Country_Region", "ConfirmedCases_test_mad", "Fatalities_test_mad"]]<filter>
LEARNING_RATE = config.lr
WEIGHT_DECAY = config.weight_decay
BATCH_SIZE = config.batch_size
EPOCHS = config.epochs
SEED_AVG = config.seed_avg
NFOLDS = config.nfold
VERBOSE = config.verbose
if config.is_debug:
EPOCHS = 3
SEED_AVG = [0, 101]
NFOLDS = 3
VERBOSE = 2
sub0.loc[sub0.ForecastId.between(6031, 6063)]<save_to_csv>
def run_training(tr_idx, va_idx, fold, seed):
seed_everything(seed)
 oof = np.zeros(( len(train), target.iloc[:, 1:].shape[1]))
predictions = np.zeros(( len(test), target.iloc[:, 1:].shape[1]))
train_df = train.iloc[tr_idx]
valid_df = train.iloc[va_idx]
x_train, y_train = train_df[feature_cols].values, train_df[target_cols].values
x_valid, y_valid = valid_df[feature_cols].values, valid_df[target_cols].values
K.clear_session()
model = create_model_3l(len(feature_cols), len(target_cols))
early_stopping = tf.keras.callbacks.EarlyStopping(
 monitor = 'val_binary_crossentropy',
mode = 'min',
patience = 10,
restore_best_weights = True,
verbose = 2 
)
 reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(
 monitor = 'val_binary_crossentropy',
mode = 'min',
factor = 0.3,
patience = 3,
verbose = 2 
)
 model.fit(
 x_train, y_train,
validation_data =(x_valid, y_valid),
epochs = EPOCHS,
batch_size = BATCH_SIZE,
callbacks = [early_stopping, reduce_lr],
verbose = 2 
)
 oof[va_idx] = model.predict(x_valid)
 predictions = model.predict(test[feature_cols].values)
 return oof, predictions
sub0.to_csv("submission.csv",index=False)
 sub0.head()<count_missing_values>
%%time
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for seed in SEED_AVG:
print(f'============== Training SEED is {seed} ==============')
 start = time.time()
oof_fold = np.zeros(( len(train), len(target_cols)))
 predictions_fold = np.zeros(( len(test), len(target_cols)))
 mskf = MultilabelStratifiedKFold(n_splits=NFOLDS, random_state=seed, shuffle=True)
 for i_fold,(tr_idx, va_idx)in enumerate(tqdm(mskf.split(X=train, y=target))):
oof_, pred_ = run_training(tr_idx, va_idx, i_fold, seed)
 oof_fold += oof_
predictions_fold += pred_ / NFOLDS
oof_score = mean_log_loss(target.drop('sig_id', axis=1 ).values, oof_fold)
 print(f'seed: {seed} fold mean log loss score is {oof_score}')
 oof += oof_fold / len(SEED_AVG)
 predictions += predictions_fold / len(SEED_AVG)
 elapsed_time = time.time() - start
print(f'SEED: {seed} Elapsed_time:{elapsed_time:.4f} sec')
 seed_log_loss = mean_log_loss(target.drop('sig_id', axis=1 ).values, oof)
 print(f'Our out of folds log loss for our seed blend model is {seed_log_loss}')
 print(f'==================== Training END ====================' )
sub0.isna().sum()<compute_test_metric>
train[target_cols] = oof
test[target_cols] = predictions
print(len(target_cols))
valid_results = df_Train_target_scored.drop(columns=target_cols ).merge(train[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 print(df_Train_target_scored.shape, valid_results.shape )
def rmsle(y, y_pred):
assert len(y)== len(y_pred)
 terms_to_sum = [(math.log(y_pred[i] + 1)- math.log(y[i] + 1)) ** 2.0 for i,pred in enumerate(y_pred)]
return(sum(terms_to_sum)*(1.0/len(y)))** 0.5
def fix_target(frame, key, target, new_target_name="target"):
corrections = 0
group_keys = frame[ key].values.tolist()
target = frame[target].values.tolist()
for i in range(1, len(group_keys)- 1):
previous_group = group_keys[i - 1]
current_group = group_keys[i]
previous_value = target[i - 1]
current_value = target[i]
if current_group == previous_group:
if current_value<previous_value:
current_value=previous_value
target[i] =current_value
target[i] =max(0,target[i])
 frame[new_target_name] = np.array(target)
 def rate(frame, key, target, new_target_name="rate"):
corrections = 0
group_keys = frame[ key].values.tolist()
target = frame[target].values.tolist()
rate=[1.0 for k in range(len(target)) ]
for i in range(1, len(group_keys)- 1):
previous_group = group_keys[i - 1]
current_group = group_keys[i]
previous_value = target[i - 1]
current_value = target[i]
if current_group == previous_group:
if previous_value!=0.0:
rate[i]=current_value/previous_value
rate[i] =max(1,rate[i])
 frame[new_target_name] = np.array(rate)
 def get_data_by_key(dataframe, key, key_value, fields=None):
mini_frame=dataframe[dataframe[key]==key_value]
if not fields is None:
mini_frame=mini_frame[fields].values
return mini_frame
directory="/kaggle/input/covid19-global-forecasting-week-4/"
train=pd.read_csv(directory + "train.csv", parse_dates=["Date"] , engine="python")
 test=pd.read_csv(directory + "test.csv", parse_dates=["Date"], engine="python")
 train["key"]=train[["Province_State","Country_Region"]].apply(lambda row: str(row[0])+ "_" + str(row[1]),axis=1)
 test["key"]=test[["Province_State","Country_Region"]].apply(lambda row: str(row[0])+ "_" + str(row[1]),axis=1)
 max_train_date=train["Date"].max()
max_test_date=test["Date"].max()
horizon=(max_test_date-max_train_date ).days
print("horizon", int(horizon))
target1="ConfirmedCases"
target2="Fatalities"
key="key"<categorify>
y_true = df_Train_target_scored[target_cols].values
y_pred = valid_results[target_cols].values
score = 0
for i in range(len(target_cols)) :
score_ = log_loss(y_true[:, i], y_pred[:, i])
 score += score_ / target.shape[1]
print("CV log_loss: ", score )
fix_target(train, key, target1, new_target_name=target1)
 fix_target(train, key, target2, new_target_name=target2)
 rate(train, key, target1, new_target_name="rate_" +target1)
 rate(train, key, target2, new_target_name="rate_" +target2)
 unique_keys=train[key].unique()
print(len(unique_keys))
train<groupby>
config.is_kaggle
<train_model><EOS> 
if config.is_kaggle:
sub = sample_submission.drop(columns=target_cols ).merge(test[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 sub.to_csv('submission.csv', index=False )