Deprecated confusion_matrix method

46 Views Asked by At

I'm working through a Udemy course and it appears that an aspect of it has not been edited to reflect recent updates. I'm attempting to create a Confusion Matrix for a classification problem using Naive Bayes, but I'm having trouble getting the material I have to work with the updated functions.

The data set is on airline reviews, and I've trimmed it down to just the text of the review and the labelled sentiment(positive, negative, neutral). Here is the relevant code, with the error I'm receiving when just changing the method rather than my arguments. The problem arises in the "report" method.

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import confusion_matrix,ConfusionMatrixDisplay,classification_report

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=101)
tfidf = TfidfVectorizer(stop_words='english')
tfidf.fit(X_train)
X_train_tfidf = tfidf.transform(X_train)
X_test_tfidf = tfidf.transform(X_test)

nb = MultinomialNB()
nb.fit(X_train_tfidf,y_train)

def report(model):
    preds = model.predict(X_test_tfidf)
    print(classification_report(y_test,preds))
    plot_confusion_matrix(model,X_test_tfidf,y_test)

print("NB MODEL")
report(nb)

Output:

NB MODEL
              precision    recall  f1-score   support

    negative       0.66      0.99      0.79      1817
     neutral       0.79      0.15      0.26       628
    positive       0.89      0.14      0.24       483

    accuracy                           0.67      2928
   macro avg       0.78      0.43      0.43      2928
weighted avg       0.73      0.67      0.59      2928


TypeError: too many positional arguments

Tried to create a confusion matrix using new method, but am receiving an error.

1

There are 1 best solutions below

0
N0bita On

plot_confusion_matrix in sklearn is deprecated and removed

but in place sklearn added ConfusionMatrixDisplay

from matplotlib import pyplot as plt
def report(model):
    preds = model.predict(X_test_tfidf)
    cm = confusion_matrix(y_test, preds)
    print(classification_report(y_test,preds))
    cm_plot = ConfusionMatrixDisplay(confusion_matrix = cm, display_labels=model.classes_)
    cm_plot.plot()
    plt.show()

These changes to the code worked fine for me.