示例#1
0
def draw_conf_matrix(confusion_matrix: ConfusionMatrix, algo_name: str,
                     fold_iter: int):
    classification_classes = confusion_matrix.classes
    confusion_matrix_df = DataFrame(confusion_matrix.to_array())
    row_sums = confusion_matrix_df.sum(axis=1).to_list()

    x_labels = [
        "{} ({})".format(label, row_sums[index])
        for index, label in enumerate(classification_classes)
    ]

    temp_df = confusion_matrix_df.T

    shape = temp_df.shape
    annotations = [[
        f'''{classification_classes[row]}\n{classification_classes[col]}\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.T,
        annot=DataFrame(annotations).T,
        xticklabels=x_labels,
        yticklabels=classification_classes,
        cmap="OrRd",
        linewidths=0.1,
        linecolor="black",
        fmt='',
        cbar=False,
    )

    fig = confusion_matrix_heatmap.get_figure()
    fig.savefig("./test_result_data/{}-fold-{}.svg".format(
        algo_name, fold_iter),
                bbox_inches='tight')
    fig.savefig("./test_result_data/{}-fold-{}.png".format(
        algo_name, fold_iter),
                bbox_inches='tight')
    plt.clf()
    pass
示例#2
0
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