0

I'm trying to print a confusion matrix, but I'm not sure if there is an easy way to print this kind of tables without write complex loops to concatenate the string. Right now, I just did it like this example. Is it there a different way.

t1 = "|" + " " \* 8 + "|" + " " \* 10 + "| Predicted |" + " " \*10 + "|\\n"
t2 = "|" + " " \* 8 + "|" + " " \* 10 + "|  Positive |" + " Negative |\\n"
t3 = "| Actual |" + " Positive |" + " " \* (10 - len(str(TP))) + str(TP) + " |" + " " \* (9 - len(str(FN))) + str(FN) + " |\\n"
t4 = "|" + " " \* 8 + "|" + " Negative |" + " " \* (10 - len(str(FP))) + str(FP) + " |" + " " \* (9 - len(str(TN))) + str(TN) + " |\\n"

print(t1+t2+t3+t4)


|        |          | Predicted |          |
|        |          |  Positive | Negative |
| Actual | Positive |         1 |        2 |
|        | Negative |         1 |        2 |

I am trying to find an easy way to print this kind of tables.

  • 1
    you can `'|'.join(row)` and have an array of cols per row. You can `f"|{value}|'` interpret variables in strings and `'{:<21} '.format(value)` to generate strings of a given size – Cpt.Hook Nov 29 '22 at 10:29
  • 1
    This may or may not be useful: https://www.w3schools.com/python/python_ml_confusion_matrix.asp – ScottC Nov 29 '22 at 10:35
  • This may also be useful: https://stackoverflow.com/questions/19233771/sklearn-plot-confusion-matrix-with-labels – ScottC Nov 29 '22 at 11:35

0 Answers0