nlp-with-transformers / notebooks

Jupyter notebooks for the Natural Language Processing with Transformers book
https://transformersbook.com/
Apache License 2.0
3.85k stars 1.19k forks source link

[CHAPTER-4] Mistake in code #124

Open kasrahabib opened 11 months ago

kasrahabib commented 11 months ago

Information

I think there is a small mistake in the code while calling the function for plot_confusion_matrix(y_preds, y_true, labels)

The problem arises in chapter:

Describe the bug


from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix

def plot_confusion_matrix(y_preds, y_true, labels):
    cm = confusion_matrix(y_true, y_preds, normalize="true")
    fig, ax = plt.subplots(figsize=(6, 6))
    disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)
    disp.plot(cmap="Blues", values_format=".2f", ax=ax, colorbar=False)
    plt.title("Normalized confusion matrix")
    plt.show()

Here is the next line that, I think, contains the mistake, where you call the function:

plot_confusion_matrix(df_tokens["labels"], df_tokens["predicted_label"],
                      tags.names)

As you can you, while calling the function, you pass the true labels as predicted and predicted as true.

Expected behavior

I think the intended usage is as follows:

plot_confusion_matrix(df_tokens["predicted_label"], df_tokens["labels"]
                      tags.names)