predicted_data_test = model.predict(processed_data_test) precision, recall, f_score, support = precision_recall_fscore_support( target_test, predicted_data_test, average='macro') report_confusion_matrix = ConfusionMatrix( actual_vector=target_test.to_list(), predict_vector=predicted_data_test) draw_conf_matrix( report_confusion_matrix, ALGORITHM_LABELS[algorithm_id], fold_iteration, ) positions = report_confusion_matrix.position() precisions = [] recalls = [] f1_scores = [] accuracies = [] report_text = "" for classname in report_confusion_matrix.classes: tp = len(positions[classname]["TP"]) tn = len(positions[classname]["TN"]) fp = len(positions[classname]["FP"]) fn = len(positions[classname]["FN"]) temp_recall = divide(tp, tp + fn) temp_precision = divide(tp, tp + fp)
def generate_and_save_report( y_pred: list, y_true: list, corpus_name: str, algorithm_name: str, fold: int, ) -> None: report = DataFrame( classification_report( y_pred, y_true, zero_division=0, output_dict=True, )) # Save laporan ke Excel report.to_excel("./reports/{}-{}-fold-{}.xlsx".format( corpus_name, algorithm_name, fold)) # Buat plot Confusion Matrix untuk masing-masing kelas pengelompokan confusion_matrix = ConfusionMatrix( actual_vector=y_true, predict_vector=y_pred, ) confusion_matrix_positions = DataFrame(confusion_matrix.position()) confusion_matrix_positions = confusion_matrix_positions.applymap( lambda positions: len(positions)) pos_classes = confusion_matrix.classes pos_classes.sort() report_text = "" for pos_class in pos_classes: tp = confusion_matrix_positions[pos_class]["TP"] tn = confusion_matrix_positions[pos_class]["TN"] fp = confusion_matrix_positions[pos_class]["FP"] fn = confusion_matrix_positions[pos_class]["FN"] accuracy = tp + tn / (tp + tn + fp + fn) report_text += get_pos_class_report_text(algorithm_name, report, pos_class, tp, tn, fp, fn, accuracy) # Simpan teks laporan with open( "./reports/{}-{}-fold-{}.txt".format(corpus_name, algorithm_name, fold_counter), "w") as report_filehandle: report_filehandle.write(report_text) confusion_matrix_df = DataFrame(confusion_matrix.to_array()) row_sums = confusion_matrix_df.sum().to_list() y_labels = pos_classes x_labels = [ "{} ({})".format(label, row_sums[index]) for index, label in enumerate(pos_classes) ] temp_df = confusion_matrix_df.T shape = temp_df.shape annotations = [[ f'''{pos_classes[col]}\n{pos_classes[row]}\n{temp_df[row][col]}''' for col in range(0, shape[0]) ] for row in range(0, shape[1])] confusion_matrix_heatmap = sns.heatmap( confusion_matrix_df, annot=annotations, xticklabels=x_labels, yticklabels=y_labels, cmap="Greens", linewidths=0.1, linecolor="black", fmt='', cbar=False, ) fig = confusion_matrix_heatmap.get_figure() fig.savefig("./reports/{}-{}-fold-{}.svg".format( corpus_name, algorithm_name, fold, ), bbox_inches='tight') plt.clf() pass