Kaggle competition: Diabetic Retinopathy Detection
submission = pd.DataFrame()
submission["ForecastId"] = np.array(test["ForecastId"])
 submission["ConfirmedCases"] = np.array(conf_list)
 submission["Fatalities"] = np.array(fat_list )<choose_model_class>
!pip install keras==2.2.4
model_conf2 = xgb.XGBRegressor(base_score=0.5, booster=None, colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints=None,
learning_rate=0.3, max_delta_step=0, max_depth=19,
min_child_weight=1, monotone_constraints=None,
n_estimators=1000, n_jobs=0, num_parallel_tree=1,
objective='reg:squarederror', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method=None,
validate_parameters=False, verbosity=None)
 model_fat2 = xgb.XGBRegressor(base_score=0.5, booster=None, colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints=None,
learning_rate=0.3, max_delta_step=0, max_depth=18,
min_child_weight=1, monotone_constraints=None,
n_estimators=1000, n_jobs=0, num_parallel_tree=1,
objective='reg:squarederror', random_state=0, reg_alpha=0,
reg_lambda=1, scale_pos_weight=1, subsample=1, tree_method=None,
validate_parameters=False, verbosity=None )<prepare_x_and_y>
class Length(layers.Layer):
def call(self, inputs, **kwargs):
return K.sqrt(K.sum(K.square(inputs), -1))
def compute_output_shape(self, input_shape):
return input_shape[:-1]
def train_function2() :
train_tmp = pd.DataFrame(train)
 y_conf = train_tmp["ConfirmedCases"]
y_fat = train_tmp["Fatalities"]
X_fat = train_tmp.drop(["Fatalities"],axis=1)
 X_conf = train_tmp.drop(["ConfirmedCases"],axis=1)
 model_fat2.fit(X_fat,y_fat)
 model_conf2.fit(X_conf,y_conf )<predict_on_test>
class Mask(layers.Layer):
def call(self, inputs, **kwargs):
if type(inputs)is list:
assert len(inputs)== 2
inputs, mask = inputs
else:
x = inputs
x =(x - K.max(x, 1, True)) / K.epsilon() + 1
mask = K.clip(x, 0, 1)
 inputs_masked = K.batch_dot(inputs, mask, [1, 1])
 return inputs_masked
def compute_output_shape(self, input_shape):
if type(input_shape[0])is tuple:
return tuple([None, input_shape[0][-1]])
 else:
return tuple([None, input_shape[-1]] )
def test_fat2(conf_list):
test_tmp = test.drop(["ForecastId"],axis=1)
 test_tmp["ConfirmedCases"] = np.array(conf_list)
 pr = model_fat2.predict(test_tmp)
 tmp_pr = []
for i in pr:
if i < 0:
tmp_pr.append(0)
 continue
tmp_pr.append(int(i))
pr_fat = tmp_pr
for i in range(1,len(tmp_pr)) :
if tmp_pr[i] < tmp_pr[i-1]:
tmp_pr[i] = tmp_pr[i-1]
return tmp_pr<predict_on_test>
def squash(vectors, axis=-1):
s_squared_norm = K.sum(K.square(vectors), axis, keepdims=True)
 scale = s_squared_norm /(1 + s_squared_norm)/ K.sqrt(s_squared_norm)
 return scale * vectors
def test_conf2(fat_list):
test_tmp = test.drop(["ForecastId"],axis=1)
 test_tmp["Fatalities"] = np.array(fat_list)
 pr = model_conf2.predict(test_tmp)
 tmp_pr = []
for i in pr:
if i < 0:
tmp_pr.append(0)
 continue
tmp_pr.append(int(i))
pr_conf = tmp_pr
for i in range(1,len(tmp_pr)) :
if tmp_pr[i] < tmp_pr[i-1]:
tmp_pr[i] = tmp_pr[i-1]
return tmp_pr<concatenate>
class CapsuleLayer(layers.Layer):
def __init__(self, num_capsule, dim_vector, num_routing=3,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
**kwargs):
super(CapsuleLayer, self ).__init__(**kwargs)
 self.num_capsule = num_capsule
self.dim_vector = dim_vector
self.num_routing = num_routing
self.kernel_initializer = initializers.get(kernel_initializer)
 self.bias_initializer = initializers.get(bias_initializer)
 def build(self, input_shape):
self.input_num_capsule = input_shape[1]
self.input_dim_vector = input_shape[2]
self.W = self.add_weight(shape=[self.input_num_capsule, self.num_capsule, self.input_dim_vector, self.dim_vector],
initializer=self.kernel_initializer,
name='W')
 self.bias = self.add_weight(shape=[1, self.input_num_capsule, self.num_capsule, 1, 1],
initializer=self.bias_initializer,
name='bias',
trainable=False)
 self.built = True
def call(self, inputs, training=None):
inputs_expand = K.expand_dims(K.expand_dims(inputs, 2), 2)
 inputs_tiled = K.tile(inputs_expand, [1, 1, self.num_capsule, 1, 1])
 inputs_hat = tf.scan(lambda ac, x: K.batch_dot(x, self.W, [3, 2]),
elems=inputs_tiled,
initializer=K.zeros([self.input_num_capsule, self.num_capsule, 1, self.dim_vector]))
assert self.num_routing > 0, 'The num_routing should be > 0.'
for i in range(self.num_routing):
c = tf.nn.softmax(self.bias, dim=2)
 outputs = squash(K.sum(c * inputs_hat, 1, keepdims=True))
if i != self.num_routing - 1:
self.bias += K.sum(inputs_hat * outputs, -1, keepdims=True)
 return K.reshape(outputs, [-1, self.num_capsule, self.dim_vector])
 def compute_output_shape(self, input_shape):
return tuple([None, self.num_capsule, self.dim_vector] )
conf_list2 = test_conf2(fat_list)
 fat_list2 = test_fat(conf_list )<create_dataframe>
def PrimaryCap(inputs, dim_vector, n_channels, kernel_size, strides, padding):
output = layers.Conv2D(filters=dim_vector*n_channels, kernel_size=kernel_size, strides=strides, padding=padding )(inputs)
 outputs = layers.Reshape(target_shape=[-1, dim_vector] )(output)
 return layers.Lambda(squash )(outputs )
submission2 = pd.DataFrame()
submission2["ForecastId"] = np.array(test["ForecastId"])
 submission2["ConfirmedCases"] = np.array(conf_list2)
 submission2["Fatalities"] = np.array(fat_list2 )<save_to_csv>
def CapsNet(input_shape, n_class, num_routing):
x = layers.Input(shape=input_shape)
 conv1 = layers.Conv2D(filters=256, kernel_size=9, strides=1, padding='valid', activation='relu', name='conv1' )(x)
 primarycaps = PrimaryCap(conv1, dim_vector=8, n_channels=32, kernel_size=9, strides=2, padding='valid')
 digitcaps = CapsuleLayer(num_capsule=n_class, dim_vector=16, num_routing=num_routing, name='digitcaps' )(primarycaps)
 out_caps = Length(name='out_caps' )(digitcaps)
 y = layers.Input(shape=(n_class,))
masked = Mask()([digitcaps, y])
 x_recon = layers.Dense(512, activation='relu' )(masked)
 x_recon = layers.Dense(1024, activation='relu' )(x_recon)
 x_recon = layers.Dense(width*breadth*3, activation='sigmoid' )(x_recon)
 x_recon = layers.Reshape(target_shape=[width, breadth, 3], name='out_recon' )(x_recon)
 return models.Model([x, y], [out_caps, x_recon] )
submission2.to_csv("submission.csv",index = False )<load_from_csv>
def margin_loss(y_true, y_pred):
L = y_true * K.square(K.maximum(0., 0.9 - y_pred)) + \
0.5 *(1 - y_true)* K.square(K.maximum(0., y_pred - 0.1))
return K.mean(K.sum(L, 1))
pd.set_option('mode.chained_assignment', None)
 test = pd.read_csv(".. /input/covid19-global-forecasting-week-2/test.csv")
 train = pd.read_csv(".. /input/covid19-global-forecasting-week-2/train.csv" )<data_type_conversions>
width, breadth = 32, 32
train['Province_State'].fillna('', inplace=True)
 test['Province_State'].fillna('', inplace=True)
 train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'])
 train = train.sort_values(['Country_Region','Province_State','Date'])
 test = test.sort_values(['Country_Region','Province_State','Date'] )<compute_test_metric>
def RMSLE(pred,actual):
return np.sqrt(np.mean(np.power(( np.log(pred+1)-np.log(actual+1)) ,2)) )<feature_engineering>
trainCSV = pd.read_csv('.. /input/diabetic-retinopathy-detection/trainLabels.csv')
 trainCSV['PatientId'] = trainCSV['image'].map(lambda x: x.split('_')[0])
 trainCSV['imagePath'] = trainCSV['image'].map(lambda x: os.path.join('.. /input/diabetic-retinopathy-detection/','{}.jpeg'.format(x)))
 trainCSV['exists'] = trainCSV['imagePath'].map(os.path.exists)
 trainCSV['leftorright'] = trainCSV['image'].map(lambda x: 'left' if x.split('_')[-1]=='left' else 'right')
 trainCSV['label'] = trainCSV['level'].map(lambda x: to_categorical(x, 5))
trainCSV.dropna(inplace = True)
 trainCSV = trainCSV[trainCSV['exists']]
feature_day = [1,20,50,100,200,500,1000]
def CreateInput(data):
feature = []
for day in feature_day:
data.loc[:,'Number day from ' + str(day)+ ' case'] = 0
if(train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].count() > 0):
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].max()
else:
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]['Date'].min()
for i in range(0, len(data)) :
if(data['Date'].iloc[i] > fromday):
day_denta = data['Date'].iloc[i] - fromday
data['Number day from ' + str(day)+ ' case'].iloc[i] = day_denta.days
feature = feature + ['Number day from ' + str(day)+ ' case']
return data[feature]<prepare_x_and_y>
from PIL import Image
import time
import sys
pred_data_all = pd.DataFrame()
for country in train['Country_Region'].unique() :
for province in train[(train['Country_Region'] == country)]['Province_State'].unique() :
df_train = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]
df_test = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
X_train = CreateInput(df_train)
 y_train_confirmed = df_train['ConfirmedCases'].ravel()
y_train_fatalities = df_train['Fatalities'].ravel()
X_pred = CreateInput(df_test)
 for day in sorted(feature_day,reverse = True):
feature_use = 'Number day from ' + str(day)+ ' case'
idx = X_train[X_train[feature_use] == 0].shape[0]
if(X_train[X_train[feature_use] > 0].shape[0] >= 10):
break
adjusted_X_train = X_train[idx:][feature_use].values.reshape(-1, 1)
 adjusted_y_train_confirmed = y_train_confirmed[idx:]
adjusted_y_train_fatalities = y_train_fatalities[idx:]
idx = X_pred[X_pred[feature_use] == 0].shape[0]
adjusted_X_pred = X_pred[idx:][feature_use].values.reshape(-1, 1)
 model = make_pipeline(PolynomialFeatures(2), BayesianRidge())
 model.fit(adjusted_X_train,adjusted_y_train_confirmed)
 y_hat_confirmed = model.predict(adjusted_X_pred)
 model.fit(adjusted_X_train,adjusted_y_train_fatalities)
 y_hat_fatalities = model.predict(adjusted_X_pred)
 pred_data = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
pred_data['ConfirmedCases_hat'] = np.concatenate(( np.repeat(0, len(pred_data)- len(y_hat_confirmed)) , y_hat_confirmed), axis = 0)
 pred_data['Fatalities_hat'] = np.concatenate(( np.repeat(float(0), len(pred_data)- len(y_hat_fatalities)) , y_hat_fatalities), axis = 0)
 pred_data_all = pred_data_all.append(pred_data )<merge>
def transformImagetoArray(imagePathsList, width=480, breadth=480):
startTime = time.time()
imagesArrayList = []
for imagePath in imagePathsList:
image = np.array(Image.open(imagePath ).resize(( width, breadth)) , np.float ).reshape(width, breadth, 3)
 imagesArrayList.append(image)
 print("needed_time_for_makingArrays: {}".format(time.time() -startTime)+ "[sec]")
 imagesArray = np.asarray(imagesArrayList)
 print('imagesArray: {} MB'.format(str(sys.getsizeof(imagesArray)/(10**6))))
return imagesArray
df_val = pd.merge(pred_data_all,train[['Date','Country_Region','Province_State','ConfirmedCases','Fatalities']],on=['Date','Country_Region','Province_State'], how='left')
 df_val.loc[df_val['Fatalities_hat'] < 0,'Fatalities_hat'] = 0
df_val.loc[df_val['ConfirmedCases_hat'] < 0,'ConfirmedCases_hat'] = 0<create_dataframe>
x = transformImagetoArray(list(trainCSV['imagePath']), width=width, breadth=breadth)
 y = np.asarray(list(trainCSV['label']))
df_val_1 = df_val.copy()<compute_test_metric>
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2 )
RMSLE(df_val[(df_val['ConfirmedCases'].isnull() == False)]['ConfirmedCases'].values,df_val[(df_val['ConfirmedCases'].isnull() == False)]['ConfirmedCases_hat'].values )<compute_test_metric>
RMSLE(df_val[(df_val['Fatalities'].isnull() == False)]['Fatalities'].values,df_val[(df_val['Fatalities'].isnull() == False)]['Fatalities_hat'].values )<compute_test_metric>
del x
gc.collect()
val_score = []
for country in df_val['Country_Region'].unique() :
df_val_country = df_val[(df_val['Country_Region'] == country)&(df_val['Fatalities'].isnull() == False)]
val_score.append([country, RMSLE(df_val_country['ConfirmedCases'].values,df_val_country['ConfirmedCases_hat'].values),RMSLE(df_val_country['Fatalities'].values,df_val_country['Fatalities_hat'].values)] )<create_dataframe>
print('x train: %s' % str(x_train.shape))
print('x test: %s' % str(x_test.shape))
print('y train: %s' % str(y_train.shape))
print('y test: %s' % str(y_test.shape))
df_val_score = pd.DataFrame(val_score)
 df_val_score.columns = ['Country','ConfirmedCases_Scored','Fatalities_Scored']
df_val_score.sort_values('ConfirmedCases_Scored', ascending = False )<groupby>
def train(model, data, epoch_size_frac=1.0, epochs=100, batch_size=64): 
(x_train, y_train),(x_test, y_test)= data
log = callbacks.CSVLogger('log.csv')
 checkpoint = callbacks.ModelCheckpoint('weights-{epoch:02d}val_loss-{val_loss}.h5',
save_best_only=True, save_weights_only=False, verbose=1)
 lr_decay = callbacks.LearningRateScheduler(schedule=lambda epoch: 0.001 * np.exp(-epoch / 10.))
 early_stopping = callbacks.EarlyStopping(monitor = 'val_loss', min_delta=0, patience = 5, verbose = 1)
 model.compile(optimizer='adam',
loss=[margin_loss, 'mse'],
loss_weights=[1., 0.0005],
metrics={'out_caps': 'accuracy'})
 def train_generator(x, y, batch_size, shift_fraction=0.) :
train_datagen = ImageDataGenerator(width_shift_range=shift_fraction,
height_shift_range=shift_fraction)
 generator = train_datagen.flow(x, y, batch_size=batch_size)
 while 1:
x_batch, y_batch = generator.next()
yield([x_batch, y_batch], [y_batch, x_batch])
 model.fit_generator(generator=train_generator(x_train, y_train, batch_size, 0.1),
max_queue_size=2,
steps_per_epoch=int(epoch_size_frac*y_train.shape[0] / batch_size),
epochs=epochs,
validation_data=[[x_test, y_test], [y_test, x_test]],
callbacks=[log, checkpoint, lr_decay, early_stopping])
 model.save('trained_model.h5')
 print('Trained model saved to 'trained_model.h5'')
 return model
df_val[df_val['Country_Region'] == country].groupby(['Date','Country_Region'] ).sum().reset_index()<feature_engineering>
train(model=model, data=(( x_train, y_train),(x_test[:60], y_test[:60])) ,
epoch_size_frac = 1 )
feature_day = [1,20,50,100,200,500,1000]
def CreateInput(data):
feature = []
for day in feature_day:
data.loc[:,'Number day from ' + str(day)+ ' case'] = 0
if(train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].count() > 0):
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].max()
else:
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]['Date'].min()
for i in range(0, len(data)) :
if(data['Date'].iloc[i] > fromday):
day_denta = data['Date'].iloc[i] - fromday
data['Number day from ' + str(day)+ ' case'].iloc[i] = day_denta.days
feature = feature + ['Number day from ' + str(day)+ ' case']
return data[feature]
pred_data_all = pd.DataFrame()
for country in train['Country_Region'].unique() :
for province in train[(train['Country_Region'] == country)]['Province_State'].unique() :
df_train = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]
df_test = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
X_train = CreateInput(df_train)
 y_train_confirmed = df_train['ConfirmedCases'].ravel()
y_train_fatalities = df_train['Fatalities'].ravel()
X_pred = CreateInput(df_test)
 for day in sorted(feature_day,reverse = True):
feature_use = 'Number day from ' + str(day)+ ' case'
idx = X_train[X_train[feature_use] == 0].shape[0]
if(X_train[X_train[feature_use] > 0].shape[0] >= 10):
break
adjusted_X_train = X_train[idx:][feature_use].values.reshape(-1, 1)
 adjusted_y_train_confirmed = y_train_confirmed[idx:]
adjusted_y_train_fatalities = y_train_fatalities[idx:]
idx = X_pred[X_pred[feature_use] == 0].shape[0]
adjusted_X_pred = X_pred[idx:][feature_use].values.reshape(-1, 1)
 pred_data = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
max_train_date = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]['Date'].max()
min_test_date = pred_data['Date'].min()
model = ExponentialSmoothing(adjusted_y_train_confirmed, trend = 'additive' ).fit()
y_hat_confirmed = model.forecast(pred_data[pred_data['Date'] > max_train_date].shape[0])
 y_train_confirmed = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['Date'] >= min_test_date)]['ConfirmedCases'].values
y_hat_confirmed = np.concatenate(( y_train_confirmed,y_hat_confirmed), axis = 0)
 model = ExponentialSmoothing(adjusted_y_train_fatalities, trend = 'additive' ).fit()
y_hat_fatalities = model.forecast(pred_data[pred_data['Date'] > max_train_date].shape[0])
 y_train_fatalities = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['Date'] >= min_test_date)]['Fatalities'].values
y_hat_fatalities = np.concatenate(( y_train_fatalities,y_hat_fatalities), axis = 0)
 pred_data['ConfirmedCases_hat'] = y_hat_confirmed
pred_data['Fatalities_hat'] = y_hat_fatalities
pred_data_all = pred_data_all.append(pred_data)
 df_val = pd.merge(pred_data_all,train[['Date','Country_Region','Province_State','ConfirmedCases','Fatalities']],on=['Date','Country_Region','Province_State'], how='left')
 df_val.loc[df_val['Fatalities_hat'] < 0,'Fatalities_hat'] = 0
df_val.loc[df_val['ConfirmedCases_hat'] < 0,'ConfirmedCases_hat'] = 0
df_val_2 = df_val.copy()<feature_engineering>
def combine_images(generated_images):
num = generated_images.shape[0]
width = int(np.sqrt(num))
height = int(np.ceil(float(num)/width))
shape = generated_images.shape[1:3]
image = np.zeros(( height*shape[0], width*shape[1]),
dtype=generated_images.dtype)
 for index, img in enumerate(generated_images):
i = int(index/width)
 j = index % width
image[i*shape[0]:(i+1)*shape[0], j*shape[1]:(j+1)*shape[1]] = \
img[:, :, 0]
return image
def test(model, data):
x_test, y_test = data
y_pred, x_recon = model.predict([x_test, y_test], batch_size=100)
 print('-'*50)
 print('Test acc:', np.sum(np.argmax(y_pred, 1)== np.argmax(y_test, 1)) /y_test.shape[0])
 img = combine_images(np.concatenate([x_test[:50],x_recon[:50]]))
image = img * 255
Image.fromarray(image.astype(np.uint8)).save("real_and_recon.png")
 print()
print('Reconstructed images are saved to./real_and_recon.png')
 print('-'*50)
 plt.imshow(plt.imread("real_and_recon.png",))
plt.show()
feature_day = [1,20,50,100,200,500,1000]
def CreateInput(data):
feature = []
for day in feature_day:
data.loc[:,'Number day from ' + str(day)+ ' case'] = 0
if(train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].count() > 0):
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['ConfirmedCases'] < day)]['Date'].max()
else:
fromday = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]['Date'].min()
for i in range(0, len(data)) :
if(data['Date'].iloc[i] > fromday):
day_denta = data['Date'].iloc[i] - fromday
data['Number day from ' + str(day)+ ' case'].iloc[i] = day_denta.days
feature = feature + ['Number day from ' + str(day)+ ' case']
return data[feature]
pred_data_all = pd.DataFrame()
for country in train['Country_Region'].unique() :
for province in train[(train['Country_Region'] == country)]['Province_State'].unique() :
df_train = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]
df_test = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
X_train = CreateInput(df_train)
 y_train_confirmed = df_train['ConfirmedCases'].ravel()
y_train_fatalities = df_train['Fatalities'].ravel()
X_pred = CreateInput(df_test)
 for day in sorted(feature_day,reverse = True):
feature_use = 'Number day from ' + str(day)+ ' case'
idx = X_train[X_train[feature_use] == 0].shape[0]
if(X_train[X_train[feature_use] > 0].shape[0] >= 10):
break
adjusted_X_train = X_train[idx:][feature_use].values.reshape(-1, 1)
 adjusted_y_train_confirmed = y_train_confirmed[idx:]
adjusted_y_train_fatalities = y_train_fatalities[idx:]
idx = X_pred[X_pred[feature_use] == 0].shape[0]
adjusted_X_pred = X_pred[idx:][feature_use].values.reshape(-1, 1)
 pred_data = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
max_train_date = train[(train['Country_Region'] == country)&(train['Province_State'] == province)]['Date'].max()
min_test_date = pred_data['Date'].min()
model = SARIMAX(adjusted_y_train_confirmed, order=(1,1,0),
measurement_error=True ).fit(disp=False)
 y_hat_confirmed = model.forecast(pred_data[pred_data['Date'] > max_train_date].shape[0])
 y_train_confirmed = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['Date'] >= min_test_date)]['ConfirmedCases'].values
y_hat_confirmed = np.concatenate(( y_train_confirmed,y_hat_confirmed), axis = 0)
 model = SARIMAX(adjusted_y_train_fatalities, order=(1,1,0),
measurement_error=True ).fit(disp=False)
 y_hat_fatalities = model.forecast(pred_data[pred_data['Date'] > max_train_date].shape[0])
 y_train_fatalities = train[(train['Country_Region'] == country)&(train['Province_State'] == province)&(train['Date'] >= min_test_date)]['Fatalities'].values
y_hat_fatalities = np.concatenate(( y_train_fatalities,y_hat_fatalities), axis = 0)
 pred_data['ConfirmedCases_hat'] = y_hat_confirmed
pred_data['Fatalities_hat'] = y_hat_fatalities
pred_data_all = pred_data_all.append(pred_data)
 df_val = pd.merge(pred_data_all,train[['Date','Country_Region','Province_State','ConfirmedCases','Fatalities']],on=['Date','Country_Region','Province_State'], how='left')
 df_val.loc[df_val['Fatalities_hat'] < 0,'Fatalities_hat'] = 0
df_val.loc[df_val['ConfirmedCases_hat'] < 0,'ConfirmedCases_hat'] = 0
df_val_3 = df_val.copy()<compute_test_metric>
test(model=model, data=(x_test[:100], y_test[:100]))
method_list = ['Poly Bayesian Ridge','Exponential Smoothing','SARIMA']
method_val = [df_val_1,df_val_2,df_val_3]
for i in range(0,3):
df_val = method_val[i]
method_score = [method_list[i]] + [RMSLE(df_val[(df_val['ConfirmedCases'].isnull() == False)]['ConfirmedCases'].values,df_val[(df_val['ConfirmedCases'].isnull() == False)]['ConfirmedCases_hat'].values)] + [RMSLE(df_val[(df_val['Fatalities'].isnull() == False)]['Fatalities'].values,df_val[(df_val['Fatalities'].isnull() == False)]['Fatalities_hat'].values)]
print(method_score )<save_to_csv>
del x_train, x_test, y_train, y_test
gc.collect()
df_val = df_val_3
submission = df_val[['ForecastId','ConfirmedCases_hat','Fatalities_hat']]
submission.columns = ['ForecastId','ConfirmedCases','Fatalities']
submission.to_csv('submission.csv', index=False)
 submission<import_modules>
def predictandSaveSubmission() :
imagePaths = os.listdir('.. /input/resized-2015-2019-blindness-detection-images/resized test 15')
 predictionList = []
for imagePath in imagePaths:
tmp = [int(imagePath.split('.')[0].split('_')[0]), imagePath.split('.')[0].split('_')[1], imagePath.split('.')[0]]
imagePath = '.. /input/resized-2015-2019-blindness-detection-images/resized test 15/' + imagePath
imageArray = np.array([np.array(Image.open(imagePath ).resize(( width, breadth)) , np.float ).reshape(width, breadth, 3)])
 y_pred, _ = model.predict_on_batch([imageArray, np.zeros(( 1, 5)) ])
 tmp += [1]
predictionList.append(tmp)
 predictionList.sort(key=lambda x:(x[0], x[1]))
predictionDict = {}
predictionDict['image'] = [i[2] for i in predictionList]
predictionDict['level'] = [i[3] for i in predictionList]
df_submission = pd.DataFrame(predictionDict)
 df_submission.to_csv("submission.csv",index=False )
import pandas as pd
from datetime import datetime
from sklearn import preprocessing
from xgboost import XGBRegressor
from sklearn.tree import DecisionTreeRegressor
from lightgbm import LGBMRegressor
from catboost import CatBoostRegressor 
<load_from_csv>
predictandSaveSubmission()