Kaggle competition: TReNDS Neuroimaging
cmdataset = get_validation_dataset(ordered = True)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()<create_dataframe>
ridge = pd.read_csv('/kaggle/input/trends-master-ensemble/submission_ridge.csv')
 modra = pd.read_csv('/kaggle/input/trends-master-ensemble/sub.csv')
 tunguz_n_IC20 = pd.read_csv('/kaggle/input/rapids-ensemblenoic20/submission_rapids_ensemble(1 ).csv')
 rapids = pd.read_csv('/kaggle/input/trends-master-ensemble/submission1.csv')
 bagging = pd.read_csv('/kaggle/input/baggingregressor-rapids-ensemble/submission_rapids_ensemble_with_baggingregressor.csv')
 SS_skewed = pd.read_csv('/kaggle/input/rapids-ensemblenoic20/06163.csv')
 stack = pd.read_csv('/kaggle/input/trends-multi-layer-model/submission.csv' )
test_ds = get_test_dataset(ordered = True)
 test_images_ds = test_ds.map(lambda image, idnum: image)
<define_variables>
all_subs = pd.DataFrame(columns=['Id', 'modra', 'bagging', 'SS_skewed'])
 all_subs['Id'] = ridge['Id']
all_subs['modra'] = modra['Predicted']
all_subs['bagging'] = bagging['Predicted']
all_subs['SS_skewed'] = SS_skewed['Predicted']
dataset = get_validation_dataset()
dataset = dataset.unbatch().batch(20)
 batch = iter(dataset)
 images, labels = next(batch )<predict_on_test>
all_subs['stack'] = all_subs.merge(stack, how='left', left_on='Id', right_on='Id')['Predicted']
print(datetime.now())
 for j in range(start_model, end_model):
val_probabilities[j] = models[j].predict(images_ds)
 test_probabilities[j] = models[j].predict(test_images_ds)
 all_probabilities[j] = models[j].predict(images)
 print(datetime.now())
<find_best_params>
scores = {
'modra' : math.e **(5.0 - 0.33),
'bagging': math.e **(5.0 - 0.31),
'SS_skewed' : math.e **(5.0 - 0.1),
'stack' : math.e **(5.0 - 0.0)
 }
for j in range(start_model, finished_models):
display_training_curves(historys[j].history['loss'], historys[j].history['val_loss'], 'loss', 211)
 display_training_curves(historys[j].history['sparse_categorical_accuracy'], historys[j].history['val_sparse_categorical_accuracy'], 'accuracy', 212)
 for j in range(start_model, finished_models):
print('model number:', j, ', Train Accuracy:', max(historys[j].history['sparse_categorical_accuracy']), ', Validation Accuracy:', max(historys[j].history['val_sparse_categorical_accuracy']))
for j in range(start_model, finished_models):
print('model number:', j, ', Train Loss:', min(historys[j].history['loss']), ', Validation Loss:', min(historys[j].history['val_loss']))<compute_test_metric>
columns = list(all_subs.columns)
 columns.remove('Id')
 total = 0
for col in columns:
total += scores[col]
total
def getFitPrecisionRecall(correct_labels, predictions):
score = f1_score(correct_labels, predictions, labels = range(len(CLASSES)) , average = 'macro')
 precision = precision_score(correct_labels, predictions, labels = range(len(CLASSES)) , average = 'macro')
 recall = recall_score(correct_labels, predictions, labels = range(len(CLASSES)) , average = 'macro')
 return score, precision, recall<compute_test_metric>
weight_sum = 0
for col in columns:
weight = scores[col] / total
weight_sum += weight
print(weight)
 all_subs[col] = all_subs[col] * weight
weight_sum
cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels = range(len(CLASSES)))
 score, precision, recall = getFitPrecisionRecall(cm_correct_labels, cm_predictions)
 cmat =(cmat.T / cmat.sum(axis = -1)).T
display_confusion_matrix(cmat, score, precision, recall)
 print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))<define_variables>
all_subs['Predicted'] = + all_subs['modra'] + all_subs['bagging'] + all_subs['SS_skewed'] + all_subs['stack']
def create_submission_file(filename, probabilities):
predictions = np.argmax(probabilities, axis = -1)
 print('Generating submission file...', filename)
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt(filename, np.rec.fromarrays([test_ids, predictions]), fmt = ['%s', '%d'], delimiter = ',', header = 'id,label', comments = '')
<save_to_csv>
sub = all_subs[['Id', 'Predicted']]
sub
<find_best_params><EOS> 
sub.to_csv('blend_sub.csv', index=False )