Kaggle competition: NFL Big Data Bowl
<SOS> metric: CRPS Kaggle data source: nfl-big-data-bowl<install_modules>
sns.set_style('darkgrid')
 mpl.rcParams['figure.figsize'] = [15,10]
pd.set_option('mode.chained_assignment', None )
!pip install.. /input/kerasapplications/keras-team-keras-applications-3b180cb -f./ --no-index
!pip install.. /input/efficientnet/efficientnet-1.1.0/ -f./ --no-index<import_modules>
env = nflrush.make_env()
import os
import cv2
import pydicom
import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import random
from tqdm.notebook import tqdm
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import mean_absolute_error
from tensorflow_addons.optimizers import RectifiedAdam
from tensorflow.keras import Model
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras.optimizers import Nadam
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
from PIL import Image<define_variables>
train_raw = pd.read_csv('.. /input/nfl-big-data-bowl-2020/train.csv', dtype={'WindSpeed': 'object'} )
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed )<define_variables>
piv_cols=['PlayerHeight','PlayerWeight','PlayerAge','X','Y',
'Orientation_x','Orientation_y','Dir_x','Dir_y','S','A','Dis',
'is_off','is_rusher','is_rb','is_l','is_sk']
seed_everything(42 )<set_options>
cat_cols=['Week','Quarter','Down','OffenseFormation', 'DefendersInTheBox',
'outside','rain','snow','Turf','is_home']
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )<init_hyperparams>
cont_cols=['till_reg','YardsFromOwnGoal','PossScore','DefScore',
'Distance','handoff_delay']
Dropout_model = 0.3855
FVC_weight = 0.2
Confidence_weight = 0.2<load_from_csv>
tall_pipe=make_column_transformer(
(make_pipeline(SimpleImputer(strategy='mean'), StandardScaler()),['PlayerHeight','PlayerWeight','PlayerAge','X','Y',
'Orientation_x','Orientation_y','Dir_x','Dir_y','S','A','Dis']), 
('passthrough',['is_off','is_rusher','is_rb','is_l','is_sk'])
)
 cont_pipe=make_column_transformer(
(make_pipeline(SimpleImputer(strategy='median'),StandardScaler()),cont_cols)
)
 cat_pipe=make_column_transformer(
(make_pipeline(SimpleImputer(strategy='most_frequent'),OrdinalEncoder()),cat_cols)
)
train = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/train.csv' )<concatenate>
def feature_magic(train_in,tall_piper=tall_pipe,cat_piper=cat_pipe,cont_piper=cont_pipe,piv_cols=piv_cols,cat_cols=cat_cols,lefty=False,training=False):
train=train_in.copy()
if lefty:
train_left=train.copy()
train_left['PlayId']=-train_left['PlayId']
train=pd.concat([train,train_left])
 train=train.reset_index(drop=True)
 train.loc[train.VisitorTeamAbbr == "ARI",'VisitorTeamAbbr'] = "ARZ"
train.loc[train.HomeTeamAbbr == "ARI",'HomeTeamAbbr'] = "ARZ"
train.loc[train.VisitorTeamAbbr == "BAL",'VisitorTeamAbbr']= "BLT"
train.loc[train.HomeTeamAbbr == "BAL",'HomeTeamAbbr'] = "BLT"
train.loc[train.VisitorTeamAbbr == "CLE",'VisitorTeamAbbr'] = "CLV"
train.loc[train.HomeTeamAbbr == "CLE",'HomeTeamAbbr'] = "CLV"
train.loc[train.VisitorTeamAbbr == "HOU",'VisitorTeamAbbr'] = "HST"
train.loc[train.HomeTeamAbbr == "HOU",'HomeTeamAbbr'] = "HST"
train['is_rusher']=train.NflId==train.NflIdRusher
train['is_home']=train.Team=='home'
train['is_off']=(train.is_home &(train['HomeTeamAbbr']==train['PossessionTeam'])) |(~train.is_home &(train['HomeTeamAbbr']!=train['PossessionTeam']))
train['YardsFromOwnGoal']=train['YardLine']
train.loc[train.FieldPosition!=train.PossessionTeam,'YardsFromOwnGoal']=50+50-train.loc[train.FieldPosition!=train.PossessionTeam,'YardsFromOwnGoal']
train['to_left']=train.PlayDirection=='left'
train.loc[train.to_left,'X']=120-train.loc[train.to_left,'X']
train['X']=train['X']-train['YardsFromOwnGoal']
train.loc[train.to_left,'Y']=(160/3)-train.loc[train.to_left,'Y']
train['Y']=train.Y-(160/3)/2
train.loc[train.PlayId<0,'Y']=-train.loc[train.PlayId<0,'Y']
train.loc[train.to_left,'Orientation']=train.loc[train.to_left,'Orientation']-180
train.loc[train.to_left,'Dir']=train.loc[train.to_left,'Dir']-180
train.loc[train['Season'] == 2017, 'Orientation'] = np.mod(90 + train.loc[train['Season'] == 2017, 'Orientation'], 360)
 deg_cols=['Orientation','Dir']
for deg_col in deg_cols:
train[deg_col+'_x']=np.sin(np.radians(train[deg_col]))
train[deg_col+'_y']=np.cos(np.radians(train[deg_col]))
train.loc[train.PlayId<0,deg_col+'_y']=-train.loc[train.PlayId<0,deg_col+'_y']
train['TimeSnap']=pd.to_datetime(train.TimeSnap)
 train['PlayerBirthDate']=pd.to_datetime(train['PlayerBirthDate'],utc=True)
 train['PlayerAge']=train['TimeSnap']-train['PlayerBirthDate']
train['PlayerAge']=train['PlayerAge'].apply(lambda x:x.total_seconds() /(365.25*24*3600))
train.PlayerHeight=train.PlayerHeight.apply(heightsplit)
 posmap={'CB':'DB','WR':'WR','G':'OL','T':'OL','DE':'DL','DT':'DL','OLB':'LB','TE':'TE','FS':'DB','C':'OL','RB':'RB','QB':'QB',
'SS':'DB','ILB':'LB','MLB':'LB','NT':'DL','LB':'LB','OT':'OL','FB':'FB','OG':'OL','DB':'DB','S':'DB','HB':'RB','SAF':'DB','DL':'DL'}
train.Position=train.Position.apply(lambda x: posmap[x])
 train['is_rb']=(train.Position=='RB')|(train.Position=='FB')
 train['is_l']=(train.Position=='OL')|(train.Position=='DL')|(train.Position=='TE')|(train.Position=='FB')
 train['is_sk']=(train.Position=='WR')|(train.Position=='DB')|(train.Position=='TE')
 train=train.sort_values(['PlayId','Y','X','is_off'])
 trains=train.copy()
train_run=train[train.is_rusher]
if training:
train[piv_cols]=tall_piper.fit_transform(train[piv_cols])
 else:
train[piv_cols]=tall_piper.transform(train[piv_cols])
 convdat=train.groupby('PlayId',as_index=False ).apply(lambda x:x[piv_cols].values)
 convdat=np.dstack(convdat,)
 convdat=np.swapaxes(convdat,0,2)
 convdat=convdat.reshape(convdat.shape[0],convdat.shape[1],convdat.shape[2],1)
 train_run.GameClock=train.GameClock.apply(timesplit)
 train_run['till_reg']=train_run.GameClock+(4-train_run.Quarter)*60*15
train_run['outside']=(train_run.StadiumType.str.lower().str.contains(r'ou[a-z]+')|
train_run.StadiumType.str.lower().str.contains(r'open')|
train_run.StadiumType.str.lower().str.contains(r'heinz field')|
train_run.StadiumType.str.lower().str.contains(r'bowl')
)
 train_run['Turf']=~(train_run.Turf.str.lower() =='grass')|(train_run.Turf.str.lower().str.contains(r'natur'))
train_run['rain']=(train_run.GameWeather.str.lower().str.contains(r'rain')|
train_run.GameWeather.str.lower().str.contains(r'shower'))
train_run['snow']=(train_run.GameWeather.str.lower().str.contains(r'snow'))
train_run['PossScore']=np.select([train_run.is_home,~train_run.is_home],[train_run.HomeScoreBeforePlay,train_run.VisitorScoreBeforePlay])
 train_run['DefScore']=np.select([train_run.is_home,~train_run.is_home],[train_run.VisitorScoreBeforePlay,train_run.HomeScoreBeforePlay])
 train_run['PossTeam']=np.select([train_run.is_home,~train_run.is_home],[train_run.HomeTeamAbbr,train_run.VisitorTeamAbbr])
 train_run['DefTeam']=np.select([train_run.is_home,~train_run.is_home],[train_run.VisitorTeamAbbr,train_run.HomeTeamAbbr])
 td=pd.to_datetime(train_run.TimeHandoff)-pd.to_datetime(train_run.TimeSnap)
 train_run['handoff_delay']=td.dt.total_seconds()
if training:
yards=train_run.Yards
train_run=train_run.drop(columns='Yards')
 train_cont=cont_piper.fit_transform(train_run)
 train_cat=cat_piper.fit_transform(train_run)
 train_cat=train_cat.reshape(train_cat.shape+(1,))
train_run['Yards']=yards
else:
train_cont=cont_piper.transform(train_run)
 train_cat=cat_piper.transform(train_run)
 train_cat=train_cat.reshape(train_cat.shape+(1,))
train_cat=train_cat.swapaxes(0,1)
 train_cat=train_cat.tolist()
train_run.reset_index(drop=True,inplace=True)
 return(trains,train_run,convdat,train_cont,train_cat,tall_piper,cont_piper,cat_piper )
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 )<feature_engineering>
trainn,train_runn,train_conv,train_cont,train_cat,tall_pipe,cont_pipe,cat_pipe=feature_magic(train_raw,lefty=True,training=True )
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 )<feature_engineering>
test_mask=(( train_runn.Season==2018)&(( train_runn.Week>=13)))&(train_runn.PlayId>0)
 train_mask=~(( train_runn.Season==2018)&(( train_runn.Week>=13)) )
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<choose_model_class>
cv_cat=train_cat[train_mask]
cv_cat=cv_cat.swapaxes(0,1)
 cv_cat=cv_cat.tolist()
test_cat=train_cat[test_mask]
test_cat=test_cat.swapaxes(0,1)
 test_cat=test_cat.tolist()
train_catt=train_cat.swapaxes(0,1)
 train_catt=train_catt.tolist()
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(Dropout_model )(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)) )<split>
cv_cont=train_cont[train_mask]
test_cont=train_cont[test_mask]
tr_p, vl_p = train_test_split(P, shuffle=True, train_size = 0.8 )<compute_test_metric>
cv_conv=train_conv[train_mask]
test_conv=train_conv[test_mask]
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 )<define_variables>
Y_c = np.zeros(( train_runn.shape[0], 199))
for idx, target in enumerate(train_runn['Yards']):
Y_c[idx][99 + target] = 1
cv_Y_c=Y_c[train_mask]
test_Y_c=Y_c[test_mask]
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 )<prepare_output>
from keras.layers import Dense
from keras.models import Sequential,Model
from keras.callbacks import Callback, EarlyStopping
from keras.layers import Dropout, PReLU, BatchNormalization, concatenate,ELU,AveragePooling2D, GaussianNoise, Activation,Dense, Conv2D, Flatten,MaxPooling2D,Embedding,Input,Reshape,Concatenate
from keras.optimizers import Adam
from keras.losses import categorical_crossentropy
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 )<save_to_csv>
def crps_loss(y_true, y_pred):
return K.mean(K.square(K.clip(K.cumsum(y_true, axis=1),0,1)- K.clip(K.cumsum(y_pred, axis=1),0,1)) , axis=1 )
sub[["Patient_Week","FVC","Confidence"]].to_csv("submission_img.csv", index=False )<prepare_output>
def ordinal_loss(y_true, y_pred):
weights = K.cast(K.abs(K.argmax(y_true, axis=1)- K.argmax(y_pred, axis=1)) /(K.int_shape(y_pred)[1] - 1), dtype='float32')
 return(1.0 + weights)* categorical_crossentropy(y_true, y_pred )
img_sub = sub[["Patient_Week","FVC","Confidence"]].copy()<load_from_csv>
def ordinal_loss_2(y_true, y_pred):
weights = K.cast(K.square(K.argmax(y_true, axis=1)- K.argmax(y_pred, axis=1)) /(K.int_shape(y_pred)[1] - 1), dtype='float32')
 return(1.0 + weights)* losses.categorical_crossentropy(y_true, y_pred )
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" )<concatenate>
def ordinal_loss_h(y_true, y_pred,delta=10):
weights = K.cast(K.abs(K.argmax(y_true, axis=1)- K.argmax(y_pred, axis=1)) /(K.int_shape(y_pred)[1] - 1), dtype='float32')
 mae=(1.0 + weights)* categorical_crossentropy(y_true, y_pred)
 quadratic=K.minimum(mae, delta)
 linear=mae - quadratic
return 0.5 * K.square(quadratic)+ delta * linear
tr['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = tr.append([chunk, sub] )<count_unique_values>
cat_col_map=dict(train_runn[cat_cols].nunique() )
print(tr.shape, chunk.shape, sub.shape, data.shape)
 print(tr.Patient.nunique() , chunk.Patient.nunique() , sub.Patient.nunique() ,
data.Patient.nunique() )<feature_engineering>
def cont_model(cat_cols=cat_cols,cont_cols=cont_cols):
inputs=[]
embeddings=[]
for i in cat_col_map.keys() :
input=Input(shape=(1,))
vals=cat_col_map[i]
embs=int(np.sqrt(vals))
embedding=Embedding(vals,embs,input_length=1 )(input)
 embedding=Reshape(target_shape=(embs,))(embedding)
 inputs.append(input)
 embeddings.append(embedding)
 cont_input=Input(shape=(len(cont_cols),))
inputs.append(cont_input)
 embeddings.append(cont_input)
 cont_x=Concatenate()(embeddings)
 cont_x=Dense(32,input_dim=inputs, activation=None )(cont_x)
 cont_x=BatchNormalization()(cont_x)
 cont_x=PReLU()(cont_x)
 cont_x=Dropout(0.7 )(cont_x)
 cont_x=GaussianNoise(0.2 )(cont_x)
 cont_x=Dense(32, activation=None )(cont_x)
 cont_x=BatchNormalization()(cont_x)
 cont_x=PReLU()(cont_x)
 cont_x=Dropout(0.5 )(cont_x)
 cont_x=GaussianNoise(0.2 )(cont_x)
 cont_model=Model(inputs,cont_x)
 return cont_model
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )<categorify>
def conv_model2(cols):
conv_input=Input(shape=(cols,22,1))
conv_x=conv_input
conv_3=Conv2D(64, kernel_size=(cols,3), activation='relu',
input_shape=(cols,22,1),padding='same' )(conv_x)
 conv_5=Conv2D(64, kernel_size=(cols,5), activation='relu',
input_shape=(cols,22,1),padding='same' )(conv_x)
 conv_7=Conv2D(64, kernel_size=(cols,7), activation='relu',
input_shape=(cols,22,1),padding='same' )(conv_x)
 conv_out=concatenate([conv_3,conv_5,conv_7],axis=3)
 conv_out=Flatten()(conv_out)
 conv_out=BatchNormalization()(conv_out)
 conv_out=Dropout(0.7 )(conv_out)
 conv_out=GaussianNoise(0.2 )(conv_out)
 conv_out=Dense(32, activation=None )(conv_out)
 conv_out=BatchNormalization()(conv_out)
 conv_out=PReLU()(conv_out)
 conv_model=Model(conv_input,conv_out)
 return conv_model
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 )<merge>
def conv_model(cols,player_width):
conv_input=Input(shape=(cols,22,1))
conv_x=conv_input
conv_x=Conv2D(64, kernel_size=(cols,player_width), activation='relu',
input_shape=(cols,22,1),padding='same' )(conv_x)
 conv_x=BatchNormalization(axis=1 )(conv_x)
 conv_x=AveragePooling2D(pool_size=(1,player_width),strides=1 )(conv_x)
 conv_x=Conv2D(32, kernel_size=(1,player_width), activation='relu',
padding='same' )(conv_x)
 conv_x=BatchNormalization(axis=1 )(conv_x)
 conv_x=AveragePooling2D(pool_size=(1,player_width),strides=1 )(conv_x)
 conv_x=Conv2D(32, kernel_size=(1,player_width), activation='relu',
padding='same' )(conv_x)
 conv_x=BatchNormalization(axis=1 )(conv_x)
 conv_x=AveragePooling2D(pool_size=(1,player_width),strides=1 )(conv_x)
 conv_x=Flatten()(conv_x)
 conv_x=BatchNormalization()(conv_x)
 conv_x=Dropout(0.5 )(conv_x)
 conv_x=GaussianNoise(0.2 )(conv_x)
 conv_x=Dense(32, activation=None )(conv_x)
 conv_x=BatchNormalization()(conv_x)
 conv_x=PReLU()(conv_x)
 conv_model=Model(conv_input,conv_x)
 return conv_model
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base<data_type_conversions>
cont_inputs=train_cont.shape[1]
cont_inputs
COLS = ['Sex','SmokingStatus']
FE = []
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int )<feature_engineering>
models=concatenate([cont_model_.output,conv_model_.output])
 models=BatchNormalization()(models)
 models=Dropout(0.7 )(models)
 models=GaussianNoise(0.2 )(models)
 combout=Dense(32,activation=None )(models)
 combout=BatchNormalization()(combout)
 combout=PReLU()(combout)
 combout=Dropout(0.7 )(combout)
 combout=GaussianNoise(0.2 )(combout)
 combout=Dense(32,activation=None )(models)
 combout=BatchNormalization()(combout)
 combout=PReLU()(combout)
 combout=Dropout(0.5 )(combout)
 combout=GaussianNoise(0.2 )(combout)
 combout=Dense(199, activation='softmax' )(combout )
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']<filter>
big_model=Model(inputs=cont_model_.input+[conv_model_.input],outputs=combout )
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data<compute_test_metric>
np.random.seed(214)
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<prepare_x_and_y>
big_model.compile(optimizer=Adam(learning_rate=0.005,
beta_1=0.9,
beta_2=0.999,
epsilon=1e-07,
amsgrad=False), loss=ordinal_loss,
metrics=[crps_loss] 
)
 es=EarlyStopping(patience=10, min_delta=5e-5, restore_best_weights=True, monitor='val_crps_loss')
 big_model.fit(cv_cat+[cv_cont,cv_conv], cv_Y_c, callbacks=[es], epochs=100, batch_size=512,
verbose=True,validation_data=(test_cat+[test_cont,test_conv],test_Y_c))
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))<choose_model_class>
train_preds=big_model.predict(train_catt+[train_cont,train_conv])
NFOLD = 5
kf = KFold(n_splits=NFOLD )<predict_on_test>
train_predslon=np.sum(train_preds*np.arange(-99,100),axis=1)
%%time
cnt = 0
EPOCHS = 855
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<compute_test_metric>
train_runn['pred']=train_predslon
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )<prepare_output>
lossess=np.mean(np.square(np.clip(np.cumsum(train_preds, axis=1),0,1)- np.clip(np.cumsum(Y_c, axis=1),0,1)) , axis=1)
 lossess.mean()
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 )<feature_engineering>
train_runn['loss']=lossess
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']<load_from_csv>
train_runn.loss.quantile([.1,.25,.5,.75,.8,.9,.95,1] )
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<save_to_csv>
from sklearn.metrics import r2_score
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission_regression.csv", index=False )<prepare_output>
r2_score(train_runn['Yards'],train_runn['pred'] )
reg_sub = subm[["Patient_Week","FVC","Confidence"]].copy()<sort_values>
big_preds=train_runn.sort_values('pred',ascending=False)[['PlayId','PossTeam','PossScore','DefTeam','DefScore','DisplayName','Quarter','GameClock','Down','Distance','Yards','pred','loss']].head(10)
 big_preds
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 )<feature_engineering>
small_preds=train_runn.sort_values('pred',ascending=True)[['PlayId','PossTeam','PossScore','DefTeam','DefScore','DisplayName','Quarter','GameClock','Down','Distance','Yards','pred','loss']].head(10)
 small_preds
df = df1[['Patient_Week']].copy()
df['FVC'] = FVC_weight*df1['FVC'] +(1-FVC_weight)*df2['FVC']
df['Confidence'] = Confidence_weight*df1['Confidence'] +(1-Confidence_weight)*df2['Confidence']
df.head()<save_to_csv>
iter_test=env.iter_test()
df.to_csv('submission.csv', index=False )<create_dataframe>
def clipper(yl,predsin):
lind=-yl+99
rind=100-yl+99
predsin=np.clip(np.cumsum(predsin),0,1)
 predsin[:lind]=0
predsin[rind:]=1
return predsin
df_issa1 = df.copy()<install_modules>
for(test_df, sample_prediction_df)in iter_test:
testt,test_runn,test_conv,test_cont,test_cat,tall_pipe,cont_pipe,cat_pipe=feature_magic(test_df,lefty=False,training=False)
 test_pred=big_model.predict(test_cat+[test_cont,test_conv])
 test_pred=clipper(test_runn.YardsFromOwnGoal.values[0],test_pred)
 pred_df=pd.DataFrame(data=[test_pred],columns=sample_prediction_df.columns)
 env.predict(pred_df )
<load_from_csv><EOS> 
env.write_submission_file()