#for meshs in top_10_mesh: # mesh = ' '.join(meshs) # pred_label_10.writelines(mesh.strip()+ "\r") #pred_label_10.close() #pred_label_15 = open('MultichannelCLSTM_pred_label_15.txt', 'w') #for meshs in top_15_mesh: # mesh = ' '.join(meshs) # pred_label_15.writelines(mesh.strip()+ "\r") #pred_label_15.close() end = time.time() print("Run Time: ", end - start) ########################### Evaluation Metrics ############################# # precision @k precision = precision_at_ks(pred, test_labelsIndex, ks=[1, 3, 5]) for k, p in zip([1, 3, 5], precision): print('p@{}: {:.5f}'.format(k, p)) # check how many documents that have mesh terms greater and equal to 10/15 label_row_total = np.sum(test_labels, axis=1) index_greater_10 = [ index for index, value in enumerate(label_row_total) if value >= 10 ] index_greater_15 = [ index for index, value in enumerate(label_row_total) if value >= 15 ] def get_label_using_index(org_label, index):
predicted_label = np.zeros(predictions.shape) for i in range(len(predictions)): top_k_index = (predictions[i].argsort()[-k:][::-1]).tolist() for j in top_k_index: predicted_label[i][j] = 1 predicted_label = predicted_label.astype(np.int64) return predicted_label top_10_pred = top_k_predicted(pred, 10) top_20_pred = top_k_predicted(pred, 20) end = time.time() print("Run Time: ", end - start) ########################### Evaluation Metrics ############################# # precision @k precision = precision_at_ks(pred, test_labelsIndex, ks=[1, 3, 5]) for k, p in zip([1, 3, 5], precision): print('p@{}: {:.5f}'.format(k, p)) # nDCG @k nDCG_1 = [] nDCG_3 = [] nDCG_5 = [] Hamming_loss_10 = [] Hamming_loss_20 = [] for i in range(pred.shape[0]): ndcg1 = ndcg_score(test_labels[i], pred[i], k=1, gains="linear") ndcg3 = ndcg_score(test_labels[i], pred[i], k=3, gains="linear") ndcg5 = ndcg_score(test_labels[i], pred[i], k=5, gains="linear")