Kaggle competition: OSIC Pulmonary Fibrosis Progression
<SOS> metric: LaplaceLogLikelihood Kaggle data source: osic-pulmonary-fibrosis-progression<categorify>
cross_val_score, GroupKFold, GridSearchCV,
cross_val_predict, RandomizedSearchCV)
 plt.style.use("dark_background")
 %matplotlib inline
key_to_confirmed_rate={}
key_to_fatality_rate={}
key_to_confirmed={}
key_to_fatality={}
print(len(features_cv), len(name_cv),len(standard_confirmed_cv),len(standard_fatalities_cv))
print(preds_confirmed_cv.shape,preds_confirmed_standard_cv.shape,preds_fatalities_cv.shape,preds_fatalities_standard_cv.shape)
 for j in range(len(name_cv)) :
key_to_confirmed_rate[name_cv[j]]=preds_confirmed_cv[j,:].tolist()
key_to_fatality_rate[name_cv[j]]=preds_fatalities_cv[j,:].tolist()
key_to_confirmed[name_cv[j]] =preds_confirmed_standard_cv[j,:].tolist()
key_to_fatality[name_cv[j]]=preds_fatalities_standard_cv[j,:].tolist() 
<merge>
train_files = tf.io.gfile.glob(main_dir+"/train/*/*")
 test_files = tf.io.gfile.glob(main_dir+"/test/*/*")
 sample_sub = pd.read_csv(main_dir + "/sample_submission.csv")
 train = pd.read_csv(main_dir + "/train.csv")
 test = pd.read_csv(main_dir + "/test.csv")
 print("Number of train patients: {}
Number of test patients: {:4}" 
.format(train.Patient.nunique() , test.Patient.nunique()))
print("
Total number of Train patient records: {}
Total number of Test patient records: {:6}" 
.format(len(train_files), len(test_files)))
 train.shape, test.shape, sample_sub.shape
train_new=train[["Date","ConfirmedCases","Fatalities","key","rate_ConfirmedCases","rate_Fatalities"]]
test_new_count=pd.merge(test,train_new, how="left", left_on=["key","Date"], right_on=["key","Date"] ).reset_index(drop=True)
 test_new_count<categorify>
def laplace_log_likelihood(y_true, y_pred, sigma=70):
sigma_clipped = tf.maximum(sigma, 70)
 delta_clipped = tf.minimum(tf.abs(y_true - y_pred), 1000)
 delta_clipped = tf.cast(delta_clipped, dtype=tf.float32)
 sigma_clipped = tf.cast(sigma_clipped, dtype=tf.float32)
 score = - tf.sqrt(2.0)* delta_clipped / sigma_clipped - tf.math.log(tf.sqrt(2.0)* sigma_clipped)
 return tf.reduce_mean(score )
def fillin_columns(frame,key_column, original_name, training_horizon, test_horizon, unique_values, key_to_values):
keys=frame[key_column].values
original_values=frame[original_name].values.tolist()
print(len(keys), len(original_values), training_horizon ,test_horizon,len(key_to_values))
for j in range(unique_values):
current_index=(j *(training_horizon +test_horizon)) +training_horizon
current_key=keys[current_index]
values=key_to_values[current_key]
co=0
for g in range(current_index, current_index + test_horizon):
original_values[g]=values[co]
co+=1
frame[original_name]=original_values
all_days=int(test_new.shape[0]/len(unique_keys))
tr_horizon=all_days-horizon
print(all_days,tr_horizon, horizon)
 fillin_columns(test_new_count,"key", 'ConfirmedCases', tr_horizon, horizon, len(unique_keys), key_to_confirmed)
 fillin_columns(test_new_count,"key", 'Fatalities', tr_horizon, horizon, len(unique_keys), key_to_fatality)
 fillin_columns(test_new_count,"key", 'rate_ConfirmedCases', tr_horizon, horizon, len(unique_keys), key_to_confirmed_rate)
 fillin_columns(test_new_count,"key", 'rate_Fatalities', tr_horizon, horizon, len(unique_keys), key_to_fatality_rate)
 test_dat=test_new['Fatalities'].values.tolist()
test_new_countfat=test_new_count['Fatalities'].values.tolist()
for j in range(len(test_dat)) :
if test_dat[j]<1:
test_dat[j]=test_new_countfat[j]
test_new2= test_new.copy()
test_new2['Fatalities']=test_dat
test_new2 
<save_to_csv>
def l1(s):
def scorer_func(x, y, sigma=s):
return laplace_log_likelihood(x, y, sigma=s ).numpy()
return make_scorer(scorer_func, greater_is_better=False )
submission=test_new2[["ForecastId","ConfirmedCases","Fatalities"]]
submission.to_csv("submission.csv", index=False)
<import_modules>
def base_shift(data, q=50):
x = data.copy()
temp =(x.groupby("Patient")
.apply(lambda x: x.loc[int(
 np.percentile(x['Weeks'].index, q=q)
), ["Weeks", "FVC", "Percent"]]))
temp.rename(
 {"Weeks": "Base_Week",
"FVC": "Base_FVC",
"Percent": "Base_Percent"},
axis=1, inplace=True)
 x = x.merge(temp, on='Patient')
 x['Week_Offset'] = x['Weeks'] - x['Base_Week']
return x
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import ExtraTreesRegressor, RandomForestRegressor, GradientBoostingRegressor
from sklearn.svm import SVR
from lightgbm import LGBMRegressor
from xgboost import XGBRegressor
from sklearn.preprocessing import PolynomialFeatures<load_from_csv>
def multi_baseweek_frame(data, display=True):

 op = data.merge(
 data[['Patient', 'Weeks', 'FVC', 'Percent']].rename(
 {"Weeks": "Base_Week",
"FVC": "Base_FVC",
"Percent": "Base_Percent"}, axis=1),
on='Patient')
 op['Week_Offset'] = op['Weeks'] - op['Base_Week']
op = op[op['Week_Offset'] != 0]
if display:
print("Number of Samples:{:5} -> {:5}
Number of Columns:{:5} -> {:5}".format(
 data.shape[0], op.shape[0], data.shape[1], op.shape[1]))
return op.sort_values(by=['Patient', 'Base_Week'] ).reset_index(drop=True )
test = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 train = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 sub = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )<categorify>
def augment_train_cosine(data, n_similar=3, threshold=0.25, display_sample=True):

 temp = base_shift(data.copy() , q=0)
 temp['present_minus_past'] = temp.groupby("Patient")['FVC'].transform('diff' ).fillna(0)
 temp['Week_diff'] = temp.groupby("Patient")['Weeks'].transform('diff' ).fillna(0)
 temp['pms'] =(temp['present_minus_past'] / temp['Week_diff'] ).replace([np.inf, -np.inf] ).fillna(0)
 temp['pms_min'] = temp.groupby("Patient")['pms'].transform('min')
 temp['pms_25'] = temp.groupby("Patient")['pms'].transform(lambda x: np.percentile(x, q=25))
temp['pms_mean'] = temp.groupby("Patient")['pms'].transform('mean')
 temp['pms_75'] = temp.groupby("Patient")['pms'].transform(lambda x: np.percentile(x, q=75))
temp['pms_max'] = temp.groupby("Patient")['pms'].transform('max')
 temp['pms_sum'] = temp.groupby("Patient")['pms'].transform('sum')
 temp['pmb_avg'] =(temp['Percent'] - temp['Base_Percent'] ).groupby(temp.Patient ).transform("mean")
 temp['p_std'] = temp.groupby("Patient")['Percent'].transform('std')
 temp = temp.merge(
 pd.concat([
temp.groupby("Patient" ).apply(
 lambda x:(x['Percent'].values[-1] - x['Percent'].values[0])/ 
(x['Weeks'].values[-1] - x['Weeks'].values[0])).rename("Slope"),
temp.groupby("Patient" ).apply(
 lambda x: x['Week_Offset'].iloc[np.argmax(x['pms'])] ).rename("pmsw_max"),
temp.groupby("Patient" ).apply(
 lambda x: x['Week_Offset'].iloc[np.argmin(x['pms'])] ).rename("pmsw_min")
 ], axis=1, ignore_index=False), on='Patient')
 temp = temp.groupby('Patient' ).head(1 ).reset_index(drop=True)
 temp['factor'] = temp['Base_FVC'] / temp['Base_Percent']
train_cols = [
'Patient',
'pms_25', 'pms_mean', 'pms_75',
'pms_sum', 'pmsw_min', 'pmsw_max',
'pmb_avg', 'Slope', 'factor'
]
cat_cols = np.intersect1d(['Sex', 'SmokingStatus'], train_cols)
 temp = temp[train_cols]
temp = pd.get_dummies(temp, columns=cat_cols, drop_first=True, prefix='', prefix_sep='')
 n_similar += 1
groups =(pd.DataFrame(
 np.argsort(
 cosine_similarity(temp.drop("Patient", 1), temp.drop('Patient', 1)))
 [:, -1:-n_similar-1:-1]))
groups = groups[~pd.DataFrame(np.sort(groups.values, axis=1)).duplicated() ]
groups = groups.applymap(lambda x: temp.Patient.to_dict() [x] ).apply(list, axis=1 ).to_dict()
aug_data = []
for group in tqdm(groups.values() , disable=not display_sample):
temp = base_shift(train[train.Patient.isin(group)], q=0)
 temp['base_per_diff_from_mean'] = temp['Base_Percent'] - temp['Base_Percent'].unique().mean()
temp['Percent_shifted'] = temp['Percent'] - temp['base_per_diff_from_mean']
temp['base_week_diff_from_mean'] = temp['Base_Week'] - temp['Base_Week'].unique().mean()
temp['Week_shifted'] = temp['Weeks'] - temp['base_week_diff_from_mean']
temp = pd.merge_ordered(
 temp.drop(['Age', 'Sex', 'SmokingStatus', 'Base_Week', 'Week_Offset'], axis=1), 
(temp.groupby("Week_shifted")['Percent_shifted'] 
.agg(['mean', 'count'] ).query(f'count > {n_similar * threshold}')
.drop("count", 1)) ,
on='Week_shifted', left_by='Patient' 
)
 aug_data.append(temp)
 temp = pd.concat(aug_data ).reset_index(drop=True)
 temp[['base_per_diff_from_mean', 'base_week_diff_from_mean', 'Base_Percent', 'Base_FVC']] =(
 temp.groupby("Patient")[['base_per_diff_from_mean', 'base_week_diff_from_mean',
'Base_Percent', 'Base_FVC']].fillna(method='ffill'))
temp['Week_aug'] = temp['Week_shifted'] + temp['base_week_diff_from_mean']
temp['Percent_aug'] = np.where(
 temp['Percent'].isna() ,
temp['mean'] + temp['base_per_diff_from_mean'],
temp['Percent'])
 temp = temp[~temp['Percent_aug'].isna() ]
test_ids = temp['FVC'].isna()
temp.loc[test_ids, 'FVC'] = LinearRegression().fit(
 temp.loc[~test_ids, ['Base_Percent', 'Percent_aug', 'Base_FVC']],
temp.loc[~test_ids, 'FVC'] ).predict(
 temp.loc[test_ids, ['Base_Percent', 'Percent_aug', 'Base_FVC']])
 temp = temp.groupby(["Patient", 'Week_aug'] ).mean().reset_index()
temp = temp[['Patient', 'Week_aug', 'Percent_aug', 'FVC']]
temp = temp.rename({'Week_aug': 'Weeks', 'Percent_aug': 'Percent'}, axis=1)
 temp['Weeks'] = temp['Weeks'].astype(int)
 temp = temp.merge(
(data[['Patient', 'Sex', 'Age', 'SmokingStatus']] 
.groupby('Patient' ).head(1 ).reset_index(drop=True)) ,
on='Patient')
 if display_sample:
print("Data augmented by factor: {:.2f}x".format(1 +(
 temp.shape[0] - data.shape[0])/ data.shape[0]))
f, ax = plt.subplots(nrows=4, ncols=2, figsize=(20, 20))
for i, pat in enumerate(temp.Patient.unique() [:4]):
ax[i][0].plot(*list(zip(*data[data.Patient == pat][['FVC', 'Weeks']].values))
[::-1], c='g', alpha=0.7)
 ax[i][1].plot(*list(zip(*data[data.Patient == pat][['Percent', 'Weeks']].values))
[::-1], c='g', alpha=0.7)
 ax[i][0].scatter(*list(zip(*temp[temp.Patient == pat][['FVC', 'Weeks']].values))
[::-1], c='r')
 ax[i][1].scatter(*list(zip(*temp[temp.Patient == pat][['Percent', 'Weeks']].values))
[::-1], c='r')
 ax[i][0].set(xlabel='Weeks', ylabel='FVC')
 ax[i][1].set(xlabel='Weeks', ylabel='Percent')
 f.suptitle("FVC & Percent Augmentation", y=.9)
 return temp
def fuckProvince(s):
if pd.isnull(s['Province_State']):
if pd.isnull(s['Country_Region']):
return 'Null'
else:
return s['Country_Region']
else:
return s['Province_State']<feature_engineering>
def augment_train_naive(
 data, steps=5, method='index', noise=.25, val_split=0.25,
end_pts=[None, None], display_sample=True):

 temp = data[['Patient','Weeks', 'FVC', 'Percent']].merge(
(( data.groupby("Patient")['Weeks'] 
.apply(lambda x: pd.Series(
 np.union1d(np.arange(
 end_pts[0] if end_pts[0] else x.min() ,
end_pts[1] if end_pts[1] else x.max() ,
step=steps), x))
).reset_index(level=0))),
on=['Patient', 'Weeks'], how='right')
 temp.loc[:, ['FVC', 'Percent']] =(
 temp.groupby("Patient")[['FVC', 'Percent']] 
.apply(lambda x:(
 x.interpolate(method=method, limit_direction='both')+ 
(x.std().values * np.random.uniform(-noise, noise, [len(x), 1])))))
 temp = temp.merge(
 data.groupby("Patient")[['Patient', 'Age', 'Sex', 'SmokingStatus']].head(1),
on='Patient')
 if display_sample:
f, ax = plt.subplots(nrows=4, ncols=2, figsize=(20, 20))
for i, pat in enumerate(temp.Patient.unique() [:4]):
ax[i][0].plot(*list(zip(*data[data.Patient == pat][['FVC', 'Weeks']].values))
[::-1], c='g', alpha=0.7)
 ax[i][1].plot(*list(zip(*data[data.Patient == pat][['Percent', 'Weeks']].values))
[::-1], c='g', alpha=0.7)
 ax[i][0].scatter(*list(zip(*temp[temp.Patient == pat][['FVC', 'Weeks']].values)) [::-1], c='r')
 ax[i][1].scatter(*list(zip(*temp[temp.Patient == pat][['Percent', 'Weeks']].values)) [::-1], c='r')
 ax[i][0].set(xlabel='Weeks', ylabel='FVC')
 ax[i][1].set(xlabel='Weeks', ylabel='Percent')
 f.suptitle("FVC & Percent Augmentation", y=.9)
 print("Data augmented by factor: {:.2f}x".format(1 +(
 temp.shape[0] - data.shape[0])/ data.shape[0]))
return temp
train['Province_State'] = train.apply(fuckProvince,axis=1)
 test['Province_State'] = test.apply(fuckProvince,axis=1 )<create_dataframe>
sub = sample_sub.Patient_Week.str.extract("(ID\w+)_(\-?\d+)" ).rename({0: "Patient", 1: "Weeks"}, axis=1)
 sub['Weeks'] = sub['Weeks'].astype(int)
 sub = pd.merge(sub, test[['Patient', 'Sex', 'SmokingStatus']], on='Patient')
 sub["Patient_Week"] = sub.Patient + "_" + sub.Weeks.astype(str)
 sub.head()
tr = train.copy()
ts = test.copy()<prepare_x_and_y>
class GBR(RegressorMixin, BaseEstimator):
def __init__(self, alpha=.75, **params):
self.alpha = alpha
self.umodel = self._create_model(loss='quantile', q=self.alpha, **params)
 self.mmodel = self._create_model(loss='lad', **params)
 self.lmodel = self._create_model(loss='quantile', q=1-self.alpha, **params)
 def _create_model(self, loss, q=.75, **params):
model = GradientBoostingRegressor(
 init=LinearRegression() ,
criterion='friedman_mse',
n_estimators=50, max_depth=2,
loss=loss, alpha=q, **params)
 return model
def fit(self, x, y):
self.umodel.fit(x, y)
 self.mmodel.fit(x, y)
 self.lmodel.fit(x, y)
 return self
def predict(self, X):
return self.mmodel.predict(X)
 def predict_forecast(self, X, return_bounds=False):
preds = self.mmodel.predict(X)
 upper = self.umodel.predict(X)
 lower = self.lmodel.predict(X)
 if return_bounds:
return preds, upper, lower
else:
return preds,(upper - lower )
X = train.drop(['ConfirmedCases','Fatalities'],axis=1)
 y = train[['ConfirmedCases','Fatalities']]
y1 = train['ConfirmedCases']
y2 = train['Fatalities']<feature_engineering>
op = multi_baseweek_frame(
 augment_train_cosine(train, display_sample=False, n_similar=5, threshold=.5)
 )
def fuckDate(s):
return s.month * 100 + s.day<data_type_conversions>
cat_cols = ['Sex', 'SmokingStatus']
to_drop = ['FVC', 'Percent', 'Weeks', 'factor', 'Base_Percent']
num_cols = ['Weeks', 'Week_Offset', 'Base_Week', 'Age', 'Base_FVC', 'Percent', 'Base_Percent']
cat_method = 'ord'
math = []
age_bins = 5
folds = 7
total_patients = train.Patient.unique()
np.random.shuffle(total_patients)
 val_len = len(total_patients)// folds
temp = pd.DataFrame()
preds = pd.DataFrame()
X, Y = get_model_data(
 op, factor=True, num_cols=num_cols, cat_cols=cat_cols,
age_bins=age_bins, to_drop=to_drop, display_stats=False,
cat_method=cat_method, transform_stats=stats, math=math)
 X_VAL = base_shift(train, q=0)
 Y_VAL = X_VAL['FVC'].dropna()
X_VAL['Percent'] = X_VAL['Base_Percent']
X_VAL = get_model_data(
 X_VAL, num_cols=num_cols, cat_cols=cat_cols, to_drop=to_drop, factor=True,
cat_method=cat_method, train=False, age_bins=age_bins, math=math)
 x_test = sub[['Patient', 'Weeks']].merge(
 test.rename({"Weeks": "Base_Week",
"FVC": "Base_FVC",
"Percent": "Base_Percent"}, axis=1),
on='Patient')
 x_test['Week_Offset'] = x_test['Weeks'] - x_test['Base_Week']
x_test['Percent'] = x_test['Base_Percent']
x_test = get_model_data(
 x_test, cat_cols=cat_cols, num_cols=num_cols, to_drop=to_drop, math=math,
factor=True, train=False, cat_method=cat_method,
age_bins=age_bins ).drop("Patient", 1)
 for i in range(folds):
val_patients = total_patients[(i)*val_len:(i+1)*val_len]
train_patients = np.setdiff1d(total_patients, val_patients)
 assert len(np.intersect1d(val_patients, train_patients)) == 0
x, y, =(X[X.Patient.isin(train_patients)].drop("Patient", 1), Y[X.Patient.isin(train_patients)])
 x_val, y_val =(X_VAL[X_VAL.Patient.isin(val_patients)].drop("Patient", 1),
Y_VAL[X_VAL.Patient.isin(val_patients)])
 model = GBR(alpha=0.75)
 model.fit(x, y)
 y_middle, y_upper, y_lower = model.predict_forecast(x_val, return_bounds=True)
 y_middle_pred, y_test_conf = model.predict_forecast(x_test)
 print("For Fold
i+1, - laplace_log_likelihood(y_middle, y_val),
- laplace_log_likelihood(y_middle, y_val, y_upper - y_lower)))
 temp = temp.append(pd.DataFrame(
 data=np.stack([y_upper, y_lower, y_middle, y_val], axis=1),
columns=['upper', 'lower', 'pred', 'actual'] 
))
preds = preds.append(pd.DataFrame(
 data=np.stack([y_middle_pred, y_test_conf], axis=1)/ folds,
columns=['pred', 'Confidence'] 
))
preds = preds.groupby(preds.index ).sum()
temp['Confidence'] = temp['upper'] - temp['lower']
train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'] )<feature_engineering>
pat_scores =(
 temp.reset_index(drop=True)
.groupby(train.Patient)
.apply(lambda x: -laplace_log_likelihood(x['actual'], x['pred'], x['Confidence'] ).numpy())
 ).rename('scores' ).reset_index().sort_values("scores", ascending=False)
 pat_scores = pat_scores.head(8)
 print("Worst Patient-Mean-Score: {:.2f}".format(pat_scores.scores.mean()))
pat_scores = pat_scores.Patient.values
train['Fucking_Useful_Dates'] = train['Date'].apply(fuckDate)
 test['Fucking_Useful_Dates'] = test['Date'].apply(fuckDate )<feature_engineering>
print("
|================== Summary ==================|
\
Score on Total Dataset: {:.3f} @ 70 Confidence
\
Score on Total Dataset: {:.3f} @ 225 Confidence
\
Score on Total Dataset: {:.3f} @ Pred Confidence".format(
 -laplace_log_likelihood(temp['actual'], temp['pred'], 70),
-laplace_log_likelihood(temp['actual'], temp['pred'], 225),
-laplace_log_likelihood(temp['actual'], temp['pred'], temp['Confidence'])
))
f, ax = plt.subplots(figsize=(40, 40), nrows=4, ncols=2)
 ax = ax.ravel()
for i, pat in enumerate(pat_scores): 
(temp.reset_index(drop=True ).loc[train.Patient == pat] 
.drop(["Confidence"], 1 ).plot(ax=ax[i], legend=False))
f.suptitle("Model's Worst Predictions", size=30)
 f.tight_layout(rect=[0, 0.03, 1, 0.95]);
train['Month_People_Were_Fucked'] = train['Date'].dt.month
test['Month_People_Were_Fucked'] = test['Date'].dt.month<drop_column>
sub['FVC'] = preds['pred']
sub['Confidence'] = preds['Confidence']
for i in range(len(test)) :
sub.loc[sub['Patient_Week']==test.Patient[i]+'_'+str(test.Weeks[i]), 'FVC'] = test.FVC[i]
sub.loc[sub['Patient_Week']==test.Patient[i]+'_'+str(test.Weeks[i]), 'Confidence'] = 70
sub[['Patient_Week', 'FVC', 'Confidence']].to_csv("quant_submission.csv", index=False)
 sub.head()
train.drop(['Date'],axis=1,inplace=True)
 test.drop(['Date'],axis=1,inplace=True )<create_dataframe>
cat_cols = ['Sex', 'SmokingStatus']
num_cols = ['Weeks', 'Week_Offset', 'Base_Week', 'Age', 'Base_FVC', 'Percent', 'Base_Percent']
to_drop = ["FVC", 'Percent', 'Weeks', 'Base_Week', 'Age', 'Base_Percent', 'factor']
math = []
multi_method = {}
multi_data = {}
folds = 7
methods = ['cubic', 'quadratic', 'cubicspline',
'pchip', 'akima', 'nearest', 'zero',
'slinear', 'linear', 'SIMILARITY_AUG']
total_patients = train.Patient.unique()
val_len = len(total_patients)// folds
for method in methods:
if method == 'SIMILARITY_AUG':
temp = augment_train_cosine(train, n_similar=3, threshold=0.25, display_sample=False)
 else:
temp = augment_train_naive(data=train, method=method, steps=15, noise=0., display_sample=False)
 temp = multi_baseweek_frame(temp, display=False)
 multi_data[method] = temp
X, Y = get_model_data(temp, num_cols=num_cols, cat_cols=cat_cols, to_drop=to_drop,
display_stats=False, cat_method='1h', math=math, factor=True)
 X_VAL = base_shift(train, q=0)
 Y_VAL = X_VAL['FVC'].dropna()
X_VAL['Percent'] = X_VAL['Base_Percent']
X_VAL = get_model_data(
 X_VAL, num_cols=num_cols, cat_cols=cat_cols, factor=True,
to_drop=to_drop, train=False, cat_method='1h', math=math)
 np.random.shuffle(total_patients)
 scores = {}
for i in range(folds):
val_patients = total_patients[(i)*val_len:(i+1)*val_len]
train_patients = np.setdiff1d(total_patients, val_patients)
 x, y = X[X.Patient.isin(train_patients)], Y[X.Patient.isin(train_patients)]
x_val, y_val = X_VAL[X_VAL.Patient.isin(val_patients)], Y_VAL[X_VAL.Patient.isin(val_patients)]
assert len(np.intersect1d(x_val.Patient.unique() , x.Patient.unique())) == 0
scores['Train'] = scores.get('Train', [])+ [cross_val_score(
 GBR() , x.drop("Patient", 1), y,
scoring=l1(70), cv=GroupKFold(5), groups=x.Patient ).mean() ]
lr = GBR().fit(x.drop("Patient", 1), y)
 temp = lr.predict_forecast(x_val.drop("Patient", 1))
temp = pd.DataFrame(np.stack(temp, 1), columns=['pred', 'conf'])
 temp['actual'] = y_val.reset_index(drop=True)
 scores['Val'] = scores.get('Val', [])+ [-laplace_log_likelihood(
 temp['actual'], temp['pred'], 70
).numpy() ]
scores['ValC'] = scores.get('ValC', [])+ [-laplace_log_likelihood(
 temp['actual'], temp['pred'], temp['conf']
).numpy() ]
scores['ValW'] = scores.get('ValW', [])+ [temp.apply(lambda x: -laplace_log_likelihood(
 x['actual'], x['pred'], x['conf'] ).numpy() , 1 ).nlargest(25 ).mean() ]
print("Method: {}
Train score: {:5.2f} @ {:.2f} Variance \
Val Score: {:7.2f} @ {:.2f} Variance\
ValC Score: {:6.2f} @ {:.2f} Variance\
Worst Score: {:5.2f} @ {:.2f} Variance
{}
".format(
 method.upper() , np.mean(scores['Train']), np.std(scores['Train']),
np.mean(scores['Val']), np.std(scores['Val']), np.mean(scores['ValC']),
np.std(scores['ValC']), np.mean(scores['ValW']), np.std(scores['ValW']), "=" * 35 
))
multi_method[method] = scores
pred = pd.DataFrame(columns=['ForecastId','ConfirmedCases','Fatalities'] )<prepare_output>
val_scores = [np.mean(multi_method[i]['ValW'])for i in methods]
cut_off = np.percentile(val_scores, 50)
 print("At {:.1f} cutoff, the Worst Score would be {:.3f} @ Pred Conf".format(
 cut_off, np.mean(list(filter(lambda x: x < cut_off, val_scores)))) )
pred['ForecastId'] = test['ForecastId'].copy()<categorify>
x_test = sub[['Patient', 'Weeks']].merge(
 test.rename({"Weeks": "Base_Week",
"FVC": "Base_FVC",
"Percent": "Base_Percent"}, axis=1),
on='Patient')
 x_test['Week_Offset'] = x_test['Weeks'] - x_test['Base_Week']
x_test['Percent'] = x_test['Base_Percent']
preds = {i: None for i in methods}
for method in methods:
if np.mean(multi_method[method]['ValW'])>= cut_off:
preds.pop(method)
 continue
x, y = get_model_data(
 multi_data[method], num_cols=num_cols, cat_cols=cat_cols,
to_drop=to_drop, train=True, cat_method='1h',
display_stats=False, factor=True, 
)
 lr = GBR().fit(x.drop("Patient", 1), y)
 preds[method] = lr.predict_forecast(get_model_data(
 x_test, cat_cols=cat_cols, num_cols=num_cols, to_drop=to_drop, train=False, factor=True,
).drop("Patient", 1), return_bounds=True)
 preds.keys()
LE = LabelEncoder()
train['Province_State'] = LE.fit_transform(train['Province_State'])
 test['Province_State'] = LE.transform(test['Province_State'])
 LE2 = LabelEncoder()
train['Country_Region'] = LE2.fit_transform(train['Country_Region'])
 test['Country_Region'] = LE2.transform(test['Country_Region'] )<prepare_x_and_y>
temp = {}
for i in multi_method:
if i in preds.keys() :
temp['Val'] = temp.get('Val', [])+ [np.mean(multi_method[i]['Val'])]
temp['ValC'] = temp.get('ValC', [])+ [np.mean(multi_method[i]['ValC'])]
print("At same cutoff:
Best val score: {:6.3f} @ 70 Conf
\
Best Val score: {:6.3f} @ Pred Conf".format(np.mean(temp['Val']), np.mean(temp['ValC'])) )
<data_type_conversions><EOS> 
mean = np.mean(np.stack(pd.DataFrame(preds ).iloc[0].values), 0)
 upper = np.max(np.stack(pd.DataFrame(preds ).iloc[1].values), 0)
 lower = np.min(np.stack(pd.DataFrame(preds ).iloc[2].values), 0)
 sub['FVC'] = mean
sub['Confidence'] = upper - lower
sub[['Patient_Week', 'FVC', 'Confidence']].to_csv("submission.csv", index=False)
 sub.head()