Kaggle competition: OSIC Pulmonary Fibrosis Progression
<SOS> metric: LaplaceLogLikelihood Kaggle data source: osic-pulmonary-fibrosis-progression<init_hyperparams>
!pip install.. /input/kerasapplications/keras-team-keras-applications-3b180cb -f./ --no-index
!pip install.. /input/efficientnet/efficientnet-1.1.0/ -f./ --no-index
params = {'task' : 'train',
'boosting_type' : 'gbdt',
'objective' : 'regression',
'metric' : {'l2'},
'num_leaves' : 31,
'learning_rate' : 0.1,
'feature_fraction' : 0.9,
'bagging_fraction' : 0.8,
'bagging_freq': 5,
'verbose' : 0,
}<train_model>
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 )
max_rounds = 1200
num_Fold = 5
kf = KFold(n_splits = num_Fold, random_state = 1, shuffle = True)
 for i,(train_index,val_index)in enumerate(kf.split(train_source)) :
y_train = winner_train.iloc[train_index].copy()
X_train = train_source.iloc[train_index,:].copy()
test_set = test_source.copy()
print("Fold", i)
 fit_model = lgb.train(params,
train_set = lgb.Dataset(X_train, y_train),
num_boost_round = max_rounds,
verbose_eval = 50)
 test_pred = fit_model.predict(test_set)
 print("test_pred_head:", test_pred[:4], "
")
 y_test_pred += test_pred
print("Train is Done.")
 y_test_pred /= num_Fold
print("Predict_Avg:", y_test_pred )<save_to_csv>
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )
sub = pd.DataFrame(gender_submission['Id'])
 sub['winPlacePerc'] = list(map(float, y_test_pred))
sub.to_csv("submission.csv", index = False )<load_from_csv>
train = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/train.csv' )
training_df = pd.read_csv(".. /input/train_V2.csv")
 testing_df = pd.read_csv(".. /input/test_V2.csv" )<drop_column>
def get_tab(df):
vector = [(df.Age.values[0] - 30)/ 30]
if df.Sex.values[0] == 'male':
vector.append(0)
 else:
vector.append(1)
 if df.SmokingStatus.values[0] == 'Never smoked':
vector.extend([0,0])
 elif df.SmokingStatus.values[0] == 'Ex-smoker':
vector.extend([1,1])
 elif df.SmokingStatus.values[0] == 'Currently smokes':
vector.extend([0,1])
 else:
vector.extend([1,0])
 return np.array(vector )
training_df = training_df.drop(['Id', 'longestKill', 'rankPoints', 'numGroups', 'matchType'], axis=1)
 testing_df = testing_df.drop(['Id', 'longestKill', 'rankPoints', 'numGroups', 'matchType'], axis=1 )<count_values>
A = {}
TAB = {}
P = []
for i, p in tqdm(enumerate(train.Patient.unique())) :
sub = train.loc[train.Patient == p, :]
fvc = sub.FVC.values
weeks = sub.Weeks.values
c = np.vstack([weeks, np.ones(len(weeks)) ] ).T
a, b = np.linalg.lstsq(c, fvc)[0]
A[p] = a
TAB[p] = get_tab(sub)
 P.append(p )
training_df['kills'].value_counts().tail(10 )<count_values>
class IGenerator(Sequence):
BAD_ID = ['ID00011637202177653955184', 'ID00052637202186188008618']
def __init__(self, keys, a, tab, batch_size=32):
self.keys = [k for k in keys if k not in self.BAD_ID]
self.a = a
self.tab = tab
self.batch_size = batch_size
self.train_data = {}
for p in train.Patient.values:
self.train_data[p] = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 def __len__(self):
return 1000
def __getitem__(self, idx):
x = []
a, tab = [], []
keys = np.random.choice(self.keys, size = self.batch_size)
 for k in keys:
try:
i = np.random.choice(self.train_data[k], size=1)[0]
img = get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{k}/{i}')
 x.append(img)
 a.append(self.a[k])
 tab.append(self.tab[k])
 except:
print(k, i)
 x,a,tab = np.array(x), np.array(a), np.array(tab)
 x = np.expand_dims(x, axis=-1)
 return [x, tab] , a
training_df = training_df.drop(training_df[training_df.kills > 20].index)
 training_df['kills'].value_counts()<count_values>
Dense, Dropout, Activation, Flatten, Input, BatchNormalization, GlobalAveragePooling2D, Add, Conv2D, AveragePooling2D,
LeakyReLU, Concatenate 
)
 def get_efficientnet(model, shape):
models_dict = {
'b0': efn.EfficientNetB0(input_shape=shape,weights=None,include_top=False),
'b1': efn.EfficientNetB1(input_shape=shape,weights=None,include_top=False),
'b2': efn.EfficientNetB2(input_shape=shape,weights=None,include_top=False),
'b3': efn.EfficientNetB3(input_shape=shape,weights=None,include_top=False),
'b4': efn.EfficientNetB4(input_shape=shape,weights=None,include_top=False),
'b5': efn.EfficientNetB5(input_shape=shape,weights=None,include_top=False),
'b6': efn.EfficientNetB6(input_shape=shape,weights=None,include_top=False),
'b7': efn.EfficientNetB7(input_shape=shape,weights=None,include_top=False)
 }
return models_dict[model]
def build_model(shape=(512, 512, 1), model_class=None):
inp = Input(shape=shape)
 base = get_efficientnet(model_class, shape)
 x = base(inp)
 x = GlobalAveragePooling2D()(x)
 inp2 = Input(shape=(4,))
x2 = tf.keras.layers.GaussianNoise(0.2 )(inp2)
 x = Concatenate()([x, x2])
 x = Dropout(0.35 )(x)
 x = Dense(1 )(x)
 model = Model([inp, inp2] , x)
 weights = [w for w in os.listdir('.. /input/osic-model-weights')if model_class in w][0]
model.load_weights('.. /input/osic-model-weights/' + weights)
 return model
model_classes = ['b5']
models = [build_model(shape=(512, 512, 1), model_class=m)for m in model_classes]
print('Number of models: ' + str(len(models)) )
training_df = training_df.drop(training_df[training_df.DBNOs > 15].index)
 training_df['DBNOs'].value_counts()<count_values>
tr_p, vl_p = train_test_split(P,
shuffle=True,
train_size= 0.8 )
training_df = training_df.drop(training_df[training_df.weaponsAcquired > 30].index)
 training_df['weaponsAcquired'].value_counts()<count_missing_values>
def score(fvc_true, fvc_pred, sigma):
sigma_clip = np.maximum(sigma, 70)
 delta = np.abs(fvc_true - fvc_pred)
 delta = np.minimum(delta, 1000)
 sq2 = np.sqrt(2)
 metric =(delta / sigma_clip)*sq2 + np.log(sigma_clip* sq2)
 return np.mean(metric )
training_df.isnull().sum()
training_df = training_df.dropna(how='any',axis=0)
 training_df.winPlacePerc.isnull().sum()<merge>
subs = []
for model in models:
metric = []
for q in tqdm(range(1, 10)) :
m = []
for p in vl_p:
x = []
tab = []
if p in ['ID00011637202177653955184', 'ID00052637202186188008618']:
continue
ldir = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 for i in ldir:
if int(i[:-4])/ len(ldir)< 0.8 and int(i[:-4])/ len(ldir)> 0.15:
x.append(get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/{i}'))
tab.append(get_tab(train.loc[train.Patient == p, :]))
if len(x)< 1:
continue
tab = np.array(tab)
 x = np.expand_dims(x, axis=-1)
 _a = model.predict([x, tab])
 a = np.quantile(_a, q / 10)
 percent_true = train.Percent.values[train.Patient == p]
fvc_true = train.FVC.values[train.Patient == p]
weeks_true = train.Weeks.values[train.Patient == p]
fvc = a *(weeks_true - weeks_true[0])+ fvc_true[0]
percent = percent_true[0] - a * abs(weeks_true - weeks_true[0])
 m.append(score(fvc_true, fvc, percent))
print(np.mean(m))
metric.append(np.mean(m))
q =(np.argmin(metric)+ 1)/ 10
sub = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/sample_submission.csv')
 test = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 A_test, B_test, P_test,W, FVC= {}, {}, {},{},{}
STD, WEEK = {}, {}
for p in test.Patient.unique() :
x = []
tab = []
ldir = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/test/{p}/')
 for i in ldir:
if int(i[:-4])/ len(ldir)< 0.8 and int(i[:-4])/ len(ldir)> 0.15:
x.append(get_img(f'.. /input/osic-pulmonary-fibrosis-progression/test/{p}/{i}'))
tab.append(get_tab(test.loc[test.Patient == p, :]))
if len(x)<= 1:
continue
tab = np.array(tab)
 x = np.expand_dims(x, axis=-1)
 _a = model.predict([x, tab])
 a = np.quantile(_a, q)
 A_test[p] = a
B_test[p] = test.FVC.values[test.Patient == p] - a*test.Weeks.values[test.Patient == p]
P_test[p] = test.Percent.values[test.Patient == p]
WEEK[p] = test.Weeks.values[test.Patient == p]
for k in sub.Patient_Week.values:
p, w = k.split('_')
 w = int(w)
 fvc = A_test[p] * w + B_test[p]
sub.loc[sub.Patient_Week == k, 'FVC'] = fvc
sub.loc[sub.Patient_Week == k, 'Confidence'] =(
 P_test[p] - A_test[p] * abs(WEEK[p] - w)
)
 _sub = sub[["Patient_Week","FVC","Confidence"]].copy()
subs.append(_sub )
features = list(training_df.columns)
 features.remove("matchId")
 features.remove("groupId")
 features.remove("winPlacePerc")
 print("get match mean feature")
 match_mean = training_df.groupby(['matchId'])[features].agg('mean' ).reset_index()
training_df = training_df.merge(match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 match_mean = testing_df.groupby(['matchId'])[features].agg('mean' ).reset_index()
testing_df = testing_df.merge(match_mean, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 match_size = training_df.groupby(['matchId'] ).size().reset_index(name='match_size')
 training_df = training_df.merge(match_size, how='left', on=['matchId'])
 match_size = testing_df.groupby(['matchId'] ).size().reset_index(name='match_size')
 testing_df = testing_df.merge(match_size, how='left', on=['matchId'])
 del match_mean, match_size
gc.collect()<merge>
N = len(subs)
 sub = subs[0].copy()
sub["FVC"] = 0
sub["Confidence"] = 0
for i in range(N):
sub["FVC"] += subs[0]["FVC"] *(1/N)
 sub["Confidence"] += subs[0]["Confidence"] *(1/N )
print("get group size feature")
 group_size = training_df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 training_df = training_df.merge(group_size, how='left', on=['matchId', 'groupId'])
 group_size = testing_df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 testing_df = testing_df.merge(group_size, how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 group_max = training_df.groupby(['matchId','groupId'])[features].agg('max')
 group_max_rank = group_max.groupby('matchId')[features].rank(pct=True ).reset_index()
training_df = training_df.merge(group_max.reset_index() , suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 training_df = training_df.merge(group_max_rank, suffixes=["", "_max_rank"], how='left', on=['matchId', 'groupId'])
 group_max = testing_df.groupby(['matchId','groupId'])[features].agg('max')
 group_max_rank = group_max.groupby('matchId')[features].rank(pct=True ).reset_index()
testing_df = testing_df.merge(group_max.reset_index() , suffixes=["", "_max"], how='left', on=['matchId', 'groupId'])
 testing_df = testing_df.merge(group_max_rank, suffixes=["", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 group_min = training_df.groupby(['matchId','groupId'])[features].agg('min')
 group_min_rank = group_min.groupby('matchId')[features].rank(pct=True ).reset_index()
training_df = training_df.merge(group_min.reset_index() , suffixes=["", "_min"], how='left', on=['matchId', 'groupId'])
 training_df = training_df.merge(group_min_rank, suffixes=["", "_min_rank"], how='left', on=['matchId', 'groupId'])
 group_min = testing_df.groupby(['matchId','groupId'])[features].agg('min')
 group_min_rank = group_min.groupby('matchId')[features].rank(pct=True ).reset_index()
testing_df = testing_df.merge(group_min.reset_index() , suffixes=["", "_min"], how='left', on=['matchId', 'groupId'])
 testing_df = testing_df.merge(group_min_rank, suffixes=["", "_min_rank"], how='left', on=['matchId', 'groupId'])
 del group_size, group_max, group_max_rank, group_min, group_min_rank
gc.collect()<merge>
sub[["Patient_Week","FVC","Confidence"]].to_csv("submission_img.csv", index=False )
<merge>
img_sub = sub[["Patient_Week","FVC","Confidence"]].copy()
training_df.drop(["matchId", "groupId"], axis=1, inplace=True)
 testing_df.drop(["matchId", "groupId"], axis=1, inplace=True )<split>
ROOT = ".. /input/osic-pulmonary-fibrosis-progression"
BATCH_SIZE=128
tr = pd.read_csv(f"{ROOT}/train.csv")
 tr.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])
 chunk = pd.read_csv(f"{ROOT}/test.csv")
 print("add infos")
 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" )
training_df = reduce_mem_usage(training_df)
 print('_'*40)
 testing_df = reduce_mem_usage(testing_df )<feature_engineering>
tr['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = tr.append([chunk, sub] )
training_df['headshotRate'] = training_df['headshotKills'] / training_df['kills']
training_df['headshotRate'].fillna(0, inplace=True)
 training_df['headshotRate'].replace(np.inf, 0, inplace=True)
 testing_df['headshotRate'] = testing_df['headshotKills'] / training_df['kills']
testing_df['headshotRate'].fillna(0, inplace=True)
 testing_df['headshotRate'].replace(np.inf, 0, inplace=True)
 training_df['totalDistance'] = training_df['rideDistance'] + training_df['swimDistance'] + training_df['walkDistance']
testing_df['totalDistance'] = testing_df['rideDistance'] + testing_df['swimDistance'] + testing_df['walkDistance']
training_df['items'] = training_df['heals'] + training_df['boosts']
testing_df['items'] = testing_df['heals'] + testing_df['boosts']
training_df['healsPerWalkDistance'] = training_df['heals'] / training_df['walkDistance']
training_df['healsPerWalkDistance'].fillna(0, inplace=True)
 training_df['healsPerWalkDistance'].replace(np.inf, 0, inplace=True)
 testing_df['healsPerWalkDistance'] = testing_df['heals'] / testing_df['walkDistance']
testing_df['healsPerWalkDistance'].fillna(0, inplace=True)
 testing_df['healsPerWalkDistance'].replace(np.inf, 0, inplace=True)
 training_df['killsPerWalkDistance'] = training_df['kills'] / training_df['walkDistance']
training_df['killsPerWalkDistance'].fillna(0, inplace=True)
 training_df['killsPerWalkDistance'].replace(np.inf, 0, inplace=True)
 testing_df['killsPerWalkDistance'] = testing_df['kills'] / testing_df['walkDistance']
testing_df['killsPerWalkDistance'].fillna(0, inplace=True)
 testing_df['killsPerWalkDistance'].replace(np.inf, 0, inplace=True)
 training_df.head()<define_variables>
print(tr.shape, chunk.shape, sub.shape, data.shape)
 print(tr.Patient.nunique() , chunk.Patient.nunique() , sub.Patient.nunique() ,
data.Patient.nunique())
feature_MI = [
'assists',
'boosts',
'damageDealt',
'DBNOs',
'headshotKills',
'heals',
'killPlace',
'killPoints',
'kills',
'killStreaks',
'matchDuration',
'maxPlace',
'revives',
'rideDistance',
'roadKills',
'swimDistance',
'teamKills',
'vehicleDestroys',
'walkDistance',
'weaponsAcquired',
'winPoints',
'winPlacePerc',
'headshotRate',
'totalDistance',
'items',
'healsPerWalkDistance',
'killsPerWalkDistance'
]
mutual_info_df = training_df.truncate(after=-1)
<categorify>
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )
<split>
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 )
cross_validation_split = model_selection.ShuffleSplit(n_splits = 5, test_size =.3, train_size =.6, random_state = 0)
 lasso_results = pd.DataFrame(data = {'degree': [], 'test_score_mean': [], 'fit_time_mean': []})
 for degree in range(1,6):
X_train_truncated = np.asarray(training_df_truncated[['walkDistance','killPlace', 'damageDealt', 'boosts', 'weaponsAcquired']])
 X_train_truncated = np.float32(X_train_truncated)
 X_train_truncated = PolynomialFeatures(degree, interaction_only=False ).fit_transform(X_train_truncated)
 cross_validation_results = model_selection.cross_validate(Lasso(alpha = 0.00001, max_iter=10000, normalize=True), X_train_truncated, y_train_truncated, cv = cross_validation_split, return_train_score = True)
 lasso_results = lasso_results.append({'degree' : degree,
'test_score_mean' : cross_validation_results['test_score'].mean() ,
'fit_time_mean' : cross_validation_results['fit_time'].mean() }, ignore_index=True)
 print('OK degree ' + str(degree))
sns.pointplot(x = lasso_results.degree, y = lasso_results.test_score_mean)
<prepare_x_and_y>
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base
training_df_truncated = training_df.truncate(after=100000)
 X_train_truncated = np.asarray(training_df_truncated.drop(['winPlacePerc'], axis = 1))
X_train_truncated[0:1]<prepare_x_and_y>
COLS = ['Sex','SmokingStatus']
FE = []
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int )
y_train_truncated = np.asarray(training_df_truncated[['winPlacePerc']])
 y_train_truncated[0:5]<set_options>
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']
del training_df
gc.collect()<prepare_x_and_y>
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data
X_test = np.asarray(testing_df)
 X_test[0:1]<set_options>
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
def make_model(nh):
z = L.Input(( nh,), 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="CNN")
 model.compile(loss=mloss(0.65), optimizer=tf.keras.optimizers.Adam(lr=0.1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.01, amsgrad=False), metrics=[score])
 return model
del testing_df
gc.collect()<train_model>
y = tr['FVC'].values
z = tr[FE].values
ze = sub[FE].values
nh = z.shape[1]
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))
print('Train set truncated:', X_train_truncated.shape,y_train_truncated.shape)
 print('Train set:', X_train.shape,y_train.shape)
 print('Test set:', X_test.shape )<init_hyperparams>
NFOLD = 5
kf = KFold(n_splits=NFOLD )
RFR = RandomForestRegressor(criterion = 'mse', oob_score = True, random_state = 1)
 param_grid = {'min_samples_leaf' : [5, 10, 20, 40, 70, 100],
'min_samples_split' : [10, 20, 40, 60, 80, 100],
'max_depth': [10, 20, 30, 40, 50, None],
'n_estimators': [30, 40, 50]}
param_distributions = param_grid,
n_iter = 100,
cv = cross_validation_split, verbose = 5, random_state = 0, n_jobs = -1)
 print(RS.best_score_)
 print(RS.best_params_ )<train_on_grid>
%%time
cnt = 0
EPOCHS = 800
for tr_idx, val_idx in kf.split(z):
cnt += 1
print(f"FOLD {cnt}")
 net = make_model(nh)
 net.fit(z[tr_idx], y[tr_idx], batch_size=BATCH_SIZE, epochs=EPOCHS,
validation_data=(z[val_idx], y[val_idx]), verbose=0)
 print("train", net.evaluate(z[tr_idx], y[tr_idx], verbose=0, batch_size=BATCH_SIZE))
print("val", net.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE))
print("predict val...")
 pred[val_idx] = net.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 print("predict test...")
 pe += net.predict(ze, batch_size=BATCH_SIZE, verbose=0)/ NFOLD
param_grid = {'min_samples_leaf' : [5, 10, 15],
'min_samples_split' : [10, 15, 20],
'max_depth': [30, 35, 40, None],
'n_estimators': [40]}
print(GS.best_score_)
 print(GS.best_params_ )<train_model>
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )
oob_score = True,
min_samples_leaf = 5,
min_samples_split = 15,
max_depth = 30,
random_state = 1 ).fit(X_train_truncated,y_train_truncated)
 print("%.4f" % best_model.oob_score_)
 print("%.4f" % mean_absolute_error(y_train, y_hat))
importance_df = pd.concat(( pd.DataFrame(training_df_truncated.drop(['winPlacePerc'], axis=1 ).columns, columns = ['variable']),
pd.DataFrame(best_model.feature_importances_, columns = ['importance'])) ,
axis = 1 ).sort_values(by='importance', ascending = False)
 importance_df<normalization>
sub['FVC1'] = 1.*pe[:, 1]
sub['Confidence1'] = pe[:, 2] - pe[:, 0]
subm = sub[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()
subm.loc[~subm.FVC1.isnull() ].head(10 )
<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']
print('Train set truncated:', X_train_truncated.shape,y_train_truncated.shape)
 print('Train set:', X_train.shape,y_train.shape)
 print('Test set:', X_test.shape )<choose_model_class>
otest = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/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
models = [
]
model_results = pd.DataFrame(data = {'test_score_mean': [], 'fit_time_mean': [], 'mean_absolute_error': []})
 cross_validation_split = model_selection.ShuffleSplit(n_splits = 4, test_size =.3, train_size =.6, random_state = 0)
 for model in models:
cross_validation_results = model_selection.cross_validate(model, X_train_truncated, y_train_truncated, cv= cross_validation_split,
scoring = 'neg_mean_absolute_error', return_train_score=True)
 y_hat = model.fit(X_train_truncated, y_train_truncated ).predict(X_train)
 model_results = model_results.append({'test_score_mean' : cross_validation_results['test_score'].mean() ,
'fit_time_mean' : cross_validation_results['fit_time'].mean() ,
'mean_absolute_error' : mean_absolute_error(y_train, y_hat)}, ignore_index=True)
 model_results<choose_model_class>
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission_regression.csv", index=False )
LGBM = lgb.LGBMRegressor(learning_rate=0.003, metric = 'mae', n_estimators = 100, n_jobs=-1)
 param_grid = {'boosting_type' : ['gbdt', 'dart', 'goss'],
'max_depth' : [10, 20, 30, -1],
'min_data_in_leaf' : [10, 50, 100, 500, 1000],
'num_leaves' : [50, 100, 200, 500, 1000]
}
n_iter = 50, scoring = 'neg_mean_absolute_error',
cv = cross_validation_split, verbose = 10, random_state = 0, n_jobs = -1)
 print(RS.best_score_)
 print(RS.best_params_ )<train_on_grid>
reg_sub = subm[["Patient_Week","FVC","Confidence"]].copy()
param_grid = {'boosting_type' : ['goss'],
'max_depth' : [20, 30, 40, -1],
'min_data_in_leaf' : [10, 20, 50, 100],
'num_leaves' : [400, 500, 600]}
print(GS.best_score_)
 print(GS.best_params_ )<train_model>
df1 = img_sub.sort_values(by=['Patient_Week'], ascending=True ).reset_index(drop=True)
 df2 = reg_sub.sort_values(by=['Patient_Week'], ascending=True ).reset_index(drop=True )
boosting_type = 'gbdt',
max_depth = 30,
min_data_in_leaf = 10,
num_leaves = 200 ).fit(X_train_truncated, y_train_truncated ).predict(X_test)
 testing_df = pd.read_csv(".. /input/test_V2.csv")
 submission = testing_df.copy()
submission['winPlacePerc'] = yhat_test
submission.to_csv('submission.csv', columns=['Id', 'winPlacePerc'], index=False)
 submission[['Id', 'winPlacePerc']].head()<set_options>
df = df1[['Patient_Week']].copy()
df['FVC'] = 0.25*df1['FVC'] + 0.75*df2['FVC']
df['Confidence'] = 0.26*df1['Confidence'] + 0.74*df2['Confidence']
df.head()
<load_from_csv><EOS> 
df.to_csv('submission.csv', index=False )