Kaggle competition: COVID19 Global Forecasting (Week 4)
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-4)<define_variables>
import numpy as np
import pandas as pd
import datetime as datetime
from sklearn import preprocessing
n_channels = len(test_dataset.channels )<load_pretrained>
register_matplotlib_converters()
def load_n_remap(model, model_path, device):
model_state = torch.load(model_path, map_location=device)
 mapping = { k:v for k,v in zip(model_state.keys() , model.state_dict().keys())}
mapped_model_state = OrderedDict([ 
(mapping[k], v)for k,v in model_state.items() if k in mapping.keys()
])
 model.load_state_dict(mapped_model_state, strict=False)
 return model<normalization>
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6):
super().__init__()
self.p = nn.Parameter(torch.ones(1)*p)
 self.eps = eps
def forward(self, x):
return gem(x, p=self.p, eps=self.eps)
 def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
def get_model(model_path=None, n_classes=n_classes):
model = EfficientNet(num_classes=n_classes,
width_coefficient=1.4, depth_coefficient=1.8,
dropout_rate=dropout_rate)
 orig_stem = model.stem[0]
model.stem[0] = nn.Conv2d(
 in_channels=n_channels,
out_channels=model.stem[0].out_channels,
kernel_size=model.stem[0].kernel_size,
stride=model.stem[0].stride,
padding=model.stem[0].padding,
bias=model.stem[0].bias)
 del model.head[6]
if model_path is not None:
model = load_n_remap(model, model_path, device)
 return model<normalization>
training_set = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 training_set.shape
class ArcModule(nn.Linear):
def __init__(self, in_features, out_features, s, m):
super().__init__(in_features=in_features, out_features=out_features, bias=False)
 self.s = s
self.m = m
self.cos_m = math.cos(m)
 self.sin_m = math.sin(m)
 self.th = math.cos(math.pi - m)
 self.mm = math.sin(math.pi - m)* m
def forward(self, inputs, labels=None):
cos_th = F.linear(inputs, F.normalize(self.weight))
if labels is None:
assert not self.training
return cos_th * self.s
cos_th = cos_th.clamp(-1, 1)
 sin_th = torch.sqrt(1.0 - torch.pow(cos_th, 2))
cos_th_m = cos_th * self.cos_m - sin_th * self.sin_m
cos_th_m = torch.where(cos_th > self.th, cos_th_m, cos_th - self.mm)
 cond_v = cos_th - self.th
cond = cond_v <= 0
cos_th_m[cond] =(cos_th - self.mm)[cond]
if labels.dim() == 1:
labels = labels.unsqueeze(-1)
 onehot = torch.zeros_like(cos_th)
 onehot.scatter_(1, labels, 1)
 outputs = onehot * cos_th_m +(1.0 - onehot)* cos_th
outputs = outputs * self.s
return outputs
class ArcNet(nn.Module):
def __init__(self, criterion, backbone, in_features, num_classes, channel_size, s=65, m=margin):
super().__init__()
assert num_classes % 2 == 0
self.num_classes = num_classes
self.criterion = criterion
self.backbone = backbone
self.feat = nn.Sequential(
 nn.Linear(in_features, channel_size, bias=False),
nn.BatchNorm1d(channel_size, momentum=0.01), 
)
 self.arc = ArcModule(channel_size, num_classes, s=s, m=m)
 def features(self, images, labels=None):
features = self.backbone(images)
 features = self.feat(features)
 features = F.normalize(features, dim=-1)
 return features
def forward(self, images, twins=None, labels=None):
if labels is None:
features = self.features(images)
 return self.arc(features)
 else:
return self.loss(images, twins, labels)
 def loss(self, images, twins, labels, cos_w=3):
f0 = self.features(images)
 f1 = self.features(twins)
 targets =(labels % n_targets)
 same_target = targets.expand(size=(len(targets), len(targets)))
 same_target = 2*(same_target == same_target.t() ).type_as(f0)- 1
cos_dist = 1 -(torch.mm(f0, f1.t())* same_target)
 cos_dist = cos_dist**2
loss = cos_w *(torch.mean(cos_dist))
pred0 = self.arc(f0, labels)
 pred1 = self.arc(f1, labels)
 return loss + 0.5 *(self.criterion(pred0, labels)+ self.criterion(pred1, labels))<choose_model_class>
test_set = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 test_set.shape
backbone = get_model(model_path if remove_head else None, n_classes)
 model = ArcNet(criterion=FocalLoss(gamma=focal_gamma, alpha=class_weights),
backbone=backbone,
in_features=backbone.head[0].out_channels,
num_classes=n_classes,
channel_size=channel_size)
 if not remove_head:
model = load_n_remap(model, model_path, device )<find_best_params>
N_train=len(training_set[training_set['Country_Region']==my_country])
 N_train
lr = warmup_learning_rate
if train_to:
training_blocks = model.backbone.blocks[train_to:].parameters()
else:
training_blocks = chain(model.backbone.stem.parameters() , model.backbone.blocks.parameters())
 params2freeze = None
if freeze_at_1st_n_last:
params2freeze = chain(model.backbone.stem.named_parameters() , model.backbone.blocks.named_parameters())
 elif train_to:
params2freeze = chain(model.backbone.stem.named_parameters() , model.backbone.blocks[:train_to].named_parameters())
 if params2freeze is not None:
for name, param in params2freeze:
print(f'"{name}" is frozen')
 param.requires_grad = False
optimizer = optim.SGD([
{ "params": training_blocks,
"lr": lr * 0.01,
},
{ "params": chain(model.feat[0].parameters() , model.backbone.head.parameters()),
"lr": lr * 0.2,
},
{ "params": chain(model.feat[1].parameters() , model.arc.parameters()),
"lr": lr
}],
momentum=0.9, weight_decay=1e-6, nesterov=True)
<train_model>
N_test=len(test_set[test_set['Country_Region']==my_country])
 N_test
if use_amp and training:
model, optimizer = amp.initialize(model, optimizer, opt_level="O2", num_losses=1 )<train_model>
df_test = test_set.copy()
df_test['DsType']='test'
df_test['ConfirmedCases']=0
df_test['Fatalities']=0
df_test.info()
def update_fn(engine, batch):
x0 = convert_tensor(batch[0], device=device, non_blocking=True)
 x1 = convert_tensor(batch[1], device=device, non_blocking=True)
 y = convert_tensor(batch[2], device=device, non_blocking=True)
 model.train()
loss = model(x0, x1, y)
 optimizer.zero_grad()
if use_amp:
with amp.scale_loss(loss, optimizer, loss_id=0)as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
optimizer.step()
return {
"batchloss": loss.item() ,
}<import_modules>
df_union=pd.concat([df_train,df_test],sort=False ).copy()
df_union.fillna('ND', inplace = True )
from ignite.engine import Engine, Events, create_supervised_evaluator
from ignite.metrics import RunningAverage, Accuracy, Precision, Recall, Loss, TopKCategoricalAccuracy
from ignite.contrib.handlers import TensorboardLogger
from ignite.contrib.handlers.tensorboard_logger import OutputHandler, OptimizerParamsHandler<compute_test_metric>
df_union['Month']=df_union['Date'].apply(lambda s : int(s.replace('-','')[4:6]))
df_union['Day']=df_union['Date'].apply(lambda s : int(s.replace('-','')[6:9]))
df_union['Date']=df_union['Date'].apply(lambda s : datetime.datetime.strptime(s, '%Y-%m-%d'))
df_union['DateOrd']=df_union['Date'].apply(lambda s : s.toordinal())
 df_union['Province_Norm']=df_union['Country_Region']+'-'+df_union['Province_State']
df_union.tail()
trainer = Engine(update_fn)
 def siRNAaccuracyMax1108(pred, y):
" Check predictions only on classes present in the validation and test sets "
pred_ = pred.reshape([pred.shape[0], n_classes // n_targets, n_targets] ).max(dim=1 ).values
pred_ = torch.argmax(pred_[...,:1108], dim=-1)
 y_ =(y % n_targets)
 assert pred_.shape == y_.shape
return(pred_ == y_ ).float().mean().cpu().numpy()
metrics = {
'Score': Loss(siRNAaccuracyMax1108),
'Accuracy': Accuracy() ,
'Top5Accuracy': TopKCategoricalAccuracy(k=5),
}
evaluator = create_supervised_evaluator(model, metrics=metrics, device=device, non_blocking=True)
 history = Metrics(evaluator, eval_train_loader, output_transform=lambda out: out['batchloss'], interactive=is_interactive())
 history.attach(trainer, "batchloss" )<train_model>
le1 = preprocessing.LabelEncoder()
le1.fit(df_union['Country_Region'])
 df_union['Country'] = le1.transform(df_union['Country_Region'] )
log_path = f"./log"
tb_logger = TensorboardLogger(log_dir=log_path)
 tb_logger.attach(trainer,
log_handler=OutputHandler('training', ['batchloss', ]),
event_name=Events.ITERATION_COMPLETED )<init_hyperparams>
le2 = preprocessing.LabelEncoder()
le2.fit(df_union['Province_Norm'])
 df_union['Province'] = le2.transform(df_union['Province_Norm'])
 df_union.drop('Province_Norm',axis=1,inplace=True )
sim_epochs = 2
epoch_size = len(train_loader)
 cycle_mult = 1
lr_sched_params = {'param_name':'lr', 'cycle_size':epoch_size*2, 'cycle_mult':cycle_mult,
'start_value_mult':0.7, 'end_value_mult':0.5}
mom_sched_params = {'param_name':'momentum', 'cycle_size':epoch_size*2, 'cycle_mult':cycle_mult,
'start_value':0.82, 'end_value':0.92}
wd_sched_params = {'param_name':'weight_decay', 'cycle_size':epoch_size*sim_epochs, 'cycle_mult':cycle_mult,
'start_value':weight_decay/10, 'end_value':weight_decay}
lr = learning_rate
body_sched_params = {**lr_sched_params, 'start_value':lr/10, 'end_value':lr/(100*2**head_run)}
body_sched = CosineAnnealingScheduler(optimizer.param_groups[0], **body_sched_params)
 feat_sched_params = {**lr_sched_params, 'start_value':lr/5, 'end_value':lr/(50*2**head_run)}
feat_sched = CosineAnnealingScheduler(optimizer.param_groups[1], **feat_sched_params)
 head_sched_params = {**lr_sched_params, 'start_value':lr, 'end_value':lr/(20*2**head_run)}
head_sched = CosineAnnealingScheduler(optimizer.param_groups[2], **feat_sched_params)
 mom_sched = CosineAnnealingScheduler(optimizer, **mom_sched_params)
 wd_sched = CosineAnnealingScheduler(optimizer, **wd_sched_params)
 schedulers = [body_sched, feat_sched, head_sched, mom_sched, wd_sched]
names = ["lr(body)", "lr(feat)", "lr(head)", "momentum", "wd"]
lr_values0 = np.array(body_sched.simulate_values(num_events=epoch_size*sim_epochs, **body_sched_params))
lr_values1 = np.array(feat_sched.simulate_values(num_events=epoch_size*sim_epochs, **feat_sched_params))
lr_values2 = np.array(head_sched.simulate_values(num_events=epoch_size*sim_epochs, **head_sched_params))
wd_values = np.array(wd_sched.simulate_values(num_events=epoch_size*sim_epochs, **wd_sched_params))
mom_values = np.array(mom_sched.simulate_values(num_events=epoch_size*sim_epochs, **mom_sched_params))
fig = plt.figure(figsize=(16, 4))
ax = plt.subplot()
plt.title(f"Cosine annealing with start_value_mult={lr_sched_params['start_value_mult']}")
 ax.plot(lr_values0[:, 0], lr_values0[:, 1], label=names[0])
 ax.plot(lr_values1[:, 0], lr_values1[:, 1], label=names[1])
 ax.plot(lr_values2[:, 0], lr_values2[:, 1], label=names[2])
 ax.plot(wd_values[:, 0], wd_values[:, 1], label=names[4])
 ax.set_yscale('log')
 ax.set_xlabel('Batches processed')
 ax.set_ylabel("learning rate")
 ax.legend(frameon=False, loc='upper right')
 ax2 = ax.twinx()
ax2.plot(mom_values[:, 0], mom_values[:, 1], label=names[3])
 ax2.set_ylabel("momentum")
 ax2.legend(frameon=False, loc='lower right')
 _ = ax.plot()<train_on_grid>
t_max_tr = max(df_union['Date'][df_union['DsType']=='train'])
 t_min_te = min(df_union['Date'][df_union['DsType']=='test'])
 Noverlap=(t_max_tr-t_min_te ).days+1
Noverlap
scheduler = ParamGroupScheduler(schedulers=schedulers, names=names)
 trainer.add_event_handler(Events.ITERATION_STARTED, scheduler )<train_model>
def cut_neg(y):
m = 0
for i in range(0,len(y)) :
if y[i]<m : y[i]=m
return y
tb_logger.attach(trainer,
log_handler=OptimizerParamsHandler(optimizer, "lr"),
event_name=Events.EPOCH_STARTED )<set_options>
def polynomial_trend(deg,X_train,y_true,X_test):
pf = PolynomialFeatures(degree=deg)
 pr = pf.fit_transform(X_train)
 lrm = LinearRegression()
lrm.fit(pr, y_true)
 y_valid = lrm.predict(pf.fit_transform(X_train))
y_pred = lrm.predict(pf.fit_transform(X_test))
y_valid = cut_neg(y_valid)
 y_pred = cut_neg(y_pred)
 return y_valid,y_pred
def setup_logger(logger):
handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
 handler.setFormatter(formatter)
 logger.addHandler(handler)
 logger.setLevel(logging.INFO )<choose_model_class>
Nlim = 5
delta_max = 0.1
def calc_trends(X_train,y_true,X_test,deg):
pct_hist = 100*sum(1 for x in y_true if x > 0)/N_train
y=np.array(y_true ).flatten()
y_last = y[len(y)-1]
y_check = y[len(y)-1-Nlim]
if(y_check==0):
den = 1
else :
den = y_check
diff = np.abs(y_last-y_check)/den
lim = np.mean(y_true[len(y_true)-Nlim:len(y_true)])
 if(diff<=delta_max):
y_valid = np.full([len(y_true)],lim)
 y_pred = np.full([len(X_test)],lim)
 msg1 = 'cost'
msg2 = 'cost'
if(diff>delta_max):
y_valid,y_pred = polynomial_trend(deg,X_train,y_true,X_test)
 msg1 = 'poly'
msg2 = 'poly'
return y_valid, y_pred, msg1, msg2
trainer.add_event_handler(Events.ITERATION_COMPLETED, TerminateOnNan())
 def default_score_fn(engine):
score = engine.state.metrics['Score']
return score
best_model_handler = ModelCheckpoint(dirname=log_path,
filename_prefix="best",
n_saved=10,
score_name="Score",
score_function=default_score_fn,
require_empty=False)
 evaluator.add_event_handler(Events.COMPLETED, best_model_handler, {'model': model, })
 @trainer.on(Events.EPOCH_COMPLETED)
 @evaluator.on(Events.COMPLETED)
 def empty_cuda_cache(engine):
if debug and device == 'cuda': print('max_memory_allocated:', torch.cuda.max_memory_allocated())
 torch.cuda.empty_cache()
gc.collect()<init_hyperparams>
df_train = df_union[df_union['DsType']=='train'].drop('DsType',axis=1)
 df_test = df_union[df_union['DsType']=='test'].drop('DsType',axis=1)
 df_train['ConfirmedCasesValid'] = 0
df_train['FatalitiesValid'] = 0
df_train['ConfirmedCasesTrend'] = 0
df_train['FatalitiesTrend'] = 0
df_train['ConfirmedCasesResid'] = 0
df_train['FatalitiesResid'] = 0
df_test['ConfirmedCases'] = 0
df_test['Fatalities'] = 0
df_test['ConfirmedCasesTrend'] = 0
df_test['FatalitiesTrend'] = 0
df_test['ConfirmedCasesResid'] = 0
df_test['FatalitiesResid'] = 0
NUM_EPOCHS =(4 if debug else sim_epochs)
 if freeze_at_1st_n_last:
@trainer.on(Events.EPOCH_STARTED)
 def turn_on_layers(engine):
epoch = engine.state.epoch
if(epoch == 1)or(epoch == NUM_EPOCHS):
for name, param in model.named_parameters() :
if('backbone' not in name)or('head' in name):
pbar.log_message(f'training "{name}"')
 param.requires_grad = True
else:
param.requires_grad = False
elif(epoch == 2):
count = 0
for param in training_blocks:
if param.requires_grad == False:
param.requires_grad = True
count += 1
pbar.log_message(f"Epoch {epoch}: training all layers({count})" )<train_model>
y1_train = df_train['ConfirmedCases'].astype(float)
 y2_train = df_train['Fatalities'].astype(float )
if training:
state = trainer.run(train_loader, max_epochs=NUM_EPOCHS )<choose_model_class>
def isPos(x):
if(x>0):
return 1
else :
return 0
best_model = model<find_best_params>
df_train['ConfirmedCasesPos']=df_train['ConfirmedCases'].apply(isPos)
 df_train['FatalitiesPos']=df_train['Fatalities'].apply(isPos)
 df_test['ConfirmedCasesPos']=df_test['ConfirmedCases'].apply(isPos)
 df_test['FatalitiesPos']=df_test['Fatalities'].apply(isPos )
!ls {log_path}
checkpoints = next(os.walk(log_path)) [2]
checkpoints = sorted(filter(lambda f: f.endswith(".pth"), checkpoints))
scores = [c.split('=')[-1][:-4] for c in checkpoints]
best_epoch = 0
if len(scores)> 0:
best_epoch = np.argmax(scores)
 print(best_epoch, scores)
 if not checkpoints:
print('No weight files in {}'.format(log_path))
else:
model_path = f'fold{fold}h{head_num}r{head_run}_{best_epoch}_{scores[best_epoch]}.pth'
!cp {os.path.join(log_path, checkpoints[best_epoch])} {model_path}
best_model.load_state_dict(torch.load(model_path))
print(model_path)
 if not is_interactive() :
!rm {log_path}/*.pth<find_best_params>
sel_cols = ['Country_Region','Province_State','ConfirmedCasesPos','FatalitiesPos']
df_tr_cnt = df_train[sel_cols].groupby(by=['Country_Region','Province_State'] ).sum()
df_te_cnt = df_test[sel_cols].groupby(by=['Country_Region','Province_State'] ).sum()
best_model = best_model.to(device ).eval()<find_best_params>
sel_cols = ['Date','Country_Region','Province_State','ConfirmedCases','Fatalities']
df_tr_last = df_train[df_train['Date']==max(df_train['Date'])][sel_cols]
df_te_last = df_test[df_test['Date']==max(df_test['Date'])][sel_cols]
with torch.no_grad() :
y_pred = best_model(batch[0][:1].to(device))
for idx in torch.topk(y_pred, k=10)[1].squeeze(0 ).tolist() :
prob = torch.softmax(y_pred, dim=-1)[0, idx].item()
print('{label:<75}({p:.8f}%)'.format(label=str(idx), p=prob*100))
del batch<normalization>
df_tr_last = df_tr_last.merge(df_tr_cnt,on=['Country_Region','Province_State'])
 df_te_last = df_te_last.merge(df_te_cnt,on=['Country_Region','Province_State'] )
TTA = 9
def tta(sites):
pred = []
for site in sites:
site = site.to(device)
 for i in range(TTA):
x = tta9crop(site, i, resolution)
 pred.append(best_model(x ).unsqueeze(0))
pred = torch.cat(pred)
 pred = pred.reshape([pred.shape[0], pred.shape[1], n_classes // n_targets, n_targets] ).max(dim=-2 ).values
return pred[...,:1108].softmax(dim=-1 ).mean(dim=0 ).cpu()<prepare_output>
df_last = df_tr_last.merge(df_te_last,on=['Country_Region','Province_State'])
 df_last['DeltaConfirmedCases']=100*(df_last['ConfirmedCases_y']-df_last['ConfirmedCases_x'])/df_last['ConfirmedCases_x']
df_last['DeltaFatalities']=100*(df_last['Fatalities_y']-df_last['Fatalities_x'])/df_last['Fatalities_x']
df_last['DeltaConfirmedCases']=df_last['DeltaConfirmedCases'].apply(np.abs)
 df_last['DeltaFatalities']=df_last['DeltaFatalities'].apply(np.abs)
 df_last.fillna(0,inplace=True)
 df_last = df_last.replace([np.inf, -np.inf], 0 )
def inference_update_with_tta(engine, batch):
global preds, allpreds
with torch.no_grad() :
sites = batch
pred = tta(sites)
 allpreds = np.concatenate([allpreds,(-np.log(pred.numpy())).astype(np.float16)])
 preds +=(torch.argmax(pred, dim=-1)% n_targets ).tolist()<load_pretrained>
n_head = 5
d_lim1 = 120
d_lim2 = 200
sel_var ='ConfirmedCases'
sel_cols = ['Country_Region','Province_State',sel_var+'_x','Delta'+sel_var,sel_var+'Pos_x']
condition =(df_last['Delta'+sel_var]>d_lim1)&(df_last['Delta'+sel_var]<d_lim2)
 df_last[condition][sel_cols].sort_values(by=['Delta'+sel_var], ascending=True ).head(n_head )
if create_sub:
pd.DataFrame(allpreds, index=df_valid.id_code ).to_pickle(f'df_valid_fold{fold}_log.pkl.gz')
 print(glob.glob('*.gz'))<prepare_x_and_y>
n_head = 5
d_lim1 = 0
d_lim2 = 10
sel_var = 'Fatalities'
sel_cols = ['Country_Region','Province_State',sel_var+'_x','Delta'+sel_var,sel_var+'Pos_x']
condition =(df_last['Delta'+sel_var]>d_lim1)&(df_last['Delta'+sel_var]<d_lim2)
 df_last[condition][sel_cols].sort_values(by=['Delta'+sel_var], ascending=True ).head(n_head )
if create_sub:
test_y = np.exp(-pd.read_pickle('df_test_log.pkl.gz' ).astype(np.float64)).fillna(0)
 valid_y = np.exp(-pd.read_pickle(f'df_valid_fold{fold}_log.pkl.gz' ).astype(np.float64)).fillna(0)
 else:
test_y = np.exp(-pd.read_pickle(glob.glob(f'.. /input/*/df_test_log.pkl.gz')[0] ).astype(np.float64)).fillna(0)
 valid_y = np.exp(-pd.read_pickle(glob.glob(f'.. /input/*/df_valid_fold{fold}_log.pkl.gz')[0] ).astype(np.float64)).fillna(0)
 test_y = test_y / test_y.sum(axis=0)
 valid_y = valid_y / valid_y.sum(axis=0)
 df_valid['pred'] = valid_y.values.argmax(axis=1)
 df_valid['acc'] =(df_valid.pred == df_valid.sirna ).astype(int)
 print('validation accuracy(norm):', df_valid.acc.mean())
<compute_test_metric>
def plotExample() :
train_cond =(( df_train['Country_Region']==my_country)&(df_train['Province_State']==my_province))
test_cond =(( df_test['Country_Region']==my_country)&(df_test['Province_State']==my_province))
x_train_plt = df_train[train_cond]['Date']
y_train_plt = df_train[train_cond][my_variable]
y_valid_plt = df_train[train_cond][my_variable+'Valid']
y_trend_plt = df_train[train_cond][my_variable+'Trend']
y_resid_plt = df_train[train_cond][my_variable+'Resid']
x_test_plt = df_test[test_cond]['Date']
y_test_plt = df_test[test_cond][my_variable]
plt.rcParams["figure.figsize"] =(12,6)
 fig, ax = plt.subplots()
ax.plot(x_train_plt,y_train_plt,'o',color='orange', label='y_true')
 ax.plot(x_train_plt,y_valid_plt,'x',color='gray' , label ='y_valid')
 ax.plot(x_train_plt,y_trend_plt,'.',color='lightblue', label='y_trend')
 ax.plot(x_train_plt,y_resid_plt,'-',color='green', label='y_resid')
 ax.plot(x_test_plt,y_test_plt,'*',color='red', label='y_test')
 ax.set_xticks([])
 ax.legend()
preds = remove_leaked_sirna(df_valid.set_index('id_code'), valid_y.copy())
 best_score = -1
bestpreds = None
scores = []
for k in tqdm(range(10)) :
preds, newscore, sirna_preds = normalize_both_ways(preds, df_valid.sirna.values)
 print(f'Newscore:{newscore} Iter:{k}')
 scores.append(newscore)
 if newscore > best_score:
best_score = newscore
best_iter = k
bestpreds = sirna_preds 
<categorify>
sel_cols = ['Country_Region','Province_State','ConfirmedCases_x','ConfirmedCases_y','DeltaConfirmedCases','ConfirmedCasesPos_x']
df_last[sel_cols][(( df_last['Country_Region']==my_country)&(df_last['Province_State']==my_province)) ]
test_preds = remove_leaked_sirna(df_test.set_index('id_code'), test_y.copy())
 for i in tqdm(range(best_iter+1)) :
test_preds, _, _ = normalize_both_ways(test_preds)
 preds = assign_sirna(test_preds )<save_to_csv>
sel_cols = ['Country_Region','Province_State','ConfirmedCases_x','ConfirmedCases_y','DeltaConfirmedCases','ConfirmedCasesPos_x']
df_last[sel_cols][(( df_last['Country_Region']==my_country)&(df_last['Province_State']==my_province)) ]
if create_sub:
submission = pd.DataFrame({'id_code': pd.read_csv(path_data+'/sample_submission.csv' ).id_code.values,
'sirna': np.squeeze(preds ).astype(int)})
 submission.to_csv('submission.csv', index=False)
 submission.hist(bins=111)
 submission.head()<define_variables>
sel_cols = ['Country_Region','Province_State','ConfirmedCases_x','ConfirmedCases_y','DeltaConfirmedCases','ConfirmedCasesPos_x']
df_last[sel_cols][(( df_last['Country_Region']==my_country)&(df_last['Province_State']==my_province)) ]
model_id = 'ICN2247013813647982800'
score_threshold = 0.000001
gcp_service_account_json = '/kaggle/input/gcloudserviceaccountkey/kaggle-playground-170215-4ece6a076f22.json'
gcp_project_id = 'kaggle-playground-170215' 
<install_modules>
sel_cols = ['Country_Region','Province_State','ConfirmedCases_x','ConfirmedCases_y','DeltaConfirmedCases','ConfirmedCasesPos_x']
df_last[sel_cols][(( df_last['Country_Region']==my_country)&(df_last['Province_State']==my_province)) ]
!pip install google-cloud-automl<import_modules>
y1_valid = cut_neg(df_train['ConfirmedCasesValid'].copy())
 y2_valid = cut_neg(df_train['FatalitiesValid'].copy())
 y1_pred = cut_neg(df_test['ConfirmedCases'].copy())
 y2_pred = cut_neg(df_test['Fatalities'].copy() )
from concurrent.futures import ProcessPoolExecutor as PoolExecutor, as_completed
from google.cloud import automl_v1beta1
from tqdm import tqdm
import operator
import os
import pandas as pd
import sys
import time 
<predict_on_test>
mse =(mean_squared_log_error(y1_train, y1_valid)+mean_squared_log_error(y2_train, y2_valid)) /2
mse
def get_prediction(file_path, project_id, model_id):
name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
 with open(file_path, 'rb')as ff:
content = ff.read()
payload = {'image': {'image_bytes': content }}
params = {'score_threshold':str(score_threshold)}
request = prediction_client.predict(name, payload, params)
 return request 
<categorify>
submission = pd.DataFrame({'ForecastId': df_test['ForecastId'],'ConfirmedCases': y1_pred,'Fatalities': y2_pred})
 submission.head()
def make_int(s):
try:
int(s)
 return int(s)
 except ValueError:
return 1109 
<define_variables>
submission.to_csv('submission.csv',index = False )