Kaggle competition: OSIC Pulmonary Fibrosis Progression
<SOS> metric: LaplaceLogLikelihood Kaggle data source: osic-pulmonary-fibrosis-progression<merge>
!pip install fastai2 -q
f_transactions.date = pd.to_datetime(f_transactions.date, format = '%d.%m.%Y')
 f_sales = f_transactions\ 
.join(f_items.set_index('item_id'), on='item_id')\ 
.join(f_categories.set_index('item_category_id'), on='item_category_id')\ 
.join(f_shops.set_index('shop_id'), on='shop_id')
 f_sales.head(2 ).T<merge>
%matplotlib inline
f_test\ 
.join(f_sample.set_index('ID'), on='ID' ).head()<create_dataframe>
from fastai2.basics import *
from fastai2.callback.all import *
from fastai2.vision.all import *
from fastai2.medical.imaging import *
import pydicom
test = f_test.copy()<data_type_conversions>
train_df = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/train.csv')
 test_df = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv' )
f_transactions.date = pd.to_datetime(f_transactions.date, format = '%d.%m.%Y')
 transactions = f_transactions.copy()
transactions = transactions.loc[~transactions.index.isin([1573253,1573252,1618930,1571406,2909818,1829149,1088966])]
transactions['year'] = transactions.date.dt.year
transactions['month'] = transactions.date.dt.month
transactions['day'] = transactions.date.dt.day
transactions['day_of_year'] = transactions.date.dt.dayofyear
transactions['week_day'] = transactions.date.dt.weekday
transactions['activity_cnt'] = transactions.item_cnt_day.abs()
transactions['activity_rev'] = transactions.activity_cnt*transactions.item_price
transactions['revenue'] = transactions.item_cnt_day*transactions.item_price<categorify>
print(f'Total unique patients are {train_df.Patient.nunique() } out of total {len(train_df.Patient)} patients' )
items = f_items.copy()
items.at[20949,'item_category_id'] = 84
items.at[5441,'item_category_id'] = 3
items = items\ 
.join(transactions.groupby('item_id' ).agg(
 sale_start=('date','min'),
sale_end=('date','max'),
price_mean=('item_price','mean'),
item_total_revenue=('revenue','sum'), 
))<feature_engineering>
train_df.groupby('Sex')['SmokingStatus'].value_counts()
categories = f_categories.copy()
categories = categories.append({'item_category_name': 'Фирменный пакет','item_category_id': 84,}, ignore_index=True)
 categories['cat_global'] = np.select(
 [categories.item_category_id.isin(range(0,8)) ,
categories.item_category_id.isin([8,80]),
categories.item_category_id==9,
categories.item_category_id.isin(range(10,18)) ,
categories.item_category_id.isin(range(18,32)) ,
categories.item_category_id.isin([32,33,34,35,36,37,79]),
categories.item_category_id.isin(range(37,42)) ,
categories.item_category_id.isin(range(42,55)) ,
categories.item_category_id.isin(range(55,61)) ,
categories.item_category_id.isin(range(61,73)) ,
categories.item_category_id.isin(range(73,79)) ,
categories.item_category_id.isin([81,82]),
categories.item_category_id==83,
categories.item_category_id==84],
['accessories','tickets','delivery','consoles','games',
'payment_cards','movies','books','music','gifts','programs',
'discs','batteries','plastic_bags'])
 categories['cat_platform'] = np.select(
 [categories.item_category_name.str.contains('PS2'),
categories.item_category_name.str.contains('PS3'),
categories.item_category_name.str.contains('PS4'),
categories.item_category_name.str.contains('PSP'),
categories.item_category_name.str.contains('PSVita'),
categories.item_category_name.str.contains('XBOX 360'),
categories.item_category_name.str.contains('XBOX ONE'),
categories.item_category_name.str.contains('PC'),
categories.item_category_name.str.contains('MAC'),
categories.item_category_name.str.contains('Android')],
['PS2','PS3','PS4','PSP','PSVita','XBOX_360','XBOX_ONE','PC','MAC','Android'],
default='other')
 categories['cat_digital'] = categories.item_category_name\ 
.str.contains('Цифра')*1
categories['cat_in_test'] = categories.item_category_id\ 
.isin(test.set_index('item_id' ).join(items.item_category_id ).item_category_id)*1
categories['cat_to_drop'] = categories.item_category_id\ 
.isin([ 1,8,10,13,17,18,39,46,48,50,51,52,53,66,68,80,81,82])*1<feature_engineering>
source_path = Path('.. /input/osic-pulmonary-fibrosis-progression')
 source_files = os.listdir(source_path)
 print(source_files )
shops = f_shops.copy()
shops = shops.loc[~shops.shop_id.isin([0,1,11])]
shops['shop_in_test'] = shops.shop_id.isin(test.shop_id)*1
shops['shop_to_drop'] = shops.shop_id.isin([8,9,20,23,32,33,40])*1
split = shops['shop_name'].\
str.replace('Н.Новгород','ННовгород' ).\
str.replace('Сергиев Посад','СергиевПосад' ).\
str.replace('\W+',' ' ).\
str.strip().\
str.split(' ', 1 ).\
str
shops['shop_city'] = split[0]
shops['shop_name_part'] = split[1]
sold_items_cnt_per_work_day = transactions.groupby(['shop_id'] ).item_cnt_day.sum() /transactions.groupby(['shop_id'] ).date.nunique()
shops = shops.join(( sold_items_cnt_per_work_day/sold_items_cnt_per_work_day.mean() ).to_frame('shop_size'))<merge>
train_path = source_path/'train'
train_files = get_dicom_files(train_path)
 train_files
sales = transactions\ 
.join(items.set_index('item_id'), on='item_id')\ 
.join(categories.set_index('item_category_id'), on='item_category_id')\ 
.join(shops.set_index('shop_id'), on='shop_id')\ 
.loc[lambda x:(x.cat_to_drop==0)&(x.shop_to_drop==0)]<count_unique_values>
dicom_img = dcmread(train_files[0])
 dicom_img
_ = sales.groupby('date' ).shop_id.nunique()
sales = sales\ 
.set_index('date')\ 
.join(_.to_frame('shops_opened')) \ 
.reset_index() \ 
.assign(shop_compensation = lambda x:(x.shop_size*x.shops_opened)/(_.mean())) \ 
.assign(item_cnt_day_sw = lambda x: x.item_cnt_day/x.shop_compensation )<feature_engineering>
train_df[train_df.duplicated(subset = ['Patient','Weeks'])]
sales['days_from_sale_start'] =(sales.date-sales.sale_start ).dt.days
_ = sales.loc[(sales.sale_start>=datetime.datetime(2013, 6, 1))
&(~sales.cat_global.isin(['delivery']))
&(sales.days_from_sale_start<=60)]
_ = pd.pivot_table(_,
index=['cat_global','item_id'],
columns='days_from_sale_start',
values='item_cnt_day_sw',
aggfunc='sum' ).fillna(0 ).reset_index() \ 
.drop('item_id',axis=1)
 new_item_effect_cat =(_.groupby('cat_global' ).mean() ).div(( _.groupby('cat_global' ).mean() ).mean(axis=1),axis=0 ).T
new_item_effect_cat = new_item_effect_cat.div(new_item_effect_cat.tail(20 ).mean())
 new_item_effect_glob =(_.mean() /(_.mean().mean())).T
new_item_effect_glob = new_item_effect_glob.div(new_item_effect_glob.tail(20 ).mean())
 sales = sales\ 
.set_index(['cat_global','days_from_sale_start'])\ 
.join(new_item_effect_cat.unstack().to_frame('new_item_effect_cat'), how='left')\ 
.fillna(value={'new_item_effect_cat':1})\ 
.reset_index() \ 
.assign(item_cnt_day_sw=lambda x: x.item_cnt_day_sw/x.new_item_effect_cat )<feature_engineering>
train_df.drop_duplicates(keep=False, inplace = True, subset=['Patient','Weeks'] )
sales['item_cnt_day_deseasoned'] = sales.item_cnt_day\ 
.div(sales.week_day_effect_cat)\ 
.div(sales.seasonality_annual_cat )<filter>
submission_df = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/sample_submission.csv' )
sales = sales.loc[(sales.shop_in_test==1)]<drop_column>
temp_sub_df = submission_df['Patient_Week'].str.split('_', expand = True)
 temp_sub_df.rename(columns = {0: 'Patient', 1: 'Weeks'}, inplace = True )
del f_sales,transactions,f_transactions<prepare_x_and_y>
submission_df = pd.concat([submission_df, temp_sub_df], axis = 1)
 submission_df = submission_df[['Patient','Weeks','Confidence','Patient_Week']]
def get_features_normal(grid,L):
date_length_max =(np.datetime64(L[-1][0],'D')-np.datetime64('2013-01-01','D'))
X=[0]*len(L)
 y=[0]*len(L)
 for block in range(len(L)) :
date_start = L[block][0]
col_drop = ['shop_in_test',
'item_category_name',
'cat_to_drop',
'shop_name',
'shop_name_part',
'cat_in_test',
'shop_to_drop'
]
data = sales.loc[(sales.date<date_start)&(sales.date>=date_start-date_length_max),
~sales.columns.isin(col_drop)]
X_temp = grid.loc[grid.date_block==block]\ 
.join(items.loc[:,items.columns.isin(['item_id','item_category_id'])].set_index('item_id'), on='item_id')\ 
.join(categories.set_index('item_category_id'), on='item_category_id')\ 
.join(shops.set_index('shop_id'), on='shop_id')\ 
.loc[:,lambda x : ~x.columns.isin(col_drop)]
lt = len(X_temp.columns)
 X_temp = X_temp\ 
.join(data.groupby('item_id')\ 
.agg(days_first_item_sold_glob=('date','min'),
days_last_item_sold_glob=('date','max')) ,
on='item_id')\ 
.join(data.groupby(['item_id','shop_id'])\ 
.agg(days_first_item_sold_shop=('date','min'),
days_last_item_sold_shop=('date','max')) ,
on=['item_id','shop_id'])\
X_temp.iloc[:,lt:] =(( date_start-X_temp.iloc[:,lt:])/np.timedelta64(1, 'D')).fillna(-1)
 X_temp = X_temp\ 
.join(data.groupby('item_id')\ 
.agg(item_price_mean=('item_price','mean')) ,
on='item_id')\ 
.join(data.groupby('item_category_id')\ 
.agg(cat_price_mean=('item_price','mean')) ,
on='item_category_id')\
X_temp = X_temp.assign(new_item = lambda x: x.item_price_mean.isna() *1)
 X_temp.loc[X_temp.cat_price_mean.isna() ,['cat_price_mean']] = data.item_price.mean()
X_temp.loc[X_temp.item_price_mean.isna() ,['item_price_mean']] = X_temp['cat_price_mean']
lt = len(X_temp.columns)
 for i in ['item_category_id']:
X_temp = X_temp.join(sales.groupby(['shop_id',i] ).item_cnt_day.sum().to_frame('s1')\ 
.join(sales.groupby('shop_id' ).item_cnt_day.sum().to_frame('s2')) \ 
.join(sales.groupby(i ).item_cnt_day.sum().to_frame('g1')) \ 
.assign(g2=sales.item_cnt_day.sum())\ 
.assign(**{i+'_shop_to_cat_glob': lambda x:(x.s1/x.s2)/(x.g1/x.g2)})\ 
.drop(['s1','s2','g1','g2'],axis=1),
on=['shop_id',i])
 X_temp.iloc[:,-1].fillna(0,inplace=True)
 X_temp = X_temp.join(sales.loc[sales.date.isin(L[block])].groupby(['item_id','shop_id'])\ 
.agg(target=('item_cnt_day','sum')) ,
on=['item_id','shop_id'])
 j0 = 0
for j in [30,120]:
ll = len(X_temp.columns)
 dd = data.loc[(data.date>=date_start-np.timedelta64(j, 'D')) & 
(data.date<date_start-np.timedelta64(j0, 'D')) ]
X_temp = X_temp\ 
.join(dd.groupby('item_id')\ 
.agg(item_glob_sales=('item_cnt_day','sum')) ,on='item_id')\ 
.join(dd.groupby(['shop_id','item_id'])\ 
.agg(item_shop_sales=('item_cnt_day','sum')) ,on=['shop_id','item_id'])\ 
.join(dd.groupby('item_category_id')\ 
.agg(cat_glob_sales=('item_cnt_day','sum')) ,on='item_category_id')\ 
.join(dd.groupby(['shop_id','item_category_id'])\ 
.agg(cat_shop_sales=('item_cnt_day','sum')) ,on=['shop_id','item_category_id'])\ 
.join(dd.groupby('item_id')\ 
.agg(item_glob_days=('date','nunique')) ,on='item_id')\ 
.join(dd.groupby(['item_id','shop_id'])\ 
.agg(item_shop_days=('date','nunique')) ,on=['item_id','shop_id'])\
X_temp.rename(columns=dict(zip(X_temp.iloc[:,ll:].columns,
X_temp.iloc[:,ll:].columns+'_'+str(j0)+'-'+str(j))),
inplace=True)
 j0 = j
X_temp.iloc[:,lt:] = X_temp.iloc[:,lt:].fillna(0)
 X_temp.loc[:,['shop_id','item_category_id']] = X_temp.loc[:,['shop_id','item_category_id']].astype('str')
 X[block] = X_temp.loc[:,~X_temp.columns.isin(['target','cat_digital','shop_city'])]
y[block] = X_temp.target.fillna(0 ).clip(0,20)
 X = pd.concat(X)
 print('features - done')
 return X,y<define_variables>
submission_df = submission_df.merge(test_df.drop('Weeks', axis = 1), on = 'Patient' )
def data_for_cb(X,y,tb,bn,cat_features):
tune_set_cb = Pool(data=X.loc[X.date_block.isin(range(tb+1,bn)) ],
label=pd.concat(y[tb+1:]),
cat_features=cat_features)
 eval_set_cb = Pool(data=X.loc[X.date_block.isin([1])],
label=y[1],
cat_features=cat_features)
 train_set_cb = Pool(data=X.loc[X.date_block.isin(range(1,bn-tb)) ],
label=pd.concat(y[1:-tb]),
cat_features=cat_features)
 test_set_cb = Pool(data=X.loc[X.date_block.isin([0])],
label=y[0],
cat_features=cat_features)
 print('cb data - done')
 return tune_set_cb, eval_set_cb, train_set_cb, test_set_cb<categorify>
train_df['data_type'] = 'Train'
test_df['data_type'] = 'Val'
submission_df['data_type'] = 'Test'
combined_df = train_df.append([test_df, submission_df] )
def encode_X(X,cat_features):
X = pd.concat([X.loc[:,~X.columns.isin(cat_features)],
pd.get_dummies(X.loc[:,cat_features])], axis=1)
 _ = X.date_block.values
X = pd.DataFrame(data=StandardScaler().fit_transform(X), columns=X.columns)
 X.iloc[:,23:] = X.iloc[:,23:]*0.1
X.date_block = _
return X<data_type_conversions>
combined_df['Min_Weeks'] = combined_df['Weeks']
combined_df.loc[combined_df.data_type == 'Test','Min_Weeks'] = np.nan
combined_df['Min_Weeks'] = combined_df.groupby('Patient')['Min_Weeks'].transform('min' )
def data_for_rf_knn(X,y,tb,bn):
X_tune_enc = X.loc[X.date_block.isin(range(tb+1,bn)) ].astype(np.float32)
 y_tune_enc = pd.concat(y[tb+1:] ).astype(np.float32)
 X_eval_enc = X.loc[X.date_block.isin([1])].astype(np.float32)
 y_eval_enc = y[1].astype(np.float32)
 X_train_enc = X.loc[X.date_block.isin(range(1,bn-tb)) ].astype(np.float32)
 y_train_enc = pd.concat(y[1:-tb] ).astype(np.float32)
 X_test_enc = X.loc[X.date_block.isin([0])].astype(np.float32)
 y_test_enc = y[0].astype(np.float32)
 print('rf and knn data - done')
 return X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc 
<choose_model_class>
base = combined_df.loc[combined_df.Weeks == combined_df.Min_Weeks]
base = base[['Patient','FVC']].rename(columns = {'FVC':'min_FVC'})
 base.drop_duplicates(keep = 'first', inplace = True, subset = ['Patient'] )
cb = CatBoostRegressor(task_type='GPU',
iterations = 1500,
boosting_type = 'Ordered',
depth = 4)
 rf = RandomForestRegressor(min_samples_leaf=15,
n_jobs=-1,
random_state=0,
n_estimators=60)
 knn = KNeighborsRegressor(n_neighbors=30)
 lr = LinearRegression(n_jobs=-1 )<train_model>
combined_df.Weeks = combined_df.Weeks.astype(int)
 combined_df.Min_Weeks = combined_df.Min_Weeks.astype(float )
def run_models(tune_set_cb, eval_set_cb, train_set_cb, test_set_cb,X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc):
eval_df = pd.DataFrame(np.zeros(( len(y_eval_enc),5)) ,columns=['rf','cb','knn','lr','y'])
 test_df = pd.DataFrame(np.zeros(( len(y_test_enc),5)) ,columns=['rf','cb','knn','lr','y'])
 print(' ')
 print('tuning...')
 rf.fit(X_tune_enc,y_tune_enc.values)
 print('rf - done')
 cb.fit(tune_set_cb, silent=True)
 print('cb - done')
 knn.fit(cudf.DataFrame.from_pandas(X_tune_enc), y_tune_enc.values)
 print('knn - done')
 eval_df.cb = cb.predict(eval_set_cb ).clip(0,20)
 eval_df.rf = rf.predict(X_eval_enc ).clip(0,20)
 eval_df.knn = knn.predict(cudf.DataFrame.from_pandas(X_eval_enc)).to_pandas().clip(0,20)
 eval_df.y = y_eval_enc.values
print('rf','{:.4}'.format(mean_squared_error(eval_df.y, eval_df.rf,squared=False)))
 print('cb','{:.4}'.format(mean_squared_error(eval_df.y, eval_df.cb,squared=False)))
 print('knn','{:.4}'.format(mean_squared_error(eval_df.y, eval_df.knn,squared=False)))
 lr.fit(eval_df.iloc[:,:-2],eval_df.y)
 print('lr - done')
 eval_df.lr = lr.predict(eval_df.iloc[:,:-2] ).clip(0,20)
 print('lr','{:.4}'.format(mean_squared_error(eval_df.y, eval_df.lr,squared=False)))
 print('model weights',
'rf',
'{:.1%}'.format(lr.coef_[0]),
'cb',
'{:.1%}'.format(lr.coef_[1]),
'knn',
'{:.1%}'.format(lr.coef_[2]))
print(' ')
 print('training...')
 rf.fit(X_train_enc,y_train_enc.values)
 print('rf - done')
 cb.fit(train_set_cb, silent=True)
 print('cb - done')
 knn.fit(cudf.DataFrame.from_pandas(X_train_enc), y_train_enc.values)
 print('knn - done')
 test_df.rf = rf.predict(X_test_enc ).clip(0,20)
 test_df.cb = cb.predict(test_set_cb ).clip(0,20)
 test_df.knn = knn.predict(cudf.DataFrame.from_pandas(X_test_enc)).to_pandas().clip(0,20)
 test_df.lr = lr.predict(test_df.iloc[:,:-2] ).clip(0,20)
 print('all done')
 return test_df.lr<categorify>
combined_df = combined_df.merge(base, on='Patient', how='left')
 combined_df['Deviation_Weeks'] = combined_df['Weeks'] - combined_df['Min_Weeks']
del base
print('start monthly models...')
 print(' ')
 bn = 4
tb = 1
cat_features = ['cat_global','cat_platform','shop_id','item_category_id']
L = get_monthly_list(bn)
 grid = get_grid(sales,L)
 X,y = get_features_normal(grid,L)
 tune_set_cb, eval_set_cb, train_set_cb, test_set_cb = data_for_cb(X,y,tb,bn,cat_features)
 X = encode_X(X,cat_features)
 X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc = data_for_rf_knn(X,y,tb,bn)
 del X,y
pred_monthly = run_models(tune_set_cb, eval_set_cb, train_set_cb, test_set_cb,X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc )<categorify>
combined_df = pd.concat([combined_df, pd.get_dummies(combined_df[['Sex','SmokingStatus']])], axis = 1 )
print(' ')
 print('start 30d models...')
 print(' ')
 bn = 7
tb = 2
cat_features = ['cat_global','cat_platform','shop_id','item_category_id']
L = get_30d_list(bn)
 grid = get_grid(sales,L)
 X,y = get_features_normal(grid,L)
 tune_set_cb, eval_set_cb, train_set_cb, test_set_cb = data_for_cb(X,y,tb,bn,cat_features)
 X = encode_X(X,cat_features)
 X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc = data_for_rf_knn(X,y,tb,bn)
 del X,y
pred_30d = run_models(tune_set_cb, eval_set_cb, train_set_cb, test_set_cb,X_tune_enc,y_tune_enc,X_eval_enc,y_eval_enc,X_train_enc,y_train_enc,X_test_enc,y_test_enc )<save_to_csv>
scaler = MinMaxScaler()
scaled = pd.DataFrame(scaler.fit_transform(combined_df[['Age','Percent','min_FVC','Deviation_Weeks']]),
columns = ['scaled_Age', 'scaled_Percent', 'scaled_FVC', 'scaled_Deviation_Weeks'])
 combined_df = pd.concat([combined_df, scaled], axis = 1 )
f_sample.item_cnt_month =(pred_monthly*0.5+pred_30d*0.5 ).clip(0,20)
 f_sample.to_csv(r'submission.csv',index=False )<import_modules>
feature_columns = ['Sex_Male','Sex_Female','SmokingStatus_Ex-smoker','SmokingStatus_Never smoked','SmokingStatus_Currently smokes',
'scaled_Age','scaled_Percent','scaled_Deviation_Weeks','scaled_FVC']
import torch
from torch.utils.data import Dataset
from torch.utils.data import Subset
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adam
from torch.nn import CosineSimilarity
from torchvision.transforms import ToTensor
from torchvision import transforms
import torchvision.models as models
from pathlib import Path
import PIL.Image
import random
import math
from tqdm import tqdm_notebook as tqdm
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from torch.optim.lr_scheduler import ExponentialLR
import os
import glob
import cv2
import numpy as np
import PIL
from sklearn.metrics import cohen_kappa_score<set_options>
train_df = combined_df.loc[combined_df.data_type == 'Train']
test_df = combined_df.loc[combined_df.data_type == 'Val']
submission_df = combined_df.loc[combined_df.data_type == 'Test']
del combined_df
random.seed(42)
 torch.manual_seed(42)
 np.random.seed(42 )<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
def make_model() :
x1 = Input(( 9,), name="Patient")
 x2 = Dense(100, activation="relu", name="d1" )(x1)
 x3 = Dense(100, activation="relu", name="d2" )(x2)
 p1 = Dense(3, activation="relu", name="p1" )(x3)
 p2 = Dense(3, activation="relu", name="p2" )(x3)
 preds = Lambda(lambda x3: x3[0] + tf.cumsum(x3[1], axis=1),
name="preds" )([p1, p2])
 model = Model(x1, preds, name="CNN")
 model.compile(loss = mloss(0.8), optimizer = tf.keras.optimizers.Adam(lr=0.1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.005,
amsgrad=False), metrics=[score])
 return model
path = Path('.. /input/aptos2019-blindness-detection' )<normalization>
y = train_df['FVC'].values
z = train_df[feature_columns].values
sub = submission_df[feature_columns].values
pe = np.zeros(( sub.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))
class CircleCrop:
def __init__(self, radius):
self.radius = radius
circle_img = np.zeros(( 2 * radius, 2 * radius), np.uint8)
 cv2.circle(circle_img,(radius, radius), int(radius), 1, thickness=-1)
 self.mask = torch.tensor(circle_img, dtype=torch.float32)
 def __call__(self, img):
return torch.mul(img, self.mask)
 def __repr__(self):
return self.__class__.__name__ + '(radius={0})'.format(self.radius )<load_from_csv>
NFOLD = 5
BATCH_SIZE=128
kf = KFold(n_splits=NFOLD )
tr_df = pd.read_csv(path/'train.csv' )<create_dataframe>
%%time
cnt = 0
for tr_idx, val_idx in kf.split(z):
cnt += 1
print(f"FOLD {cnt}")
 model = make_model()
model.fit(z[tr_idx], y[tr_idx], batch_size=BATCH_SIZE, epochs=800,
validation_data=(z[val_idx], y[val_idx]), verbose=0)
 print("train", model.evaluate(z[tr_idx], y[tr_idx], verbose=0, batch_size=BATCH_SIZE))
print("val", model.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE))
print("predict val...")
 pred[val_idx] = model.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 print("predict test...")
 pe += model.predict(sub, batch_size=BATCH_SIZE, verbose=0)/ NFOLD
tr_ds = APTOSDataset(path/'train_images', tr_df, tr_df['diagnosis'].values, augmentation=True, img_size=256 )<load_from_csv>
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )
te_df = pd.read_csv(path/'test.csv' )<create_dataframe>
submission_df['FVC1'] = pe[:, 1]
submission_df['Confidence1'] = pe[:, 2] - pe[:, 0]
te_ds = APTOSDataset(path/'test_images', te_df, te_df['id_code'], augmentation=True, img_size=256 )<define_variables>
subm = submission_df[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()
bs = 50
nw = 4<load_pretrained>
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']
tr_dl = DataLoader(tr_ds, batch_size=bs, num_workers=nw, drop_last=False, pin_memory=True, shuffle=True )<load_pretrained>
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
<normalization><EOS> 
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission.csv", index=False )