DOC convert confusion matrix to y_true/y_pred for classification_report example
@@ -102,3 +102,27 @@ disp.figure_.suptitle("Confusion Matrix")
print(f"Confusion matrix:\n{disp.confusion_matrix}")
plt.show()
+
+###############################################################################
+# If the results from evaluating a classifier are stored in the form of a
+# :ref:`confusion matrix <confusion_matrix>` and not in terms of `y_true` and
+# `y_pred`, one can still build a :func:`~sklearn.metrics.classification_report`
+# as follows:
+
+
+# The ground truth and predicted lists
+y_true = []
+y_pred = []
+cm = disp.confusion_matrix
+
+# For each cell in the confusion matrix, add the corresponding ground truths
+# and predictions to the lists
+for gt in range(len(cm)):
+ for pred in range(len(cm)):
+ y_true += [gt] * cm[gt][pred]
+ y_pred += [pred] * cm[gt][pred]
+
+print(
+ "Classification report rebuilt from confusion matrix:\n"
+ f"{metrics.classification_report(y_true, y_pred)}\n"
+)
