Kaggle competition: OSIC Pulmonary Fibrosis Progression
dff["FrontageRatio"] =(dff["LotFrontage"] / dff["LotArea"])
 dff["HQFloor"] = dff["1stFlrSF"] + dff["2ndFlrSF"]
dff["FloorAreaRatio"] = dff["GrLivArea"] / dff["LotArea"]
dff["TotalArea"] = dff["TotalBsmtSF"] + dff["GrLivArea"]
dff["TotalPorch"] = dff["WoodDeckSF"] + dff["OpenPorchSF"] + dff["EnclosedPorch"] + dff["3SsnPorch"] + dff["ScreenPorch"]
dff["WeightedBsmtFinSF1"] = dff["BsmtFinSF1"] * dff["BsmtFinType1"]
dff["WeightedBsmtFinSF2"] = dff["BsmtFinSF2"] * dff["BsmtFinType2"]
dff["WeightedTotalBasement"] = dff["WeightedBsmtFinSF1"] + dff["BsmtFinSF2"] * dff["BsmtFinType2"] + dff["BsmtUnfSF"]
dff["TotalFullBath"] = dff["BsmtFullBath"] + dff["FullBath"]
dff["TotalHalfBath"] = dff["BsmtHalfBath"] + dff["HalfBath"]
dff["TotalBsmtBath"] = dff["BsmtFullBath"] + 0.5 * dff["BsmtHalfBath"]
dff["TotalBath"] = dff["TotalFullBath"] + 0.5 *(dff["BsmtHalfBath"] + dff["HalfBath"])+ dff["BsmtFullBath"] + 0.5 * dff["BsmtHalfBath"]
dff["HasPool"] = dff["PoolArea"].apply(lambda x: 0 if x == 0 else 1)
 dff["Has2ndFlr"] = dff["2ndFlrSF"].apply(lambda x: 0 if x == 0 else 1)
 dff["HasBsmt"] = dff["TotalBsmtSF"].apply(lambda x: 0 if x == 0 else 1)
 dff["HasFireplace"] = dff["Fireplaces"].apply(lambda x: 0 if x == 0 else 1)
 dff["HasGarage"] = dff["GarageArea"].apply(lambda x: 0 if x == 0 else 1)
 dff["HasLowQual"] = dff["LowQualFinSF"].apply(lambda x: 0 if x == 0 else 1)
 dff["HasPorch"] = dff["TotalPorch"].apply(lambda x: 0 if x == 0 else 1)
 dff["HasMiscVal"] = dff["MiscVal"].apply(lambda x: 0 if x == 0 else 1)
 dff["HasWoodDeck"] = dff["WoodDeckSF"].apply(lambda x: 0 if x == 0 else 1)
 dff["HasOpenPorch"] = dff["OpenPorchSF"].apply(lambda x: 0 if x == 0 else 1)
 dff["HasEnclosedPorch"] = dff["EnclosedPorch"].apply(lambda x: 0 if x == 0 else 1)
 dff["Has3SsnPorch"] = dff["3SsnPorch"].apply(lambda x: 0 if x == 0 else 1)
 dff["HasScreenPorch"] = dff["ScreenPorch"].apply(lambda x: 0 if x == 0 else 1)
 dff["TotalPorchType"] = dff["HasWoodDeck"] + dff["HasOpenPorch"] + dff["HasEnclosedPorch"] + dff["Has3SsnPorch"] + dff["HasScreenPorch"]
dff["TotalPorchType"] = dff["TotalPorchType"].apply(lambda x: 3 if x >=3 else x )<feature_engineering>
import numpy as np
import pandas as pd
import os
import random
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import KFold
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tqdm import tqdm
from PIL import Image
import pydicom
import pydicom
import matplotlib.pyplot as plt
from glob import glob
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import scipy.ndimage
from skimage import morphology
from skimage import measure
from skimage.transform import resize
from sklearn.cluster import KMeans
from plotly import __version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.tools import FigureFactory as FF
from plotly.graph_objs import *
dff["RestorationAge"] = dff["YearRemodAdd"] - dff["YearBuilt"]
dff["RestorationAge"] = dff["RestorationAge"].apply(lambda x: 0 if x < 0 else x)
 dff["HasRestoration"] = dff["RestorationAge"].apply(lambda x: 0 if x == 0 else 1)
 dff["YearAfterRestoration"] = dff["YrSold"] - dff["YearRemodAdd"]
dff["YearAfterRestoration"] = dff["YearAfterRestoration"].apply(lambda x: 0 if x < 0 else x)
 dff["BuildAge"] = dff["YrSold"] - dff["YearBuilt"]
dff["BuildAge"] = dff["BuildAge"].apply(lambda x: 0 if x < 0 else x)
 dff["IsNewHouse"] = dff["BuildAge"].apply(lambda x: 1 if x == 0 else 0)
 def year_map(year):
year = 1 if year <= 1895 else\ 
(2 if year <= 1916 else\ 
(3 if year <= 1919 else\ 
(4 if year <= 1929 else\ 
(5 if year <= 1941 else\ 
(6 if year <= 1945 else\ 
(7 if year <= 1964 else\ 
(8 if year <= 1980 else\ 
(9 if year <= 1991 else\ 
(10 if year < 2008 else 11)))))))))
 return year
dff["YearBuilt_bins"] = dff["YearBuilt"].apply(lambda year: year_map(year))
dff["YearRemodAdd_bins"] = dff["YearRemodAdd"].apply(lambda year: year_map(year))
dff["GarageYrBlt_bins"] = dff["GarageYrBlt"].apply(lambda year: year_map(year))
dff["YrSold"] = dff["YrSold"].astype(str)
 dff["MoSold"] = dff["MoSold"].astype(str)
 dff["Season"] = dff["MoSold"].apply(lambda x: "Winter" if x in ["12", "1", "2"] else\ 
("Spring" if x in ["3", "4", "5"] else\ 
("Summer" if x in ["6", "7", "8"] else "Fall")) )<feature_engineering>
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(42 )
dff["OverallValue"] = dff["OverallQual"] * dff["OverallCond"]
dff["ExterValue"] = dff["ExterQual"] * dff["ExterCond"]
dff["BsmtValue"] =(( dff["BsmtQual"] + dff["BsmtFinType1"] + dff["BsmtFinType2"])* dff["BsmtCond"])/ 2
dff["KitchenValue"] = dff["KitchenAbvGr"] * dff["KitchenQual"]
dff["FireplaceValue"] = dff["Fireplaces"] * dff["FireplaceQu"]
dff["GarageValue"] = dff["GarageQual"] * dff["GarageCond"]
dff["TotalValue"] = dff["OverallValue"] + dff["ExterValue"] + dff["BsmtValue"] + dff["KitchenValue"] + dff["FireplaceValue"] + dff["GarageValue"] +\
dff["HeatingQC"] + dff["Utilities"] + dff["Electrical"] - dff["Functional"] + dff["PoolQC"]
dff["TotalQual"] = dff["OverallQual"] + dff["ExterQual"] + dff["BsmtQual"] + dff["KitchenQual"] + dff["FireplaceQu"] + dff["GarageQual"] +\
dff["HeatingQC"] + dff["PoolQC"]
dff["TotalCond"] = dff["OverallCond"] + dff["ExterCond"] + dff["BsmtCond"] + dff["GarageCond"]
dff["TotalQualCond"] = dff["TotalQual"] + dff["TotalCond"]<feature_engineering>
ROOT = ".. /input/osic-pulmonary-fibrosis-progression"
BATCH_SIZE = 196
dff["BsmtSFxValue"] = dff["TotalBsmtSF"] * dff["BsmtValue"]
dff["BsmtSFxQual"] = dff["TotalBsmtSF"] * dff["BsmtQual"]
dff["TotalAreaXOverallValue"] = dff["TotalArea"] * dff["OverallValue"]
dff["TotalAreaXOverallQual"] = dff["TotalArea"] * dff["OverallQual"]
dff["GarageAreaXGarageValue"] = dff["GarageArea"] * dff["GarageValue"]
dff["GarageAreaXGarageQual"] = dff["GarageArea"] * dff["GarageQual"]<create_dataframe>
train_df = pd.read_csv(f"{ROOT}/train.csv")
 train_df.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])
 chunk = pd.read_csv(f"{ROOT}/test.csv")
 sub = pd.read_csv(f"{ROOT}/sample_submission.csv")
 sub['Patient'] = sub['Patient_Week'].apply(lambda x:x.split('_')[0])
 sub['Weeks'] = sub['Patient_Week'].apply(lambda x: int(x.split('_')[-1]))
sub = sub[['Patient','Weeks','Confidence','Patient_Week']]
sub = sub.merge(chunk.drop('Weeks', axis=1), on="Patient" )
dff2 = dff.copy()
num_cols2, cat_cols2, ord_cols2 = col_types(dff2 )<feature_engineering>
train_df['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = train_df.append([chunk, sub] )
dff2["LotShape"] = dff2["LotShape"].apply(lambda x: 1 if x in [1, 2] else(2 if x == 3 else 3))
dff2["LandSlope"] = dff2["LandSlope"].apply(lambda x: 1 if x in [1, 2] else(2 if x == 3 else 3))
dff2["OverallCond"] = dff2["OverallCond"].apply(lambda x: 1 if x in [1, 2, 3] else x-1)
 dff2["OverallQual"] = dff2["OverallQual"].apply(lambda x: 1 if x in [1, 2] else x-1)
 dff2["ExterCond"] = dff2["ExterCond"].apply(lambda x: 1 if x in [1, 2] else(2 if x == 3 else 3))
dff2["BsmtQual"] = dff2["BsmtQual"].apply(lambda x: 0 if x in [0, 1, 2] else(1 if x == 3 else(2 if x == 4 else 3)))
 dff2["BsmtCond"] = dff2["BsmtCond"].apply(lambda x: 0 if x in [0, 1, 2] else(1 if x == 3 else 2))
dff2["BsmtFinType1"] = dff2["BsmtFinType1"].apply(lambda x: 1 if x in [1, 2, 3, 4, 5] else(2 if x == 6 else x))
dff2["BsmtFinType2"] = dff2["BsmtFinType2"].apply(lambda x: 1 if x in [1, 2, 3, 4, 5] else(2 if x == 6 else x))
dff2["HeatingQC"] = dff2["HeatingQC"].apply(lambda x: 1 if x in [1, 2] else(2 if x in [3, 4] else 3))
dff2["Electrical"] = dff2["Electrical"].apply(lambda x: 1 if x in [1, 2] else x-3)
 dff2["BsmtFullBath"] = dff2["BsmtFullBath"].apply(lambda x: 2 if x >= 2 else x)
 dff2["FullBath"] = dff2["FullBath"].apply(lambda x: 1 if x <= 1 else(3 if x >= 3 else x))
dff2["HalfBath"] = dff2["HalfBath"].apply(lambda x: 1 if x >= 1 else 0)
 dff2["BedroomAbvGr"] = dff2["BedroomAbvGr"].apply(lambda x: 1 if x <=1 else(5 if x >= 5 else x))
dff2["KitchenAbvGr"] = dff2["KitchenAbvGr"].apply(lambda x: 1 if x <= 1 else(2 if x >= 2 else x))
dff2["TotRmsAbvGrd"] = dff2["TotRmsAbvGrd"].apply(lambda x: 3 if x <= 4 else(10 if x >= 11 else x-1))
dff2["Functional"] = dff2["Functional"].apply(lambda x: 1 if x == 1 else 2)
 dff2["Fireplaces"] = dff2["Fireplaces"].apply(lambda x: 2 if x >= 2 else x)
 dff2["GarageCars"] = dff2["GarageCars"].apply(lambda x: 3 if x >= 3 else x)
 dff2["GarageQual"] = dff2["GarageQual"].apply(lambda x: 1 if x <= 2 else(2 if x == 3 else 3))
dff2["GarageCond"] = dff2["GarageCond"].apply(lambda x: 1 if x <= 2 else 2)
 dff2["Fence"] = dff2["Fence"].apply(lambda x: 1 if x in [1, 3] else x )<create_dataframe>
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )
dff3 = dff2.copy()
dff3<count_values>
base = data.loc[data.Weeks == data.min_week]
base = base[['Patient','FVC']].copy()
base.columns = ['Patient','min_FVC']
base['nb'] = 1
base['nb'] = base.groupby('Patient')['nb'].transform('cumsum')
 base = base[base.nb==1]
base.drop('nb', axis=1, inplace=True )
for col in cat_cols2:
print(col, dff3[col].value_counts().size )<feature_engineering>
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base
target_encoding = ["MSSubClass", "Neighborhood", "Exterior1st", "Exterior2nd", "Condition1", "Condition2", "HouseStyle"]
for col in target_encoding:
feature_name = col + "Rank"
dff3.loc[:, feature_name] = dff3[col].map(dff3.groupby(col ).SalePrice.median())
 dff3.loc[:, feature_name] = dff3.loc[:, feature_name].rank(method = "dense" )<feature_engineering>
COLS = ['Sex','SmokingStatus']
FE = []
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int )
dff3["Exterior"] = np.where(( dff3["Exterior1st"] != dff3["Exterior2nd"]), "Mixed", dff3["Exterior1st"])
 dff3["No2ndExt"] = dff3["Exterior"].apply(lambda x: 0 if x == "Mixed" else 1 )<drop_column>
data['age'] =(data['Age'] - data['Age'].min())/(data['Age'].max() - data['Age'].min())
 data['BASE'] =(data['min_FVC'] - data['min_FVC'].min())/(data['min_FVC'].max() - data['min_FVC'].min())
 data['week'] =(data['base_week'] - data['base_week'].min())/(data['base_week'].max() - data['base_week'].min())
 data['percent'] =(data['Percent'] - data['Percent'].min())/(data['Percent'].max() - data['Percent'].min())
 FE += ['age','percent','week','BASE']
drop_cols = ["MSSubClass", "Neighborhood", "Condition1", "Condition2", "Exterior1st", "Exterior2nd", "PoolArea", "PoolQC",
"WoodDeckSF", "OpenPorchSF", "EnclosedPorch", "3SsnPorch", "ScreenPorch", "LowQualFinSF", "MiscVal", "2ndFlrSF",
"HouseStyle","YrSold", "MoSold", "YearBuilt", "YearRemodAdd", "GarageYrBlt", "Exterior", "Utilities", "Street"]
dff3.drop(drop_cols, axis = 1, inplace = True )<categorify>
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
num_, cat_, ord_ = col_types(dff3)
 def prep_data(df, cat_cols, target):
dummies = pd.get_dummies(df[cat_cols], drop_first = True)
 data = pd.concat([df, dummies], axis = 1 ).drop(cat_cols, axis = 1)
 train = data[data[target].notnull() ]
test = data[data[target].isnull() ]
return train, test
train, test = prep_data(dff3, cat_, "SalePrice")
 target = "SalePrice"
predictors = [x for x in train.columns if x not in ["Id", "SalePrice"]]<drop_column>
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return K.mean(metric)
 def qloss(y_true, y_pred):
qs = [0.2, 0.50, 0.8]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred
v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)
 return loss
high_skew = skewed[skewed.TrainSkewness_AfterYeoJohnson.abs() > 1].Feature.tolist()
print(high_skew)
 train.drop(high_skew, axis = 1, inplace = True)
 test.drop(high_skew, axis = 1, inplace = True )<concatenate>
LR = 0.1
BETA_1 = 0.9
BETA_2 = 0.999
DECAY = 0.01
AMSGRAD = False
train[target] = np.log1p(train[target])
 feature_distribution(train, target, test = False)
 train[target].skew()<define_variables>
def make_model() :
z = L.Input(( 9,), name="Patient")
 x = L.Dense(100, activation="relu", name="d1" )(z)
 x = L.Dense(100, activation="relu", name="d2" )(x)
 p1 = L.Dense(3, activation="linear", name="p1" )(x)
 p2 = L.Dense(3, activation="relu", name="p2" )(x)
 preds = L.Lambda(lambda x: x[0] + tf.cumsum(x[1], axis=1),
name="preds" )([p1, p2])
 model = M.Model(z, preds, name="NN-Optimized")
 model.compile(loss=mloss(0.8), optimizer = tf.keras.optimizers.Adam(lr = LR, beta_1 = BETA_1, beta_2 = BETA_2, epsilon = None, decay = DECAY, amsgrad = AMSGRAD), metrics=[score])
 return model
target = "SalePrice"
predictors = [x for x in train.columns if x not in ["Id", "SalePrice"]]<normalization>
y = tr['FVC'].astype('float32' ).values
z = tr[FE].values
ze = sub[FE].values
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))
scaler = RobustScaler()
train[predictors] = scaler.fit_transform(train[predictors])
 test[predictors] = scaler.transform(test[predictors] )<train_model>
NFOLD = 10
kf = KFold(n_splits=NFOLD )
X_train = train[predictors]
y_train = train[target]
X_test = test[predictors]
print(X_train.shape)
 print(X_test.shape )<normalization>
%%time
count = 0
for train_idx, val_idx in kf.split(z):
count += 1
print(f"FOLD {count}:")
 net = make_model()
net.fit(z[train_idx], y[train_idx], batch_size=BATCH_SIZE, epochs=850,
validation_data=(z[val_idx], y[val_idx]), verbose=0)
 print("Train:", net.evaluate(z[train_idx], y[train_idx], verbose=0, batch_size=BATCH_SIZE))
print("Val:", net.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE))
pred[val_idx] = net.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 print("Predicting Test...")
 pe += net.predict(ze, batch_size=BATCH_SIZE, verbose=0)/ NFOLD
selector = VarianceThreshold(0.01)
 selector.fit(X_train)
 X_train = X_train[X_train.columns[selector.get_support() ]]
selector.transform(X_test)
 X_test = X_test[X_test.columns[selector.get_support() ]]
print(X_train.shape)
 print(X_test.shape)
 X_test<define_variables>
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return K.mean(metric)
 def qloss(y_true, y_pred):
qs = [0.2, 0.50, 0.8]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred
v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)
 return loss
test_id = test[["Id"]]<compute_train_metric>
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )
kf = KFold(n_splits = 10, shuffle = True, random_state = 42)
 def rmse_cv(model, X = X_train, y = y_train):
return np.sqrt(-cross_val_score(model, X, y, scoring = 'neg_mean_squared_error', cv = kf)).mean()<choose_model_class>
sub['FVC1'] = pe[:, 1]
sub['Confidence1'] = pe[:, 2] - pe[:, 0]
lgb_model = lgb.LGBMRegressor(colsample_bytree=0.25, learning_rate=0.01,
max_depth=13, min_child_samples=7, n_estimators=10000,
num_leaves=20, objective='regression', random_state=42,
subsample=0.9330025956033094, subsample_freq=1)
 xgb_model = xgb.XGBRegressor(colsample_bytree=0.25, gamma=0.0, learning_rate=0.01, max_depth=3,
n_estimators=15000, n_jobs=-1, random_state=42,
reg_alpha=0.24206673672530965, reg_lambda=0.40464485640717085, subsample=1.0)
 gbr_model = GradientBoostingRegressor(alpha=0.8979588317644014,
learning_rate=0.01, loss='huber',
max_depth=13, max_features=0.1, min_samples_split=109,
n_estimators=10000, n_iter_no_change=100, random_state=42)
 svr_model = SVR(C=0.7682824405204463, coef0=0.0001, degree=2, epsilon=0.0001, gamma=0.0042151786393578635, max_iter=10000)
 lasso_model = Lasso(alpha=0.00012609086150256233, max_iter=5000, random_state=42)
 ridge_model = Ridge(alpha=2.651347536470113, max_iter=5000, random_state=42)
 enet_model = ElasticNet(alpha=0.0002286518512853544, l1_ratio=0.6510386358323069, max_iter=5000, random_state=42 )<create_dataframe>
subm = sub[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()
%%time
models = {
"LGBMRegressor": lgb_model,
"XGBRegressor": xgb_model,
"GradientBoostingRegressor": gbr_model,
"SVR": svr_model,
"Lasso": lasso_model,
"Ridge": ridge_model,
}
oof_df = pd.DataFrame()
predictions_df = pd.DataFrame()
for name, model in models.items() :
print("For model ", name, "
")
 i = 1
oof = np.zeros(len(X_train))
predictions = np.zeros(len(X_test))
for train_ix, test_ix in kf.split(X_train.values):
print("Out of fold predictions generating for fold ", i)
 train_X, train_y = X_train.values[train_ix], y_train[train_ix]
test_X, test_y = X_train.values[test_ix], y_train[test_ix]
if name == "LGBMRegressor":
model.fit(train_X, train_y,
eval_set = [(test_X, test_y)],
eval_metric = "rmse",
early_stopping_rounds=200,
verbose=0)
 elif name == "XGBRegressor":
model.fit(train_X, train_y,
eval_set = [(test_X, test_y)],
eval_metric = "rmse",
early_stopping_rounds=250,
verbose=0)
 else:
model.fit(train_X, train_y)
 oof[test_ix] = oof[test_ix] + model.predict(X_train.values[test_ix])
 predictions = predictions + model.predict(X_test.values)
 i = i + 1
oof_df[name] = oof
predictions_df[name] = predictions / 10
print("
Done
" )<train_model>
subm.loc[~subm.FVC1.isnull() ,'FVC'] = subm.loc[~subm.FVC1.isnull() ,'FVC1']
if sigma_mean<70:
subm['Confidence'] = sigma_opt
else:
subm.loc[~subm.FVC1.isnull() ,'Confidence'] = subm.loc[~subm.FVC1.isnull() ,'Confidence1']
oof = np.zeros(len(X_train))
predictions = np.zeros(len(X_test))
i = 1
for train_ix, test_ix in kf.split(oof_df):
print("Out of fold predictions generating for fold ", i)
 train_X, train_y = oof_df.values[train_ix], y_train[train_ix]
test_X, test_y = oof_df.values[test_ix], y_train[test_ix]
model = gbr_model
model.fit(train_X, train_y)
 oof[test_ix] = oof[test_ix] + model.predict(oof_df.values[test_ix])
 predictions = predictions + model.predict(predictions_df)
 i = i + 1
oof_stacked = oof
stack_preds = predictions / 10<save_to_csv>
otest = pd.read_csv(f"{ROOT}/test.csv")
 for i in range(len(otest)) :
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'FVC'] = otest.FVC[i]
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'Confidence'] = 0.1
<import_modules><EOS> 
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission.csv", index=False )