uclamii / model_tuner

A library to tune the hyperparameters of common ML models. Supports calibration and custom pipelines.
Apache License 2.0
3 stars 0 forks source link

corrected labels and pos args in conf_mat #33

Closed lshpaner closed 1 month ago

lshpaner commented 1 month ago

Description:

This pull request resolves the issue of misaligned "Pos" and "Neg" labels in the confusion matrix printout. The previous implementation incorrectly labeled the confusion matrix elements, leading to confusion in interpreting the results. This fix ensures that the labels correctly align with the corresponding values in the confusion matrix.

Related Issue: Closes Issue #32

Previously, the constructor had the following labels:

self.labels = ["tp", "fn", "fp", "tn"]

Changes Made:

self.labels = ["tn", "fp", "fn", "tp"]
def _confusion_matrix_print(conf_matrix, labels):
    max_length = max(len(str(conf_matrix.max())), 2)
    border = "-" * 80
    print(border)
    print(f"{'':>10}Predicted:")
    print(f"{'':>12}{'Pos':>{max_length+1}}{'Neg':>{max_length+3}}")
    print(border)
    print(
        f"Actual: Pos {conf_matrix[1,1]:>{max_length}} ({labels[3]})  {conf_matrix[1,0]:>{max_length}} ({labels[2]})"
    )
    print(
        f"{'':>8}Neg {conf_matrix[0,1]:>{max_length}} ({labels[1]})  {conf_matrix[0,0]:>{max_length}} ({labels[0]})"
    )
    print(border)

If you try and rerun the return_metrics on the example AIDS Clinical Trials Notebook, you will see that the issue is fixed, and the correct labels will be provided:

Confusion matrix on set provided: 
--------------------------------------------------------------------------------
          Predicted:
             Pos   Neg
--------------------------------------------------------------------------------
Actual: Pos  64 (tp)   40 (fn)
        Neg  20 (fp)  304 (tn)
elemets commented 1 month ago

Just tested on my machine on the breast cancer data. The confusion matrices and the classification report now match so this fix works. Merging with main.