Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<prepare_x_and_y>
! pip install -U keras-tuner
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>
Conv2D,
MaxPooling2D,
AveragePooling2D,
Dropout,
Flatten,
Input 
)
NFOLD = 5
kf = KFold(n_splits=NFOLD )<predict_on_test>
def metrics(pred_tag, y_true_tag):
print(classification_report(pred_tag, y_true_tag))
print('- ' * 20)
 print("PREC: ", precision_score(pred_tag, y_true_tag, average='micro'))
print("REC: ", recall_score(pred_tag, y_true_tag, average='micro'))
print("F1: ", f1_score(pred_tag, y_true_tag, average='micro'))
%%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<compute_test_metric>
train_id = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test_id = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 dig_id = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv' )
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>
x, y, z = 28, 28, 1
qtd_classes = 10
images = []
images_labels = []
valid = []
valid_labels = []
for count,(index, row)in enumerate(train_id.iterrows()):
images.append(row.values[1:].reshape(x, y, z))
images_labels.append(row.values[:1])
 for count,(index, row)in enumerate(dig_id.iterrows()):
valid.append(row.values[1:].reshape(x, y, z))
valid_labels.append(row.values[:1] )
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>
TESTER = []
for count,(index, row)in enumerate(test_id.iterrows()):
TESTER.append(row.values[1:].reshape(x, y, z))
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>
SUBMIT = np.asarray(TESTER)/ 255.
 TEST = np.asarray(valid)/ 255.
 TRAIN = np.asarray(images)/ 255.
 TRAIN_labels = to_categorical(np.asarray(images_labels), qtd_classes)
 TEST_labels = to_categorical(np.asarray(valid_labels), qtd_classes )
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>
train = np.concatenate([TEST, TRAIN])
 labels = np.concatenate([TRAIN_labels, TEST_labels] )
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission_regression.csv", index=False )<prepare_output>
x_train, x_test, y_train, y_test = train_test_split(train, labels, test_size = 0.3 )
reg_sub = subm[["Patient_Week","FVC","Confidence"]].copy()<sort_values>
NUM_CLASSES = 10
EPOCHS = 3
BATCH_SIZE = 64
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>
def model_cnn(hp):
inputShape = Input(shape=(x, y, z))
tmp_model = inputShape
num_layers = hp.Int('num_layers', 2, 8, default=6)
 lr = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])
 for idx in range(num_layers):
idx = str(idx)
 filters = hp.Int('filters_' + idx, 32, 256, step=32, default=64)
 tmp_model = Conv2D(filters=filters, kernel_size = 3, padding="same", activation='relu' )(tmp_model)
 if tmp_model.shape[1] >= 8:
pool_type = hp.Choice('pool_' + idx, values=['max', 'avg'])
 if pool_type == 'max':
tmp_model = MaxPooling2D(2 )(tmp_model)
 elif pool_type == 'avg':
tmp_model = AveragePooling2D(2 )(tmp_model)
 tmp_model = Dropout(0.25 )(tmp_model)
 tmp_model = Flatten()(tmp_model)
 tmp_model = Dense(units=hp.Int('units',
min_value=32,
max_value=512,
step=32), activation='relu' )(tmp_model)
 tmp_model = Dropout(0.5 )(tmp_model)
 model = keras.Model(inputShape, Dense(10, activation='softmax' )(tmp_model))
model.compile(optimizer=Adam(lr),
loss='categorical_crossentropy',
metrics=['accuracy'])
 return model
df = df1[['Patient_Week']].copy()
df['FVC'] = 0.25*df1['FVC'] + 0.75*df2['FVC']
df['Confidence'] = 0.25*df1['Confidence'] + 0.75*df2['Confidence']
df.head()<save_to_csv>
loss, accu = best_model.evaluate(x_test, y_test, verbose = 0)
 print("%s: %.2f%%" %('Accuracy...', accu))
print("%s: %.2f" %('Loss...', loss))
print('-'*35)
 preds = np.argmax(best_model.predict(x_test), axis = 1)
 y_true = np.argmax(y_test,axis = 1)
 metrics(preds, y_true )
df.to_csv('submission.csv', index=False )<import_modules>
results = best_model.predict(SUBMIT)
 results = np.argmax(results,axis = 1)
 data_out = pd.DataFrame({'id': range(len(SUBMIT)) , 'label': results} )
<import_modules><EOS> 
data_out.to_csv('submission.csv', index = None )